In these scenarios, all related items on this page to be used are in the TestSimulationCommandLine.zip sample. |
To prepare a simulation project and Configs in MagicDraw
Create Simulation Configs and set those Behaviors as executionTarget of those Simulation Configs.
All required resources must be available for the Simulation Configs, e.g., loadCSV and fmu. All features preventing simulation from the start/terminate, e.g., false autoStart, context without Classifier Behavior, chart windows at the end of execution, or output parameters at the end of execution (running Activity) will also be automatically started/terminated. |
Run each Simulation Config to verify that an expected result, either pass or fail, must be returned.
To run a project through simulate command line
Use the following simulate command syntax: simulate -project “[Path of an MDZIP project]” -config “[Config]”.
In this case, the following commands are used:
simulate -project “D:\\Simulation\\API\\TestVerdictKind.mdzip” -config “Run GeneratePassResult” simulate -project “D:\\Simulation\\API\\TestVerdictKind.mdzip” -config “Run GenerateFailResult” |
At the last line of the command prompt, pass or fail appears. You can also see the details of execution through the Simulation Log File.
To create a JUnit test case and configuration file
To configure Jenkins for the Automated testing
In the Properties box, specify md.root=[MagicDraw installation directory] and properties.files.dir=[Path containing the properties files], as shown in the example above.
The path containing the property files can be either an absolute (full) path, e.g., C:\Users\User\Desktop\TestSimulationCommandLine\tests or relative path, e.g., tests. |
Run the build. Test Result will be shown and allows you to view more details for those test cases.
Examples of the simulate command lines are as follows:
Type of argument | Parameter | Description | |
Mandatory argument | -project | Specify a file name with the path of a MagicDraw project. Both relative and absolute paths are supported. | |
-config | Specify a SimulationConfig without a qualified name to run.
| ||
-properties propertiesFileName | Specify the name of a properties file to use, e.g., in the TestSimulationCommandLine.zip sample.
| ||
Optional (server) argument | -servertype | Specify type of the server to connect, <tw> or <twcloud>, where <tw> is Teamwork Server, as the default type of the server, and <twcloud> is Teamwork Cloud. | |
-server | Specify a name or an IP Address (and port) of the server. | ||
-login | Specify a login name to log on to the server. | ||
-password | Specify a password to log on to the server. | ||
-ssl | Specify to use and enable Secured Connection (SSL), <true> or <false> as the default value. | ||
-pversion | Specify the version of the server project. | ||
-tag | Specify the tag name of the server project.
| ||
Teamwork Cloud argument | -branch | Specify a branch of Teamwork Cloud projects.
| |
-updatemodule | Specify update project usages if required, <true> or <false>, where <false> is the default. | ||
-projectpassword | Specify a project password. |
package com.nomagic.magicdraw.simulation; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.nio.file.Files; import java.nio.file.Path; import org.junit.Before; import org.junit.Test; /** * A test class for running the simulation command line and checking for the pass/fail result. * @author Chanon S. */ public class TestSimulationCommandLine { private static final String MD_ROOT_PROPERTY = "md.root"; private static final String PROPERTIES_FILES_PROPERTY = "properties.files.dir"; private static final String MD_DIR = System.getProperty(MD_ROOT_PROPERTY); private static final String SIMULATION_COMMAND_LINE = MD_DIR + "/plugins/com.nomagic.magicdraw.simulation/simulate.sh"; private static final String PROPERTIES_FILES_DIR = System.getProperty(PROPERTIES_FILES_PROPERTY); private static final String PASS_PROPERTIES = "TestGeneratePassResult.properties"; private static final String FAIL_PROPERTIES = "TestGenerateFailResult.properties"; private static final String SYSTEM_NEW_LINE = System.getProperty("line.separator"); @Before public void assertBuildExist() { System.out.println(MD_ROOT_PROPERTY + "=" + MD_DIR); System.out.println(PROPERTIES_FILES_PROPERTY + "=" + PROPERTIES_FILES_DIR); assertNotNull(MD_ROOT_PROPERTY + " is not specified.", MD_DIR); assertNotNull(PROPERTIES_FILES_PROPERTY + " is not specified.", PROPERTIES_FILES_DIR); assertTrue("MagicDraw directory does not exist, MD_DIR=" + MD_DIR, new File(MD_DIR).exists()); assertTrue("Properties files directory does not exist,PROPERTIES_FILES_DIR=" + PROPERTIES_FILES_DIR, new File(PROPERTIES_FILES_DIR).exists()); assertTrue("Could not find simulate.sh file, SIMULATION_COMMAND_LINE=", new File(SIMULATION_COMMAND_LINE).exists()); } @Test(timeout = 120000) public void testGeneratePassResult() { // This project and Simulation Config generate "pass" as the result of TestCase. String propertyFilePath = PROPERTIES_FILES_DIR + "/" + PASS_PROPERTIES; String consoleOutput = runWithProperties(propertyFilePath); System.out.println("Console output=" + consoleOutput); boolean result = consoleOutput.contains(SYSTEM_NEW_LINE + "pass" + SYSTEM_NEW_LINE); assertTrue(consoleOutput, result); } @Test(timeout = 120000) public void testGenerateFailResult() { // This project and Simulation Config generate "fail" as the result of TestCase. String propertyFilePath = PROPERTIES_FILES_DIR + "/" + FAIL_PROPERTIES; String consoleOutput = runWithProperties(propertyFilePath); boolean result = consoleOutput.contains(SYSTEM_NEW_LINE + "pass" + SYSTEM_NEW_LINE); assertTrue(consoleOutput, result); } private String execute(String command) { System.out.println("Executing, command=" + command); StringBuilder result = new StringBuilder(); try { Process p = Runtime.getRuntime().exec(command); BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; while ((line = input.readLine()) != null) { result.append(line); result.append(SYSTEM_NEW_LINE); } } catch (IOException e) { e.printStackTrace(); } return result.toString(); } private String runWithProperties(String propFilePath) { String absPropPath = propFilePath; if (propFilePath != null) { Path path = new File(propFilePath).toPath(); if (!Files.exists(path)) { fail("Properties file <" + propFilePath + "> not found."); } else { absPropPath = path.toAbsolutePath().toString(); } } String cmd = propFilePath == null || propFilePath.isEmpty() ? SIMULATION_COMMAND_LINE : SIMULATION_COMMAND_LINE + " -properties \"" + absPropPath + "\""; return execute(cmd); } } |
<project name="Run JUnit and Generate Reports" default="build" basedir="."> <target name="build" depends="set.properties, prepare, run.junit" /> <target name="set.properties"> <property name="md.root" location="${md.root}" /> <property name="properties.files.dir" location="${properties.files.dir}" /> <property name="test.reports.dir" location="test-reports"/> </target> <target name="prepare"> <delete dir="${test.reports.dir}" /> <mkdir dir="${test.reports.dir}" /> </target> <target name="run.junit"> <junit printsummary="yes" fork="yes"> <classpath> <pathelement location="lib/TestSimulationCommandLine.jar"/> <pathelement location="lib/junit-4.12.jar"/> <pathelement location="lib/hamcrest-core-1.3.jar"/> </classpath> <jvmarg value="-Xmx1200m" /> <jvmarg value="-Xms256m" /> <jvmarg value="-XX:PermSize=128M" /> <jvmarg value="-XX:MaxPermSize=256M" /> <jvmarg value="-XX:-UseSplitVerifier" /> <jvmarg value="-noverify" /> <sysproperty key="md.root" value="${md.root}" /> <sysproperty key="properties.files.dir" value="${properties.files.dir}" /> <batchtest todir="${test.reports.dir}"> <zipfileset src="lib/TestSimulationCommandLine.jar"> <include name="**/*.class"/> </zipfileset> </batchtest> <formatter type="xml" /> </junit> </target> </project> |