The main objective of the "Create a test case" command when applied to a Class object MyClass is to create a MyClassTest test class in the test model and at the proper place in the test model package hierarchy.
-
Go to "
implementation /
java /
org.sample.junit /
handlers /
commands" package
-
On "
commands" create a new class
: TestCaseCreator
-
Select the
TestCaseCreator class, then open the Java Designer property view
-
Run
Generate, then
Edit
-
Update the TestCaseCreator class with the following content:
(TestCaseCreator.java source file)
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
package org.sample.junit.handlers.commands;
import com.modeliosoft.modelio.javadesigner.annotations.objid;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.modelio.api.modelio.model.IModelingSession;
import org.modelio.api.modelio.model.ITransaction;
import org.modelio.api.modelio.model.IUmlModel;
import org.modelio.api.modelio.Modelio;
import org.modelio.api.module.IModule;
import org.modelio.metamodel.mmextensions.infrastructure.ExtensionNotFoundException;
import org.modelio.metamodel.uml.infrastructure.Dependency;
import org.modelio.metamodel.uml.infrastructure.Stereotype;
import org.modelio.metamodel.uml.statik.Class;
import org.modelio.metamodel.uml.statik.Package;
public class TestCaseCreator {
public boolean createTestCase(Class classToTest, IModule module) {
// Check that there is no already existing test class
for (Dependency dep : classToTest.getImpactedDependency()) {
if (dep.isStereotyped(module.getName(), "JUnitDependency")) {
MessageDialog.openError(Display.getCurrent().getActiveShell(),
"Error",
"Command cannot be applied: class already has a test case");
return false;
}
}
IModelingSession session = module.getModuleContext().getModelingSession();
try (ITransaction t = session.createTransaction("Create a test case")) {
// Build the test class name
String testClassName = classToTest.getName() + "Test";
// Get the package parent of the test class
// The getTestCaseParentPackage() returns an IPackage
// which is created on the fly if necessary (ok as we are in a transaction)
Package testCaseParentPackage = getTestCaseParentPackage(classToTest, module);
// Create the class using the convenient factory method createClass()
Class testClass = createTestClass(session, testClassName, testCaseParentPackage);
// Add the «JUnit» stereotype to the class
stereotypeTestCase(testClass, module);
// Link test class to the class to test
linkTestCase(classToTest, testClass, module);
t.commit();
return true;
} catch (Exception e) {
// Report error to the log
module.getModuleContext().getLogService().error(e);
return false;
}
}
private Class createTestClass(IModelingSession session, String testClassName, Package testCaseParentPackage) {
IUmlModel model = session.getModel();
Class testClass = model.createClass(testClassName, testCaseParentPackage);
return testClass;
}
private Package getTestCaseParentPackage(Class classToTest, IModule module) {
IModelingSession session = module.getModuleContext().getModelingSession();
try (ITransaction t = session.createTransaction("Create test hierarchy")) {
// Creating/updating the test model hierarchy
// TODO for now, create the test class in the same package as the class to test
Package parent = (Package) classToTest.getOwner();
t.commit();
return parent;
} catch (Exception e) {
// Report error to the log
module.getModuleContext().getLogService().error(e);
return null;
}
}
private boolean stereotypeTestCase(Class testClass, IModule module) {
IModelingSession session = module.getModuleContext().getModelingSession();
try (ITransaction t = session.createTransaction("Stereotype a test case")) {
Stereotype s = session.getMetamodelExtensions().getStereotype(module.getName(), "JUnit", module.getModuleContext().getModelioServices().getMetamodelService().getMetamodel().getMClass(Class.class));
if (s != null) {
// Add the stereotype to the class
testClass.getExtension().add(s);
t.commit();
return true;
} else {
MessageDialog.openError(Display.getCurrent().getActiveShell(),
"Error",
"Stereotype JUnit not found, check your installation");
return false;
}
}
}
private boolean linkTestCase(Class classToTest, Class testClass, IModule module) {
IModelingSession session = module.getModuleContext().getModelingSession();
try (ITransaction t = session.createTransaction("Stereotype a test case")) {
//Create the dependency
IUmlModel model = session.getModel();
model.createDependency(testClass,classToTest, module.getName(), "JUnitDependency");
t.commit();
return true;
} catch (ExtensionNotFoundException e) {
MessageDialog.openError(Display.getCurrent().getActiveShell(),
"Error",
"Stereotype JUnitDependency not found, check your installation");
return false;
} catch (Exception e) {
// Report error to the log
module.getModuleContext().getLogService().error(e);
return false;
}
}
}
-
Once the TestCaseCreator class source file has been completed, update the model by clicking on the
"Update model from sources" button.