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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/processing/filer/TestInvalidRelativeNames.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,100 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6502392
    1.30 + * @summary Invalid relative names for Filer.createResource and Filer.getResource
    1.31 + * @library /tools/javac/lib
    1.32 + * @build   JavacTestingAbstractProcessor
    1.33 + * @compile TestInvalidRelativeNames.java
    1.34 + * @compile/process -processor TestInvalidRelativeNames java.lang.Object
    1.35 + */
    1.36 +
    1.37 +import java.io.*;
    1.38 +import java.util.*;
    1.39 +import javax.annotation.processing.*;
    1.40 +import javax.lang.model.*;
    1.41 +import javax.lang.model.element.*;
    1.42 +import javax.tools.Diagnostic;
    1.43 +import javax.tools.StandardLocation;
    1.44 +
    1.45 +public class TestInvalidRelativeNames extends JavacTestingAbstractProcessor {
    1.46 +    enum Kind { CREATE_WRITER, GET_READER, CREATE_OUTPUT_STREAM, GET_INPUT_STREAM };
    1.47 +
    1.48 +    static final String[] invalidRelativeNames = {
    1.49 +            "/boo", "goo/../hoo", "./ioo", ""
    1.50 +    };
    1.51 +
    1.52 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    1.53 +        if (roundEnv.processingOver()) {
    1.54 +            for (String relative: invalidRelativeNames) {
    1.55 +                for (Kind kind: Kind.values()) {
    1.56 +                    test(relative, kind);
    1.57 +                }
    1.58 +            }
    1.59 +        }
    1.60 +        return true;
    1.61 +    }
    1.62 +
    1.63 +    void test(String relative, Kind kind) {
    1.64 +        System.out.println("test relative path: " + relative + ", kind: " + kind);
    1.65 +        try {
    1.66 +            switch (kind) {
    1.67 +                case CREATE_WRITER:
    1.68 +                    Writer writer = filer.createResource(
    1.69 +                            StandardLocation.SOURCE_OUTPUT, "", relative).openWriter();
    1.70 +                    writer.close();
    1.71 +                    break;
    1.72 +
    1.73 +                case GET_READER:
    1.74 +                    Reader reader = filer.getResource(
    1.75 +                            StandardLocation.SOURCE_OUTPUT, "", relative).openReader(true);
    1.76 +                    reader.close();
    1.77 +                    break;
    1.78 +
    1.79 +                case CREATE_OUTPUT_STREAM:
    1.80 +                    OutputStream out = filer.createResource(
    1.81 +                            StandardLocation.SOURCE_OUTPUT, "", relative).openOutputStream();
    1.82 +                    out.close();
    1.83 +                    break;
    1.84 +
    1.85 +                case GET_INPUT_STREAM:
    1.86 +                    InputStream in = filer.createResource(
    1.87 +                            StandardLocation.SOURCE_OUTPUT, "", relative).openInputStream();
    1.88 +                    in.close();
    1.89 +                    break;
    1.90 +            }
    1.91 +        } catch (IllegalArgumentException expected) {
    1.92 +            System.out.println("expected exception thrown: " + expected);
    1.93 +            return;
    1.94 +        } catch (Exception e) {
    1.95 +            messager.printMessage(Diagnostic.Kind.ERROR,
    1.96 +                    "relative path: " + relative + ", kind: " + kind + ", unexpected exception: " + e);
    1.97 +            return;
    1.98 +        }
    1.99 +        messager.printMessage(Diagnostic.Kind.ERROR,
   1.100 +                "relative path: " + relative + ", kind: " + kind + ", no exception thrown");
   1.101 +    }
   1.102 +}
   1.103 +

mercurial