From 65fb61daf4a5750e41d15c8820a835f57a5d48da Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sun, 20 Aug 2023 13:35:52 -0700 Subject: [PATCH] Added more tests --- .../thauvin/erik/semver/VersionProcessor.java | 13 ++-- .../erik/semver/VersionProcessorTest.java | 78 ++++++++++++++++++- .../net/thauvin/erik/semver/VersionTest.java | 8 +- 3 files changed, 91 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/thauvin/erik/semver/VersionProcessor.java b/src/main/java/net/thauvin/erik/semver/VersionProcessor.java index 1c9f150..5ad6f22 100644 --- a/src/main/java/net/thauvin/erik/semver/VersionProcessor.java +++ b/src/main/java/net/thauvin/erik/semver/VersionProcessor.java @@ -34,7 +34,6 @@ package net.thauvin.erik.semver; import com.github.mustachejava.DefaultMustacheFactory; import com.github.mustachejava.Mustache; -import com.github.mustachejava.MustacheFactory; import com.github.mustachejava.MustacheNotFoundException; import javax.annotation.processing.*; @@ -79,6 +78,11 @@ public class VersionProcessor extends AbstractProcessor { return template; } + private Mustache compileTemplate(final File dir, final String template) { + final var mf = new DefaultMustacheFactory(dir); + return mf.compile(template); + } + private void error(final String s) { log(Diagnostic.Kind.ERROR, s); } @@ -232,9 +236,8 @@ public class VersionProcessor extends AbstractProcessor { private void writeTemplate(final String type, final VersionInfo versionInfo, final String template) throws IOException { - final File dir = getLocalFile(""); - final MustacheFactory mf = new DefaultMustacheFactory(dir); - final Mustache mustache = mf.compile(template); + final var dir = getLocalFile(""); + final var mustache = compileTemplate(dir, template); final String templateName = switch (mustache.getName()) { case Constants.DEFAULT_JAVA_TEMPLATE -> "default (Java)"; @@ -243,7 +246,7 @@ public class VersionProcessor extends AbstractProcessor { }; note("Loaded template: " + templateName); - final String fileName = versionInfo.getClassName() + '.' + type; + final var fileName = versionInfo.getClassName() + '.' + type; if (Constants.KOTLIN_TYPE.equalsIgnoreCase(type)) { final String kaptGenDir = processingEnv.getOptions().get(Constants.KAPT_KOTLIN_GENERATED_OPTION_NAME); if (kaptGenDir == null) { diff --git a/src/test/java/net/thauvin/erik/semver/VersionProcessorTest.java b/src/test/java/net/thauvin/erik/semver/VersionProcessorTest.java index ad3b742..702b841 100644 --- a/src/test/java/net/thauvin/erik/semver/VersionProcessorTest.java +++ b/src/test/java/net/thauvin/erik/semver/VersionProcessorTest.java @@ -32,13 +32,17 @@ package net.thauvin.erik.semver; +import com.github.mustachejava.Mustache; import org.junit.jupiter.api.Test; +import java.io.File; +import java.io.IOException; +import java.io.StringWriter; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Properties; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; /** * The VersionProcessorTest class. @@ -49,7 +53,63 @@ import static org.junit.jupiter.api.Assertions.assertEquals; */ class VersionProcessorTest { private final VersionProcessor processor = new VersionProcessor(); - private final Version version = new VersionTest(); + private final VersionTest version = new VersionTest(); + + @SuppressWarnings("PMD.AvoidAccessibilityAlteration") + @Test + void testCompileTemplate() + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException { + final var method = processor.getClass().getDeclaredMethod("compileTemplate", File.class, String.class); + method.setAccessible(true); + + final var mustache = (Mustache) method.invoke(processor, new File("src/main/resources"), + Constants.DEFAULT_JAVA_TEMPLATE); + + assertEquals(Constants.DEFAULT_JAVA_TEMPLATE, mustache.getName(), Constants.DEFAULT_JAVA_TEMPLATE); + + try (var writer = new StringWriter()) { + mustache.execute(writer, version).flush(); + assertEquals(String.format(""" + /* + * This file is automatically generated. + * Do not modify! -- ALL CHANGES WILL BE ERASED! + */ + + package %s; + + import java.util.Date; + + /** + * Provides semantic version information. + * + * @author Semantic Version Annotation Processor + */ + public final class MyTest { + public static final String PROJECT = "%s"; + public static final Date BUILDDATE = new Date(L); + public static final int MAJOR = %d; + public static final int MINOR = %d; + public static final int PATCH = %d; + public static final String PRERELEASE = "%s"; + public static final String PRERELEASE_PREFIX = "%s"; + public static final String BUILDMETA = "%s"; + public static final String BUILDMETA_PREFIX = "%s"; + public static final String SEPARATOR = "%s"; + public static final String VERSION = ""; + + /** + * Disables the default constructor. + */ + private MyTest() { + throw new UnsupportedOperationException("Illegal constructor call."); + } + } + """, version.packageName(), version.project(), version.major(), version.minor(), version.patch(), + version.preRelease(), version.preReleasePrefix(), version.buildMeta(), + version.buildMetaPrefix(), version.separator()), + writer.toString()); + } + } @SuppressWarnings("PMD.AvoidAccessibilityAlteration") @Test @@ -62,6 +122,20 @@ class VersionProcessorTest { assertEquals("James Bond", versionInfo.getProject(), "getProject(James Bond)"); } + @SuppressWarnings("PMD.AvoidAccessibilityAlteration") + @Test + void testGetTemplate() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + final Method method = processor.getClass().getDeclaredMethod("getTemplate", boolean.class, Version.class); + method.setAccessible(true); + + assertEquals(version.template(), method.invoke(processor, true, version), version.template); + version.setTemplate(Constants.DEFAULT_JAVA_TEMPLATE); + assertEquals(Constants.DEFAULT_TEMPLATE_NAME, method.invoke(processor, true, version), + "default"); + assertEquals(Constants.DEFAULT_KOTLIN_TEMPLATE, method.invoke(processor, false, version), + "kotlin"); + } + @SuppressWarnings("PMD.AvoidAccessibilityAlteration") @Test void testParseIntProperty() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { diff --git a/src/test/java/net/thauvin/erik/semver/VersionTest.java b/src/test/java/net/thauvin/erik/semver/VersionTest.java index 0153b1d..e14f359 100644 --- a/src/test/java/net/thauvin/erik/semver/VersionTest.java +++ b/src/test/java/net/thauvin/erik/semver/VersionTest.java @@ -43,6 +43,8 @@ import java.lang.annotation.Annotation; */ @SuppressWarnings({"ClassExplicitlyAnnotation", "SameReturnValue", "java:S2187", "PMD.TestClassWithoutTestCases"}) class VersionTest implements Version { + String template = "myversion.mustache"; + @Override public Class annotationType() { return null; @@ -160,11 +162,15 @@ class VersionTest implements Version { @Override public String template() { - return "myversion.mustache"; + return template; } @Override public String type() { return "kt"; } + + public void setTemplate(final String template) { + this.template = template; + } }