// Copyright © 2011-2014, Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0

package fi.jumi.actors.generator;

import org.junit.*;
import org.junit.rules.TemporaryFolder;

import javax.tools.*;
import java.util.*;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class AnnotationProcessorTest {

    @Rule
    public final TemporaryFolder tempDir = new TemporaryFolder();

    @Test
    public void foo() {
        compile(new JavaSourceFromString("DummyInterface", "" +
                "package com.example;\n" +
                "@fi.jumi.actors.generator.GenerateEventizer\n" +
                "public interface DummyInterface {\n" +
                "}"
        ));
    }

    private void compile(JavaFileObject... compilationUnits) {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
        List<String> options = Arrays.asList("-d", tempDir.getRoot().getAbsolutePath());
        boolean success = compiler.getTask(null, null, diagnostics, options, null, Arrays.asList(compilationUnits)).call();
        for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
            System.err.println(diagnostic);
        }
        assertThat("compile succeeded", success, is(true));
    }
}

@@ -316,14 +316,37 @@
 ava.
-util.*
+io.*;%0Aimport java.util.Arrays
 ;%0A%0Ai
@@ -433,18 +433,17 @@
 atchers.
-is
+*
 ;%0A%0Apubli
@@ -562,35 +562,184 @@
     
-@Test%0A    public void foo()
+private File outputDir;%0A%0A    @Before%0A    public void setup() %7B%0A        outputDir = tempDir.getRoot();%0A    %7D%0A%0A    @Test%0A    public void generates_eventizers() throws IOException
  %7B%0A 
@@ -997,16 +997,220 @@
      ));
+%0A%0A        assertThat(new File(outputDir, %22com/example/Foo.java%22), hasProperty(%22file%22, equalTo(true)));%0A        assertThat(new File(outputDir, %22com/example/Foo.class%22), hasProperty(%22file%22, equalTo(true)));
 %0A    %7D%0A%0A
@@ -1269,16 +1269,35 @@
 onUnits)
+ throws IOException
  %7B%0A     
@@ -1474,89 +1474,195 @@
     
-List%3CString%3E options = Arrays.asList(%22-d%22, tempDir.getRoot().getAbsolutePath(
+StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);%0A        fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(outputDir
 ));%0A
+%0A
     
@@ -1706,20 +1706,27 @@
 k(null, 
-null
+fileManager
 , diagno
@@ -1736,15 +1736,12 @@
 cs, 
-options
+null
 , nu

Check that the annotation processor is executed