@Test public void testErrorCodePattern() { asserNotMatch(""); asserNotMatch("[]"); asserNotMatch("[a]"); asserNotMatch("[]()"); asserNotMatch("[](a)"); asserMatch("[a]()", "a", ""); asserMatch("[a](b)", "a", "b"); asserMatch("[ERROR_01](5,foobar)", "ERROR_01", "5,foobar"); }
@Test public void testCompilationFailure() throws Exception { CompilerAssert<?, ?> compiler = compiler("compiler.failure"); assertEquals(1, compiler.failCompile().size()); }
private void asserNotMatch(String test) { Matcher matcher = Message.PATTERN.matcher(test); assertFalse("Was not expecting " + Message.PATTERN + " to match " + test, matcher.matches()); }
private void asserMatch(String test, String expectedCode, String expectedArguments) { Matcher matcher = Message.PATTERN.matcher(test); assertTrue("Was expecting " + Message.PATTERN + " to match " + test, matcher.matches()); assertEquals(expectedCode, matcher.group(1)); assertEquals(expectedArguments, matcher.group(2)); }
@Test public void testAnnotationState() { CaptureAnnotationProcessor processor = new CaptureAnnotationProcessor().with(StringArray.class); compiler("compiler.annotationstate.multivalued").with(compilerProvider).with(processor).assertCompile(); assertTrue(m1.isUndeclared("value")); List<?> value = assertInstanceOf(List.class, m1.resolve("value")); assertNull(m1.get("value")); assertEquals(Collections.emptyList(), value); assertTrue(m2.isDeclared("value")); value = assertInstanceOf(List.class, m2.resolve("value")); assertSame(value, m2.get("value")); assertEquals(Collections.emptyList(), value); assertTrue(m3.isDeclared("value")); value = assertInstanceOf(List.class, m3.resolve("value")); assertSame(value, m3.get("value")); assertEquals(Arrays.asList("warning_value"), value); assertTrue(m4.isDeclared("value")); value = assertInstanceOf(List.class, m4.resolve("value")); assertSame(value, m4.get("value")); assertEquals(Arrays.asList("warning_value"), value); assertTrue(m5.isDeclared("value")); value = assertInstanceOf(List.class, m5.resolve("value")); assertSame(value, m5.get("value")); assertEquals(Arrays.asList("warning_value_1", "warning_value_2"), value);
@Test public void testProcessorErrorOnElement() throws Exception { DiskFileSystem fs = diskFS("compiler.annotationexception"); Compiler compiler = Compiler.builder().javaCompiler(compilerProvider).sourcePath(fs).output(new RAMFileSystem()).build(); @javax.annotation.processing.SupportedSourceVersion(javax.lang.model.SourceVersion.RELEASE_6) try { compiler.compile(); fail(); assertEquals(1, errors.size()); CompilationError error = errors.get(0); assertEquals(null, error.getCode()); assertEquals(Collections.<String>emptyList(), error.getArguments()); assertEquals(fs.getPath("compiler", "annotationexception", "A.java"), error.getSourceFile()); assertTrue(error.getMessage().contains("the_message")); assertNotNull(error.getSourceFile()); assertNotNull(error.getLocation()); String absolutePath = error.getSourceFile().getAbsolutePath(); char separator = File.separatorChar; String[] absoluteNames = Tools.split(absolutePath, separator); assertTrue("Was expecting " + absolutePath + " to have at least three names ", absoluteNames.length > 3); assertEquals( "Was expecting " + absolutePath + " to end with compiler/annotationexceptions/A.java", Arrays.asList("compiler", "annotationexception", "A.java"),
DiskFileSystem fs = diskFS("compiler.annotationexception"); Compiler compiler = Compiler.builder().javaCompiler(compilerProvider).sourcePath(fs).output(new RAMFileSystem()).build(); @javax.annotation.processing.SupportedSourceVersion(javax.lang.model.SourceVersion.RELEASE_6) assertEquals(1, errors.size()); CompilationError error = errors.get(0); assertEquals(null, error.getCode()); assertEquals(Collections.<String>emptyList(), error.getArguments()); assertEquals(null, error.getSource()); assertTrue(error.getMessage().contains("the_message")); assertNull(error.getSourceFile()); assertNull(error.getLocation());
@Test public void testDelete() throws Exception { DeleteResourceProcessor processor = new DeleteResourceProcessor(); CompilerAssert<File, File> compiler = compiler("compiler.deleteresource").with(compilerProvider).with(processor); File foo = compiler.getClassOutput().makePath(compiler.getClassOutput().getRoot(), "foo.txt"); Tools.write("foo", foo); compiler.assertCompile(); assertTrue(processor.done); boolean expected = compilerProvider == JavaCompilerProvider.ECJ; assertEquals(expected, processor.deleted); assertEquals(!expected, foo.exists()); }
private void testResource(StandardLocation location) throws IOException, CompilationException { CompilerAssert<File, File> compiler = compiler("compiler.missingresource").with(compilerProvider).with(new ReadResource(location)); ReadWriteFileSystem<File> output; switch (location) { break; default: throw failure("was not expecting " + location); assertEquals(2, children.size()); foo = children.get("foo.txt"); assertEquals("new_foo_value", output.getResource(foo).getObject().getCharSequence(Charset.defaultCharset())); File juu = children.get("juu.txt"); assertEquals("juu_value", output.getResource(juu).getObject().getCharSequence(Charset.defaultCharset()).toString());
DiskFileSystem fs = diskFS("compiler.errorcode"); Compiler compiler = Compiler. builder(). assertEquals(1, errors.size()); CompilationError error = errors.get(0); assertEquals(code, error.getCode()); assertEquals(Arrays.asList("5", "foobar"), error.getArguments());
Compiler compiler = Compiler.builder().sourcePath(ramFS).output(output).build(); compiler.compile(); assertEquals(2, output.size(ReadFileSystem.FILE)); Timestamped<Resource> aClass = output.getResource(new String[]{"foo", "A"}); assertNotNull(aClass); Timestamped<Resource> bClass = output.getResource(new String[]{"foo", "B"}); assertNotNull(bClass); assertEquals(1, output.size(ReadFileSystem.FILE)); bClass = output.getResource(new String[]{"foo", "B"}); assertNotNull(bClass);
@Test public void testAnnotationState() { CaptureAnnotationProcessor processor = new CaptureAnnotationProcessor().with(StringArray.class); compiler("compiler.annotationstate.multivalued").with(compilerProvider).with(processor).assertCompile(); assertTrue(m1.isUndeclared("value")); List<?> value = assertInstanceOf(List.class, m1.resolve("value")); assertNull(m1.get("value")); assertEquals(Collections.emptyList(), value); assertTrue(m2.isDeclared("value")); value = assertInstanceOf(List.class, m2.resolve("value")); assertSame(value, m2.get("value")); assertEquals(Collections.emptyList(), value); assertTrue(m3.isDeclared("value")); value = assertInstanceOf(List.class, m3.resolve("value")); assertSame(value, m3.get("value")); assertEquals(Arrays.asList("warning_value"), value); assertTrue(m4.isDeclared("value")); value = assertInstanceOf(List.class, m4.resolve("value")); assertSame(value, m4.get("value")); assertEquals(Arrays.asList("warning_value"), value); assertTrue(m5.isDeclared("value")); value = assertInstanceOf(List.class, m5.resolve("value")); assertSame(value, m5.get("value")); assertEquals(Arrays.asList("warning_value_1", "warning_value_2"), value);
@Test public void testProcessorErrorOnElement() throws Exception { DiskFileSystem fs = diskFS("compiler.annotationexception"); Compiler compiler = Compiler.builder().javaCompiler(compilerProvider).sourcePath(fs).output(new RAMFileSystem()).build(); @javax.annotation.processing.SupportedSourceVersion(javax.lang.model.SourceVersion.RELEASE_6) try { compiler.compile(); fail(); assertEquals(1, errors.size()); CompilationError error = errors.get(0); assertEquals(null, error.getCode()); assertEquals(Collections.<String>emptyList(), error.getArguments()); assertEquals(fs.getPath("compiler", "annotationexception", "A.java"), error.getSourceFile()); assertTrue(error.getMessage().contains("the_message")); assertNotNull(error.getSourceFile()); assertNotNull(error.getLocation()); String absolutePath = error.getSourceFile().getAbsolutePath(); char separator = File.separatorChar; String[] absoluteNames = Tools.split(absolutePath, separator); assertTrue("Was expecting " + absolutePath + " to have at least three names ", absoluteNames.length > 3); assertEquals( "Was expecting " + absolutePath + " to end with compiler/annotationexceptions/A.java", Arrays.asList("compiler", "annotationexception", "A.java"),
DiskFileSystem fs = diskFS("compiler.annotationexception"); Compiler compiler = Compiler.builder().javaCompiler(compilerProvider).sourcePath(fs).output(new RAMFileSystem()).build(); @javax.annotation.processing.SupportedSourceVersion(javax.lang.model.SourceVersion.RELEASE_6) assertEquals(1, errors.size()); CompilationError error = errors.get(0); assertEquals(null, error.getCode()); assertEquals(Collections.<String>emptyList(), error.getArguments()); assertEquals(null, error.getSource()); assertTrue(error.getMessage().contains("the_message")); assertNull(error.getSourceFile()); assertNull(error.getLocation());
@Test public void testDelete() throws Exception { DeleteResourceProcessor processor = new DeleteResourceProcessor(); CompilerAssert<File, File> compiler = compiler("compiler.deleteresource").with(compilerProvider).with(processor); File foo = compiler.getClassOutput().makePath(compiler.getClassOutput().getRoot(), "foo.txt"); Tools.write("foo", foo); compiler.assertCompile(); assertTrue(processor.done); boolean expected = compilerProvider == JavaCompilerProvider.ECJ; assertEquals(expected, processor.deleted); assertEquals(!expected, foo.exists()); }
private void testResource(StandardLocation location) throws IOException, CompilationException { CompilerAssert<File, File> compiler = compiler("compiler.missingresource").with(compilerProvider).with(new ReadResource(location)); ReadWriteFileSystem<File> output; switch (location) { break; default: throw failure("was not expecting " + location); assertEquals(2, children.size()); foo = children.get("foo.txt"); assertEquals("new_foo_value", output.getResource(foo).getObject().getCharSequence(Charset.defaultCharset())); File juu = children.get("juu.txt"); assertEquals("juu_value", output.getResource(juu).getObject().getCharSequence(Charset.defaultCharset()).toString());
DiskFileSystem fs = diskFS("compiler.errorcode"); Compiler compiler = Compiler. builder(). assertEquals(1, errors.size()); CompilationError error = errors.get(0); assertEquals(code, error.getCode()); assertEquals(Arrays.asList("5", "foobar"), error.getArguments());
private void asserMatch(String test, String expectedCode, String expectedArguments) { Matcher matcher = Message.PATTERN.matcher(test); assertTrue("Was expecting " + Message.PATTERN + " to match " + test, matcher.matches()); assertEquals(expectedCode, matcher.group(1)); assertEquals(expectedArguments, matcher.group(2)); }
Compiler compiler = Compiler.builder().sourcePath(ramFS).output(output).build(); compiler.compile(); assertEquals(2, output.size(ReadFileSystem.FILE)); Timestamped<Resource> aClass = output.getResource(new String[]{"foo", "A"}); assertNotNull(aClass); Timestamped<Resource> bClass = output.getResource(new String[]{"foo", "B"}); assertNotNull(bClass); assertEquals(1, output.size(ReadFileSystem.FILE)); bClass = output.getResource(new String[]{"foo", "B"}); assertNotNull(bClass);
@Test public void testCompilationFailure() throws Exception { CompilerAssert<?, ?> compiler = compiler("compiler.failure"); assertEquals(1, compiler.failCompile().size()); }