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

Fixed unmapped type declarations (rendered class no longer extends TestControl...

Fixed unmapped type declarations (rendered class no longer extends TestControl + use qualified names as registry keys)
parent 47a68762
Loading
Loading
Loading
Loading
+61 −27
Original line number Diff line number Diff line
@@ -117,7 +117,8 @@ 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", 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";
			VALIDATOR_FIELD = COMPONENT_FIELD + ".validator", REPORTER_FIELD = COMPONENT_FIELD + ".reporter", HELPER_FIELD = "runtimeHelper",
			DECLARED_TYPES_FIELD = "declared_types";

	public static final String COMPONENT_CLASS_SUFFIX = "_Component", BEHAVIOR_METHOD_PREFIX = "td_",
			ASSERTION_EXCEPTION = "junit.framework.AssertionFailedError",
@@ -167,7 +168,11 @@ public class JUnitTestGenerator extends Renderer {

	private ComponentInstance currentTester = null;

	private Map<DataType, String> declaredTypes = new Hashtable<DataType, String>();
	/**
	 * The Java variables holding the TypeImpl of the declared types, used
	 * temporarily during type declarations.
	 */
	private Map<DataType, String> typeVariables = new Hashtable<DataType, String>();

	/**
	 * Suffix of the variable holding the {@code TemplateImpl} that accompanies a
@@ -536,7 +541,21 @@ public class JUnitTestGenerator extends Renderer {
		blockOpen();
		line("return " + COMPONENT_FIELD + ";");
		blockClose();
		newLine();
		
		line("/**\n"
				+ "	 * This is used to declare types and mappings if using unmapped data in TRI.\n"
				+ "	 */");
		line("protected Map<String, TypeImpl> " + DECLARED_TYPES_FIELD + " = new Hashtable<String, TypeImpl>();");
		append("protected void addType(String name, TypeImpl type)");
		blockOpen();
		line("this." + DECLARED_TYPES_FIELD + ".put(name, type);");
		blockClose();
		append("protected TypeImpl getType(String name)");
		blockOpen();
		line("return this." + DECLARED_TYPES_FIELD + ".get(name);");
		blockClose();
		newLine();

		// Declare TimeLabels
		td.eAllContents().forEachRemaining(e -> {
@@ -551,7 +570,7 @@ public class JUnitTestGenerator extends Renderer {
		writeTestShutdown(td);

		newLine();
		writeTypes(td);
		declareUnmappedTypes(td);

		newLine();
		writeTestDescription(td, tester);
@@ -649,6 +668,9 @@ public class JUnitTestGenerator extends Renderer {
		writeElement(tester);
		line(");");
		
		if (isUnmapped())
			line("declareTypes();");

		blockClose();
		newLine();
	}
@@ -689,7 +711,9 @@ public class JUnitTestGenerator extends Renderer {
		append(COMPONENT_FIELD + ".getGateReference(\"" + tester.getComponent().getName() + "\", \"" + tester.getGate().getName() + "\")");
	}

	private void writeTypes(TestDescription tc) {
	private void declareUnmappedTypes(TestDescription tc) {
		// Each rendered class declares its own type registry
		typeVariables.clear();
		if (!isUnmapped())
			return;

@@ -702,9 +726,9 @@ public class JUnitTestGenerator extends Renderer {
			if (e instanceof DataUse) {
				DataType type = ((DataUse) e).resolveDataType();
				if (type != null)
					declareType(type, declaredTypes);
					declareType(type);
			} else if (e instanceof Variable) {
				declareType(((Variable) e).getDataType(), declaredTypes);
				declareType(((Variable) e).getDataType());
			}

		}
@@ -739,31 +763,33 @@ public class JUnitTestGenerator extends Renderer {
		newLine();
	}

	private String declareType(DataType t, Map<DataType, String> declaredTypes) {
		if (declaredTypes.containsKey(t))
			return declaredTypes.get(t);
		String name = super.getElementName(t);
		declaredTypes.put(t, name);
	private void declareType(DataType t) {
		if (typeVariables.containsKey(t))
			return;
		// Types are registered under their TDL name, independent of any mapping
		String name = t.getQualifiedName();
		String variable = createTypeVariable(name);
		typeVariables.put(t, variable);

		if (t instanceof StructuredDataType) {
			for (Member m : ((StructuredDataType) t).allMembers()) {
				DataType mType = m.getDataType();
				declareType(mType, declaredTypes);
				declareType(mType);
			}
			for (org.etsi.mts.tdl.Extension e : ((StructuredDataType) t).getExtension()) {
				DataType superType = (DataType) e.getExtending();
				declareType(superType, declaredTypes);
				declareType(superType);
			}
		} else if (t instanceof CollectionDataType) {
			DataType iType = ((CollectionDataType) t).getItemType();
			declareType(iType, declaredTypes);
			declareType(iType);
		}

		// TODO add mapping name to mapping
		DataElementMapping mapping = getMapping(t);
		if (mapping == null)
			mapping = getMapping(t, settings.useMapping);
		String mappingName = name + "_mapping";
		String mappingName = variable + "_mapping";
		if (mapping != null) {
			writeMapping(mapping, mappingName, false);
			if (t instanceof StructuredDataType) {
@@ -771,14 +797,14 @@ public class JUnitTestGenerator extends Renderer {
					StructuredDataType superType = (StructuredDataType) e.getExtending();
					DataElementMapping superMapping = getMapping(superType);
					if (superMapping != null) {
						String superMappingName = super.getElementName(superType) + "_mapping";
						String superMappingName = typeVariables.get(superType) + "_mapping";
						line(mappingName + ".setParent(" + superMappingName + ");");
					}
				}
			}
		}

		line(CORE_PACKAGE + ".TypeImpl " + name + " = new " + CORE_PACKAGE + ".TypeImpl()");
		line(CORE_PACKAGE + ".TypeImpl " + variable + " = new " + CORE_PACKAGE + ".TypeImpl()");
		writeSetName(t);
		writeAddAnnotations(t);
		if (mapping != null) {
@@ -787,19 +813,25 @@ public class JUnitTestGenerator extends Renderer {
		if (t instanceof StructuredDataType) {
			line(".setIsStructure(true)");
			for (Member m : ((StructuredDataType) t).allMembers()) {
				String mTypeName = declaredTypes.get(m.getDataType());
				line(".setParameter(\"" + m.getName() + "\", " + mTypeName + ")");
				line(".setParameter(\"" + m.getName() + "\", " + typeVariables.get(m.getDataType()) + ")");
			}

		} else if (t instanceof CollectionDataType) {
			line(".setIsCollection(true)");
			String iTypeName = declaredTypes.get(((CollectionDataType) t).getItemType());
			line(".setItemType(" + iTypeName + ")");
			line(".setItemType(" + typeVariables.get(((CollectionDataType) t).getItemType()) + ")");
		}
		line(";");

		line("addType(\"" + name + "\", " + name + ");");
		return name;
		line("addType(\"" + escape(name) + "\", " + variable + ");");
	}

	private String createTypeVariable(String qualifiedName) {
		String base = "type_" + qualifiedName.replaceAll("[^A-Za-z0-9_]", "_");
		String variable = base;
		int i = 2;
		while (typeVariables.containsValue(variable))
			variable = base + "_" + i++;
		return variable;
	}

	private void writeMapping(DataElementMapping mapping, String mappingVarName, boolean inline) {
@@ -875,7 +907,7 @@ public class JUnitTestGenerator extends Renderer {

		append("public void test_" + getElementName(tc) + "()");
		blockOpen();
		line(BEHAVIOR_METHOD_PREFIX + getElementName(tc) + "(" + COMPONENT_FIELD + ");");;
		line(BEHAVIOR_METHOD_PREFIX + getElementName(tc) + "(" + COMPONENT_FIELD + ");");
		blockClose();
		newLine();
		
@@ -1299,7 +1331,7 @@ public class JUnitTestGenerator extends Renderer {
			else if (b instanceof TestDescriptionReference) {
				TestDescription td = ((TestDescriptionReference) b).getTestDescription();
				String tdName = getElementName(td);
				line("new " + tdName + "()." + BEHAVIOR_METHOD_PREFIX + tdName + "(" + COMPONENT_FIELD + ");");;
				line("new " + tdName + "()." + BEHAVIOR_METHOD_PREFIX + tdName + "(" + COMPONENT_FIELD + ");");

			}

@@ -2238,8 +2270,10 @@ public class JUnitTestGenerator extends Renderer {
			}
		}

		String typeName = declaredTypes.get(d.resolveDataType());
		String unmappedDataInitializer = " = new " + CORE_PACKAGE + ".ValueImpl(getType(\"" + typeName + "\"))";
		DataType dataUseType = d.resolveDataType();
		String typeName = typeVariables.containsKey(dataUseType) ? dataUseType.getQualifiedName() : null;
		String unmappedDataInitializer = " = new " + CORE_PACKAGE + ".ValueImpl("
				+ (typeName != null ? "getType(\"" + escape(typeName) + "\")" : "") + ")";

		if (dataInstance != null) {
			DataElementMapping m = getMapping(dataInstance);
+1 −18
Original line number Diff line number Diff line
@@ -3,9 +3,9 @@ package org.etsi.mts.tdl.execution.java.rt.core;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
@@ -61,8 +61,6 @@ public class TestControl {
				}
			};

	protected Map<String, TypeImpl> types = new Hashtable<String, TypeImpl>();

	public TestControl(com.google.inject.Module guiceModule) {
		super();

@@ -145,7 +143,6 @@ public class TestControl {
			hub.setAnyReceiver(anyReceiver);
		}

		declareTypes();
	}
	
	public NamedElement getTesterComponent() {
@@ -670,18 +667,4 @@ public class TestControl {
		}
	}

	/**
	 * This is called once before each test case. Implemented by generated code to
	 * declare types and mappings if using unmapped data in TRI.
	 */
	protected void declareTypes() {
	}

	protected void addType(String name, TypeImpl type) {
		this.types.put(name, type);
	}

	protected TypeImpl getType(String name) {
		return this.types.get(name);
	}
}