Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit 81fc70f

Browse files
keithchongsghung
authored andcommitted
[44] Create new SWTBot, UI-based test plugin
Signed-off-by: Keith Chong <kchong@ca.ibm.com>
1 parent 31b680f commit 81fc70f

File tree

12 files changed

+608
-0
lines changed

12 files changed

+608
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
4+
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
5+
<classpathentry kind="src" path="src">
6+
<attributes>
7+
<attribute name="test" value="true"/>
8+
</attributes>
9+
</classpathentry>
10+
<classpathentry kind="output" path="bin"/>
11+
</classpath>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.class
2+
/bin
3+
/build
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>org.eclipse.codewind.openapi.ui.test</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.pde.ManifestBuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.pde.SchemaBuilder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
</buildSpec>
24+
<natures>
25+
<nature>org.eclipse.pde.PluginNature</nature>
26+
<nature>org.eclipse.jdt.core.javanature</nature>
27+
</natures>
28+
</projectDescription>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.compliance=1.8
5+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
6+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
7+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Manifest-Version: 1.0
2+
Bundle-ManifestVersion: 2
3+
Bundle-Name: OpenAPI UI Test Plugin
4+
Bundle-SymbolicName: org.eclipse.codewind.openapi.ui.test
5+
Bundle-Version: 1.0.0.qualifier
6+
Bundle-Activator: org.eclipse.codewind.openapi.ui.test.Activator
7+
Require-Bundle: org.eclipse.ui,
8+
org.eclipse.core.runtime,
9+
org.eclipse.swtbot.eclipse.finder,
10+
org.eclipse.swtbot.junit4_x,
11+
org.junit,
12+
org.eclipse.ui.ide,
13+
org.eclipse.core.resources,
14+
org.eclipse.codewind.openapi.test
15+
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
16+
Automatic-Module-Name: org.eclipse.codewind.openapi.ui.test
17+
Bundle-ActivationPolicy: lazy
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source.. = src/
2+
output.. = bin/
3+
bin.includes = META-INF/,\
4+
.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2019 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* IBM Corporation - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.codewind.openapi.ui.test;
14+
15+
import org.eclipse.ui.plugin.AbstractUIPlugin;
16+
import org.osgi.framework.BundleContext;
17+
18+
public class Activator extends AbstractUIPlugin {
19+
20+
// The plug-in ID
21+
public static final String PLUGIN_ID = "org.eclipse.codewind.openapi.ui.test"; //$NON-NLS-1$
22+
23+
// The shared instance
24+
private static Activator plugin;
25+
26+
/**
27+
* The constructor
28+
*/
29+
public Activator() {
30+
}
31+
32+
@Override
33+
public void start(BundleContext context) throws Exception {
34+
super.start(context);
35+
plugin = this;
36+
}
37+
38+
@Override
39+
public void stop(BundleContext context) throws Exception {
40+
plugin = null;
41+
super.stop(context);
42+
}
43+
44+
/**
45+
* Returns the shared instance
46+
*
47+
* @return the shared instance
48+
*/
49+
public static Activator getDefault() {
50+
return plugin;
51+
}
52+
53+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2019 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* IBM Corporation - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.codewind.openapi.ui.test;
14+
15+
import org.eclipse.codewind.openapi.ui.test.menus.ContextMenuTest;
16+
import org.junit.runner.RunWith;
17+
import org.junit.runners.Suite;
18+
19+
@RunWith(Suite.class)
20+
@Suite.SuiteClasses({
21+
ContextMenuTest.class
22+
})
23+
public class AllUiTests {
24+
25+
public AllUiTests() {
26+
// Empty
27+
}
28+
29+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2019 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* IBM Corporation - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.codewind.openapi.ui.test;
14+
15+
import java.io.FileNotFoundException;
16+
import java.io.IOException;
17+
import java.util.List;
18+
import java.util.Stack;
19+
import java.util.StringTokenizer;
20+
21+
import org.eclipse.codewind.openapi.test.Constants;
22+
import org.eclipse.codewind.openapi.test.util.TestUtilities;
23+
import org.eclipse.codewind.openapi.ui.test.menus.utils.UITestUtilities;
24+
import org.eclipse.core.resources.IFile;
25+
import org.eclipse.core.resources.IProject;
26+
import org.eclipse.core.runtime.CoreException;
27+
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
28+
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
29+
import org.eclipse.swtbot.swt.finder.SWTBotTestCase;
30+
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
31+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
32+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
33+
34+
public class BaseTestCase extends SWTBotTestCase {
35+
36+
protected SWTWorkbenchBot swtWorkbenchBot = new SWTWorkbenchBot();
37+
38+
protected IProject testProject;
39+
protected IFile definitionFile;
40+
41+
// Configurable options to test and should be overridden
42+
protected String newProjectName = "TestProject";
43+
protected String sourceDefinition = Constants.PETSTORE_30;
44+
protected String targetDefinitionInProject = "petstore.yaml";
45+
46+
public BaseTestCase() {
47+
// Empty
48+
}
49+
50+
protected void createGeneralProject(String projectName) {
51+
try {
52+
this.testProject = TestUtilities.createGeneralProject(projectName);
53+
} catch (CoreException e) {
54+
fail();
55+
}
56+
assertNotNull("Project creation", this.testProject);
57+
assertEquals("Test project name", projectName, this.testProject.getName());
58+
}
59+
60+
/**
61+
*
62+
* @param sourceFile - source OpenAPI definition in the test plugin resources folder
63+
* @param destinationFile - path of target OpenAPI definition in the test project but excludes
64+
* the project name
65+
*/
66+
protected void copyDefinitionToProject(String sourceFile, String destinationFile) {
67+
try {
68+
this.definitionFile = TestUtilities.copyDefinitionToProject(sourceFile, destinationFile, this.testProject);
69+
assertTrue("Copied file exists in workspace", this.definitionFile.exists());
70+
assertEquals("Definition is in the test project", this.testProject, this.definitionFile.getProject());
71+
} catch (CoreException e) {
72+
e.printStackTrace();
73+
fail(e.getMessage());
74+
} catch (FileNotFoundException e) {
75+
e.printStackTrace();
76+
fail(e.getMessage());
77+
} catch (IOException e) {
78+
e.printStackTrace();
79+
fail(e.getMessage());
80+
}
81+
}
82+
83+
protected void setup() {
84+
createGeneralProject(this.newProjectName);
85+
copyDefinitionToProject(this.sourceDefinition, this.targetDefinitionInProject);
86+
}
87+
88+
protected void doTestInProjectExplorer(boolean openApiMenuShouldAppear) {
89+
SWTBotView view = UITestUtilities.getProjectExplorerView();
90+
StringTokenizer token = new StringTokenizer(targetDefinitionInProject, "/");
91+
Stack<String> pathSegments = new Stack<String>();
92+
while (token.hasMoreElements()) {
93+
pathSegments.add(0, token.nextToken());
94+
}
95+
verifyContextMenu(openApiMenuShouldAppear, view, newProjectName, pathSegments);
96+
}
97+
98+
// Accept first child node of root node
99+
protected void verifyContextMenu(boolean expectedOpenApiMenuExists, SWTBotView view, String rootNode, Stack<String> pathSegments) {
100+
SWTBotTreeItem targetTreeItem = UITestUtilities.findTreeItem(rootNode, view, pathSegments);
101+
verifyContextMenu(expectedOpenApiMenuExists, targetTreeItem);
102+
}
103+
104+
// Accept a path to the child node relative to the root node
105+
protected void verifyContextMenu(boolean expectedOpenApiMenuExists, SWTBotView view, String rootNode, String childNode) {
106+
SWTBotTreeItem targetTreeItem = UITestUtilities.findTreeItem(rootNode, view, childNode);
107+
verifyContextMenu(expectedOpenApiMenuExists, targetTreeItem);
108+
}
109+
110+
private void verifyContextMenu(boolean expectedOpenApiMenuExists, SWTBotTreeItem targetTreeItem) {
111+
assertNotNull("The test file should be in the project and the tree node exists", targetTreeItem);
112+
try {
113+
SWTBotMenu contextMenu = targetTreeItem.contextMenu(UIConstants.MENU_OPENAPI_GENERATE);
114+
if (expectedOpenApiMenuExists) {
115+
assertNotNull("OpenAPI Generate menu should exist", contextMenu);
116+
List<String> cascadeMenuItems = contextMenu.menuItems();
117+
for (String s: cascadeMenuItems) {
118+
System.out.println(s);
119+
}
120+
assertTrue("There should be three menu actions", cascadeMenuItems.size() == 3);
121+
assertTrue("Client API stub... menu exists", cascadeMenuItems.get(0).equals(UIConstants.MENU_GEN_CLIENT));
122+
assertTrue("Server API stub... menu exists", cascadeMenuItems.get(1).equals(UIConstants.MENU_GEN_SERVER));
123+
assertTrue("HTML documentation...", cascadeMenuItems.get(2).equals(UIConstants.MENU_HTML));
124+
}
125+
} catch (Exception e) {
126+
if (!expectedOpenApiMenuExists) {
127+
assertTrue("OpenAPI Generate menu should not be available on non-OpenAPI 3.0 documents", e instanceof WidgetNotFoundException);
128+
return;
129+
}
130+
throw e; // Fail test if test otherwise
131+
}
132+
}
133+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2019 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* IBM Corporation - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.codewind.openapi.ui.test;
14+
15+
public class UIConstants {
16+
17+
public UIConstants() {
18+
// Empty
19+
}
20+
21+
// The following four strings are hard coded into the org.eclipse.codewind.openapi.ui plugin.properties
22+
public static String MENU_OPENAPI_GENERATE = "OpenAPI Generate";
23+
public static String MENU_GEN_CLIENT = "Client API stub...";
24+
public static String MENU_GEN_SERVER = "Server API stub...";
25+
public static String MENU_HTML = "HTML documentation...";
26+
}

0 commit comments

Comments
 (0)