jdk/test/java/nio/charset/Charset/NIOCharsetAvailabilityTest.java
author sherman
Thu, 26 Feb 2015 14:40:43 -0800
changeset 29123 c748c18fb05c
parent 28969 f980bee32887
child 31673 135283550686
permissions -rw-r--r--
8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system Summary: to use module's runtime filesystem to iterate the class files Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     1
/*
29123
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
     2
 * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     4
 *
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     8
 *
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    13
 * accompanied this code).
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    14
 *
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5167
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5167
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5167
diff changeset
    21
 * questions.
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    22
 */
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    23
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    24
/*
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    25
 * @test
29123
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    26
 * @bug 4777124 6920545 6911753 8073924
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    27
 * @summary Verify that all Charset subclasses are available through the API
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    28
 */
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    29
29123
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    30
import java.net.URI;
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    31
import java.nio.charset.Charset;
29123
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    32
import java.nio.file.FileSystem;
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    33
import java.nio.file.FileSystems;
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    34
import java.nio.file.Files;
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    35
import java.nio.file.Path;
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    36
import java.util.HashSet;
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    37
import java.util.Set;
29123
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    38
import java.util.stream.Collectors;
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    39
import java.util.stream.Stream;
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    40
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    41
public class NIOCharsetAvailabilityTest {
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    42
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
29123
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    44
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    45
        // build the set of all Charset subclasses in the
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    46
        // two known charset implementation packages
29123
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    47
        FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    48
        Set<Class> charsets =
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    49
            Stream.concat(Files.walk(fs.getPath("/java.base/sun/nio/cs/")),
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    50
                          Files.walk(fs.getPath("/jdk.charsets/sun/nio/cs/ext/")))
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    51
                 .map( p -> p.subpath(1, p.getNameCount()).toString())
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    52
                 .filter( s ->  s.indexOf("$") == -1 && s.endsWith(".class"))
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    53
                 .map( s -> {
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    54
                     try {
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    55
                         return Class.forName(s.substring(0, s.length() - 6)
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    56
                                               .replace('/', '.'));
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    57
                     } catch (Exception x) {
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    58
                         throw new RuntimeException(x);
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    59
                     }
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    60
                  })
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    61
                 .filter( clz -> {
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    62
                     Class superclazz = clz.getSuperclass();
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    63
                     while (superclazz != null && !superclazz.equals(Object.class)) {
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    64
                         if (superclazz.equals(Charset.class)) {
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    65
                             return true;
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    66
                         } else {
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    67
                             superclazz = superclazz.getSuperclass();
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    68
                         }
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    69
                     }
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    70
                     return false;
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    71
                  })
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    72
                 .collect(Collectors.toCollection(HashSet::new));
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    73
        // remove the charsets that the API says are available
29123
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    74
        Charset.availableCharsets()
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    75
               .values()
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    76
               .stream()
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    77
               .forEach(cs -> {
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    78
                   if (!charsets.contains(cs.getClass())) {
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    79
                       System.out.println(" missing -> " + cs.getClass());
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    80
                   }
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    81
                   charsets.remove(cs.getClass());
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
    82
                });
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    83
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    84
        // remove the known pseudo-charsets that serve only to implement
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    85
        // other charsets, but shouldn't be known to the public
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    86
        charsets.remove(Class.forName("sun.nio.cs.Unicode"));
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    87
        charsets.remove(Class.forName("sun.nio.cs.ext.ISO2022"));
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    88
        charsets.remove(Class.forName("sun.nio.cs.ext.ISO2022_CN_GB"));
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
    89
        charsets.remove(Class.forName("sun.nio.cs.ext.ISO2022_CN_CNS"));
13366
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
    90
        charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0208_MS932"));
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
    91
        charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0212_MS5022X"));
2f5fdf6d8c22 6653797: Reimplement JDK charset repository charsets.jar
sherman
parents: 5506
diff changeset
    92
        charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0208_MS5022X"));
28969
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 27798
diff changeset
    93
        try {
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 27798
diff changeset
    94
            charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0208_Solaris"));
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 27798
diff changeset
    95
            charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0212_Solaris"));
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 27798
diff changeset
    96
        } catch (ClassNotFoundException x) {
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 27798
diff changeset
    97
            // these two might be moved into stdcs
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 27798
diff changeset
    98
            charsets.remove(Class.forName("sun.nio.cs.JIS_X_0208_Solaris"));
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 27798
diff changeset
    99
            charsets.remove(Class.forName("sun.nio.cs.JIS_X_0212_Solaris"));
f980bee32887 8073152: Update Standard/ExtendedCharsets to work with module system
sherman
parents: 27798
diff changeset
   100
        }
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   101
        // report the charsets that are implemented but not available
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   102
        if (charsets.size() > 0) {
29123
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
   103
            charsets.stream()
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
   104
                    .forEach( clz ->
c748c18fb05c 8073924: Update test/java/nio/charset/Charset/NIOCharsetAvailability.java to work with module system
sherman
parents: 28969
diff changeset
   105
                        System.out.println("Unused Charset subclass: " + clz));
4817
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   106
            throw new RuntimeException();
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   107
        }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   108
    }
67f425ac409b 6920732: opensource test/java/nio/charset
sherman
parents:
diff changeset
   109
}