langtools/test/tools/javac/api/T6306137.java
author jjg
Wed, 29 Oct 2014 17:25:23 -0700
changeset 27319 030080f03e4f
parent 22444 c79ee1e6742f
child 30730 d3ce7619db2c
permissions -rw-r--r--
8062348: langtools tests should close file manager (group 1) Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
     2
 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
 * @test
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
 * @bug     6306137
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * @summary JSR 199: encoding option doesn't affect standard file manager
15708
fbf749010eba 8007698: jtreg test T6306137.java won't compile with ASCII encoding
jjh
parents: 15569
diff changeset
    28
 * @compile -encoding utf-8 T6306137.java
fbf749010eba 8007698: jtreg test T6306137.java won't compile with ASCII encoding
jjh
parents: 15569
diff changeset
    29
 * @run main T6306137
18390
f5cad700f200 8008023: Get rid of utf8 chars in two tests
vromero
parents: 15708
diff changeset
    30
 * @author  Peter von der Ah\u00e9
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.io.File;
18648
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    34
import java.io.IOException;
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    35
import java.nio.charset.Charset;
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    36
import java.nio.file.Files;
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    37
import java.util.ArrayList;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import java.util.Arrays;
18648
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    39
import java.util.List;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import javax.tools.*;
18648
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    41
import static java.nio.file.StandardOpenOption.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
public class T6306137 {
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    boolean error;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    final StandardJavaFileManager fm;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    final JavaCompiler compiler;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    Iterable<? extends JavaFileObject> files;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    DiagnosticListener<JavaFileObject> dl;
18648
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    49
    final File testFile = new File("Utf8.java");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
18648
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    51
    T6306137() throws IOException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        dl = new DiagnosticListener<JavaFileObject>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
                public void report(Diagnostic<? extends JavaFileObject> message) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
                    if (message.getKind() == Diagnostic.Kind.ERROR)
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
                        error = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
                    System.out.println(message.getSource()
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
                                       +":"+message.getStartPosition()+":"
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
                                       +message.getStartPosition()+":"+message.getPosition());
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
                    System.out.println(message.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
                    System.out.format("Found problem: %s%n", message.getCode());
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
                    System.out.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        compiler = ToolProvider.getSystemJavaCompiler();
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        fm = compiler.getStandardFileManager(dl, null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        files =
18648
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    67
            fm.getJavaFileObjectsFromFiles(Arrays.asList(testFile));
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    68
        createTestFile();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    }
18648
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    70
    final void createTestFile() throws IOException {
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    71
        List<String> scratch = new ArrayList<>();
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    72
        scratch.add("// @author Peter von der Ah" + (char)0xe9);
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    73
        scratch.add("class Utf8{}");
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    74
        Files.write(testFile.toPath(), scratch, Charset.forName("UTF-8"),
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    75
                CREATE, TRUNCATE_EXISTING);
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    76
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    void test(String encoding, boolean good) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        error = false;
22444
c79ee1e6742f 8031745: Remove unneeded/obsolete -source/-target options in javac tests, part 1
darcy
parents: 18648
diff changeset
    79
        Iterable<String> args = Arrays.asList("-encoding", encoding, "-d", ".");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        compiler.getTask(null, fm, dl, args, null, files).call();
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        if (error == good) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
            if (error) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
                throw new AssertionError("Error reported");
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                throw new AssertionError("No error reported");
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    90
    void close() throws IOException {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    91
        fm.close();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    92
    }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    93
18648
38447fa68349 8016908: TEST_BUG: removing non-ascii characters causes tests to fail
ksrini
parents: 18390
diff changeset
    94
    public static void main(String[] args) throws IOException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        T6306137 self = new T6306137();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    96
        try {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    97
            self.test("utf-8", true);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    98
            self.test("ascii", false);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
    99
            self.test("utf-8", true);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   100
        } finally {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   101
            self.close();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 22444
diff changeset
   102
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
}