Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9684ca671e692a08595636bd13ee9aae3d18a9bd (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*******************************************************************************
 * Copyright (c) 2006, 2013 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.jpa.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.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jpt.common.core.internal.operations.JptFileCreationDataModelProperties;
import org.eclipse.jpt.common.core.internal.utility.JptPlugin;
import org.eclipse.jpt.common.core.internal.utility.ProjectTools;
import org.eclipse.jpt.common.core.tests.internal.projects.FacetedProjectTestHarness;
import org.eclipse.jpt.common.core.tests.internal.projects.ProjectTestHarness;
import org.eclipse.jpt.common.core.tests.internal.projects.JavaProjectTestHarness;
import org.eclipse.jpt.common.utility.internal.ClassTools;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
import org.eclipse.jpt.common.utility.internal.StringTools;
import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
import org.eclipse.jpt.jpa.core.JpaPreferences;
import org.eclipse.jpt.jpa.core.JpaProject;
import org.eclipse.jpt.jpa.core.JpaProjectManager;
import org.eclipse.jpt.jpa.core.internal.facet.JpaFacetInstallDataModelProvider;
import org.eclipse.jpt.jpa.core.internal.operations.OrmFileCreationDataModelProvider;
import org.eclipse.wst.common.frameworks.datamodel.DataModelFactory;
import org.eclipse.wst.common.frameworks.datamodel.IDataModel;

@SuppressWarnings("nls")
public class JpaProjectManagerTests
	extends TestCase
{
	private FacetedProjectTestHarness projectTestHarness;


	public JpaProjectManagerTests(String name) {
		super(name);
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		this.trace("+++++ " + this.getName() + " +++++");
		this.projectTestHarness = this.buildProjectTestHarness();
	}

	private void trace(String message) {
		ClassTools.execute(this.getGenericJpaProjectManagerClass(), "trace", String.class, message);
	}

	// InternalJpaProjectManager is package-private
	private Class<?> getGenericJpaProjectManagerClass() {
		return this.getJpaProjectManager().getClass();
	}

	protected JpaProjectManager getJpaProjectManager() {
		return (JpaProjectManager) ResourcesPlugin.getWorkspace().getAdapter(JpaProjectManager.class);
	}

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

	private JpaProject getJpaProject() {
		return this.getJpaProject(this.getProject());
	}

	private JpaProject getJpaProject(IProject project) {
		try {
			return this.getJpaProject_(project);
		} catch (InterruptedException ex) {
			throw new RuntimeException(ex);
		}
	}

	private JpaProject getJpaProject_(IProject project) throws InterruptedException {
		return this.getJpaProjectReference(project).getValue();
	}

	private JpaProject.Reference getJpaProjectReference(IProject project) {
		return (JpaProject.Reference) project.getAdapter(JpaProject.Reference.class);
	}

	private IProject getProject() {
		return this.projectTestHarness.getProject();
	}

	/** 
	 * Builds a project with the java and utility facets installed, and with
	 * pre-existing entities added.
	 */
	private FacetedProjectTestHarness buildProjectTestHarness() throws Exception {
		JavaProjectTestHarness harness = new JavaProjectTestHarness(this.getClass().getSimpleName(), true);
		harness.createCompilationUnit("test.pkg", "TestEntity.java", "@Entity public class TestEntity {}");
		harness.createCompilationUnit("test.pkg", "TestEntity2.java", "@Entity public class TestEntity2 {}");
		return harness;
	}	

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

	public void testJpaProjectManager() {
		assertNotNull(this.getJpaProjectManager());
	}
	
	private IDataModel buildJpaConfigDataModel() {
		IDataModel dataModel = DataModelFactory.createDataModel(new JpaFacetInstallDataModelProvider());
		return dataModel;
	}

	private void createDefaultOrmXmlFile() throws Exception {
		IDataModel config =
			DataModelFactory.createDataModel(new OrmFileCreationDataModelProvider());
		config.setProperty(JptFileCreationDataModelProperties.CONTAINER_PATH, 
				this.getJpaProject().getProject().getFolder("src/META-INF").getFullPath());
		config.getDefaultOperation().execute(null, null);
	}

	public void testProjectCloseReopen() throws Exception {
		this.projectTestHarness.installFacet(JpaProject.FACET_ID, JpaProject.FACET_VERSION_STRING, buildJpaConfigDataModel());
		this.createDefaultOrmXmlFile();
		JpaProject jpaProject = this.getJpaProject();
		assertNotNull(jpaProject);

		this.projectTestHarness.getProject().close(null);
		assertFalse("Project is not closed", this.projectTestHarness.getProject().isOpen());
		jpaProject = this.getJpaProject();
		assertNull("JpaProject is not null", jpaProject);

		this.projectTestHarness.getProject().open(null);
		assertTrue(this.projectTestHarness.getProject().isOpen());
		jpaProject = this.getJpaProject();
		assertNotNull("JpaProject is null", jpaProject);
		assertEquals(4, jpaProject.getJpaFilesSize());
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.projectTestHarness, "src/test/pkg/TestEntity.java")));
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.projectTestHarness, "src/test/pkg/TestEntity2.java")));

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

	public void testProjectDeleteReimport() throws Exception {
		this.projectTestHarness.installFacet(JpaProject.FACET_ID, JpaProject.FACET_VERSION_STRING, buildJpaConfigDataModel());
		this.createDefaultOrmXmlFile();
		JpaProject jpaProject = this.getJpaProject();
		assertNotNull(jpaProject);
		assertEquals(1, IterableTools.size(this.getJpaProjectManager().waitToGetJpaProjects()));

		// flush the prefs file so it will still be there after we delete the project
		// and our settings are discovered when the project is restored
		this.flushProjectPrefs();

		this.projectTestHarness.getProject().delete(false, true, null);
		jpaProject = this.getJpaProject();
		assertNull(jpaProject);
		assertEquals(0, IterableTools.size(this.getJpaProjectManager().waitToGetJpaProjects()));
		assertEquals(0, ResourcesPlugin.getWorkspace().getRoot().getProjects().length);

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

		assertTrue(project.isOpen());
		assertTrue(ProjectTools.hasFacet(project, JpaProject.FACET));
		jpaProject = this.getJpaProject(project);
		assertNotNull(jpaProject);
		assertEquals(4, jpaProject.getJpaFilesSize());
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.projectTestHarness, "src/test/pkg/TestEntity.java")));
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.projectTestHarness, "src/test/pkg/TestEntity2.java")));
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.projectTestHarness, "src/META-INF/persistence.xml")));
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.projectTestHarness, "src/META-INF/orm.xml")));
	}

	protected void flushProjectPrefs() throws Exception {
		JptPlugin plugin = this.getPlugin();
		IEclipsePreferences prefs = (IEclipsePreferences) ObjectTools.execute(plugin, "getProjectPreferences", IProject.class, this.projectTestHarness.getProject());
		prefs.flush();
	}

	protected JptPlugin getPlugin() throws Exception {
		return (JptPlugin) ClassTools.execute(JpaPreferences.class, "getPlugin");
	}

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

		this.projectTestHarness.installFacet(JpaProject.FACET_ID, JpaProject.FACET_VERSION_STRING, buildJpaConfigDataModel());
		assertEquals(1, IterableTools.size(this.getJpaProjectManager().waitToGetJpaProjects()));
		JpaProject jpaProject = this.getJpaProject();
		assertNotNull(jpaProject);
		assertEquals(3, jpaProject.getJpaFilesSize());
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.projectTestHarness, "src/test/pkg/TestEntity.java")));
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.projectTestHarness, "src/test/pkg/TestEntity2.java")));

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

		this.projectTestHarness.uninstallFacet(JpaProject.FACET_ID, "1.0");
		assertEquals(0, IterableTools.size(this.getJpaProjectManager().waitToGetJpaProjects()));
		jpaProject = this.getJpaProject();
		assertNull(jpaProject);
	}

	public void testEditFacetSettingsFileAddThenRemoveJpaFacet() throws Exception {
		assertNull(this.getJpaProject());

		// add the JPA facet by modifying the facet settings file directly
		IFile facetSettingsFile = this.getFile(this.projectTestHarness, ".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=\"java\" version=\"1.5\"/>";
		String newString = oldString + StringTools.CR + "  " + "<installed facet=\"jpt.jpa\" version=\"1.0\"/>";
		String newDocument = oldDocument.replaceAll(oldString, newString);

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

		assertEquals(1, IterableTools.size(this.getJpaProjectManager().waitToGetJpaProjects()));
		JpaProject jpaProject = this.getJpaProject();
		assertNotNull(jpaProject);
		// persistence.xml and orm.xml do not get created in this situation (?)
		assertEquals(2, jpaProject.getJpaFilesSize());
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.projectTestHarness, "src/test/pkg/TestEntity.java")));
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.projectTestHarness, "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")));


		// now remove the JPA facet
		facetSettingsFile.setContents(new ByteArrayInputStream(oldDocument.getBytes()), false, false, null);
		assertEquals(0, IterableTools.size(this.getJpaProjectManager().waitToGetJpaProjects()));
		jpaProject = this.getJpaProject();
		assertNull(jpaProject);
	}
	
	public void testEditFacetSettingsFileRemoveThenAddJpaFacet() throws Exception {
		this.projectTestHarness.installFacet(JpaProject.FACET_ID, JpaProject.FACET_VERSION_STRING, buildJpaConfigDataModel());
		JpaProject jpaProject = this.getJpaProject();
		assertNotNull(jpaProject);

		// remove the JPA facet by modifying the facet settings file directly
		IFile facetSettingsFile = this.getFile(this.projectTestHarness, ".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=\"java\" version=\"1.5\"/>" + StringTools.CR + "  " + "<installed facet=\"jpt.jpa\" version=\"1.0\"/>";
		String newString = "<installed facet=\"java\" version=\"1.5\"/>";
		String newDocument = oldDocument.replaceAll(oldString, newString);

		facetSettingsFile.setContents(new ByteArrayInputStream(newDocument.getBytes()), false, false, null);
		assertEquals(0, IterableTools.size(this.getJpaProjectManager().waitToGetJpaProjects()));
		jpaProject = this.getJpaProject();
		assertNull(jpaProject);

		// now add the JPA facet back
		facetSettingsFile.setContents(new ByteArrayInputStream(oldDocument.getBytes()), false, false, null);
		assertEquals(1, IterableTools.size(this.getJpaProjectManager().waitToGetJpaProjects()));
		jpaProject = this.getJpaProject();
		assertNotNull(jpaProject);
		assertEquals(3, jpaProject.getJpaFilesSize());
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.projectTestHarness, "src/test/pkg/TestEntity.java")));
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.projectTestHarness, "src/test/pkg/TestEntity2.java")));
		assertNotNull(jpaProject.getJpaFile(this.getFile(this.projectTestHarness, "src/META-INF/persistence.xml")));
		assertNull(jpaProject.getJpaFile(this.getFile(this.projectTestHarness, "src/META-INF/orm.xml")));
	}
}

Back to the top