Skip to main content
summaryrefslogtreecommitdiffstats
blob: d6340569991f3310597866c80c94524cf29b8f07 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*******************************************************************************
 * Copyright (c) 2006, 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.model;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import junit.framework.TestCase;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jpt.core.JpaProject;
import org.eclipse.jpt.core.JptCorePlugin;
import org.eclipse.jpt.core.internal.GenericJpaModel;
import org.eclipse.jpt.core.internal.JpaModelManager;
import org.eclipse.jpt.core.tests.internal.projects.TestFacetedProject;
import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject;
import org.eclipse.jpt.core.tests.internal.projects.TestPlatformProject;
import org.eclipse.jpt.utility.internal.ClassTools;

@SuppressWarnings("nls")
public class JpaModelTests extends TestCase {

	/** carriage return */
	public static final String CR = System.getProperty("line.separator");

	protected TestFacetedProject testProject;
	public JpaModelTests(String name) {
		super(name);
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		if (this.debug()) {
			this.printName();
		}
		this.testProject = this.buildTestProject();
	}

	private boolean debug() {
		Boolean debug = (Boolean) ClassTools.staticFieldValue(JpaModelManager.class, "DEBUG");
		return debug.booleanValue();
	}

	private void printName() {
		String name = this.getName();
		System.out.println();
		System.out.println();
		this.printNameBorder(name);
		System.out.println(name);
		this.printNameBorder(name);
	}

	private void printNameBorder(String name) {
		for (int i = name.length(); i-- > 0; ) {
			System.out.print('=');
		}
		System.out.println();
	}

	@Override
	protected void tearDown() throws Exception {
		this.testProject.getProject().delete(true, true, null);
		this.testProject = null;
		super.tearDown();
	}

	/** 
	 * Builds a project with the java and utility facets installed, and with
	 * pre-existing entities added.
	 */
	private TestFacetedProject buildTestProject() throws Exception {
		TestJavaProject testProject = TestJavaProject.buildJavaProject(ClassTools.shortClassNameForObject(this), true);
		testProject.installFacet("jst.utility", "1.0");
		testProject.createCompilationUnit("test.pkg", "TestEntity.java", "@Entity public class TestEntity {}");
		testProject.createCompilationUnit("test.pkg", "TestEntity2.java", "@Entity public class TestEntity2 {}");
		return testProject;
	}	

	private IFile getFile(TestPlatformProject p, String path) {
		return p.getProject().getFile(new Path(path));
	}

	public void testJpaModel() {
		assertNotNull(JptCorePlugin.getJpaModel());
	}

