File: AnnotationProcessingTest.java

package info (click to toggle)
libnb-javaparser-java 9%2B2018-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 65,172 kB
  • sloc: java: 440,096; xml: 6,359; sh: 865; makefile: 314
file content (270 lines) | stat: -rw-r--r-- 11,838 bytes parent folder | download | duplicates (2)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * Copyright 2003-2004 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package global;

import com.sun.source.util.JavacTask;
import com.sun.tools.javac.Main;
import com.sun.tools.javac.api.JavacTaskImpl;
import global.ap1.AP;
import global.ap1.ClassBasedAP;
import global.ap1.ErrorProducingAP;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;
import junit.framework.TestCase;

/**
 *
 * @author lahvac
 */
public class AnnotationProcessingTest extends TestCase {

    public AnnotationProcessingTest(String name) {
        super(name);
    }

    static class MyFileObject extends SimpleJavaFileObject {
        private String text;
        public MyFileObject(String text) {
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
            this.text = text;
        }
        @Override
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            return text;
        }
    }

    public void testNoFalseEnterErrors() throws IOException {
        String code = "package test; @global.ap1.Ann(fqnToGenerate=\"test.G\", content=\"package test; public class G {}\") public class Test extends G {}";

        performErrorsTest(code, 0);
    }
    
    public void testNoFalseEnterWarnings() throws IOException {
        String code = "package test; public class Test { @SuppressWarnings(\"deprecation\") public java.rmi.server.Skeleton t() { return null; } }";

        performErrorsTest(code, Arrays.asList("-Xlint"), 0);
    }

    public void testCorrectEnterErrors() throws IOException {
        String code = "package test; @global.ap1.Ann(fqnToGenerate=\"test.H\", content=\"package test; public class H {}\") public class Test extends Undefined {}";

        performErrorsTest(code, 1);
    }

    public void testDuplicatedErrorsReported() throws IOException {
        String code = "package test; @global.ap1.Ann(fqnToGenerate=\"test.H\", content=\"package test; public class H {}\") public class Test {}";

        performAPErrorsTest(code, ErrorProducingAP.class.getName(), new FileContent[0], "14-117:message 1", "14-117:message 2", "14-117:message 3");
    }

    public void testDependentAP() throws IOException {
        String code = "package test; public class Test { Auxiliary aux; }";
        String auxiliary = "package test; @global.ap1.Ann(fqnToGenerate=\"test.G\", content=\"package test; public class G {}\") public class Auxiliary extends G { private Unknown t; private Aux a; }";
        String aux = "package test; @global.ap1.Ann(fqnToGenerate=\"test.H\", content=\"package test; public class H {}\") public class Aux extends H { private Unknown t; }";

        performAPErrorsTest(code, AP.class.getName(), 
                new FileContent[] {
                    new FileContent("test/Auxiliary.java", auxiliary),
                    new FileContent("test/Aux.java", aux)
                },
                "140-140:cannot find symbol\n  symbol:   class Unknown\n  location: class test.Auxiliary",
                "134-134:cannot find symbol\n  symbol:   class Unknown\n  location: class test.Aux");
    }

    public void testNoAP() throws IOException {
        String code = "package test; public class Test { Auxiliary aux; }";
        String auxiliary = "package test; public class Auxiliary { private Unknown t; private Aux a; }";
        String aux = "package test; public class Aux { private Unknown t; }";

        performAPErrorsTest(code, null,
                new FileContent[] {
                    new FileContent("test/Auxiliary.java", auxiliary),
                    new FileContent("test/Aux.java", aux)
                },
                "47-47:cannot find symbol\n  symbol:   class Unknown\n  location: class test.Auxiliary",
                "41-41:cannot find symbol\n  symbol:   class Unknown\n  location: class test.Aux");
    }

    private void performErrorsTest(String code, int expectedErrors) throws IOException {
        performErrorsTest(code, new ArrayList<String>(), expectedErrors);
    }
    
    private void performErrorsTest(String code, Collection<? extends String> extraOptions, int expectedErrors) throws IOException {
        File sourceOutput = File.createTempFile("NoFalseErrorsFromAP", "");
        sourceOutput.delete();
        assertTrue(sourceOutput.mkdirs());

        final String bootPath = System.getProperty("sun.boot.class.path"); //NOI18N
        final String version = System.getProperty("java.vm.specification.version"); //NOI18N
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
        assert tool != null;

        URL myself = AnnotationProcessingTest.class.getProtectionDomain().getCodeSource().getLocation();
        DiagnosticCollector<JavaFileObject> diagnostic = new DiagnosticCollector<JavaFileObject>();
        List<String> options = new ArrayList<String>(Arrays.asList("-bootclasspath",  bootPath, "-source", version, "-classpath", myself.toExternalForm(), "-processor", AP.class.getName(), "-s", sourceOutput.getAbsolutePath(), "-XDbackgroundCompilation"));
        options.addAll(extraOptions);
        JavacTask ct = (JavacTask)tool.getTask(null, null, diagnostic, options, null, Arrays.asList(new MyFileObject(code)));
        ct.analyze();
        assertEquals(diagnostic.getDiagnostics().toString(), expectedErrors, diagnostic.getDiagnostics().size());

        //intentionally not deleting thwn the test fails to simply diagnostic
        delete(sourceOutput);
    }

    private void performAPErrorsTest(String code, String apName, FileContent[] auxiliary, String... goldenErrors) throws IOException {
        File temp = File.createTempFile("NoFalseErrorsFromAP", "");
        temp.delete();
        assertTrue(temp.mkdirs());

        File sourceOutput = new File(temp, "out");

        assertTrue(sourceOutput.mkdirs());

        File source = new File(temp, "src");

        assertTrue(source.mkdirs());

        if (auxiliary != null) {
            for (FileContent fc : auxiliary) {
                File aux = new File(source, fc.path);

                aux.getParentFile().mkdirs();

                FileWriter w = new FileWriter(aux);

                w.write(fc.content);
                w.close();
            }
        }

        final String bootPath = System.getProperty("sun.boot.class.path"); //NOI18N
        final String version = System.getProperty("java.vm.specification.version"); //NOI18N
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
        assert tool != null;

        URL myself = AnnotationProcessingTest.class.getProtectionDomain().getCodeSource().getLocation();
        DiagnosticCollector<JavaFileObject> diagnostic = new DiagnosticCollector<JavaFileObject>();
        List<String> options = new LinkedList<String>();
        options.addAll(Arrays.asList("-bootclasspath",  bootPath, "-source", version, "-classpath", myself.toExternalForm()));
        if (apName != null) {
            options.addAll(Arrays.asList("-processor", apName));
        } else {
            options.add("-proc:none");
        }
        options.addAll(Arrays.asList("-s", sourceOutput.getAbsolutePath(), "-XDbackgroundCompilation", "-sourcepath", source.getAbsolutePath()));
        JavacTask ct = (JavacTask)tool.getTask(null, null, diagnostic, options, null, Arrays.asList(new MyFileObject(code)));
        ct.analyze();

        List<String> actualErrors = new ArrayList<String>();

        for (Diagnostic<? extends JavaFileObject> d : diagnostic.getDiagnostics()) {
            actualErrors.add(d.getStartPosition() + "-" + d.getEndPosition() + ":" + d.getMessage(null));
        }

        assertEquals(Arrays.asList(goldenErrors), actualErrors);

        //intentionally not deleting thwn the test fails to simply diagnostic
        delete(sourceOutput);
    }

    public void testAPNoSources() throws IOException {
        File sourceOutput = File.createTempFile("APNoSources", "");
        
        sourceOutput.delete();
        assertTrue(sourceOutput.mkdirs());

        final String bootPath = System.getProperty("sun.boot.class.path"); //NOI18N
        final String version = System.getProperty("java.vm.specification.version"); //NOI18N
        URL myself = AnnotationProcessingTest.class.getProtectionDomain().getCodeSource().getLocation();
        
        Main.compile(new String[] {
            "-bootclasspath",  bootPath,
            "-source", version,
            "-target", version,
            "-classpath", myself.toExternalForm(),
            "-processor", ClassBasedAP.class.getName(),
            "-s", sourceOutput.getAbsolutePath(),
            "java.lang.String"
        });

        assertTrue(new File(sourceOutput, "java.lang.String.txt").canRead());
        
        //intentionally not deleting thwn the test fails to simply diagnostic
        delete(sourceOutput);
    }
    
    private static void delete(File d) {
        if (d.isDirectory()) {
            for (File f : d.listFiles()) {
                delete(f);
            }
        }
        d.delete();
    }

    private static class FileContent {
        final String path;
        final String content;

        public FileContent(String path, String content) {
            this.path = path;
            this.content = content;
        }
    }

    public void testKeepBrokenAttributes228628() throws IOException {
        final String bootPath = System.getProperty("sun.boot.class.path"); //NOI18N
        final String version = System.getProperty("java.vm.specification.version"); //NOI18N
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
        assert tool != null;

        String code = "package test; @Test(NonExistent.class) public @interface Test { public Class<?> value(); }";

        JavacTaskImpl ct = (JavacTaskImpl)tool.getTask(null, null, null, Arrays.asList("-bootclasspath",  bootPath, "-source", version, "-Xjcov", "-XDshouldStopPolicy=GENERATE"), null, Arrays.asList(new global.Test116436.MyFileObject(code)));
        Iterable<? extends Element> classes = ct.enter();
        TypeElement test = (TypeElement) classes.iterator().next();
        
        assertEquals(1, test.getAnnotationMirrors().get(0).getElementValues().size());
    }
    
}