Skip to main content
summaryrefslogtreecommitdiffstats
blob: 34e3ba11895a08df9f70de80c8ee6492467c1553 (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
/*******************************************************************************
 * <copyright>
 *
 * Copyright (c) 2005, 2010 SAP AG.
 * 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:
 *    Stefan Dimov - initial API, implementation and documentation
 *
 * </copyright>
 *
 *******************************************************************************/
package org.eclipse.jpt.jpadiagrameditor.ui.tests.internal.modelintegration.ui;

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.easymock.EasyMock;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.graphiti.dt.IDiagramTypeProvider;
import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.platform.IDiagramEditor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jpt.common.utility.internal.iterables.EmptyIterable;
import org.eclipse.jpt.jpa.core.JpaFile;
import org.eclipse.jpt.jpa.core.JpaProject;
import org.eclipse.jpt.jpa.core.JpaStructureNode;
import org.eclipse.jpt.jpa.core.JptJpaCorePlugin;
import org.eclipse.jpt.jpa.core.context.java.JavaPersistentType;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.modelintegration.ui.JPAEditorMatchingStrategy;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.modelintegration.util.IModelIntegrationUtil;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.provider.IJPAEditorFeatureProvider;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.IEclipseFacade;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.IJPADiagramEditorInput;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.IJPAEditorUtil;
import org.eclipse.jpt.jpadiagrameditor.ui.tests.internal.JPACreateFactory;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PartInitException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ModelIntegrationTest {
	
	private static final String CODE_GENERATED = "CODE_GENERATED";
	private IJPAEditorFeatureProvider featureProvider;
	final String TEST_PROJECT = "Test";
	private JpaProject jpaProject = null;
	private JPACreateFactory factory = null;
	IEclipseFacade eclipseFacade = null;
	private String testProjectName = TEST_PROJECT + "_" + System.currentTimeMillis();
	IFile entityFile = null;
	
	@Before
	public void setUp() throws Exception {
		factory = JPACreateFactory.instance();
		jpaProject = factory.createJPAProject(testProjectName);
		assertNotNull(jpaProject);
		entityFile = factory.createEntity(jpaProject, "org.eclipse.Entity1");
		Thread.sleep(2000);
		featureProvider = EasyMock.createMock(IJPAEditorFeatureProvider.class);
		expect(featureProvider.getBusinessObjectForPictogramElement(null)).andReturn(JPACreateFactory.getPersistentType(entityFile));
		expect(featureProvider.getCompilationUnit(isA(JavaPersistentType.class))).andReturn(JavaCore.createCompilationUnitFrom(entityFile)).anyTimes();
	}
	
	@Test
	public void testJPAEditorMatchingStrategyWithEntityFileEditorInputTrue() {
		IEditorReference editorRef = EasyMock.createMock(IEditorReference.class);
		IFileEditorInput input = EasyMock.createMock(IFileEditorInput.class);
		expect(input.getFile()).andStubReturn(entityFile);
		expect(input.getName()).andStubReturn(CODE_GENERATED);
		ICompilationUnit cu = createCompilationUnitFrom(entityFile); 
		
		IJPAEditorUtil ut = EasyMock.createMock(IJPAEditorUtil.class);
		JavaPersistentType inputJptType = (JavaPersistentType) JPACreateFactory.getPersistentType(entityFile);
		expect(ut.getJPType(cu)).andStubReturn(inputJptType);
		expect(featureProvider.getJPAEditorUtil()).andStubReturn(ut);
		Diagram d = EasyMock.createMock(Diagram.class);
		IJPADiagramEditorInput dEdInput = EasyMock.createMock(IJPADiagramEditorInput.class);
		try {
			expect(editorRef.getEditorInput()).andStubReturn(dEdInput);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
		expect(dEdInput.getDiagram()).andStubReturn(d);
		IDiagramTypeProvider dtp = EasyMock.createMock(IDiagramTypeProvider.class);
		expect(featureProvider.getDiagramTypeProvider()).andStubReturn(dtp);
		IModelIntegrationUtil moinIntUtil = EasyMock.createMock(IModelIntegrationUtil.class); 
		expect(featureProvider.getMoinIntegrationUtil()).andStubReturn(moinIntUtil);
		expect(moinIntUtil.getProjectByDiagram(d)).andStubReturn(jpaProject);
		PictogramElement pe = EasyMock.createMock(PictogramElement.class);
		expect(featureProvider.getPictogramElementForBusinessObject(inputJptType)).andStubReturn(pe);
		IDiagramEditor dEd = EasyMock.createMock(IDiagramEditor.class);
		expect(dtp.getDiagramEditor()).andStubReturn(dEd);
		dEd.setPictogramElementForSelection(pe);

		JPAEditorMatchingStrategy str = new JPAEditorMatchingStrategy(featureProvider);
		
		EasyMock.replay(editorRef, d, dtp, pe, dEd, moinIntUtil, dEdInput, ut, input, featureProvider);
		assertTrue(str.matches(editorRef, input));
	}
	
	@Test
	public void testJPAEditorMatchingStrategyWithEntityFileEditorInputFalse() {		
		IEditorReference editorRef = EasyMock.createMock(IEditorReference.class);
		IFileEditorInput input = EasyMock.createMock(IFileEditorInput.class);
		expect(input.getFile()).andStubReturn(entityFile);
		expect(input.getName()).andStubReturn(CODE_GENERATED);
		ICompilationUnit cu = createCompilationUnitFrom(entityFile); 
		
		IJPAEditorUtil ut = EasyMock.createMock(IJPAEditorUtil.class);
		JavaPersistentType inputJptType = (JavaPersistentType) JPACreateFactory.getPersistentType(entityFile);
		expect(ut.getJPType(cu)).andStubReturn(inputJptType);
		expect(featureProvider.getJPAEditorUtil()).andStubReturn(ut);
		Diagram d = EasyMock.createMock(Diagram.class);
		IJPADiagramEditorInput dEdInput = EasyMock.createMock(IJPADiagramEditorInput.class);
		try {
			expect(editorRef.getEditorInput()).andStubReturn(dEdInput);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
		expect(dEdInput.getDiagram()).andStubReturn(d);
		IDiagramTypeProvider dtp = EasyMock.createMock(IDiagramTypeProvider.class);
		expect(featureProvider.getDiagramTypeProvider()).andStubReturn(dtp);
		IModelIntegrationUtil moinIntUtil = EasyMock.createMock(IModelIntegrationUtil.class); 
		expect(featureProvider.getMoinIntegrationUtil()).andStubReturn(moinIntUtil);
		JpaProject anotherJpaProject = EasyMock.createMock(JpaProject.class);
		expect(moinIntUtil.getProjectByDiagram(d)).andStubReturn(anotherJpaProject);
		PictogramElement pe = EasyMock.createMock(PictogramElement.class);
		expect(featureProvider.getPictogramElementForBusinessObject(inputJptType)).andStubReturn(pe);
		IDiagramEditor dEd = EasyMock.createMock(IDiagramEditor.class);
		expect(dtp.getDiagramEditor()).andStubReturn(dEd);
		dEd.setPictogramElementForSelection(pe);

		JPAEditorMatchingStrategy str = new JPAEditorMatchingStrategy(featureProvider);
		
		EasyMock.replay(editorRef, d, dtp, pe, dEd, moinIntUtil, anotherJpaProject, dEdInput, ut, input, featureProvider);
		assertFalse(str.matches(editorRef, input));
	}	
	
	@Test
	public void testJPAEditorMatchingStrategyWithJPADiagramEditorInputTrue() {		
		IEditorReference editorRef = EasyMock.createMock(IEditorReference.class);
		expect(editorRef.getPartName()).andStubReturn("ProjectName");
		IJPADiagramEditorInput input = EasyMock.createMock(IJPADiagramEditorInput.class);
		expect(input.getProjectName()).andStubReturn("ProjectName");
		JPAEditorMatchingStrategy str = new JPAEditorMatchingStrategy(featureProvider);		
		EasyMock.replay(editorRef, input, featureProvider);
		assertTrue(str.matches(editorRef, input));
	}

	@Test
	public void testJPAEditorMatchingStrategyWithJPADiagramEditorInputFalse() {		
		IEditorReference editorRef = EasyMock.createMock(IEditorReference.class);
		expect(editorRef.getPartName()).andStubReturn("ProjectName");
		IJPADiagramEditorInput input = EasyMock.createMock(IJPADiagramEditorInput.class);
		expect(input.getProjectName()).andStubReturn("DifferentProjectName");
		JPAEditorMatchingStrategy str = new JPAEditorMatchingStrategy(featureProvider);		
		EasyMock.replay(editorRef, input, featureProvider);
		assertFalse(str.matches(editorRef, input));
	}	
	
	
//	@Test
//	public void testJPAEditorMatchingStrategyWithFileEditorInput() {
//		IEditorReference editorRef = EasyMock.createMock(IEditorReference.class);
//		IFileEditorInput input = EasyMock.createMock(IFileEditorInput.class);
//		IJPAEditorUtil ut = EasyMock.createMock(IJPAEditorUtil.class);
//		expect(featureProvider.getJPAEditorUtil()).andStubReturn(ut);
//		JavaPersistentType jpt = EasyMock.createMock(JavaPersistentType.class);
//		ICompilationUnit cu = JavaCore.createCompilationUnitFrom(entityFile);
//		expect(ut.getJPType(cu)).andStubReturn(jpt);
//		PersistenceUnit pu = EasyMock.createMock(PersistenceUnit.class);
//		expect(jpt.getPersistenceUnit()).andStubReturn(pu);
//		expect(pu.getJpaProject()).andStubReturn(jpaProject);
//		expect(input.getFile()).andStubReturn(entityFile);
//		expect(input.getName()).andStubReturn(entityFile.getName());
//		JPAEditorMatchingStrategy str = new JPAEditorMatchingStrategy(featureProvider);		
//		EasyMock.replay(editorRef, input, ut, jpt, pu, featureProvider);
//		assertFalse(str.matches(editorRef, input));
//	}
	
	
	
	public ICompilationUnit createCompilationUnitFrom(IFile file) {
		return JavaCore.createCompilationUnitFrom(file);
	}
		
	@After	
	public void tearDown() throws Exception {		
		deleteAllProjects();
	}
	
	private void deleteAllProjects() throws Exception {
		IProgressMonitor monitor= new NullProgressMonitor();
		ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
			public void run(IProgressMonitor monitor) throws CoreException {
				IProject project = jpaProject.getProject();
			    project.close(monitor);	    	
			    project.close(monitor);
			    project.delete(true, true, monitor);				
			}
		} , monitor);
	}	
	
}

Back to the top