Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ba947e049defdf520255f7755e1302a9d85850ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*******************************************************************************
 * Copyright (c) 2007, 2008 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.core.tests.internal.platform;


import java.util.Iterator;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jpt.core.JpaPlatform;
import org.eclipse.jpt.core.MappingKeys;
import org.eclipse.jpt.core.context.java.JavaAttributeMapping;
import org.eclipse.jpt.core.context.java.JavaAttributeMappingDefinition;
import org.eclipse.jpt.core.context.java.JavaTypeMapping;
import org.eclipse.jpt.core.context.java.JavaTypeMappingDefinition;
import org.eclipse.jpt.core.internal.facet.JpaFacetDataModelProperties;
import org.eclipse.jpt.core.internal.facet.JpaFacetDataModelProvider;
import org.eclipse.jpt.core.resource.java.JPA;
import org.eclipse.jpt.core.tests.extension.resource.ExtensionTestPlugin;
import org.eclipse.jpt.core.tests.extension.resource.JavaTestAttributeMapping;
import org.eclipse.jpt.core.tests.extension.resource.JavaTestAttributeMappingDefinition;
import org.eclipse.jpt.core.tests.extension.resource.JavaTestTypeMapping;
import org.eclipse.jpt.core.tests.extension.resource.JavaTestTypeMappingDefinition;
import org.eclipse.jpt.core.tests.extension.resource.TestJavaBasicMapping;
import org.eclipse.jpt.core.tests.extension.resource.TestJavaEntity;
import org.eclipse.jpt.core.tests.extension.resource.TestJpaFactory;
import org.eclipse.jpt.core.tests.extension.resource.TestJpaPlatformProvider;
import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase;
import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject;
import org.eclipse.jpt.core.tests.internal.projects.TestJpaProject;
import org.eclipse.jpt.utility.internal.iterators.ArrayIterator;
import org.eclipse.wst.common.componentcore.datamodel.properties.IFacetDataModelProperties;
import org.eclipse.wst.common.frameworks.datamodel.IDataModel;
import org.eclipse.wst.common.project.facet.core.IActionConfigFactory;

public class JpaPlatformTests extends ContextModelTestCase
{
	protected TestJpaProject testProject;

	protected static final String PROJECT_NAME = "ExtensionTestProject";
	protected static final String PACKAGE_NAME = "extension.test";
	
	public static final String TEST_PLUGIN_CLASS = ExtensionTestPlugin.class.getName();
	public static final String TEST_PLUGIN_ID = "org.eclipse.jpt.core.tests.extension.resource";

	public static final String TEST_PLATFORM_CLASS_NAME = TestJpaPlatformProvider.class.getName();
	public static final String TEST_PLATFORM_LABEL = "Test Jpa Platform";
	public static final String TEST_JPA_FACTORY = TestJpaFactory.class.getName();
	public static final String TEST_TYPE_MAPPING_PROVIDER_CLASS = JavaTestTypeMappingDefinition.class.getName();
	public static final String TEST_ATTRIBUTE_MAPPING_PROVIDER_CLASS = JavaTestAttributeMappingDefinition.class.getName();
	
	public JpaPlatformTests(String name) {
		super(name);
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		JpaPlatformExtensionTests.verifyExtensionTestProjectExists();
	}

	@Override
	protected TestJavaProject buildJavaProject(boolean autoBuild) throws Exception {
		return super.buildJpaProject(PROJECT_NAME, autoBuild, this.buildConfig());
	}

	protected IDataModel buildConfig() throws Exception {
		IActionConfigFactory configFactory = new JpaFacetDataModelProvider();
		IDataModel config = (IDataModel) configFactory.create();
		config.setProperty(IFacetDataModelProperties.FACET_VERSION_STR, "1.0"); //$NON-NLS-1$
		config.setProperty(JpaFacetDataModelProperties.PLATFORM_ID, TestJpaPlatformProvider.ID);
		return config;
	}

	private ICompilationUnit createTestEntity() throws Exception {
		return this.createTestType(new DefaultAnnotationWriter() {
			@Override
			public Iterator<String> imports() {
				return new ArrayIterator<String>(JPA.ENTITY);
			}
			@Override
			public void appendTypeAnnotationTo(StringBuilder sb) {
				sb.append("@Entity").append(CR);
			}
		});
	}

	
	protected JpaPlatform jpaPlatform() {
		return this.getJpaProject().getJpaPlatform();
	}

	public void testJpaFactory() {
		assertTrue(jpaPlatform().getJpaFactory().getClass().getName().equals(TEST_JPA_FACTORY));
	}
	
	public void testBuildJavaTypeMappingFromMappingKey() throws Exception {
		createTestEntity();
		addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
		
		JavaTypeMappingDefinition mappingDefinition =
			jpaPlatform().getJavaTypeMappingDefinition(JavaTestTypeMapping.TEST_TYPE_MAPPING_KEY);
		JavaTypeMapping mapping = 
			mappingDefinition.buildMapping(getJavaPersistentType(), jpaPlatform().getJpaFactory());
		assertTrue(mapping instanceof JavaTestTypeMapping);
		
		mappingDefinition = 
			jpaPlatform().getJavaTypeMappingDefinition(MappingKeys.ENTITY_TYPE_MAPPING_KEY);
		mapping = mappingDefinition.buildMapping(getJavaPersistentType(), jpaPlatform().getJpaFactory());
		assertTrue(mapping instanceof TestJavaEntity);	
	}
	
	public void testBuildJavaAttributeMappingFromMappingKey() throws Exception {
		createTestEntity();
		addXmlClassRef(FULLY_QUALIFIED_TYPE_NAME);
		
		JavaAttributeMappingDefinition mappingDefinition = 
			jpaPlatform().getSpecifiedJavaAttributeMappingDefinition(JavaTestAttributeMapping.TEST_ATTRIBUTE_MAPPING_KEY);
		JavaAttributeMapping mapping = 
			mappingDefinition.buildMapping(getJavaPersistentType().getAttributeNamed("name"), jpaPlatform().getJpaFactory());	
		assertTrue(mapping instanceof JavaTestAttributeMapping);
		
		mappingDefinition = jpaPlatform().getSpecifiedJavaAttributeMappingDefinition(
				MappingKeys.BASIC_ATTRIBUTE_MAPPING_KEY);
		mapping = mappingDefinition.buildMapping(
					getJavaPersistentType().getAttributeNamed("name"), jpaPlatform().getJpaFactory());
		assertTrue(mapping instanceof TestJavaBasicMapping);
	}
}

Back to the top