@Test public void bindingNavigationModelInPrivateField() { JavaFileObject source = JavaFileObjects.forSourceString( "test.TestActivity", Joiner.on('\n') .join( "package test;", "import dart.DartModel;", "import dart.Dart;", "import java.lang.Object;", "public class TestActivity {", " @DartModel private TestActivityNavigationModel navigationModel;", "}", "class TestActivityNavigationModel {", "}", "class TestActivityNavigationModel__ExtraBinder {", " public static void bind(Dart.Finder finder, TestActivityNavigationModel navigationModel, Object source) {", " }", "}")); Compilation compilation = javac().withProcessors(navigationModelBinderProcessors()).compile(source); assertThat(compilation) .hadErrorContaining( "@DartModel field must not be private or static. (test.TestActivity.navigationModel)") .inFile(source); }
/** Returns the diagnostics that are in the given file. Fails the test if none are found. */ private ImmutableList<Diagnostic<? extends JavaFileObject>> findDiagnosticsInFile( JavaFileObject expectedFile) { String expectedFilePath = expectedFile.toUri().getPath(); ImmutableList<Diagnostic<? extends JavaFileObject>> diagnosticsInFile = filterDiagnostics( diagnostic -> { JavaFileObject source = diagnostic.getSource(); return source != null && source.toUri().getPath().equals(expectedFilePath); }); if (diagnosticsInFile.isEmpty()) { failExpectingMatchingDiagnostic( " in %s, but found it in %s", expectedFile.getName(), sourceFilesWithDiagnostics()); } return diagnosticsInFile; }
/** Returns the diagnostics that are in the given file. Fails the test if none are found. */ private ImmutableList<Diagnostic<? extends JavaFileObject>> findDiagnosticsInFile( JavaFileObject expectedFile) { String expectedFilePath = expectedFile.toUri().getPath(); ImmutableList<Diagnostic<? extends JavaFileObject>> diagnosticsInFile = filterDiagnostics( diagnostic -> { JavaFileObject source = diagnostic.getSource(); return source != null && source.toUri().getPath().equals(expectedFilePath); }); if (diagnosticsInFile.isEmpty()) { failExpectingMatchingDiagnostic( " in %s, but found it in %s", expectedFile.getName(), sourceFilesWithDiagnostics()); } return diagnosticsInFile; }
private ImmutableSet<String> sourceFilesWithDiagnostics() { return mapDiagnostics( diagnostic -> diagnostic.getSource() == null ? "(no associated file)" : diagnostic.getSource().getName()) .collect(toImmutableSet()); } }
/** Asserts that the note, warning, or error was found in a given file. */ @CanIgnoreReturnValue public DiagnosticOnLine inFile(JavaFileObject expectedFile) { return new DiagnosticOnLine(this, expectedFile, findDiagnosticsInFile(expectedFile)); }
private DiagnosticInFile hadDiagnosticContainingMatch( String diagnosticMatchDescription, Pattern expectedPattern, Diagnostic.Kind kind, Diagnostic.Kind... more) { String expectedDiagnostic = String.format("%s %s", kindSingular(kind), diagnosticMatchDescription); return new DiagnosticInFile( expectedDiagnostic, findMatchingDiagnostics(expectedDiagnostic, expectedPattern, kind, more)); }
private DiagnosticInFile hadDiagnosticContainingMatch( String diagnosticMatchDescription, Pattern expectedPattern, Diagnostic.Kind kind, Diagnostic.Kind... more) { String expectedDiagnostic = String.format("%s %s", kindSingular(kind), diagnosticMatchDescription); return new DiagnosticInFile( expectedDiagnostic, findMatchingDiagnostics(expectedDiagnostic, expectedPattern, kind, more)); }
private ImmutableSet<String> sourceFilesWithDiagnostics() { return mapDiagnostics( diagnostic -> diagnostic.getSource() == null ? "(no associated file)" : diagnostic.getSource().getName()) .collect(toImmutableSet()); } }
/** Asserts that the note, warning, or error was found in a given file. */ @CanIgnoreReturnValue public DiagnosticOnLine inFile(JavaFileObject expectedFile) { return new DiagnosticOnLine(this, expectedFile, findDiagnosticsInFile(expectedFile)); }
assertThat(compilation) .hadErrorContaining("Class must not be private") .inFile(javaFileObject) .onLine(20) .atColumn(26);
assertThat(compilation) .hadErrorContaining("Class must not be private") .inFile(javaFileObject) .onLine(25) .atColumn(26);
@Test public void testPrivateProperty() { JavaFileObject javaFileObject = JavaFileObjects.forSourceString(getName("TestPrivateProperty"), "" + "package com.evernote.android.state.test;\n" + "\n" + "import com.evernote.android.state.State;\n" + "\n" + "public class TestPrivateProperty {\n" + " @State\n" + " private int test;\n" + "\n" + " private int getTest() {\n" + " return test;\n" + " }\n" + "\n" + " public void setTest(int test) {\n" + " this.test = test;\n" + " }\n" + "}\n"); Compilation compilation = Compiler.javac().withProcessors(new StateProcessor()).compile(javaFileObject); assertThat(compilation).failed(); assertThat(compilation) .hadErrorContaining("Field test must be either non-private or provide a getter and setter method") .inFile(javaFileObject) .onLine(7) .atColumn(17); }
@Test public void testTypeNotSupported() { JavaFileObject javaFileObject = JavaFileObjects.forSourceString(getName("TestTypeNotSupported"), "" + "package com.evernote.android.state.test;\n" + "\n" + "import com.evernote.android.state.State;\n" + "\n" + "public class TestTypeNotSupported {\n" + " @State\n" + " public Other test;\n" + "\n" + " public static final class Other {}\n" + "}\n"); Compilation compilation = Compiler.javac().withProcessors(new StateProcessor()).compile(javaFileObject); assertThat(compilation).failed(); assertThat(compilation) .hadErrorContaining("Don't know how to put com.evernote.android.state.test.TestTypeNotSupported.Other into a bundle") .inFile(javaFileObject) .onLine(7) .atColumn(18); } }
@Test public void testPrivateClass() { JavaFileObject javaFileObject = JavaFileObjects.forSourceString(getName("TestPrivateClass"), "" + "package com.evernote.android.state.test;\n" + "\n" + "import com.evernote.android.state.State;\n" + "\n" + "public class TestPrivateClass {\n" + " private static class Inner {\n" + " @State\n" + " public int test;\n" + " }\n" + "}\n"); Compilation compilation = Compiler.javac().withProcessors(new StateProcessor()).compile(javaFileObject); assertThat(compilation).failed(); assertThat(compilation) .hadErrorContaining("Class must not be private") .inFile(javaFileObject) .onLine(6) .atColumn(20); }
@Test public void testPrivateField() { JavaFileObject javaFileObject = JavaFileObjects.forSourceString(getName("TestPrivateField"), "" + "package com.evernote.android.state.test;\n" + "\n" + "import com.evernote.android.state.State;\n" + "\n" + "public class TestPrivateField {\n" + " @State\n" + " private int test;\n" + "}\n"); Compilation compilation = Compiler.javac().withProcessors(new StateProcessor()).compile(javaFileObject); assertThat(compilation).failed(); assertThat(compilation) .hadErrorContaining("Field test must be either non-private or provide a getter and setter method") .inFile(javaFileObject) .onLine(7) .atColumn(17); }
@Test public void testStatic() { JavaFileObject javaFileObject = JavaFileObjects.forSourceString(getName("TestStatic"), "" + "package com.evernote.android.state.test;\n" + "\n" + "import com.evernote.android.state.State;\n" + "\n" + "public class TestStatic {\n" + " @State\n" + " public static int test;\n" + "}\n"); Compilation compilation = Compiler.javac().withProcessors(new StateProcessor()).compile(javaFileObject); assertThat(compilation).failed(); assertThat(compilation) .hadErrorContaining("Field must not be static") .inFile(javaFileObject) .onLine(7) .atColumn(23); }
@Test public void testFinal() { JavaFileObject javaFileObject = JavaFileObjects.forSourceString(getName("TestFinal"), "" + "package com.evernote.android.state.test;\n" + "\n" + "import com.evernote.android.state.State;\n" + "\n" + "public class TestFinal {\n" + " @State\n" + " public final int test = 5;\n" + "}\n"); Compilation compilation = Compiler.javac().withProcessors(new StateProcessor()).compile(javaFileObject); assertThat(compilation).failed(); assertThat(compilation) .hadErrorContaining("Field must not be final") .inFile(javaFileObject) .onLine(7) .atColumn(22); }
@Test public void bindingMoreThanOneNavigationModel() { JavaFileObject source = JavaFileObjects.forSourceString( "test.TestActivity", Joiner.on('\n') .join( "package test;", "import dart.DartModel;", "import dart.Dart;", "import java.lang.Object;", "public class TestActivity {", " @DartModel TestActivityNavigationModel navigationModel1;", " @DartModel TestActivityNavigationModel navigationModel2;", "}", "class TestActivityNavigationModel {", "}", "class TestActivityNavigationModel__ExtraBinder {", " public static void bind(Dart.Finder finder, TestActivityNavigationModel navigationModel, Object source) {", " }", "}")); Compilation compilation = javac().withProcessors(navigationModelBinderProcessors()).compile(source); assertThat(compilation) .hadErrorContaining("Component TestActivity cannot bind more than one NavigationModel.") .inFile(source); }
@Test public void bindingNavigationModelInStaticField() { JavaFileObject source = JavaFileObjects.forSourceString( "test.TestActivity", Joiner.on('\n') .join( "package test;", "import dart.DartModel;", "import dart.Dart;", "import java.lang.Object;", "public class TestActivity {", " @DartModel static TestActivityNavigationModel navigationModel;", "}", "class TestActivityNavigationModel {", "}", "class TestActivityNavigationModel__ExtraBinder {", " public static void bind(Dart.Finder finder, TestActivityNavigationModel navigationModel, Object source) {", " }", "}")); Compilation compilation = javac().withProcessors(navigationModelBinderProcessors()).compile(source); assertThat(compilation) .hadErrorContaining( "@DartModel field must not be private or static. (test.TestActivity.navigationModel)") .inFile(source); }
@Test public void failsIfInPrivateClass() { JavaFileObject source = JavaFileObjects.forSourceString( "test.TestNavigationModel", Joiner.on('\n') .join( "package test;", "import dart.BindExtra;", "public class TestNavigationModel {", " private static class Inner {", " @BindExtra(\"key\") String extra;", " }", "}")); Compilation compilation = javac().withProcessors(extraBinderProcessorsWithoutParceler()).compile(source); assertThat(compilation) .hadErrorContaining("DartModel class Inner must not be private, static or abstract.") .inFile(source) .onLine(4); }