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
 57    private Class createTestClass(IModelingSession session, String testClassName, Package testCaseParentPackage) {
 58        IUmlModel model = session.getModel();
 59        Class testClass = model.createClass(testClassName, testCaseParentPackage);
 60        return testClass;
 61    }
 62
 63    private Package getTestCaseParentPackage(Class classToTest, IModule module) {
 64        IModelingSession session = module.getModuleContext().getModelingSession();
 65        try (ITransaction t = session.createTransaction("Create test hierarchy")) {
 66            // Creating/updating the test model hierarchy
 67        
 68            // TODO for now, create the test class in the same package as the class to test
 69            Package parent = (Package) classToTest.getOwner();
 70        
 71            t.commit();
 72            return parent;
 73        } catch (Exception e) {
 74            // Report error to the log
 75            module.getModuleContext().getLogService().error(e);
 76            return null;
 77        }
 78    }
 79
 80    private boolean stereotypeTestCase(Class testClass, IModule module) {
 81        IModelingSession session = module.getModuleContext().getModelingSession();
 82        try (ITransaction t = session.createTransaction("Stereotype a test case")) {
 83            Stereotype s = session.getMetamodelExtensions().getStereotype(module.getName(), "JUnit", module.getModuleContext().getModelioServices().getMetamodelService().getMetamodel().getMClass(Class.class));
 84            if (s != null) {
 85                // Add the stereotype to the class
 86                testClass.getExtension().add(s);
 87                t.commit();
 88                return true;
 89            } else {
 90                MessageDialog.openError(Display.getCurrent().getActiveShell(),
 91                        "Error",
 92                        "Stereotype JUnit not found, check your installation");
 93                return false;
 94            }
 95        }
 96    }
 97
 98    private boolean linkTestCase(Class classToTest, Class testClass, IModule module) {
 99        IModelingSession session = module.getModuleContext().getModelingSession();
100        try (ITransaction t = session.createTransaction("Stereotype a test case")) {
101            //Create the dependency
102            IUmlModel model = session.getModel();
103            model.createDependency(testClass,classToTest, module.getName(), "JUnitDependency");
104        
105            t.commit();
106            return true;
107        } catch (ExtensionNotFoundException e) {
108            MessageDialog.openError(Display.getCurrent().getActiveShell(),
109                    "Error",
110                    "Stereotype JUnitDependency not found, check your installation");
111            return false;
112        } catch (Exception e) {
113            // Report error to the log
114            module.getModuleContext().getLogService().error(e);
115            return false;
116        }
117    }
118}

<< Previous Index Next >>