Skip to content

Commit

Permalink
Sanitize test class name removing $ (#831)
Browse files Browse the repository at this point in the history
* change replacement for java identifiers to underscore

* sanitize test name from skip token

* add function tests

* add java keyword test
  • Loading branch information
MauricioUyaguari authored Sep 5, 2024
1 parent 326d1cf commit 4fc4d05
Show file tree
Hide file tree
Showing 22 changed files with 283 additions and 11 deletions.
5 changes: 5 additions & 0 deletions legend-sdlc-test-generation-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@
<artifactId>maven-compat</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.finos.legend.engine</groupId>
<artifactId>legend-engine-test-runner-function</artifactId>
<scope>test</scope>
</dependency>
<!-- TEST -->
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void testFullModel() throws Exception
assertDirectoryEmpty(outputDir);
executeMojo(projectDir);

SortedMap<String, String> expected = loadExpectedJavaSources("org/finos/legend/sdlc/test/junit/junit4/execution/TestRelationalMapping.java", "org/finos/legend/sdlc/test/junit/junit4/legend/demo/TestSingleQuoteInResultM2M.java", "org/finos/legend/sdlc/test/junit/junit4/model/mapping/TestSourceToTargetM2M.java", "org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestTestService.java", "org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestTestService2.java", "org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestServiceStoreMapping.java");
SortedMap<String, String> expected = loadExpectedJavaSources("org/finos/legend/sdlc/test/junit/junit4/execution/TestRelationalMapping.java", "org/finos/legend/sdlc/test/junit/junit4/legend/demo/TestSingleQuoteInResultM2M.java", "org/finos/legend/sdlc/test/junit/junit4/model/mapping/TestSourceToTargetM2M.java", "org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestTestService.java", "org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestTestService2.java", "org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestServiceStoreMapping.java", "org/finos/legend/sdlc/test/junit/junit4/model/domain/TestFunctionTest__String_0_1_.java");

SortedMap<String, String> actual = loadJavaSourcesFromDirectory(outputDir.toPath());
Assert.assertEquals(expected, actual);
Expand All @@ -125,7 +125,7 @@ public void testFullModelWithExcludedEntities() throws Exception
assertDirectoryEmpty(outputDir);
executeMojo(projectDir);

SortedMap<String, String> expected = loadExpectedJavaSources("org/finos/legend/sdlc/test/junit/junit4/legend/demo/TestSingleQuoteInResultM2M.java", "org/finos/legend/sdlc/test/junit/junit4/model/mapping/TestSourceToTargetM2M.java", "org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestTestService.java", "org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestServiceStoreMapping.java");
SortedMap<String, String> expected = loadExpectedJavaSources("org/finos/legend/sdlc/test/junit/junit4/legend/demo/TestSingleQuoteInResultM2M.java", "org/finos/legend/sdlc/test/junit/junit4/model/mapping/TestSourceToTargetM2M.java", "org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestTestService.java", "org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestServiceStoreMapping.java", "org/finos/legend/sdlc/test/junit/junit4/model/domain/TestFunctionTest__String_0_1_.java");

SortedMap<String, String> actual = loadJavaSourcesFromDirectory(outputDir.toPath());
Assert.assertEquals(expected, actual);
Expand All @@ -143,7 +143,7 @@ public void testFullModelWithExcludedPackages() throws Exception
assertDirectoryEmpty(outputDir);
executeMojo(projectDir);

SortedMap<String, String> expected = loadExpectedJavaSources("org/finos/legend/sdlc/test/junit/junit4/execution/TestRelationalMapping.java", "org/finos/legend/sdlc/test/junit/junit4/legend/demo/TestSingleQuoteInResultM2M.java");
SortedMap<String, String> expected = loadExpectedJavaSources("org/finos/legend/sdlc/test/junit/junit4/execution/TestRelationalMapping.java", "org/finos/legend/sdlc/test/junit/junit4/legend/demo/TestSingleQuoteInResultM2M.java", "org/finos/legend/sdlc/test/junit/junit4/model/domain/TestFunctionTest__String_0_1_.java");

SortedMap<String, String> actual = loadJavaSourcesFromDirectory(outputDir.toPath());
Assert.assertEquals(expected, actual);
Expand All @@ -161,7 +161,7 @@ public void testFullModelWithExcludedEntitiesAndPackages() throws Exception
assertDirectoryEmpty(outputDir);
executeMojo(projectDir);

SortedMap<String, String> expected = loadExpectedJavaSources("org/finos/legend/sdlc/test/junit/junit4/legend/demo/TestSingleQuoteInResultM2M.java", "org/finos/legend/sdlc/test/junit/junit4/model/mapping/TestSourceToTargetM2M.java");
SortedMap<String, String> expected = loadExpectedJavaSources("org/finos/legend/sdlc/test/junit/junit4/legend/demo/TestSingleQuoteInResultM2M.java", "org/finos/legend/sdlc/test/junit/junit4/model/mapping/TestSourceToTargetM2M.java", "org/finos/legend/sdlc/test/junit/junit4/model/domain/TestFunctionTest__String_0_1_.java");

SortedMap<String, String> actual = loadJavaSourcesFromDirectory(outputDir.toPath());
Assert.assertEquals(expected, actual);
Expand All @@ -179,7 +179,7 @@ public void testFullModelWithDependencies() throws Exception
assertDirectoryEmpty(outputDir);
executeMojo(projectDir);

SortedMap<String, String> expected = loadExpectedJavaSources("org/finos/legend/sdlc/test/junit/junit4/execution/TestRelationalMapping.java", "org/finos/legend/sdlc/test/junit/junit4/legend/demo/TestSingleQuoteInResultM2M.java", "org/finos/legend/sdlc/test/junit/junit4/model/mapping/TestSourceToTargetM2M.java", "org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestTestService.java", "org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestTestService2.java", "org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestServiceStoreMapping.java", "org/finos/legend/sdlc/test/junit/junit4/model/TestMyMapping.java");
SortedMap<String, String> expected = loadExpectedJavaSources("org/finos/legend/sdlc/test/junit/junit4/execution/TestRelationalMapping.java", "org/finos/legend/sdlc/test/junit/junit4/legend/demo/TestSingleQuoteInResultM2M.java", "org/finos/legend/sdlc/test/junit/junit4/model/mapping/TestSourceToTargetM2M.java", "org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestTestService.java", "org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestTestService2.java", "org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestServiceStoreMapping.java", "org/finos/legend/sdlc/test/junit/junit4/model/TestMyMapping.java", "org/finos/legend/sdlc/test/junit/junit4/model/domain/TestFunctionTest__String_0_1_.java");

SortedMap<String, String> actual = loadJavaSourcesFromDirectory(outputDir.toPath());
Assert.assertEquals(expected, actual);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.finos.legend.sdlc.test.junit.junit4.model.domain;

import org.finos.legend.sdlc.test.junit.pure.v1.AbstractTestableTest;

public class TestFunctionTest__String_0_1_ extends AbstractTestableTest
{
@Override
protected String getEntityPath()
{
return "model::domain::FunctionTest__String_$0_1$_";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"classifierPath" : "meta::pure::metamodel::function::ConcreteFunctionDefinition",
"content" : {
"_type" : "function",
"body" : [ {
"_type" : "string",
"value" : ""
} ],
"name" : "FunctionTest__String_$0_1$_",
"package" : "model::domain",
"parameters" : [ ],
"postConstraints" : [ ],
"preConstraints" : [ ],
"returnMultiplicity" : {
"lowerBound" : 0,
"upperBound" : 1
},
"returnType" : "String",
"stereotypes" : [ ],
"taggedValues" : [ ]
}
}
5 changes: 5 additions & 0 deletions legend-sdlc-test-generation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@
<artifactId>classgraph</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.finos.legend.engine</groupId>
<artifactId>legend-engine-test-runner-function</artifactId>
<scope>test</scope>
</dependency>
<!-- TEST -->
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class AbstractTestClassGenerator extends JavaCodeGenerator
{
private static final String ENTITY_PATH_PARAM = "entityPath";

private static final String TEST_SKIP_TOKEN = "$";

private final String packagePrefix;

private static final String TEST_PREFIX = "Test";
Expand Down Expand Up @@ -64,7 +66,12 @@ private String generateTestClassPackage(PackageableElement protocolElement)

private String generateTestClassName(PackageableElement protocolElement)
{
return appendJavaIdentifier(new StringBuilder(TEST_PREFIX), generateTestName(protocolElement.name)).toString();
return sanitizeTestName(appendJavaIdentifier(new StringBuilder(TEST_PREFIX), generateTestName(protocolElement.name)).toString());
}

private String sanitizeTestName(String name)
{
return name.replace(TEST_SKIP_TOKEN, "");
}

private String generateTestName(String name)
Expand All @@ -87,10 +94,10 @@ private String generateTestName(String name)

private StringBuilder appendJavaIdentifier(StringBuilder builder, String string)
{
char replacement = '$';
char replacement = '_';
if (string.isEmpty())
{
return builder.append(replacement);
return builder.append(replacement).append(replacement);
}
if (SourceVersion.isKeyword(string))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,26 @@ public void testServiceStoreMapping()
"testTestSuites::ServiceStoreMapping");
}

@Test
public void testFunctionTest()
{
testGeneration(
"model.domain.TestFunctionTest__String_0_1_",
"generated/java/model/domain/TestFunctionTest__String_0_1_.java",
null,
"model::domain::FunctionTest__String_$0_1$_");
testGeneration(
"org.finos.legend.sdlc.test.junit.junit4.model.domain.TestFunctionTest__String_0_1_",
"generated/java/org/finos/legend/sdlc/test/junit/junit4/model/domain/TestFunctionTest__String_0_1_.java",
"org.finos.legend.sdlc.test.junit.junit4",
"model::domain::FunctionTest__String_$0_1$_");
testGeneration(
"other.test.pkg.model.domain.TestFunctionTest__String_0_1_",
"generated/java/other/test/pkg/model/domain/TestFunctionTest__String_0_1_.java",
"other.test.pkg",
"model::domain::FunctionTest__String_$0_1$_");
}

@Test
public void testNonTestable()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ public void testSourceToTargetM2M()
testCompilation("model.mapping.TestSourceToTargetM2M", "model::mapping::SourceToTargetM2M");
}

@Test
public void testJavaKeyWordCompilation()
{
testCompilation("model.mapping.Test_public", "model::mapping::public");
testCompilation("model._synchronized.Test__", "model::synchronized::_");
}

@Test
public void testTestService()
{
Expand All @@ -109,6 +116,12 @@ public void testServiceStoreMapping()
testCompilation("testTestSuites.TestServiceStoreMapping", "testTestSuites::ServiceStoreMapping");
}

@Test
public void testFunctionTest()
{
testCompilation("model.domain.TestFunctionTest__String_0_1_", "model::domain::FunctionTest__String_$0_1$_");
}

private void testCompilation(String expectedClassName, String entityPath)
{
testCompilation(Collections.singletonList(expectedClassName), entityPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,52 @@ public static void cleanUp() throws Exception
@Test
public void testWriteTestClassesWithoutRootPackage() throws IOException
{
testWriteTestClasses(null, "generated/java/execution/TestRelationalMapping.java", "generated/java/execution/TestRelationalMapping.java", "generated/java/legend/demo/TestSingleQuoteInResultM2M.java", "generated/java/model/mapping/TestSourceToTargetM2M.java", "generated/java/testTestSuites/TestTestService.java", "generated/java/testTestSuites/TestTestService2.java", "generated/java/testTestSuites/TestServiceStoreMapping.java", "generated/java/testTestSuites/TestMyServiceIsVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVery_38d23576.java", "generated/java/testTestSuites/TestMyServiceIsââVeryââVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryââââVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryV_a0bb3f4b.java");
testWriteTestClasses(null, "generated/java/execution/TestRelationalMapping.java",
"generated/java/execution/TestRelationalMapping.java", "generated/java/legend/demo/TestSingleQuoteInResultM2M.java",
"generated/java/model/mapping/TestSourceToTargetM2M.java", "generated/java/testTestSuites/TestTestService.java",
"generated/java/testTestSuites/TestTestService2.java", "generated/java/testTestSuites/TestServiceStoreMapping.java",
"generated/java/testTestSuites/TestMyServiceIsVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVery_38d23576.java",
"generated/java/testTestSuites/TestMyServiceIsââVeryââVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryââââVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryV_a0bb3f4b.java",
"generated/java/model/domain/TestFunctionTest__String_0_1_.java",
"generated/java/model/_synchronized/Test__.java",
"generated/java/model/mapping/Test_public.java"
);
}

@Test
public void testWriteTestClasses() throws IOException
{
testWriteTestClasses("org.finos.legend.sdlc.test.junit.junit4", "generated/java/org/finos/legend/sdlc/test/junit/junit4/execution/TestRelationalMapping.java", "generated/java/org/finos/legend/sdlc/test/junit/junit4/legend/demo/TestSingleQuoteInResultM2M.java", "generated/java/org/finos/legend/sdlc/test/junit/junit4/model/mapping/TestSourceToTargetM2M.java", "generated/java/org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestTestService.java", "generated/java/org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestTestService2.java", "generated/java/org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestServiceStoreMapping.java", "generated/java/org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestMyServiceIsVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVery_38d23576.java", "generated/java/org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestMyServiceIsââVeryââVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryââââVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryV_a0bb3f4b.java");
testWriteTestClasses("org.finos.legend.sdlc.test.junit.junit4",
"generated/java/org/finos/legend/sdlc/test/junit/junit4/execution/TestRelationalMapping.java",
"generated/java/org/finos/legend/sdlc/test/junit/junit4/legend/demo/TestSingleQuoteInResultM2M.java",
"generated/java/org/finos/legend/sdlc/test/junit/junit4/model/mapping/TestSourceToTargetM2M.java",
"generated/java/org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestTestService.java",
"generated/java/org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestTestService2.java",
"generated/java/org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestServiceStoreMapping.java",
"generated/java/org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestMyServiceIsVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVery_38d23576.java",
"generated/java/org/finos/legend/sdlc/test/junit/junit4/testTestSuites/TestMyServiceIsââVeryââVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryââââVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryV_a0bb3f4b.java",
"generated/java/org/finos/legend/sdlc/test/junit/junit4/model/domain/TestFunctionTest__String_0_1_.java",
"generated/java/org/finos/legend/sdlc/test/junit/junit4/model/_synchronized/Test__.java",
"generated/java/org/finos/legend/sdlc/test/junit/junit4/model/mapping/Test_public.java"
);
}

@Test
public void testWriteTestClassesOtherPackage() throws IOException
{
testWriteTestClasses("other.test.pkg", "generated/java/other/test/pkg/execution/TestRelationalMapping.java", "generated/java/other/test/pkg/legend/demo/TestSingleQuoteInResultM2M.java", "generated/java/other/test/pkg/model/mapping/TestSourceToTargetM2M.java", "generated/java/other/test/pkg/testTestSuites/TestTestService.java", "generated/java/other/test/pkg/testTestSuites/TestTestService2.java", "generated/java/other/test/pkg/testTestSuites/TestServiceStoreMapping.java", "generated/java/other/test/pkg/testTestSuites/TestMyServiceIsVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVery_38d23576.java", "generated/java/other/test/pkg/testTestSuites/TestMyServiceIsââVeryââVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryââââVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryV_a0bb3f4b.java");
testWriteTestClasses("other.test.pkg",
"generated/java/other/test/pkg/execution/TestRelationalMapping.java",
"generated/java/other/test/pkg/legend/demo/TestSingleQuoteInResultM2M.java",
"generated/java/other/test/pkg/model/mapping/TestSourceToTargetM2M.java",
"generated/java/other/test/pkg/testTestSuites/TestTestService.java",
"generated/java/other/test/pkg/testTestSuites/TestTestService2.java",
"generated/java/other/test/pkg/testTestSuites/TestServiceStoreMapping.java",
"generated/java/other/test/pkg/testTestSuites/TestMyServiceIsVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVery_38d23576.java",
"generated/java/other/test/pkg/testTestSuites/TestMyServiceIsââVeryââVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryââââVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryV_a0bb3f4b.java",
"generated/java/other/test/pkg/model/domain/TestFunctionTest__String_0_1_.java",
"generated/java/other/test/pkg/model/_synchronized/Test__.java",
"generated/java/other/test/pkg/model/mapping/Test_public.java"
);
}

private void testWriteTestClasses(String rootPackage, String... expectedResources) throws IOException
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"classifierPath" : "meta::pure::metamodel::function::ConcreteFunctionDefinition",
"content" : {
"_type" : "function",
"body" : [ {
"_type" : "string",
"value" : ""
} ],
"name" : "FunctionTest__String_$0_1$_",
"package" : "model::domain",
"parameters" : [ ],
"postConstraints" : [ ],
"preConstraints" : [ ],
"returnMultiplicity" : {
"lowerBound" : 0,
"upperBound" : 1
},
"returnType" : "String",
"stereotypes" : [ ],
"taggedValues" : [ ]
}
}
Loading

0 comments on commit 4fc4d05

Please sign in to comment.