Commit 32c38634 authored by Martti Käärik's avatar Martti Käärik
Browse files

Extract test component (and control) into class field (instead of inheritance)...

Extract test component (and control) into class field (instead of inheritance) and create separate method for test behaviour to facilitate test description references #106
parent 37188bb7
Loading
Loading
Loading
Loading
Loading
+56 −31
Original line number Diff line number Diff line
@@ -106,10 +106,10 @@ public class JUnitTestGenerator extends Renderer {

	public static final String PACKAGE_PREFIX = "org.etsi.mts.tdl.execution.java",
			CORE_PACKAGE = PACKAGE_PREFIX + ".rt.core", TRI_PACKAGE = PACKAGE_PREFIX + ".tri",
			TESTER_CLASS = "TestControl", FUNCTIONS_FIELD = "functions", SYSTEM_ADAPTER_FIELD = "systemAdapter",
			VALIDATOR_FIELD = "validator", REPORTER_FIELD = "reporter", HELPER_FIELD = "runtimeHelper";
			TESTER_CLASS = "TestControl", COMPONENT_FIELD = "tc", FUNCTIONS_FIELD = COMPONENT_FIELD + ".functions", SYSTEM_ADAPTER_FIELD = COMPONENT_FIELD + ".systemAdapter",
			VALIDATOR_FIELD = COMPONENT_FIELD + ".validator", REPORTER_FIELD = COMPONENT_FIELD + ".reporter", HELPER_FIELD = "runtimeHelper";

	public static final String COMPONENT_CLASS_SUFFIX = "_Component",
	public static final String COMPONENT_CLASS_SUFFIX = "_Component", BEHAVIOR_METHOD_PREFIX = "td_",
			ASSERTION_EXCEPTION = "junit.framework.AssertionFailedError",
			FUTURE_EXECUTION_EXCEPTION = "java.util.concurrent.ExecutionException",
			INTERRUPTED_EXCEPTION = "InterruptedException", BREAK_EXCEPTION = "BreakException",
@@ -353,7 +353,7 @@ public class JUnitTestGenerator extends Renderer {

		// XXX Gates

		append("protected " + className + "()");
		append("public " + className + "()");
		blockOpen();
		append("super(");
		append("new ");
@@ -414,13 +414,16 @@ public class JUnitTestGenerator extends Renderer {
		gatherImports(td, imports);
		writeImports(imports);

		String componentClassName = classNames.get(tester.getType());

		line("@TestInstance(TestInstance.Lifecycle.PER_CLASS)");
		append("public class " + className + " extends " + componentClassName);
		append("public class " + className);
		blockOpen();
		newLine();
		
		// Component instance
		String componentClassName = classNames.get(tester.getType());
		line("private " + componentClassName + " " + COMPONENT_FIELD + ";");

		// Declare TimeLabels
		td.eAllContents().forEachRemaining(e -> {
			if (e instanceof TimeLabel) {
@@ -430,13 +433,13 @@ public class JUnitTestGenerator extends Renderer {
		});
		newLine();

		writeTestConfiguration(td);
		writeTestConfiguration(td, tester);

		newLine();
		writeTypes(td);

		newLine();
		writeTestDescription(td);
		writeTestDescription(td, tester);

		blockClose();

@@ -471,6 +474,8 @@ public class JUnitTestGenerator extends Renderer {
	protected void gatherImports(Set<String> imports) {
//		imports.add("org.junit.*");
		imports.add("org.junit.jupiter.api.*");
		imports.add("org.junit.jupiter.params.*");
		imports.add("org.junit.jupiter.params.provider.*");
//		imports.add("org.junit.Test");
//		imports.add("org.junit.Assert");

@@ -482,19 +487,24 @@ public class JUnitTestGenerator extends Renderer {

		imports.add(TRI_PACKAGE + ".*");
		imports.add(CORE_PACKAGE + ".*");
		imports.add(CORE_PACKAGE + "." + TESTER_CLASS + ".*");
	}

	protected void gatherImports(Element el, Set<String> imports) {
		this.elementMappings.keySet().forEach(e -> addImport(getMapping(e), imports));
	}

	private void writeTestConfiguration(TestDescription tc) {
	private void writeTestConfiguration(TestDescription tc, ComponentInstance tester) {
		TestConfiguration config = tc.getTestConfiguration();

		line("@BeforeAll");
		append("public void configure_" + getElementName(tc) + "()");
		blockOpen();
		
		// Component instance
		String componentClassName = classNames.get(tester.getType());
		line(COMPONENT_FIELD + " = new " + componentClassName + "();");

		final List<String> connectionNames = new ArrayList<String>();
		config.getConnection().forEach(conn -> {
			String connectionName = getElementName(conn);
@@ -510,7 +520,7 @@ public class JUnitTestGenerator extends Renderer {
			callClose();

		});
		append("configure(new Connection[] {");
		append(COMPONENT_FIELD + ".configure(new Connection[] {");
		boolean first = true;
		for (String cn : connectionNames) {
			if (!first)
@@ -541,7 +551,7 @@ public class JUnitTestGenerator extends Renderer {
	}

	private void writeGetConnection(GateReference tester, GateReference remote) {
		append("getConnection(\"" + tester.getComponent().getName() + "\", \"" + tester.getGate().getName() + "\", \""
		append(COMPONENT_FIELD + ".getConnection(\"" + tester.getComponent().getName() + "\", \"" + tester.getGate().getName() + "\", \""
				+ remote.getComponent().getName() + "\", \"" + remote.getGate().getName() + "\")");
	}

@@ -703,7 +713,7 @@ public class JUnitTestGenerator extends Renderer {
			append("new ElementImpl(\"" + e.getName() + "\")");
	}

	private void writeTestDescription(TestDescription tc) {
	private void writeTestDescription(TestDescription tc, ComponentInstance tester) {

		for (TestObjective to : tc.getTestObjective()) {
			line("/**");
@@ -716,10 +726,21 @@ public class JUnitTestGenerator extends Renderer {

		line("@Test");
		
		// XXX parameters

		append("public void test_" + getElementName(tc) + "()");
		blockOpen();
		line(BEHAVIOR_METHOD_PREFIX + getElementName(tc) + "(" + COMPONENT_FIELD + ");");;
		blockClose();
		newLine();
		

		String componentClassName = classNames.get(tester.getType());
		append("public void " + BEHAVIOR_METHOD_PREFIX + getElementName(tc) + "(" + componentClassName + " " + COMPONENT_FIELD + ")");
		blockOpen();
		newLine();
		

		append("try");
		blockOpen();

@@ -860,7 +881,6 @@ public class JUnitTestGenerator extends Renderer {
			else if (b instanceof Assignment) {
				VariableUse v = ((Assignment) b).getVariable();
				initializeDataUse(((Assignment) b).getExpression(), dataUseVariables);
				append("this.");
				write(v, dataUseVariables);
				append(" = ");
				write(((Assignment) b).getExpression(), dataUseVariables);
@@ -997,7 +1017,8 @@ public class JUnitTestGenerator extends Renderer {
			// Test description call
			else if (b instanceof TestDescriptionReference) {
				TestDescription td = ((TestDescriptionReference) b).getTestDescription();
				// XXX
				String tdName = getElementName(td);
				line("new " + tdName + "()." + BEHAVIOR_METHOD_PREFIX + tdName + "(" + COMPONENT_FIELD + ");");;

			}

@@ -1031,7 +1052,7 @@ public class JUnitTestGenerator extends Renderer {
				Set<String> innerExceptions = new HashSet<String>();
				write(eb.getBlock(), exceptionalBehaviourName, null, innerExceptions);

				line("addExceptionalBehaviour(" + exceptionalBehaviourName + ");");
				line(COMPONENT_FIELD + ".addExceptionalBehaviour(" + exceptionalBehaviourName + ");");
				newLine();
				exceptionalBehaviours.add(exceptionalBehaviourName);
			}
@@ -1099,14 +1120,14 @@ public class JUnitTestGenerator extends Renderer {

			newLine();
			exceptionalBehaviours.forEach(eb -> {
				line("removeExceptionalBehaviour(" + eb + ");");
				line(COMPONENT_FIELD + ".removeExceptionalBehaviour(" + eb + ");");
			});
		}

		if (futures == null) {
			if (!myFutures.isEmpty()) {
				String futureName = getElementName(b) + "_future";
				line("Future<ExecutionResult> " + futureName + " = next();");
				line("Future<ExecutionResult> " + futureName + " = " + COMPONENT_FIELD + ".next();");

				append("try ");
				blockOpen();
@@ -1129,7 +1150,7 @@ public class JUnitTestGenerator extends Renderer {
							for (ValueAssignment va : localTarget.getValueAssignment()) {
								lineComment("ValueAssignment");
								Variable var = va.getVariable();
								append(getElementName(var));
								append(COMPONENT_FIELD + "." + getElementName(var));
								append(" = ");
								append("(" + getElementName(var.getDataType()) + ")");
								line("result.data.getValue();");
@@ -1151,7 +1172,7 @@ public class JUnitTestGenerator extends Renderer {

				line(" else ");
				blockOpen();
				append("ExceptionalBehaviour exceptionalBehaviour = getExceptionalBehaviour(");
				append("ExceptionalBehaviour exceptionalBehaviour = " + COMPONENT_FIELD + ".getExceptionalBehaviour(");
				append(futureName);
				line(");");
				append("if (exceptionalBehaviour != null)");
@@ -1167,11 +1188,11 @@ public class JUnitTestGenerator extends Renderer {

				// Cancel futures
				myFutures.forEach(f -> {
					line("stop(" + f.varName + ");");
					line(COMPONENT_FIELD + ".stop(" + f.varName + ");");

					if (f.hasExceptionals) {
						append(f.varName);
						line("_exceptionals.forEach(f -> stop(f));");
						line("_exceptionals.forEach(f -> " + COMPONENT_FIELD + ".stop(f));");
					}
				});

@@ -1200,7 +1221,7 @@ public class JUnitTestGenerator extends Renderer {
				append("List<Future<ExecutionResult>> ");
				append(future.varName);
				append("_exceptionals = ");
				line("executeExceptionals();");
				line(COMPONENT_FIELD + ".executeExceptionals();");
			}
			return future;
		}
@@ -1258,7 +1279,7 @@ public class JUnitTestGenerator extends Renderer {
						blockOpen();

						lineComment("Disable while exceptional behaviour is executed");
						line("removeExceptionalBehaviour(" + exceptionalBehaviourName + ");");
						line(COMPONENT_FIELD + ".removeExceptionalBehaviour(" + exceptionalBehaviourName + ");");
						newLine();

						append("try");
@@ -1287,7 +1308,7 @@ public class JUnitTestGenerator extends Renderer {
			blockOpen();

			lineComment("Enable the exceptional behaviour again");
			line("addExceptionalBehaviour(" + exceptionalBehaviourName + ");");
			line(COMPONENT_FIELD + ".addExceptionalBehaviour(" + exceptionalBehaviourName + ");");
			blockClose();
			blockCloseMethod();

@@ -1321,7 +1342,7 @@ public class JUnitTestGenerator extends Renderer {
	private FutureInfo declareFuture(Behaviour b, TimeConstraint tc, Map<DataUse, String> dataUseVariables) {
		String futureName = "timeConstraint_" + getElementName(tc) + "_" + getElementName(b);

		line("Future<ExecutionResult> " + futureName + " = timeConstraint(() -> ");
		line("Future<ExecutionResult> " + futureName + " = " + COMPONENT_FIELD + ".timeConstraint(() -> ");
		blockOpen();

		DataUse exp = tc.getTimeConstraintExpression();
@@ -1351,7 +1372,7 @@ public class JUnitTestGenerator extends Renderer {
			String futureName = "timeout_" + getElementName(b);

			String timerName = getTimerName(((TimerOperation) b).getTimer());
			append(typeSignature + " timeout_" + getElementName(b) + " = timeout(" + timerName + ")");
			append(typeSignature + " timeout_" + getElementName(b) + " = " + COMPONENT_FIELD + ".timeout(" + timerName + ")");
			if (autoSubmit)
				append(".execute()");
			line(";");
@@ -1364,7 +1385,7 @@ public class JUnitTestGenerator extends Renderer {
			DataUse period = ((TimeOperation) b).getPeriod();
			initializeDataUse(period, dataUseVariables);

			append(typeSignature + " " + futureName + " = sleep(");
			append(typeSignature + " " + futureName + " = " + COMPONENT_FIELD + ".sleep(");
			write(period, dataUseVariables);
			append(")");
			if (autoSubmit)
@@ -1379,7 +1400,7 @@ public class JUnitTestGenerator extends Renderer {
			DataUse period = ((TimeOperation) b).getPeriod();
			initializeDataUse(period, dataUseVariables);

			append(typeSignature + " " + futureName + " = noInput(");
			append(typeSignature + " " + futureName + " = " + COMPONENT_FIELD + ".noInput(");
			write(period, dataUseVariables);
			append(", ");
			GateReference gr = ((Quiescence) b).getGateReference();
@@ -1401,7 +1422,7 @@ public class JUnitTestGenerator extends Renderer {
			DataUse arg = m.getArgument();
			initializeDataUse(arg, dataUseVariables);

			append(typeSignature + " " + futureName + " = " + (m.isIsTrigger() ? "trigger" : "receive") + "(");
			append(typeSignature + " " + futureName + " = " + COMPONENT_FIELD + "." + (m.isIsTrigger() ? "trigger" : "receive") + "(");
			convertToData(arg, dataUseVariables);
			append(", ");
			writeGetConnection(localTarget.getTargetGate(), m.getSourceGate());
@@ -1437,7 +1458,7 @@ public class JUnitTestGenerator extends Renderer {
				arguments.forEach(arg -> initializeDataUse(arg.getDataUse(), dataUseVariables));
				replyArguments.forEach(arg -> initializeDataUse(arg.getDataUse(), dataUseVariables));

				append(typeSignature + " " + futureName + " = call(");
				append(typeSignature + " " + futureName + " = " + COMPONENT_FIELD + ".call(");
				writeCallArguments(pc, reply[0], true, dataUseVariables);
				append(", ");
				writeGetConnection(localTarget.getTargetGate(), pc.getSourceGate());
@@ -1677,6 +1698,8 @@ public class JUnitTestGenerator extends Renderer {
			return;
		if (d instanceof SpecialValueUse)
			return;
		if (d instanceof VariableUse)
			return;

		for (MemberReference r : d.getReduction()) {
			DataUse idx = r.getCollectionIndex();
@@ -1862,7 +1885,7 @@ public class JUnitTestGenerator extends Renderer {
				append(classQName + "." + functionName);

			} else if (predefinedFunction != null) {
				append("this." + FUNCTIONS_FIELD + "." + getPredefinedFunction(predefinedFunction));
				append(FUNCTIONS_FIELD + "." + getPredefinedFunction(predefinedFunction));

			}

@@ -1992,6 +2015,8 @@ public class JUnitTestGenerator extends Renderer {
				append("TimeUnit." + getElementName(type) + ".toSeconds(" + dataUseVariables.get(d) + ")");

			} else {
				if (d instanceof VariableUse)
					append(COMPONENT_FIELD + ".");
				append(dataUseVariables.get(d));
				if (isUnmapped())
					append(".asData()");