From fbddb4ac51c3366cacf2c88af81395a68a8b44bd Mon Sep 17 00:00:00 2001 From: Rohitesh Date: Fri, 11 Jun 2021 19:32:21 +0530 Subject: [PATCH] Migrating all the template tests to Junit5 --- .../java/spoon/test/template/PatternTest.java | 40 ++++++++-------- .../template/TemplateArrayAccessTest.java | 4 +- .../template/TemplateClassAccessTest.java | 4 +- .../test/template/TemplateEnumAccessTest.java | 4 +- .../TemplateInvocationSubstitutionTest.java | 8 ++-- .../template/TemplateReplaceReturnTest.java | 4 +- .../spoon/test/template/TemplateTest.java | 46 +++++++++---------- .../test/template/core/ParameterInfoTest.java | 12 ++--- 8 files changed, 61 insertions(+), 61 deletions(-) diff --git a/src/test/java/spoon/test/template/PatternTest.java b/src/test/java/spoon/test/template/PatternTest.java index a0bafea7c88..2c7e248f3ae 100644 --- a/src/test/java/spoon/test/template/PatternTest.java +++ b/src/test/java/spoon/test/template/PatternTest.java @@ -16,7 +16,7 @@ */ package spoon.test.template; -import org.junit.Test; +import org.junit.jupiter.api.Test; import spoon.Launcher; import spoon.pattern.ConflictResolutionMode; import spoon.pattern.Match; @@ -81,13 +81,13 @@ import java.util.Set; import java.util.stream.Collectors; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; import static spoon.test.SpoonTestHelpers.assumeNotWindows; @@ -592,10 +592,10 @@ public void testMatchPossesiveMultiValueMinCount() throws Exception { List matches = pattern.getMatches(ctClass.getMethodsByName("testMatch1").get(0).getBody()); // Quantifier.POSSESSIVE matches exactly the right number of times - assertEquals("count="+countFinal, countFinal, getCollectionSize(matches.get(0).getParameters().getValue("statements2"))); + assertEquals(countFinal, getCollectionSize(matches.get(0).getParameters().getValue("statements2")), "count="+countFinal); // Quantifier.GREEDY gets the rest - assertEquals("count="+countFinal, 5-countFinal, getCollectionSize(matches.get(0).getParameters().getValue("statements1"))); + assertEquals(5-countFinal, getCollectionSize(matches.get(0).getParameters().getValue("statements1")), "count="+countFinal); } } @@ -629,10 +629,10 @@ public void testMatchPossesiveMultiValueMinCount2() throws Exception { List matches = pattern.getMatches(ctClass.getMethodsByName("testMatch1").get(0).getBody()); //the last template has nothing to match -> no match - assertEquals("count="+countFinal, 1, matches.size()); - assertEquals("count="+countFinal, 4-countFinal, getCollectionSize(matches.get(0).getParameters().getValue("statements1"))); - assertEquals("count="+countFinal, countFinal, getCollectionSize(matches.get(0).getParameters().getValue("statements2"))); - assertEquals("count="+countFinal, 2, getCollectionSize(matches.get(0).getParameters().getValue("inlinedSysOut"))); + assertEquals(1, matches.size(), "count="+countFinal); + assertEquals(4-countFinal, getCollectionSize(matches.get(0).getParameters().getValue("statements1")), "count="+countFinal); + assertEquals(countFinal, getCollectionSize(matches.get(0).getParameters().getValue("statements2")), "count="+countFinal); + assertEquals(2, getCollectionSize(matches.get(0).getParameters().getValue("inlinedSysOut")), "count="+countFinal); } for (int count = 5; count < 7; count++) { @@ -642,7 +642,7 @@ public void testMatchPossesiveMultiValueMinCount2() throws Exception { List matches = pattern.getMatches(ctClass.getMethodsByName("testMatch1").get(0).getBody()); //the possessive matcher eats too much. There is no target element for last `printedValue` variable - assertEquals("count="+countFinal, 0, matches.size()); + assertEquals(0, matches.size(), "count="+countFinal); } } @@ -667,13 +667,13 @@ public void testMatchGreedyMultiValueMinCount2() throws Exception { if (count < 7) { //the last template has nothing to match -> no match - assertEquals("count=" + count, 1, matches.size()); - assertEquals("count=" + count, Math.max(0, 3 - count), getCollectionSize(matches.get(0).getParameters().getValue("statements1"))); - assertEquals("count=" + count, count - Math.max(0, count - 4), getCollectionSize(matches.get(0).getParameters().getValue("statements2"))); - assertEquals("count=" + count, Math.max(2, 3 - Math.max(0, count - 3)), getCollectionSize(matches.get(0).getParameters().getValue("printedValue"))); + assertEquals(1, matches.size(), "count=" + count); + assertEquals(Math.max(0, 3 - count), getCollectionSize(matches.get(0).getParameters().getValue("statements1")), "count=" + count); + assertEquals(count - Math.max(0, count - 4), getCollectionSize(matches.get(0).getParameters().getValue("statements2")), "count=" + count); + assertEquals(Math.max(2, 3 - Math.max(0, count - 3)), getCollectionSize(matches.get(0).getParameters().getValue("printedValue")), "count=" + count); } else { //the possessive matcher eats too much. There is no target element for last `printedValue` variable - assertEquals("count=" + count, 0, matches.size()); + assertEquals(0, matches.size(), "count=" + count); } } } diff --git a/src/test/java/spoon/test/template/TemplateArrayAccessTest.java b/src/test/java/spoon/test/template/TemplateArrayAccessTest.java index 365fe97769e..295cc1ebaf3 100644 --- a/src/test/java/spoon/test/template/TemplateArrayAccessTest.java +++ b/src/test/java/spoon/test/template/TemplateArrayAccessTest.java @@ -16,9 +16,9 @@ */ package spoon.test.template; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.Test; +import org.junit.jupiter.api.Test; import spoon.Launcher; import spoon.reflect.code.CtStatement; diff --git a/src/test/java/spoon/test/template/TemplateClassAccessTest.java b/src/test/java/spoon/test/template/TemplateClassAccessTest.java index f5322838640..5e691c89c17 100644 --- a/src/test/java/spoon/test/template/TemplateClassAccessTest.java +++ b/src/test/java/spoon/test/template/TemplateClassAccessTest.java @@ -16,9 +16,9 @@ */ package spoon.test.template; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.Test; +import org.junit.jupiter.api.Test; import spoon.Launcher; import spoon.reflect.code.CtStatement; diff --git a/src/test/java/spoon/test/template/TemplateEnumAccessTest.java b/src/test/java/spoon/test/template/TemplateEnumAccessTest.java index a0a8c95ac82..ced7575439e 100644 --- a/src/test/java/spoon/test/template/TemplateEnumAccessTest.java +++ b/src/test/java/spoon/test/template/TemplateEnumAccessTest.java @@ -16,12 +16,12 @@ */ package spoon.test.template; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.File; import java.lang.annotation.ElementType; -import org.junit.Test; +import org.junit.jupiter.api.Test; import spoon.Launcher; import spoon.OutputType; diff --git a/src/test/java/spoon/test/template/TemplateInvocationSubstitutionTest.java b/src/test/java/spoon/test/template/TemplateInvocationSubstitutionTest.java index 8d5e13747ae..f6f2da276c3 100644 --- a/src/test/java/spoon/test/template/TemplateInvocationSubstitutionTest.java +++ b/src/test/java/spoon/test/template/TemplateInvocationSubstitutionTest.java @@ -16,11 +16,11 @@ */ package spoon.test.template; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.fail; -import org.junit.Test; +import org.junit.jupiter.api.Test; import spoon.Launcher; import spoon.reflect.code.CtBlock; diff --git a/src/test/java/spoon/test/template/TemplateReplaceReturnTest.java b/src/test/java/spoon/test/template/TemplateReplaceReturnTest.java index c8ec770b75a..03645665df0 100644 --- a/src/test/java/spoon/test/template/TemplateReplaceReturnTest.java +++ b/src/test/java/spoon/test/template/TemplateReplaceReturnTest.java @@ -16,12 +16,12 @@ */ package spoon.test.template; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static spoon.testing.utils.ModelUtils.getOptimizedString; import java.io.File; -import org.junit.Test; +import org.junit.jupiter.api.Test; import spoon.Launcher; import spoon.OutputType; diff --git a/src/test/java/spoon/test/template/TemplateTest.java b/src/test/java/spoon/test/template/TemplateTest.java index 49275e36571..d4dcfacd2df 100644 --- a/src/test/java/spoon/test/template/TemplateTest.java +++ b/src/test/java/spoon/test/template/TemplateTest.java @@ -16,7 +16,7 @@ */ package spoon.test.template; -import org.junit.Test; +import org.junit.jupiter.api.Test; import spoon.Launcher; import spoon.SpoonException; import spoon.compiler.SpoonResourceHelper; @@ -100,15 +100,15 @@ import static java.util.Arrays.asList; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; import static spoon.test.SpoonTestHelpers.assumeNotWindows; import static spoon.testing.utils.ModelUtils.getOptimizedString; @@ -293,9 +293,9 @@ private void assertCommentHasGeneratedBy(CtElement e, String templateQName, Map< } else { assertNotNull("Javadoc comment is missing for "+e.toString(), docComment); int idx = docComment.indexOf("Generated by"); - assertTrue("Javadoc comment doesn't contain Generated by. There is:\n"+docComment, idx>=0); + assertTrue(idx>=0, "Javadoc comment doesn't contain Generated by. There is:\n"+docComment); Matcher m = generatedByRE.matcher(docComment); - assertTrue("Unexpected Generated by:\n"+docComment, m.matches()); + assertTrue(m.matches(), "Unexpected Generated by:\n"+docComment); assertEquals(templateQName, m.group(1)); assertEquals(generatedByMember, m.group(2)); assertTrue(templateQName.endsWith("."+m.group(3))); @@ -308,7 +308,7 @@ private void assertCommentHasGeneratedBy(CtElement e, String templateQName, Map< assertTrue(m2.find()); String memberSimpleName = m2.group(); assertTrue(memberSimpleName.length()>2); - assertTrue("Source file "+e.getPosition().getFile().getAbsolutePath()+":"+lineNr+" doesn't contain member name "+memberSimpleName, sourceLine.contains(memberSimpleName)); + assertTrue(sourceLine.contains(memberSimpleName), "Source file "+e.getPosition().getFile().getAbsolutePath()+":"+lineNr+" doesn't contain member name "+memberSimpleName); } } @@ -395,7 +395,7 @@ public void testTemplateWithWrongUsedStringParam() throws Exception { try { m.getBody().getStatement(0).toString(); } catch (SpoonException e) { - assertTrue("The error description doesn't contain name of invalid field. There is:\n" + e.getMessage(), e.getMessage().contains("testparam")); + assertTrue(e.getMessage().contains("testparam"), "The error description doesn't contain name of invalid field. There is:\n" + e.getMessage()); } } @@ -640,16 +640,16 @@ public void testTemplateMatcher() throws Exception { private boolean checkParameters(String methodName, Match match, String... keyValues) { if (methodName.equals(getMethodName(match.getMatchingElement()))) { - assertEquals("The arguments of keyValues must be in pairs", 0, keyValues.length % 2); + assertEquals(0, keyValues.length % 2, "The arguments of keyValues must be in pairs"); Map allParams = new HashMap<>(match.getParameters().asMap()); int count = keyValues.length / 2; for (int i = 0; i < count; i++) { String key = keyValues[i * 2]; String expectedValue = keyValues[i * 2 + 1]; Object realValue = allParams.remove(key); - assertEquals("parameter " + key, expectedValue, getOptimizedString(realValue)); + assertEquals(expectedValue, getOptimizedString(realValue), "parameter " + key); } - assertTrue("Unexpected parameter values: " + allParams, allParams.isEmpty()); + assertTrue(allParams.isEmpty(), "Unexpected parameter values: " + allParams); return true; } return false; @@ -731,27 +731,27 @@ public void testTemplateMatcherWithWholePackage() { CtElement match = matches.get(0); - assertTrue("Match is not a if", match instanceof CtIf); + assertTrue(match instanceof CtIf, "Match is not a if"); CtElement matchParent = match.getParent(); - assertTrue("Match parent is not a block", matchParent instanceof CtBlock); + assertTrue(matchParent instanceof CtBlock, "Match parent is not a block"); CtElement matchParentParent = matchParent.getParent(); - assertTrue("Match grand parent is not a method", matchParentParent instanceof CtMethod); + assertTrue(matchParentParent instanceof CtMethod, "Match grand parent is not a method"); CtMethod methodHello = (CtMethod)matchParentParent; - assertEquals("Match grand parent is not a method called hello", "hello", methodHello.getSimpleName()); + assertEquals("hello", methodHello.getSimpleName(), "Match grand parent is not a method called hello"); CtElement methodParent = methodHello.getParent(); - assertTrue("Parent of the method is not a class",methodParent instanceof CtClass); + assertTrue(methodParent instanceof CtClass, "Parent of the method is not a class"); CtClass bservice = (CtClass) methodParent; - assertEquals("Parent of the method is not a class called BServiceImpl", "BServiceImpl", bservice.getSimpleName()); + assertEquals("BServiceImpl", bservice.getSimpleName(), "Parent of the method is not a class called BServiceImpl"); } @Test diff --git a/src/test/java/spoon/test/template/core/ParameterInfoTest.java b/src/test/java/spoon/test/template/core/ParameterInfoTest.java index 3805936121e..7e2fc7ba710 100644 --- a/src/test/java/spoon/test/template/core/ParameterInfoTest.java +++ b/src/test/java/spoon/test/template/core/ParameterInfoTest.java @@ -1,10 +1,10 @@ package spoon.test.template.core; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.fail; import java.util.Arrays; import java.util.Collections; @@ -15,7 +15,7 @@ import java.util.function.BiConsumer; import java.util.function.Consumer; -import org.junit.Test; +import org.junit.jupiter.api.Test; import spoon.pattern.internal.parameter.ListParameterInfo; import spoon.pattern.internal.parameter.MapParameterInfo;