/*
 * Copyright (C) 2013 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.testing.compile;

import static com.google.common.truth.Truth.assertThat;
import static javax.tools.JavaFileObject.Kind.CLASS;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.net.URI;
import javax.tools.JavaFileObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 *  Tests {@link JavaFileObjects}.
 *
 *  @author Gregory Kick
 */
@RunWith(JUnit4.class)
public class JavaFileObjectsTest {
  @Test
  public void forResource_inJarFile() {
    JavaFileObject resourceInJar = JavaFileObjects.forResource("java/lang/Object.class");
    assertThat(resourceInJar.getKind()).isEqualTo(CLASS);
    assertThat(resourceInJar.toUri()).isEqualTo(URI.create("/java/lang/Object.class"));
    assertThat(resourceInJar.getName()).isEqualTo("/java/lang/Object.class");
    assertThat(resourceInJar.isNameCompatible("Object", CLASS)).isTrue();
  }

  @Test public void forSourceLines() throws IOException {
    JavaFileObject fileObject = JavaFileObjects.forSourceLines("example.HelloWorld",
        "package example;",
        "",
        "final class HelloWorld {",
        "  void sayHello() {",
        "    System.out.println(\"hello!\");",
        "  }",
        "}");
    assertThat(fileObject.getCharContent(false)).isEqualTo(
        "package example;\n"
            + "\n"
            + "final class HelloWorld {\n"
            + "  void sayHello() {\n"
            + "    System.out.println(\"hello!\");\n"
            + "  }\n"
            + "}");
  }

  @Test public void forSourceLinesWithoutName() {
    try {
      JavaFileObjects.forSourceLines(
          "package example;",
          "",
          "final class HelloWorld {",
          "  void sayHello() {",
          "    System.out.println(\"hello!\");",
          "  }",
          "}");
      fail("An exception should have been thrown.");
    } catch (IllegalArgumentException expected) {}
  }
}

@@ -805,29 +805,8 @@
 on;%0A
-import java.net.URI;%0A
 impo
@@ -1137,16 +1137,24 @@
 eInJar =
+%0A       
  JavaFil
@@ -1175,31 +1175,61 @@
 source(%22
-java/lang/Objec
+com/google/testing/compile/JavaFileObjectsTes
 t.class%22
@@ -1329,57 +1329,94 @@
 ri()
-).isEqualTo(URI.create(%22/java/lang/
+.getPath())%0A        .endsWith(%22/com/google/testing/compile/JavaFile
 Object
+sTest
 .class%22)
 );%0A 
@@ -1411,17 +1411,16 @@
 .class%22)
-)
 ;%0A    as
@@ -1456,36 +1456,74 @@
 e())
-.isEqualTo(%22/java/lang/Objec
+%0A        .endsWith(%22/com/google/testing/compile/JavaFileObjectsTes
 t.cl
@@ -1576,21 +1576,34 @@
 atible(%22
-Objec
+JavaFileObjectsTes
 t%22, CLAS

Fix JavaFileObjectsTest.forResource_inJarFile so that it will work in Java 9 as well, where Object.class is not loadable as a resource.