test/tools/javac/processing/filer/TestInvalidRelativeNames.java

Fri, 21 Dec 2012 08:45:43 -0800

author
darcy
date
Fri, 21 Dec 2012 08:45:43 -0800
changeset 1466
b52a38d4536c
parent 699
d2aaaec153e8
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8005282: Use @library tag with non-relative path for javac tests
Reviewed-by: jjg

jjg@698 1 /*
jjg@698 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
jjg@698 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@698 4 *
jjg@698 5 * This code is free software; you can redistribute it and/or modify it
jjg@698 6 * under the terms of the GNU General Public License version 2 only, as
jjg@698 7 * published by the Free Software Foundation.
jjg@698 8 *
jjg@698 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@698 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@698 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@698 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@698 13 * accompanied this code).
jjg@698 14 *
jjg@698 15 * You should have received a copy of the GNU General Public License version
jjg@698 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@698 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@698 18 *
jjg@698 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@698 20 * or visit www.oracle.com if you need additional information or have any
jjg@698 21 * questions.
jjg@698 22 */
jjg@698 23
jjg@698 24 /*
jjg@698 25 * @test
jjg@698 26 * @bug 6502392
jjg@698 27 * @summary Invalid relative names for Filer.createResource and Filer.getResource
darcy@1466 28 * @library /tools/javac/lib
darcy@699 29 * @build JavacTestingAbstractProcessor
jjg@698 30 * @compile TestInvalidRelativeNames.java
jjg@698 31 * @compile/process -processor TestInvalidRelativeNames java.lang.Object
jjg@698 32 */
jjg@698 33
jjg@698 34 import java.io.*;
jjg@698 35 import java.util.*;
jjg@698 36 import javax.annotation.processing.*;
jjg@698 37 import javax.lang.model.*;
jjg@698 38 import javax.lang.model.element.*;
jjg@698 39 import javax.tools.Diagnostic;
jjg@698 40 import javax.tools.StandardLocation;
jjg@698 41
darcy@699 42 public class TestInvalidRelativeNames extends JavacTestingAbstractProcessor {
jjg@698 43 enum Kind { CREATE_WRITER, GET_READER, CREATE_OUTPUT_STREAM, GET_INPUT_STREAM };
jjg@698 44
jjg@698 45 static final String[] invalidRelativeNames = {
jjg@698 46 "/boo", "goo/../hoo", "./ioo", ""
jjg@698 47 };
jjg@698 48
jjg@698 49 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
jjg@698 50 if (roundEnv.processingOver()) {
jjg@698 51 for (String relative: invalidRelativeNames) {
jjg@698 52 for (Kind kind: Kind.values()) {
jjg@698 53 test(relative, kind);
jjg@698 54 }
jjg@698 55 }
jjg@698 56 }
jjg@698 57 return true;
jjg@698 58 }
jjg@698 59
jjg@698 60 void test(String relative, Kind kind) {
jjg@698 61 System.out.println("test relative path: " + relative + ", kind: " + kind);
jjg@698 62 try {
jjg@698 63 switch (kind) {
jjg@698 64 case CREATE_WRITER:
jjg@698 65 Writer writer = filer.createResource(
jjg@698 66 StandardLocation.SOURCE_OUTPUT, "", relative).openWriter();
jjg@698 67 writer.close();
jjg@698 68 break;
jjg@698 69
jjg@698 70 case GET_READER:
jjg@698 71 Reader reader = filer.getResource(
jjg@698 72 StandardLocation.SOURCE_OUTPUT, "", relative).openReader(true);
jjg@698 73 reader.close();
jjg@698 74 break;
jjg@698 75
jjg@698 76 case CREATE_OUTPUT_STREAM:
jjg@698 77 OutputStream out = filer.createResource(
jjg@698 78 StandardLocation.SOURCE_OUTPUT, "", relative).openOutputStream();
jjg@698 79 out.close();
jjg@698 80 break;
jjg@698 81
jjg@698 82 case GET_INPUT_STREAM:
jjg@698 83 InputStream in = filer.createResource(
jjg@698 84 StandardLocation.SOURCE_OUTPUT, "", relative).openInputStream();
jjg@698 85 in.close();
jjg@698 86 break;
jjg@698 87 }
jjg@698 88 } catch (IllegalArgumentException expected) {
jjg@698 89 System.out.println("expected exception thrown: " + expected);
jjg@698 90 return;
jjg@698 91 } catch (Exception e) {
jjg@698 92 messager.printMessage(Diagnostic.Kind.ERROR,
jjg@698 93 "relative path: " + relative + ", kind: " + kind + ", unexpected exception: " + e);
jjg@698 94 return;
jjg@698 95 }
jjg@698 96 messager.printMessage(Diagnostic.Kind.ERROR,
jjg@698 97 "relative path: " + relative + ", kind: " + kind + ", no exception thrown");
jjg@698 98 }
jjg@698 99 }
jjg@698 100

mercurial