	public void testProjectCloseReopen() throws Exception {
		this.testProject.installFacet("jpt.jpa", "1.0");

		this.testProject.getProject().close(null);
		assertFalse(this.testProject.getProject().isOpen());
		JpaProject jpaProject = JptCorePlugin.getJpaProject(this.testProject.getProject());
		assertNull(jpaProject);

		this.testProject.getProject().open(null);
		assertTrue(this.testProject.getProject().isOpen());
		jpaProject = JptCorePlugin.getJpaProject(this.testProject.getProject());
		assertNotNull(jpaProject);
		assertEquals(4, jpaProject.jpaFilesSize());
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.testProject, "src/test/pkg/TestEntity.java")));
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.testProject, "src/test/pkg/TestEntity2.java")));

		assertNotNull(jpaProject.getJpaFile(this.getFile(this.testProject, "src/META-INF/persistence.xml")));
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.testProject, "src/META-INF/orm.xml")));
	}

	public void testProjectDeleteReimport() throws Exception {
		this.testProject.installFacet("jpt.jpa", "1.0");
		JpaProject jpaProject = JptCorePlugin.getJpaProject(this.testProject.getProject());
		assertNotNull(jpaProject);
		assertEquals(1, JptCorePlugin.getJpaModel().jpaProjectsSize());

		this.testProject.getProject().delete(false, true, null);
		jpaProject = JptCorePlugin.getJpaProject(this.testProject.getProject());
		assertNull(jpaProject);
		assertEquals(0, JptCorePlugin.getJpaModel().jpaProjectsSize());
		assertEquals(0, ResourcesPlugin.getWorkspace().getRoot().getProjects().length);

		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(this.testProject.getProject().getName());
		project.create(null);
		assertEquals(1, ResourcesPlugin.getWorkspace().getRoot().getProjects().length);
		project.open(null);

		assertTrue(project.isOpen());
		assertTrue(JptCorePlugin.projectHasJpaFacet(project));
		jpaProject = JptCorePlugin.getJpaProject(project);
		assertNotNull(jpaProject);
		assertEquals(4, jpaProject.jpaFilesSize());
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.testProject, "src/test/pkg/TestEntity.java")));
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.testProject, "src/test/pkg/TestEntity2.java")));
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.testProject, "src/META-INF/persistence.xml")));
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.testProject, "src/META-INF/orm.xml")));
	}

	public void testFacetInstallUninstall() throws Exception {
		assertNull(JptCorePlugin.getJpaProject(this.testProject.getProject()));

		this.testProject.installFacet("jpt.jpa", "1.0");
		assertEquals(1, JptCorePlugin.getJpaModel().jpaProjectsSize());
		JpaProject jpaProject = JptCorePlugin.getJpaProject(this.testProject.getProject());
		assertNotNull(jpaProject);
		assertEquals(4, jpaProject.jpaFilesSize());
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.testProject, "src/test/pkg/TestEntity.java")));
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.testProject, "src/test/pkg/TestEntity2.java")));

		assertNotNull(jpaProject.getJpaFile(this.getFile(this.testProject, "src/META-INF/persistence.xml")));
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.testProject, "src/META-INF/orm.xml")));

		this.testProject.uninstallFacet("jpt.jpa", "1.0");
		assertEquals(0, JptCorePlugin.getJpaModel().jpaProjectsSize());
		jpaProject = JptCorePlugin.getJpaProject(this.testProject.getProject());
		assertNull(jpaProject);
	}

	//TODO - Commented out this test, since it was failing in the I-Build and we're not sure why.
	//See bug 221757
	public void testEditFacetSettingsFile() throws Exception {
		assertNull(JptCorePlugin.getJpaProject(this.testProject.getProject()));

		// add the JPA facet by modifying the facet settings file directly
		IFile facetSettingsFile = this.getFile(this.testProject, ".settings/org.eclipse.wst.common.project.facet.core.xml");
		InputStream inStream = new BufferedInputStream(facetSettingsFile.getContents());
		int fileSize = inStream.available();
		byte[] buf = new byte[fileSize];
		inStream.read(buf);
		inStream.close();

		String oldDocument = new String(buf);
		String oldString = "<installed facet=\"jst.utility\" version=\"1.0\"/>";
		String newString = oldString + CR + "  " + "<installed facet=\"jpt.jpa\" version=\"1.0\"/>";
		String newDocument = oldDocument.replaceAll(oldString, newString);

		facetSettingsFile.setContents(new ByteArrayInputStream(newDocument.getBytes()), false, false, null);

		// TODO moved more stuff to the error console until we can figure out why it fails intermittently  ~kfb
//		assertEquals(1, JptCorePlugin.getJpaModel().jpaProjectsSize());
//		JpaProject jpaProject = JptCorePlugin.getJpaProject(this.testProject.getProject());
//		assertNotNull(jpaProject);
//		// persistence.xml and orm.xml do not get created in this situation (?)
//		assertEquals(2, jpaProject.jpaFilesSize());
//		assertNotNull(jpaProject.getJpaFile(this.getFile(this.testProject, "src/test/pkg/TestEntity.java")));
//		assertNotNull(jpaProject.getJpaFile(this.getFile(this.testProject, "src/test/pkg/TestEntity2.java")));
////		assertNotNull(jpaProject.getJpaFile(this.getFile(this.testProject, "src/META-INF/persistence.xml")));
////		assertNotNull(jpaProject.getJpaFile(this.getFile(this.testProject, "src/META-INF/orm.xml")));
		int size = JptCorePlugin.getJpaModel().jpaProjectsSize();
		if (size != 1) {
			System.err.println("bogus size: " + size);
			System.err.println("bogus project: " + JptCorePlugin.getJpaProject(this.testProject.getProject()));
		}

		// now remove the JPA facet
		facetSettingsFile.setContents(new ByteArrayInputStream(oldDocument.getBytes()), false, false, null);
// TODO moved this stuff to the error console until we can figure out why it fails intermittently  ~bjv
//		assertEquals(0, JptCorePlugin.jpaModel().jpaProjectsSize());
//		jpaProject = JptCorePlugin.jpaProject(testProject.getProject());
//		assertNull(jpaProject);
		int newSize = JptCorePlugin.getJpaModel().jpaProjectsSize();
		if (newSize != 0) {
			System.err.println("bogus size: " + newSize);
			System.err.println("bogus project: " + JptCorePlugin.getJpaProject(this.testProject.getProject()));
		}
	}

	/**
	 * make sure the DEBUG constants are 'false' before checking in the code
	 */
	public void testDEBUG() {
		this.verifyDEBUG(JpaModelManager.class);
		this.verifyDEBUG(GenericJpaModel.class);
	}

	private void verifyDEBUG(Class<?> clazz) {
		assertFalse("Recompile with \"DEBUG = false\": " + clazz.getName(),
				((Boolean) ClassTools.staticFieldValue(clazz, "DEBUG")).booleanValue());
	}

}

Back to the top