TestFatalityOfParseErrors.java revision 553:9d9f26857129
128257Smsmith/*
228257Smsmith * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
328257Smsmith * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
428257Smsmith *
528257Smsmith * This code is free software; you can redistribute it and/or modify it
628257Smsmith * under the terms of the GNU General Public License version 2 only, as
728257Smsmith * published by the Free Software Foundation.
828257Smsmith *
928257Smsmith * This code is distributed in the hope that it will be useful, but WITHOUT
1028257Smsmith * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1128257Smsmith * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1228257Smsmith * version 2 for more details (a copy is included in the LICENSE file that
1328257Smsmith * accompanied this code).
1428257Smsmith *
1528257Smsmith * You should have received a copy of the GNU General Public License version
1628257Smsmith * 2 along with this work; if not, write to the Free Software Foundation,
1728257Smsmith * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1828257Smsmith *
1928257Smsmith * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2028257Smsmith * or visit www.oracle.com if you need additional information or have any
2128257Smsmith * questions.
2228257Smsmith */
2328257Smsmith
2428257Smsmith/*
2528257Smsmith * @test
2628257Smsmith * @bug 6403459
2728257Smsmith * @summary Test that generating programs with syntax errors is a fatal condition
2832929Seivind * @author  Joseph D. Darcy
29119418Sobrien * @compile TestFatalityOfParseErrors.java
30119418Sobrien * @compile/fail -XprintRounds -processor TestFatalityOfParseErrors -proc:only TestFatalityOfParseErrors.java
31119418Sobrien */
3242475Snsouch
3342475Snsouchimport java.util.Set;
3442475Snsouchimport javax.annotation.processing.*;
3532929Seivindimport javax.lang.model.SourceVersion;
3642475Snsouchimport static javax.lang.model.SourceVersion.*;
3742475Snsouchimport javax.lang.model.element.*;
3828257Smsmithimport javax.lang.model.util.*;
39187576Sjhbimport static javax.tools.Diagnostic.Kind.*;
40187576Sjhb
4128257Smsmithimport java.io.PrintWriter;
4255939Snsouchimport java.io.IOException;
4328257Smsmith
4428257Smsmith/**
4528257Smsmith * Write out an incomplete source file and observe that the next round
4628257Smsmith * is marked as an error.
4728257Smsmith */
4855939Snsouch@SupportedAnnotationTypes("*")
4955939Snsouchpublic class TestFatalityOfParseErrors extends AbstractProcessor {
5055939Snsouch    int round = 0;
5155939Snsouch    Messager messager;
5255939Snsouch    Filer filer;
5355939Snsouch
5428257Smsmith    public boolean process(Set<? extends TypeElement> annotations,
5538061Smsmith                           RoundEnvironment roundEnvironment) {
5628257Smsmith        try {
5728257Smsmith            PrintWriter pw = null;
5828257Smsmith            round++;
5942475Snsouch
6055939Snsouch            switch (round) {
6128257Smsmith            case 1:
6255939Snsouch                pw = new PrintWriter(filer.createSourceFile("SyntaxError").openWriter());
6342475Snsouch                pw.println("class SyntaxError {");
6442475Snsouch                pw.close();
6542475Snsouch                break;
6655939Snsouch
6742475Snsouch            case 2:
6855939Snsouch                pw = new PrintWriter(filer.createSourceFile("SimpleClass").openWriter());
6942475Snsouch                pw.println("class SimpleClass {}");
7042475Snsouch                pw.close();
7142475Snsouch
7242475Snsouch                if (!roundEnvironment.errorRaised() || !roundEnvironment.processingOver() ) {
7342475Snsouch                    System.err.println(roundEnvironment);
7442475Snsouch                    throw new RuntimeException("Second round not erroneous as expected.");
7542475Snsouch                }
7642475Snsouch                if (!roundEnvironment.getRootElements().isEmpty()) {
7742475Snsouch                    System.err.println(roundEnvironment);
7842475Snsouch                    throw new RuntimeException("Root elements not empty as expected.");
7955939Snsouch                }
8042475Snsouch                break;
8155939Snsouch
8242475Snsouch            default:
8355939Snsouch                throw new RuntimeException("Unexpected round number " + round);
8455939Snsouch            }
8555939Snsouch        } catch (IOException ioException) {
8642475Snsouch            throw new RuntimeException(ioException);
8742475Snsouch        }
8842475Snsouch        return true;
8942475Snsouch    }
9042475Snsouch
9142475Snsouch    public SourceVersion getSupportedSourceVersion() {
9242475Snsouch        return SourceVersion.latest();
9342475Snsouch    }
9455957Snsouch
9555939Snsouch    public void init(ProcessingEnvironment processingEnv) {
9642475Snsouch        super.init(processingEnv);
97187576Sjhb        messager = processingEnv.getMessager();
98185003Sjhb        filer    = processingEnv.getFiler();
99187576Sjhb    }
100187576Sjhb}
10142475Snsouch