diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-02 21:57:54 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-02 21:57:54 +0200 |
commit | 4e794b5ed03e5020770becb068d11e6838feec64 (patch) | |
tree | f17cf754db970e1e20fff22f2195e1d75f11a6cd /container-compiler-plugin/src/test/java/io/trygvis | |
parent | 7c3bf7a43911375589fd5c16f9a3e85e7fda0c80 (diff) | |
download | container-playground-4e794b5ed03e5020770becb068d11e6838feec64.tar.gz container-playground-4e794b5ed03e5020770becb068d11e6838feec64.tar.bz2 container-playground-4e794b5ed03e5020770becb068d11e6838feec64.tar.xz container-playground-4e794b5ed03e5020770becb068d11e6838feec64.zip |
wip
Diffstat (limited to 'container-compiler-plugin/src/test/java/io/trygvis')
3 files changed, 184 insertions, 0 deletions
diff --git a/container-compiler-plugin/src/test/java/io/trygvis/container/compiler/EntityHandlerTest.java b/container-compiler-plugin/src/test/java/io/trygvis/container/compiler/EntityHandlerTest.java new file mode 100644 index 0000000..98fcaea --- /dev/null +++ b/container-compiler-plugin/src/test/java/io/trygvis/container/compiler/EntityHandlerTest.java @@ -0,0 +1,27 @@ +package io.trygvis.container.compiler; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import static io.trygvis.container.compiler.EntityHandler.sqlName; +import static org.fest.assertions.Assertions.assertThat; + +@Test(singleThreaded = false) +public class EntityHandlerTest { + + @DataProvider(name = "sqlName", parallel = true) + public static Object[][] SqlNameDataProvider() { + return new Object[][] { + new Object[]{"MyClass", "my_class"}, + new Object[]{"myField", "my_field"}, + new Object[]{"name", "name"}, + new Object[]{"first_name", "first_name"}, + new Object[]{"first_name_", "first_name_"}, + }; + } + + @Test(dataProvider = "sqlName") + public void testSqlName(String input, String output) throws Exception { + assertThat(sqlName(input)).isEqualTo(output); + } +} diff --git a/container-compiler-plugin/src/test/java/io/trygvis/container/compiler/InMemoryJavaFileManager.java b/container-compiler-plugin/src/test/java/io/trygvis/container/compiler/InMemoryJavaFileManager.java new file mode 100644 index 0000000..e476d3e --- /dev/null +++ b/container-compiler-plugin/src/test/java/io/trygvis/container/compiler/InMemoryJavaFileManager.java @@ -0,0 +1,74 @@ +package io.trygvis.container.compiler; + +import org.apache.commons.io.output.ByteArrayOutputStream; + +import javax.tools.FileObject; +import javax.tools.ForwardingJavaFileManager; +import javax.tools.JavaFileObject; +import javax.tools.SimpleJavaFileObject; +import javax.tools.StandardJavaFileManager; +import java.io.CharArrayWriter; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.OutputStream; +import java.io.Writer; +import java.net.URI; +import java.util.HashMap; +import java.util.Map; + +public class InMemoryJavaFileManager extends ForwardingJavaFileManager<StandardJavaFileManager> { + public final Map<String, String> codes = new HashMap<>(); + + public InMemoryJavaFileManager(StandardJavaFileManager standardFileManager) { + super(standardFileManager); + } + + @Override + public FileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling) throws IOException { + System.out.println("io.trygvis.container.compiler.InMemoryJavaFileManager.getFileForOutput"); + throw new RuntimeException(""); + } + + @Override + public JavaFileObject getJavaFileForOutput(Location location, final String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException { + System.out.println("io.trygvis.container.compiler.InMemoryJavaFileManager.getJavaFileForOutput"); + return new SimpleJavaFileObject(URI.create("wat://woot/" + className.replace('.', '/') + ".java"), kind) { + String code; + + @Override + public Writer openWriter() throws IOException { + return new CharArrayWriter() { + @Override + public void close() { + super.close(); + System.out.println("Closing writer to: className = " + className); + code = super.toString(); + codes.put(className, code); + } + }; + } + + @Override + public OutputStream openOutputStream() throws IOException { + return new ByteArrayOutputStream() { + @Override + public void close() throws IOException { + super.close(); + // ignored for now +// System.out.println("Closing output stream to: className = " + className); +// System.out.println(new String(super.toByteArray())); + } + }; + } + + @Override + public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { + if(code == null) { + throw new FileNotFoundException(className); + } + + return code; + } + }; + } +} diff --git a/container-compiler-plugin/src/test/java/io/trygvis/container/compiler/ProcessorTest.java b/container-compiler-plugin/src/test/java/io/trygvis/container/compiler/ProcessorTest.java new file mode 100644 index 0000000..e070f3c --- /dev/null +++ b/container-compiler-plugin/src/test/java/io/trygvis/container/compiler/ProcessorTest.java @@ -0,0 +1,83 @@ +package io.trygvis.container.compiler; + +import org.apache.commons.io.IOUtils; +import org.testng.annotations.Test; + +import javax.tools.Diagnostic; +import javax.tools.DiagnosticCollector; +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.SimpleJavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.ToolProvider; +import java.net.URI; +import java.nio.charset.Charset; +import java.util.Arrays; +import java.util.Locale; + +import static java.util.Collections.singletonList; +import static org.fest.assertions.Assertions.assertThat; + +public class ProcessorTest { + Charset UTF_8 = Charset.forName("utf-8"); + + /** + * A file object used to represent source coming from a string. + */ + public class JavaSourceFromString extends SimpleJavaFileObject { + /** + * The source code of this "file". + */ + final String code; + + /** + * Constructs a new JavaSourceFromString. + * + * @param name the name of the compilation unit represented by this file object + * @param code the source code for the compilation unit represented by this file object + */ + JavaSourceFromString(String name, String code) { + super(URI.create("string:///" + name.replace('.', '/') + JavaFileObject.Kind.SOURCE.extension), + JavaFileObject.Kind.SOURCE); + this.code = code; + } + + @Override + public CharSequence getCharContent(boolean ignoreEncodingErrors) { + return code; + } + } + + @Test + public void testBasic() throws Exception { + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + + DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<>(); + StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(collector, Locale.ENGLISH, UTF_8); + + InMemoryJavaFileManager fileManager = new InMemoryJavaFileManager(standardFileManager); + + JavaSourceFromString myEntity = new JavaSourceFromString("Person", IOUtils.toString(getClass().getResource("/Person.java"), UTF_8)); + + JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, collector, null, + null, singletonList(myEntity)); + + task.setProcessors(Arrays.asList(new MyProcessor())); + + Boolean result = task.call(); + + for (Diagnostic<? extends JavaFileObject> diagnostic : collector.getDiagnostics()) { +// System.out.println("diagnostic = " + diagnostic); + System.out.println("diagnostic.source = ->" + diagnostic.getSource().getName() + "<-"); + System.out.println("diagnostic.message = " + diagnostic.getMessage(Locale.ENGLISH)); + } + + assertThat(collector.getDiagnostics()).isEmpty(); + assertThat(result).isTrue(); + + fileManager.close(); + + assertThat(fileManager.codes.keySet()).containsOnly("Person_Sql"); + System.out.println(fileManager.codes.get("Person_Sql")); + } +} |