package com.petterfactory.couchbaseliteorm.compiler;

import com.google.testing.compile.JavaFileObjects;

import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import javax.tools.JavaFileObject;

import static com.google.common.truth.Truth.ASSERT;
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;
import static com.petterfactory.couchbaseliteorm.compiler.TestProcessors.exampleProcessors;

/**
 * Created by brais on 6/1/15.
 */
public class ExampleProcessorTest {
  private String compilerPath;

  @Before
  public void init() {
    // FIXME this is a workaround.
    this.compilerPath = System.getProperty("compilerProjectPath");
    if (this.compilerPath == null) {
      this.compilerPath = "./compiler";
    }
  }

  @Test
  public void simpleCompile() throws IOException {
    final JavaFileObject inputFile = JavaFileObjects.forSourceString("com.samples.Person",
        new String(Files.readAllBytes(Paths.get(compilerPath + "/../compiler-source-samples/src/main/java/com/samples/Person.java")), "UTF-8")
    );
    final JavaFileObject expectedFile = JavaFileObjects.forSourceString("com.samples.Person$$Example",
        new String(Files.readAllBytes(Paths.get(compilerPath + "/../compiler-source-samples/src/main/java/com/samples/Person$$Example.java")), "UTF-8")
    );

    ASSERT.about(javaSource())
        .that(inputFile)
        .processedWith(exampleProcessors())
        .compilesWithoutError()
        .and()
        .generatesSources(expectedFile);
  }
}

@@ -204,16 +204,43 @@
 .Files;%0A
+import java.nio.file.Path;%0A
 import j
@@ -626,16 +626,571 @@
 rPath;%0A%0A
+  private static JavaFileObject getJavaFileObject(String compilerPath, String fullyQualifiedName) throws IOException %7B%0A    return JavaFileObjects.forSourceString(fullyQualifiedName,%0A        new String(Files.readAllBytes(qualifiedNameToPath(compilerPath, fullyQualifiedName)), %22UTF-8%22)%0A    );%0A  %7D%0A%0A  private static Path qualifiedNameToPath(String compilerPath, String fullyQualifiedName) %7B%0A    return Paths.get(compilerPath%0A        + %22/../compiler-source-samples/src/main/java/%22%0A        + fullyQualifiedName.replaceAll(%22%5C%5C.%22, %22/%22)%0A        + %22.java%22);%0A  %7D%0A%0A
   @Befor
@@ -1496,24 +1496,27 @@
 inputFile = 
+get
 JavaFileObje
@@ -1521,195 +1521,43 @@
 ject
-s.forSourceString(%22com.samples.Person%22,%0A        new String(Files.readAllBytes(Paths.get(compilerPath + %22/../compiler-source-samples/src/main/java/com/samples/Person.java%22)), %22UTF-8%22)%0A    
+(compilerPath, %22com.samples.Person%22
 );%0A 
@@ -1595,16 +1595,19 @@
 dFile = 
+get
 JavaFile
@@ -1616,213 +1616,52 @@
 ject
-s.forSourceString(%22com.samples.Person$$Example%22,%0A        new String(Files.readAllBytes(Paths.get(compilerPath + %22/../compiler-source-samples/src/main/java/com/samples/Person$$Example.java%22)), %22UTF-8%22)%0A    
+(compilerPath, %22com.samples.Person$$Example%22
 );%0A%0A

Improve the readability