• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.googlejavaformat.java.filer;
16 
17 import static com.google.common.truth.Truth.assertThat;
18 
19 import com.google.common.base.Joiner;
20 import com.google.testing.compile.CompilationRule;
21 import java.io.IOException;
22 import java.io.StringWriter;
23 import java.io.Writer;
24 import java.net.URI;
25 import java.util.ArrayList;
26 import java.util.List;
27 import javax.annotation.processing.Filer;
28 import javax.annotation.processing.Messager;
29 import javax.lang.model.element.AnnotationMirror;
30 import javax.lang.model.element.AnnotationValue;
31 import javax.lang.model.element.Element;
32 import javax.tools.FileObject;
33 import javax.tools.JavaFileManager.Location;
34 import javax.tools.JavaFileObject;
35 import javax.tools.JavaFileObject.Kind;
36 import javax.tools.SimpleJavaFileObject;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.junit.runners.JUnit4;
41 
42 /** Tests for {@link com.google.googlejavaformat.java.filer.FormattingFiler}. */
43 @RunWith(JUnit4.class)
44 public class FormattingFilerTest {
45 
46   @Rule public CompilationRule compilationRule = new CompilationRule();
47 
48   @Test
invalidSyntaxDoesNotThrowError()49   public void invalidSyntaxDoesNotThrowError() throws IOException {
50     List<String> logMessages = new ArrayList<>();
51     Messager messager =
52         new Messager() {
53           @Override
54           public void printMessage(javax.tools.Diagnostic.Kind kind, CharSequence msg) {
55             logMessages.add(kind + ";" + msg);
56           }
57 
58           @Override
59           public void printMessage(javax.tools.Diagnostic.Kind kind, CharSequence msg, Element e) {}
60 
61           @Override
62           public void printMessage(
63               javax.tools.Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a) {}
64 
65           @Override
66           public void printMessage(
67               javax.tools.Diagnostic.Kind kind,
68               CharSequence msg,
69               Element e,
70               AnnotationMirror a,
71               AnnotationValue v) {}
72         };
73 
74     String file = Joiner.on('\n').join("package foo;", "public class Bar {");
75     FormattingFiler formattingFiler = new FormattingFiler(new FakeFiler(), messager);
76     try (Writer writer = formattingFiler.createSourceFile("foo.Bar").openWriter()) {
77       writer.write(file);
78     }
79 
80     assertThat(logMessages).containsExactly("NOTE;Error formatting foo.Bar");
81   }
82 
83   @Test
formatsFile()84   public void formatsFile() throws IOException {
85     FormattingFiler formattingFiler = new FormattingFiler(new FakeFiler());
86     JavaFileObject sourceFile = formattingFiler.createSourceFile("foo.Bar");
87     try (Writer writer = sourceFile.openWriter()) {
88       writer.write("package foo;class Bar{private String      baz;\n\n\n\n}");
89     }
90 
91     assertThat(sourceFile.getCharContent(false).toString())
92         .isEqualTo(
93             Joiner.on('\n')
94                 .join(
95                     "package foo;",
96                     "",
97                     "class Bar {",
98                     "  private String baz;",
99                     "}",
100                     "")); // trailing newline
101   }
102 
103   private static class FakeFiler implements Filer {
104     @Override
createSourceFile(CharSequence name, Element... originatingElements)105     public JavaFileObject createSourceFile(CharSequence name, Element... originatingElements)
106         throws IOException {
107       return new ObservingJavaFileObject(name.toString(), Kind.SOURCE);
108     }
109 
110     @Override
createClassFile(CharSequence name, Element... originatingElements)111     public JavaFileObject createClassFile(CharSequence name, Element... originatingElements)
112         throws IOException {
113       return new ObservingJavaFileObject(name.toString(), Kind.CLASS);
114     }
115 
116     @Override
createResource( Location location, CharSequence pkg, CharSequence relativeName, Element... originatingElements)117     public FileObject createResource(
118         Location location,
119         CharSequence pkg,
120         CharSequence relativeName,
121         Element... originatingElements)
122         throws IOException {
123       return new ObservingJavaFileObject(pkg.toString() + relativeName, Kind.OTHER);
124     }
125 
126     @Override
getResource(Location location, CharSequence pkg, CharSequence relativeName)127     public FileObject getResource(Location location, CharSequence pkg, CharSequence relativeName)
128         throws IOException {
129       return new ObservingJavaFileObject(pkg.toString() + relativeName, Kind.OTHER);
130     }
131   }
132 
133   private static class ObservingJavaFileObject extends SimpleJavaFileObject {
134     private final StringWriter output = new StringWriter();
135 
ObservingJavaFileObject(String name, Kind kind)136     ObservingJavaFileObject(String name, Kind kind) {
137       super(URI.create(name), kind);
138     }
139 
140     @Override
openWriter()141     public Writer openWriter() throws IOException {
142       return output;
143     }
144 
145     @Override
getCharContent(boolean ignoreEncodingErrors)146     public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
147       return output.toString();
148     }
149   }
150 }
151