Processing commands: model transformation

Step 1: Checking for an already existing test case

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.

In the src/main/java/org/modelio/junit/command directory, create the Java file named TestCaseCreator.java
And put the following content :(TestCaseCreator.java source file)

  1package org.modelio.junit.command;
  2
  3import org.eclipse.jface.dialogs.MessageDialog;
  4import org.eclipse.swt.widgets.Display;
  5import org.modelio.api.modelio.model.IModelingSession;
  6import org.modelio.api.modelio.model.ITransaction;
  7import org.modelio.api.modelio.model.IUmlModel;
  8import org.modelio.api.module.IModule;
  9import org.modelio.metamodel.mmextensions.infrastructure.ExtensionNotFoundException;
 10import org.modelio.metamodel.uml.infrastructure.Dependency;
 11import org.modelio.metamodel.uml.infrastructure.Stereotype;
 12import org.modelio.metamodel.uml.statik.Class;
 13import org.modelio.metamodel.uml.statik.Package;
 14
 15public class TestCaseCreator {
 16
 17    public boolean createTestCase(Class classToTest, IModule module) {
 18        // Check that there is no already existing test class
 19        for (Dependency dep : classToTest.getImpactedDependency()) {
 20            if (dep.isStereotyped(module.getName(), "JUnitDependency")) {
 21                MessageDialog.openError(Display.getCurrent().getActiveShell(),
 22                        "Error",
 23                        "Command cannot be applied: class already has a test case");
 24                return false;
 25            }
 26        }
 27        
 28        IModelingSession session = module.getModuleContext().getModelingSession();
 29        try (ITransaction t = session.createTransaction("Create a test case")) {
 30            // Build the test class name
 31            String testClassName = classToTest.getName() + "Test";
 32        
 33            // Get the package parent of the test class
 34            // The getTestCaseParentPackage() returns an IPackage
 35            // which is created on the fly if necessary (ok as we are in a transaction)
 36            Package testCaseParentPackage = getTestCaseParentPackage(classToTest, module);
 37        
 38            // Create the class using the convenient factory method createClass()
 39            Class testClass = createTestClass(session, testClassName, testCaseParentPackage);
 40        
 41            // Add the << JUnit >> stereotype to the class
 42            stereotypeTestCase(testClass, module);
 43        
 44            // Link test class to the class to test
 45            linkTestCase(classToTest, testClass, module);
 46        
 47            t.commit();
 48            return true;
 49        } catch (Exception e) {
 50            // Report error to the log
 51            module.getModuleContext().getLogService().error(e);
 52            return false;
 53        }
 54    }
 55
 56    private Class createTestClass(IModelingSession session, String testClassName, Package testCaseParentPackage) {
 57        IUmlModel model = session.getModel();
 58        Class testClass = model.createClass(testClassName, testCaseParentPackage);
 59        return testClass;
 60    }
 61
 62    private Package getTestCaseParentPackage(Class classToTest, IModule module) {
 63        IModelingSession session = module.getModuleContext().getModelingSession();
 64        try (ITransaction t = session.createTransaction("Create test hierarchy")) {
 65            // Creating/updating the test model hierarchy
 66        
 67            // TODO for now, create the test class in the same package as the class to test
 68            Package parent = (Package) classToTest.getOwner();
 69        
 70            t.commit();
 71            return parent;
 72        } catch (Exception e) {
 73            // Report error to the log
 74            module.getModuleContext().getLogService().error(e);
 75            return null;
 76        }
 77    }
 78
 79    private boolean stereotypeTestCase(Class testClass, IModule module) {
 80        IModelingSession session = module.getModuleContext().getModelingSession();
 81        try (ITransaction t = session.createTransaction("Stereotype a test case")) {
 82            Stereotype s = session.getMetamodelExtensions().getStereotype(module.getName(), "JUnit", module.getModuleContext().getModelioServices().getMetamodelService().getMetamodel().getMClass(Class.class));
 83            if (s != null) {
 84                // Add the stereotype to the class
 85                testClass.getExtension().add(s);
 86                t.commit();
 87                return true;
 88            } else {
 89                MessageDialog.openError(Display.getCurrent().getActiveShell(),
 90                        "Error",
 91                        "Stereotype JUnit not found, check your installation");
 92                return false;
 93            }
 94        }
 95    }
 96
 97    private boolean linkTestCase(Class classToTest, Class testClass, IModule module) {
 98        IModelingSession session = module.getModuleContext().getModelingSession();
 99        try (ITransaction t = session.createTransaction("Stereotype a test case")) {
100            //Create the dependency
101            IUmlModel model = session.getModel();
102            model.createDependency(testClass,classToTest, module.getName(), "JUnitDependency");
103        
104            t.commit();
105            return true;
106        } catch (ExtensionNotFoundException e) {
107            MessageDialog.openError(Display.getCurrent().getActiveShell(),
108                    "Error",
109                    "Stereotype JUnitDependency not found, check your installation");
110            return false;
111        } catch (Exception e) {
112            // Report error to the log
113            module.getModuleContext().getLogService().error(e);
114            return false;
115        }
116    }
117}

<< Previous Index Next >>