Skip to content

Commit

Permalink
[#82] Wrapped (Java)FileObject to hide IOException
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Stamann committed Mar 14, 2024
1 parent e4994d0 commit ffa04b6
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 33 deletions.
63 changes: 59 additions & 4 deletions cute/src/main/java/io/toolisticon/cute/CuteApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import javax.annotation.processing.Processor;
import javax.lang.model.element.Element;
import javax.lang.model.element.NestingKind;
import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.JavaFileManager;
Expand Down Expand Up @@ -1255,12 +1256,12 @@ public List<CompilerMessage> getCompilerMessages() {
return this.compilationResult.getDiagnostics().getDiagnostics().stream().map(CompilerMessage::new).collect(Collectors.toList());
}

public List<FileObject> getFileObjects() {
return Collections.unmodifiableList(this.compilationResult.getCompileTestFileManager().getGeneratedFileObjects());
public List<FileObjectWrapper> getFileObjects() {
return this.compilationResult.getCompileTestFileManager().getGeneratedFileObjects().stream().map(FileObjectWrapper::new).collect(Collectors.toList());
}

public List<JavaFileObject> getJavaFileObjects() {
return Collections.unmodifiableList(this.compilationResult.getCompileTestFileManager().getGeneratedJavaFileObjects());
public List<JavaFileObjectWrapper> getJavaFileObjects() {
return this.compilationResult.getCompileTestFileManager().getGeneratedJavaFileObjects().stream().map(JavaFileObjectWrapper::new).collect(Collectors.toList());
}


Expand Down Expand Up @@ -1301,6 +1302,60 @@ public String getSource() {

}

public static class JavaFileObjectWrapper {
final JavaFileObject javaFileObject;

public JavaFileObjectWrapper(JavaFileObject javaFileObject) {
this.javaFileObject = javaFileObject;

}

public JavaFileObject.Kind getKind(){
return javaFileObject.getKind();
}

public NestingKind getNestingKind(){
return javaFileObject.getNestingKind();
}

public String getName(){
return javaFileObject.getName();
}

public String getContent(){
try {
return javaFileObject.getCharContent(true).toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}



}

public static class FileObjectWrapper {
final FileObject fileObject;

public FileObjectWrapper(FileObject fileObject) {
this.fileObject = fileObject;
}

public String getName(){
return fileObject.getName();
}

public String getContent(){
try {
return fileObject.getCharContent(true).toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}



}



Expand Down
52 changes: 23 additions & 29 deletions cute/src/test/java/io/toolisticon/cute/CuteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import java.util.stream.Collectors;

/**
* Unit tests for {@link Cute}.
*/
public class CuteTest {

@Test
public void test_UnitTest_successfulCompilation_build() throws IOException{
public void test_UnitTest_successfulCompilation_build() throws IOException {

JavaFileObject testSource = Mockito.mock(JavaFileObject.class);
JavaFileObject expectedGeneratedSource = JavaFileObjectUtils.readFromString("Jupp.txt", "TATA!");
Expand Down Expand Up @@ -67,13 +65,9 @@ public void unitTest(ProcessingEnvironment processingEnvironment) {
.executeTest().doManualAssertions(e -> {
MatcherAssert.assertThat("Expected to find warning message that contains WARNING", !(e.getCompilerMessages().stream().filter(f -> f.getKind() == Diagnostic.Kind.WARNING).filter(f -> f.getMessage().contains("WARNING")).count() == 0));
MatcherAssert.assertThat("Should not find generated SOURCE FILES", e.getJavaFileObjects().stream().filter(f -> f.getKind() == JavaFileObject.Kind.SOURCE).count() == 0);
MatcherAssert.assertThat("Should find generated RESOURCE file that contains TATA", e.getFileObjects().stream().filter(f -> f.getName().equals("/root/Jupp.txt")).filter(f -> {
try {
return f.getCharContent(true).toString().contains("TATA");
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}).count() == 1);
MatcherAssert.assertThat("Should find generated RESOURCE file that contains TATA", e.getFileObjects().stream().filter(f -> f.getName().equals("/root/Jupp.txt")).filter(f ->
f.getContent().toString().contains("TATA")
).count() == 1);
MatcherAssert.assertThat("Should be true", true);
});

Expand Down Expand Up @@ -934,17 +928,17 @@ public void unitTest(SimpleTestProcessor1 unit, ProcessingEnvironment processing

@Test(expected = ValidatorException.class)
public void blackBoxTest_nullValuedProcessor() {
Cute.blackBoxTest().given().processor((Class<? extends Processor>)null);
Cute.blackBoxTest().given().processor((Class<? extends Processor>) null);
}

@Test(expected = ValidatorException.class)
public void blackBoxTest_nullValuedProcessors() {
Cute.blackBoxTest().given().processor((Class<? extends Processor>)null);
Cute.blackBoxTest().given().processor((Class<? extends Processor>) null);
}

@Test(expected = ValidatorException.class)
public void blackBoxTest_nullValuedProcessorInArray() {
Cute.blackBoxTest().given().processors((Class<? extends Processor>)null, (Class<? extends Processor>)null);
Cute.blackBoxTest().given().processors((Class<? extends Processor>) null, (Class<? extends Processor>) null);
}

@Test(expected = ValidatorException.class)
Expand Down Expand Up @@ -1017,8 +1011,8 @@ public void blackBoxTest_justCompileCodeAndDoClassTests() {
.compilationSucceeds()
.andThat().generatedClass("io.toolisticon.cute.TestClass").testedSuccessfullyBy(new GeneratedClassesTestForSpecificClass() {
@Override
public void doTests(Class<?> clazz,CuteClassLoader cuteClassLoader) throws Exception{
MatcherAssert.assertThat(clazz.getCanonicalName(),Matchers.is("io.toolisticon.cute.TestClass"));
public void doTests(Class<?> clazz, CuteClassLoader cuteClassLoader) throws Exception {
MatcherAssert.assertThat(clazz.getCanonicalName(), Matchers.is("io.toolisticon.cute.TestClass"));

Object instance = clazz.getConstructor().newInstance();
MatcherAssert.assertThat(instance, Matchers.notNullValue());
Expand All @@ -1036,17 +1030,17 @@ public void blackBoxTest_justCompileCodeAndDoClassTests2() {
.compilationSucceeds()
.andThat().generatedClass("io.toolisticon.cute.TestClassWithInnerClasses").testedSuccessfullyBy(new GeneratedClassesTestForSpecificClass() {
@Override
public void doTests(Class<?>clazz, CuteClassLoader cuteClassLoader) throws Exception{
MatcherAssert.assertThat(clazz.getCanonicalName(),Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses"));
public void doTests(Class<?> clazz, CuteClassLoader cuteClassLoader) throws Exception {
MatcherAssert.assertThat(clazz.getCanonicalName(), Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses"));

Class<?> innerClazz = cuteClassLoader.getClass("io.toolisticon.cute.TestClassWithInnerClasses$InnerClass");
MatcherAssert.assertThat(innerClazz.getCanonicalName(),Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses.InnerClass"));
MatcherAssert.assertThat(innerClazz.getCanonicalName(), Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses.InnerClass"));

Class<?> staticInnerClazz = cuteClassLoader.getClass("io.toolisticon.cute.TestClassWithInnerClasses$StaticInnerClass");
MatcherAssert.assertThat(staticInnerClazz.getCanonicalName(),Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses.StaticInnerClass"));
MatcherAssert.assertThat(staticInnerClazz.getCanonicalName(), Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses.StaticInnerClass"));

Class<?> innerInterface = cuteClassLoader.getClass("io.toolisticon.cute.TestClassWithInnerClasses$InnerInterface");
MatcherAssert.assertThat(innerInterface.getCanonicalName(),Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses.InnerInterface"));
MatcherAssert.assertThat(innerInterface.getCanonicalName(), Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses.InnerInterface"));

Object instance = clazz.getConstructor().newInstance();
MatcherAssert.assertThat(instance, Matchers.notNullValue());
Expand All @@ -1065,18 +1059,18 @@ public void blackBoxTest_justCompileCodeAndDoClassTest3() {
.compilationSucceeds()
.andThat().generatedClassesTestedSuccessfullyBy(new GeneratedClassesTest() {
@Override
public void doTests(CuteClassLoader cuteClassLoader) throws Exception{
public void doTests(CuteClassLoader cuteClassLoader) throws Exception {
Class<?> clazz = cuteClassLoader.getClass("io.toolisticon.cute.TestClassWithInnerClasses");
MatcherAssert.assertThat(clazz.getCanonicalName(),Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses"));
MatcherAssert.assertThat(clazz.getCanonicalName(), Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses"));

Class<?> innerClazz = cuteClassLoader.getClass("io.toolisticon.cute.TestClassWithInnerClasses$InnerClass");
MatcherAssert.assertThat(innerClazz.getCanonicalName(),Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses.InnerClass"));
MatcherAssert.assertThat(innerClazz.getCanonicalName(), Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses.InnerClass"));

Class<?> staticInnerClazz = cuteClassLoader.getClass("io.toolisticon.cute.TestClassWithInnerClasses$StaticInnerClass");
MatcherAssert.assertThat(staticInnerClazz.getCanonicalName(),Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses.StaticInnerClass"));
MatcherAssert.assertThat(staticInnerClazz.getCanonicalName(), Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses.StaticInnerClass"));

Class<?> innerInterface = cuteClassLoader.getClass("io.toolisticon.cute.TestClassWithInnerClasses$InnerInterface");
MatcherAssert.assertThat(innerInterface.getCanonicalName(),Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses.InnerInterface"));
MatcherAssert.assertThat(innerInterface.getCanonicalName(), Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses.InnerInterface"));

Object instance = clazz.getConstructor().newInstance();
MatcherAssert.assertThat(instance, Matchers.notNullValue());
Expand All @@ -1095,9 +1089,9 @@ public void blackBoxTest_justCompileCodeAndDoClassTest4() {
.compilationSucceeds()
.andThat().generatedClass("io.toolisticon.cute.TestClassWithInnerClasses$InnerClass").testedSuccessfullyBy(new GeneratedClassesTestForSpecificClass() {
@Override
public void doTests( Class<?> innerClazz, CuteClassLoader cuteClassLoader) throws Exception{
public void doTests(Class<?> innerClazz, CuteClassLoader cuteClassLoader) throws Exception {

MatcherAssert.assertThat(innerClazz.getCanonicalName(),Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses.InnerClass"));
MatcherAssert.assertThat(innerClazz.getCanonicalName(), Matchers.is("io.toolisticon.cute.TestClassWithInnerClasses.InnerClass"));


}
Expand All @@ -1114,7 +1108,7 @@ public void blackBoxTest_justCompileCodeAndDoClassTestWithImplementedInterface()
.compilationSucceeds()
.andThat().generatedClass("io.toolisticon.cute.TestClassWithImplementedInterface").testedSuccessfullyBy(new GeneratedClassesTestForSpecificClass() {
@Override
public void doTests(Class<?> clazz, CuteClassLoader cuteClassLoader) throws Exception{
public void doTests(Class<?> clazz, CuteClassLoader cuteClassLoader) throws Exception {

SimpleTestInterface unit = (SimpleTestInterface) clazz.getConstructor().newInstance();
MatcherAssert.assertThat(unit.saySomething(), Matchers.is("WHATS UP?"));
Expand All @@ -1134,7 +1128,7 @@ public void blackBoxTest_justCompileCodeAndDoClassTestWithImplementedInterfaceAn
.compilationSucceeds()
.andThat().generatedClass("io.toolisticon.cute.TestClassWithImplementedInterface").testedSuccessfullyBy(new GeneratedClassesTestForSpecificClass() {
@Override
public void doTests(Class<?> clazz, CuteClassLoader cuteClassLoader) throws Exception{
public void doTests(Class<?> clazz, CuteClassLoader cuteClassLoader) throws Exception {

SimpleTestInterface unit = (SimpleTestInterface) clazz.getConstructor().newInstance();
MatcherAssert.assertThat(unit.saySomething(), Matchers.is("WHATS UP???"));
Expand Down

0 comments on commit ffa04b6

Please sign in to comment.