Added more tests
This commit is contained in:
parent
5cf167e7bf
commit
65fb61daf4
3 changed files with 91 additions and 8 deletions
|
@ -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) {
|
||||
|
|
|
@ -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 <code>VersionProcessorTest</code> 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 <a href="https://github.com/ethauvin/semver">Semantic Version Annotation Processor</a>
|
||||
*/
|
||||
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 {
|
||||
|
|
|
@ -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<? extends Annotation> 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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue