Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2a4674e9cb7601ae31a8051fb6486525f74cf025 (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
package org.eclipse.jpt.jpadiagrameditor.swtbot.tests.internal;

import static org.eclipse.swtbot.swt.finder.waits.Conditions.shellCloses;
import static org.eclipse.swtbot.swt.finder.waits.Conditions.shellIsActive;

import java.util.Iterator;

import junit.framework.ComparisonFailure;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jpt.jpa.core.JpaProject;
import org.eclipse.jpt.jpa.core.context.orm.OrmXml;
import org.eclipse.jpt.jpa.core.context.persistence.MappingFileRef;
import org.eclipse.jpt.jpa.core.context.persistence.PersistenceUnit;
import org.eclipse.jpt.jpadiagrameditor.swtbot.tests.utils.ContextMenuHelper;
import org.eclipse.jpt.jpadiagrameditor.swtbot.tests.utils.EditorProxy;
import org.eclipse.jpt.jpadiagrameditor.swtbot.tests.utils.RelationshipsUtils;
import org.eclipse.jpt.jpadiagrameditor.swtbot.tests.utils.Utils;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.i18n.JPAEditorMessages;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.propertypage.JPADiagramPropertyPage;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JpaArtifactFactory;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.gef.finder.SWTBotGefTestCase;
import org.eclipse.swtbot.eclipse.gef.finder.SWTGefBot;
import org.eclipse.swtbot.eclipse.gef.finder.widgets.SWTBotGefEditor;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.eclipse.swtbot.swt.finder.widgets.TimeoutException;
import org.junit.After;

@SuppressWarnings("restriction")
public class AbstractSwtBotEditorTest extends SWTBotGefTestCase{
	protected static JPACreateFactory factory = JPACreateFactory.instance();
	protected static JpaProject jpaProject;

	protected static SWTGefBot bot = new SWTGefBot();
	protected static SWTWorkbenchBot workbenchBot = new SWTWorkbenchBot();
	protected static EditorProxy editorProxy;

	protected static SWTBotGefEditor jpaDiagramEditor;
	protected static RelationshipsUtils relUtils;
	protected static OrmXml ormXml;
	
	
	public static void createJPa10Project(String name) throws Exception{
		createJPa20Project(name, "1.0", false);
	}
	
	public static void createJPa20Project(String name) throws Exception{
		createJPa20Project(name, "2.0", false);
	}
	
	public static void createJPa20ProjectWithOrm(String name) throws Exception{
		createJPa20Project(name, "2.0", true);
	}
	
	private static void createJPa20Project(String name, String version, boolean withOrmXml) throws Exception{
		closeWelcomeScreen();

		createProject(name, version);
		
		editorProxy = new EditorProxy(workbenchBot, bot);

		if(withOrmXml) {
			setOrmXml();
		}

		jpaDiagramEditor = editorProxy.openDiagramOnJPAProjectNode(
				jpaProject.getName());
	
		Utils.printlnFormatted("------> JPA diagram editor is opened");
		
		editorProxy.setJpaDiagramEditor(jpaDiagramEditor);

		relUtils = new RelationshipsUtils(jpaDiagramEditor, editorProxy, jpaProject);
		if(withOrmXml) {
			assertNotNull(ormXml);
			relUtils.setOrmXml(ormXml);
		}
		
		Utils.printlnFormatted("======> Test are ready to start!");
//		Thread.sleep(2000);
	}

	private static void setOrmXml() throws InterruptedException, CoreException {
		JPACreateFactory.waitNonSystemJobs();
		jpaDiagramEditorPropertiesPage(jpaProject.getName());
		
		JPACreateFactory.waitNonSystemJobs();

		jpaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
		//assert that the orm.xml file exists.
		ormXml = getOrmXMl(jpaProject);
		assertNotNull(ormXml);
		editorProxy.setOrmXml(ormXml);
	}

	private static void createProject(String name, String version)
			throws CoreException {
		factory = JPACreateFactory.instance();
		if(version.equals("1.0")){
			jpaProject = factory.createJPAProject(name);
		} else {
			jpaProject = factory.createJPA20Project(name);
		}
		assertNotNull(jpaProject);
		assertEquals(name, jpaProject.getName());
		
		Utils.printlnFormatted("-----> JPA project is created!");
	}

	private static void closeWelcomeScreen() {
		SWTBotPreferences.TIMEOUT = 1000;
		try {
			bot.viewByTitle("Welcome").close();
		} catch (Exception e) {
			// ignore
		} finally {
			SWTBotPreferences.TIMEOUT = 5000;
		}
		workbenchBot.perspectiveByLabel("JPA").activate();
		Utils.printlnFormatted("-----> JPA perspective is activated successfully!");
		bot.closeAllEditors();
		Utils.printlnFormatted("-----> All editors are closed!");
	}
	
	private static void jpaDiagramEditorPropertiesPage(String name) {
		SWTBot propertiesPageBot = ContextMenuHelper.openProjectProperties(workbenchBot, name);
		
		//Test JPA Diagram Editor properties page
		SWTBotTree peropertiesPageTree = propertiesPageBot.tree();
		ContextMenuHelper.selectNodeInTree(peropertiesPageTree, "JPA");
		
		try{
			assertEquals("Could Not Accept Changes ", propertiesPageBot.activeShell().getText());
			propertiesPageBot.activeShell().bot().button(IDialogConstants.OK_LABEL).click();
		} catch (ComparisonFailure e) {
			
		}
		
		propertiesPageBot.comboBoxWithLabel("Type:").setSelection("Disable Library Configuration");
		propertiesPageBot.button("Apply").click();
	
		try {
			JPACreateFactory.waitNonSystemJobs();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ContextMenuHelper.selectNodeInTree(peropertiesPageTree, "JPA", "JPA Diagram Editor");
		assertFalse(propertiesPageBot.checkBoxInGroup("Add to entity mappings in XML", "XML entity mappings").isChecked());
		// assert the "XML entity mappings" group is disabled
		assertFalse(propertiesPageBot.labelInGroup("Mapping file:", "XML entity mappings").isEnabled());
		assertFalse(propertiesPageBot.textWithLabelInGroup("Mapping file:", "XML entity mappings").isEnabled());
		assertEquals("META-INF/orm.xml", propertiesPageBot.textWithLabelInGroup("Mapping file:", "XML entity mappings").getText());
		assertFalse(propertiesPageBot.buttonInGroup("Browse...", "XML entity mappings").isEnabled());
	
		propertiesPageBot.checkBoxInGroup("Add to entity mappings in XML", "XML entity mappings").select();
		// assert the "XML entity mappings" group is enabled
		assertTrue(propertiesPageBot.labelInGroup("Mapping file:", "XML entity mappings").isEnabled());
		assertTrue(propertiesPageBot.textWithLabelInGroup("Mapping file:", "XML entity mappings").isEnabled());
		assertEquals("META-INF/orm.xml", propertiesPageBot.textWithLabelInGroup("Mapping file:", "XML entity mappings").getText());
		assertTrue(propertiesPageBot.buttonInGroup("Browse...", "XML entity mappings").isEnabled());
		
		//checks that the orm.xml does not exist
		//assert that there is an error message and the OK button is disabled
		SWTBotText errorMsg = propertiesPageBot.text(1);
		assertEquals(JPAEditorMessages.JPADiagramPropertyPage_NotExistsXmlErrorMsg, errorMsg.getText());
		assertFalse(propertiesPageBot.button(IDialogConstants.OK_LABEL).isEnabled());

		//create new orm.xml with the dialogs which open, when the Browse button is pressed
		//press the Browse button
		propertiesPageBot.buttonInGroup("Browse...", "XML entity mappings").click();
		//assert that the "Mapping File" dialog appears
		workbenchBot.waitUntil(shellIsActive("Mapping File"), 10000);
		SWTBotShell createMappingdialog = workbenchBot.shell("Mapping File");
		//assert that the "Ok" button of the "Mapping File" dialog is disabled, because there is no
		//orm.xml
		assertFalse(createMappingdialog.bot().button(IDialogConstants.OK_LABEL).isEnabled());
		//press the "New..." button of the "mapping File" dialog to create a new orm.xml file
		createMappingdialog.bot().button("New...").click();
		//assert that the "New Mapping File" dialog is opened.
		workbenchBot.waitUntil(shellIsActive("New Mapping File"));
		//press the "Finish" button of the "New Mapping File" dialog to create new orm.xml
		workbenchBot.shell("New Mapping File").bot().button(IDialogConstants.FINISH_LABEL).click();
		//wait the "New Mapping File" dialog to be closed.
		workbenchBot.waitUntil(shellCloses(workbenchBot.shell("New Mapping File")));
		//assert that the "Ok" button of the "Mapping File" dialog is now enabled and press it.
		assertTrue(createMappingdialog.bot().button(IDialogConstants.OK_LABEL).isEnabled());
		createMappingdialog.bot().button(IDialogConstants.OK_LABEL).click();
		//wait until the "Mapping File" dialog is closed.
		workbenchBot.waitUntil(shellCloses(createMappingdialog));
		
		//assert that there is no error message in the properties page, saying that the orm.xml file does
		//not exists.
		errorMsg = propertiesPageBot.text(1);
		assertNotSame(JPAEditorMessages.JPADiagramPropertyPage_NotExistsXmlErrorMsg, errorMsg.getText());
		//assert that the "Ok" button of the "JPA Diagram Editor" property page is already enabled
		//and press it.
		assertTrue(propertiesPageBot.button(IDialogConstants.OK_LABEL).isEnabled());		
		propertiesPageBot.button(IDialogConstants.OK_LABEL).click();
		//wait until the property page is closed.
		try {
			workbenchBot.waitUntil(shellCloses(propertiesPageBot.activeShell()));
		} catch (TimeoutException e) {
			
		}
	}
	
//	private static void createJpaProjectViaWizard(String name, String version) {
//		ContextMenuHelper.clickMenu("File", "New", "Other...");
//		SWTBot swtBot = workbenchBot.shell("New").bot();
//		ContextMenuHelper.selectTreeItem(swtBot.tree(), "JPA", "JPA Project");
//		swtBot.button(IDialogConstants.NEXT_LABEL).click();
//		
//		swtBot.textWithLabel("Project name:").setText(name);
//		swtBot.comboBoxInGroup("Target runtime").setSelection("jre6");
//		swtBot.comboBoxInGroup("JPA version").setSelection(version);
//		swtBot.button(IDialogConstants.NEXT_LABEL).click();
//		
//		swtBot.button(IDialogConstants.NEXT_LABEL).click();
//		
//		swtBot.comboBoxInGroup("Platform").setSelection("Generic 2.0");
//		swtBot.comboBoxWithLabel("Type:").setSelection("Disable Library Configuration");
//		
////		swtBot.checkBox("Create mapping file (orm.xml)").select();
//				
//		swtBot.button(IDialogConstants.FINISH_LABEL).click();
//		
//		swtBot.waitUntil(shellCloses(swtBot.activeShell()), 30000);
//	}
	
	@After
	public void tearDown() throws Exception {
		editorProxy.deleteResources(jpaProject, ormXml!=null);
	}

	private static OrmXml getOrmXMl(JpaProject jpaProject){
		jpaProject.getContextModelRoot().synchronizeWithResourceModel();
		
		try {
			JPACreateFactory.waitNonSystemJobs();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		PersistenceUnit unit = JpaArtifactFactory.instance().getPersistenceUnit(jpaProject);
		assertNotNull(unit);
		unit.synchronizeWithResourceModel();
		unit.update();
		
		
		assertTrue(unit.getMappingFileRefsSize() > 0);
		if(unit.getMappingFileRefsSize() == 0)
			return null;

	    String ormFileName = JPADiagramPropertyPage.getOrmXmlFileName(jpaProject.getProject());	    
	    assertNotNull(ormFileName);
	    Iterator<MappingFileRef> iter = unit.getMappingFileRefs().iterator();
	    while(iter.hasNext()){
	    	MappingFileRef mapFile = iter.next();
	    	assertEquals(ormFileName, mapFile.getFileName());
	    	if(mapFile.getFileName().equals(ormFileName)){
	    		OrmXml ormXml = (OrmXml) mapFile.getMappingFile();
	    	    return ormXml;
	    	}
	    }
	   return null;
	}
}

Back to the top