/*
 * Copyright (C) 2015 Square, 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.squareup.javawriter;

import java.util.Date;
import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;

public final class JavaFileTest {
  @Test public void noImports() throws Exception {
    String source = new JavaFile.Builder()
        .packageName("com.squareup.tacos")
        .typeSpec(TypeSpec.classBuilder("Taco")
            .build())
        .build()
        .toString();
    assertThat(source).isEqualTo(""
        + "package com.squareup.tacos;\n"
        + "\n"
        + "class Taco {\n"
        + "}\n");
  }

  @Test public void singleImport() throws Exception {
    String source = new JavaFile.Builder()
        .packageName("com.squareup.tacos")
        .typeSpec(TypeSpec.classBuilder("Taco")
            .addField(FieldSpec.of(Date.class, "madeFreshDate"))
            .build())
        .build()
        .toString();
    assertThat(source).isEqualTo(""
        + "package com.squareup.tacos;\n"
        + "\n"
        + "import java.util.Date;\n"
        + "\n"
        + "class Taco {\n"
        + "  Date madeFreshDate;\n"
        + "}\n");
  }

  @Test public void conflictingImports() throws Exception {
    String source = new JavaFile.Builder()
        .packageName("com.squareup.tacos")
        .typeSpec(TypeSpec.classBuilder("Taco")
            .addField(FieldSpec.of(Date.class, "madeFreshDate"))
            .addField(FieldSpec.of(java.sql.Date.class, "madeFreshDatabaseDate"))
            .build())
        .build()
        .toString();
    assertThat(source).isEqualTo(""
        + "package com.squareup.tacos;\n"
        + "\n"
        + "import java.util.Date;\n"
        + "\n"
        + "class Taco {\n"
        + "  Date madeFreshDate;\n"
        + "\n"
        + "  java.sql.Date madeFreshDatabaseDate;\n"
        + "}\n");
  }
}

@@ -646,16 +646,58 @@
 l.Date;%0A
+import javax.lang.model.element.Modifier;%0A
 import o
@@ -2433,11 +2433,820 @@
 n%22);%0A  %7D
+%0A%0A  @Test public void defaultPackage() throws Exception %7B%0A    String source = new JavaFile.Builder()%0A        .typeSpec(TypeSpec.classBuilder(%22HelloWorld%22)%0A            .addMethod(MethodSpec.methodBuilder(%22main%22)%0A                .addModifiers(Modifier.PUBLIC, Modifier.STATIC)%0A                .addParameter(String%5B%5D.class, %22args%22)%0A                .addCode(%22$T.out.println($S);%5Cn%22, System.class, %22Hello World!%22)%0A                .build())%0A            .build())%0A        .build()%0A        .toString();%0A    assertThat(source).isEqualTo(%22%22%0A        + %22import java.lang.String;%5Cn%22%0A        + %22import java.lang.System;%5Cn%22%0A        + %22%5Cn%22%0A        + %22class HelloWorld %7B%5Cn%22%0A        + %22  public static void main(String%5B%5D args) %7B%5Cn%22%0A        + %22    System.out.println(%5C%22Hello World!%5C%22);%5Cn%22%0A        + %22  %7D%5Cn%22%0A        + %22%7D%5Cn%22);%0A  %7D
 %0A%7D%0A

Test that the default package works.