aboutsummaryrefslogtreecommitdiff
path: root/javatests/com/google/turbine/processing/TurbineFilerTest.java
blob: f76a08d111c5092f886c7162f560924c3e26628e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright 2019 Google Inc. All Rights Reserved.
 *
 * 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.turbine.processing;

import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;

import com.google.common.base.Supplier;
import com.google.common.io.CharStreams;
import com.google.turbine.diag.SourceFile;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.processing.FilerException;
import javax.lang.model.element.Element;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;
import org.jspecify.nullness.Nullable;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class TurbineFilerTest {

  private final Set<String> seen = new HashSet<>();
  private TurbineFiler filer;

  @Before
  public void setup() {
    Function<String, Supplier<byte[]>> classpath =
        new Function<String, Supplier<byte[]>>() {
          @Override
          public @Nullable Supplier<byte[]> apply(String input) {
            return null;
          }
        };
    this.filer = new TurbineFiler(seen, classpath, TurbineFilerTest.class.getClassLoader());
  }

  @Test
  public void hello() throws IOException {
    JavaFileObject sourceFile = filer.createSourceFile("com.foo.Bar", (Element[]) null);
    try (OutputStream os = sourceFile.openOutputStream()) {
      os.write("hello".getBytes(UTF_8));
    }
    assertThat(sourceFile.getLastModified()).isEqualTo(0);

    JavaFileObject classFile = filer.createClassFile("com.foo.Baz", (Element[]) null);
    try (OutputStream os = classFile.openOutputStream()) {
      os.write("goodbye".getBytes(UTF_8));
    }
    assertThat(classFile.getLastModified()).isEqualTo(0);

    Collection<SourceFile> roundSources = filer.finishRound();
    assertThat(roundSources).hasSize(1);
    assertThat(filer.generatedSources()).hasSize(1);
    assertThat(filer.generatedClasses()).hasSize(1);

    SourceFile source = getOnlyElement(roundSources);
    assertThat(source.path()).isEqualTo("com/foo/Bar.java");
    assertThat(source.source()).isEqualTo("hello");

    Map.Entry<String, byte[]> clazz = getOnlyElement(filer.generatedClasses().entrySet());
    assertThat(clazz.getKey()).isEqualTo("com/foo/Baz.class");
    assertThat(new String(clazz.getValue(), UTF_8)).isEqualTo("goodbye");
  }

  @Test
  public void existing() throws IOException {
    seen.add("com/foo/Bar.java");
    seen.add("com/foo/Baz.class");

    assertThrows(
        FilerException.class, () -> filer.createSourceFile("com.foo.Bar", (Element[]) null));
    JavaFileObject unused = filer.createSourceFile("com.foo.Baz", (Element[]) null);

    unused = filer.createClassFile("com.foo.Bar", (Element[]) null);
    assertThrows(
        FilerException.class, () -> filer.createClassFile("com.foo.Baz", (Element[]) null));
  }

  @Test
  public void get() throws IOException {
    for (StandardLocation location :
        Arrays.asList(
            StandardLocation.CLASS_OUTPUT,
            StandardLocation.SOURCE_OUTPUT,
            StandardLocation.ANNOTATION_PROCESSOR_PATH,
            StandardLocation.CLASS_PATH)) {
      assertThrows(FileNotFoundException.class, () -> filer.getResource(location, "", "NoSuch"));
    }
  }

  @Test
  public void sourceOutput() throws IOException {
    JavaFileObject classFile = filer.createSourceFile("com.foo.Bar", (Element[]) null);
    try (Writer writer = classFile.openWriter()) {
      writer.write("hello");
    }
    Collection<SourceFile> unused = filer.finishRound();

    FileObject output = filer.getResource(StandardLocation.SOURCE_OUTPUT, "com.foo", "Bar.java");
    assertThat(new String(output.openInputStream().readAllBytes(), UTF_8)).isEqualTo("hello");
    assertThat(output.getCharContent(false).toString()).isEqualTo("hello");
    assertThat(CharStreams.toString(output.openReader(true))).isEqualTo("hello");
  }

  @Test
  public void classOutput() throws IOException {
    JavaFileObject classFile = filer.createClassFile("com.foo.Baz", (Element[]) null);
    try (OutputStream os = classFile.openOutputStream()) {
      os.write("goodbye".getBytes(UTF_8));
    }
    Collection<SourceFile> unused = filer.finishRound();

    FileObject output = filer.getResource(StandardLocation.CLASS_OUTPUT, "com.foo", "Baz.class");
    assertThat(new String(output.openInputStream().readAllBytes(), UTF_8)).isEqualTo("goodbye");
    assertThat(output.getCharContent(false).toString()).isEqualTo("goodbye");
    assertThat(CharStreams.toString(output.openReader(true))).isEqualTo("goodbye");
  }

  @Test
  public void classpathResources() throws IOException {
    FileObject resource =
        filer.getResource(StandardLocation.ANNOTATION_PROCESSOR_PATH, "META-INF", "MANIFEST.MF");

    assertThat(new String(resource.openInputStream().readAllBytes(), UTF_8))
        .contains("Manifest-Version:");
    assertThat(CharStreams.toString(resource.openReader(true))).contains("Manifest-Version:");
    assertThat(resource.getCharContent(false).toString()).contains("Manifest-Version:");
  }
}