langtools/test/tools/javap/ControlCharTest.java
changeset 39919 3d3573afc062
equal deleted inserted replaced
39918:00696f150132 39919:3d3573afc062
       
     1 /*
       
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8143366
       
    27  * @summary Check that control chars are displayed correctly
       
    28  * @modules jdk.jdeps/com.sun.tools.javap
       
    29  * @run testng ControlCharTest
       
    30  */
       
    31 
       
    32 import org.testng.Assert;
       
    33 import org.testng.annotations.AfterClass;
       
    34 import org.testng.annotations.BeforeClass;
       
    35 import org.testng.annotations.DataProvider;
       
    36 import org.testng.annotations.Test;
       
    37 
       
    38 import java.io.IOException;
       
    39 import java.io.PrintWriter;
       
    40 import java.nio.file.Files;
       
    41 import java.nio.file.Path;
       
    42 import java.nio.file.Paths;
       
    43 import java.util.Scanner;
       
    44 import java.util.regex.Pattern;
       
    45 
       
    46 public class ControlCharTest {
       
    47     private String classpath;
       
    48     private Path output;
       
    49     private Pattern ptrn;
       
    50 
       
    51     @BeforeClass
       
    52     public void initialize() throws Exception {
       
    53         String testClasses = System.getProperty("test.classes", ".");
       
    54         classpath = "-classpath " + testClasses + " ControlCharTest$Strings";
       
    55         String userdir = System.getProperty("user.dir", ".");
       
    56         output = Paths.get(userdir, "output.txt");
       
    57         String regex = Pattern.quote("\\u0001\\u0002\\u0003")  // \u0001\u0002\u0003
       
    58                 + ".+123.+"                   // 123
       
    59                 + Pattern.quote("\\\\u0000")  // \\u0000
       
    60                 + ".+"
       
    61                 + Pattern.quote("\\u0000")    // \u0000
       
    62                 ;
       
    63         ptrn = Pattern.compile(regex, Pattern.DOTALL);
       
    64     }
       
    65 
       
    66     @AfterClass
       
    67     public void close() throws IOException {
       
    68         Files.deleteIfExists(output);
       
    69     }
       
    70 
       
    71     @DataProvider(name = "options")
       
    72     public Object[][] createData() {
       
    73         return new Object[][] {
       
    74                 { "-v", ""},
       
    75                 { "-constants", ""}
       
    76         };
       
    77     }
       
    78     @Test(dataProvider = "options")
       
    79     public void test(String option, String ignore) throws Exception {
       
    80         String cmdline = option + " " + classpath;
       
    81         javap(cmdline.split(" +"));
       
    82         try (Scanner scnr = new Scanner(output)) {
       
    83             Assert.assertNotNull(scnr.findWithinHorizon(ptrn, 0));
       
    84         }
       
    85     }
       
    86 
       
    87     private void javap(String... args) throws Exception {
       
    88         try (PrintWriter out = new PrintWriter(output.toFile())) {
       
    89             int rc = com.sun.tools.javap.Main.run(args, out);
       
    90             if (rc < 0)
       
    91                 throw new Exception("javap exited, rc=" + rc);
       
    92         }
       
    93     }
       
    94 
       
    95     // small class to test
       
    96     static class Strings {
       
    97         static final String s = "\1\2\3";
       
    98         static final String s1 = "123";
       
    99         static final String s2 = "\\u0000";
       
   100         static final String s3 = "\0";
       
   101         static String f() { return s + s1 + s2 + s3; }
       
   102     }
       
   103 }
       
   104 
       
   105