Path: blob/master/test/langtools/tools/javap/ControlCharTest.java
41145 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 814336626* @summary Check that control chars are displayed correctly27* @modules jdk.jdeps/com.sun.tools.javap28* @run testng ControlCharTest29*/3031import org.testng.Assert;32import org.testng.annotations.AfterClass;33import org.testng.annotations.BeforeClass;34import org.testng.annotations.DataProvider;35import org.testng.annotations.Test;3637import java.io.IOException;38import java.io.PrintWriter;39import java.nio.file.Files;40import java.nio.file.Path;41import java.nio.file.Paths;42import java.util.Scanner;43import java.util.regex.Pattern;4445public class ControlCharTest {46private String classpath;47private Path output;48private Pattern ptrn;4950@BeforeClass51public void initialize() throws Exception {52String testClasses = System.getProperty("test.classes", ".");53classpath = "-classpath " + testClasses + " ControlCharTest$Strings";54String userdir = System.getProperty("user.dir", ".");55output = Paths.get(userdir, "output.txt");56String regex = Pattern.quote("\\u0001\\u0002\\u0003") // \u0001\u0002\u000357+ ".+123.+" // 12358+ Pattern.quote("\\\\u0000") // \\u000059+ ".+"60+ Pattern.quote("\\u0000") // \u000061;62ptrn = Pattern.compile(regex, Pattern.DOTALL);63}6465@AfterClass66public void close() throws IOException {67Files.deleteIfExists(output);68}6970@DataProvider(name = "options")71public Object[][] createData() {72return new Object[][] {73{ "-v", ""},74{ "-constants", ""}75};76}77@Test(dataProvider = "options")78public void test(String option, String ignore) throws Exception {79String cmdline = option + " " + classpath;80javap(cmdline.split(" +"));81try (Scanner scnr = new Scanner(output)) {82Assert.assertNotNull(scnr.findWithinHorizon(ptrn, 0));83}84}8586private void javap(String... args) throws Exception {87try (PrintWriter out = new PrintWriter(output.toFile())) {88int rc = com.sun.tools.javap.Main.run(args, out);89if (rc < 0)90throw new Exception("javap exited, rc=" + rc);91}92}9394// small class to test95static class Strings {96static final String s = "\1\2\3";97static final String s1 = "123";98static final String s2 = "\\u0000";99static final String s3 = "\0";100static String f() { return s + s1 + s2 + s3; }101}102}103104105106107