NameCodecTest.java revision 1786:80120e9b3273
1260684Skaiw/* 2260684Skaiw * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3260684Skaiw * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4260684Skaiw * 5260684Skaiw * This code is free software; you can redistribute it and/or modify it 6260684Skaiw * under the terms of the GNU General Public License version 2 only, as 7260684Skaiw * published by the Free Software Foundation. Oracle designates this 8260684Skaiw * particular file as subject to the "Classpath" exception as provided 9260684Skaiw * by Oracle in the LICENSE file that accompanied this code. 10260684Skaiw * 11260684Skaiw * This code is distributed in the hope that it will be useful, but WITHOUT 12260684Skaiw * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13260684Skaiw * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14260684Skaiw * version 2 for more details (a copy is included in the LICENSE file that 15260684Skaiw * accompanied this code). 16260684Skaiw * 17260684Skaiw * You should have received a copy of the GNU General Public License version 18260684Skaiw * 2 along with this work; if not, write to the Free Software Foundation, 19260684Skaiw * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20260684Skaiw * 21260684Skaiw * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22260684Skaiw * or visit www.oracle.com if you need additional information or have any 23260684Skaiw * questions. 24260684Skaiw */ 25260684Skaiwpackage jdk.nashorn.internal.runtime.linker.test; 26260684Skaiw 27260684Skaiwimport static org.testng.Assert.assertEquals; 28260684Skaiw 29260684Skaiwimport jdk.nashorn.internal.runtime.linker.NameCodec; 30260684Skaiwimport org.testng.annotations.Test; 31260684Skaiw 32260684Skaiw/** 33260684Skaiw * Test for jdk.nashorn.intenal.runtime.linker.NameCodec.java. This test is 34260684Skaiw * derived from BytecodeNameTest.java from (older) mlvm code @ 35260684Skaiw * http://hg.openjdk.java.net/mlvm/mlvm/file/tip/netbeans/meth/test/sun/invoke/util/BytecodeNameTest.java 36260684Skaiw * 37260684Skaiw * @bug 8141285: NameCode should pass tests from BytecodeNameTest.java 38260684Skaiw */ 39260684Skaiwpublic class NameCodecTest { 40260684Skaiw 41260684Skaiw static String[][] SAMPLES = { 42260684Skaiw // mangled, source 43260684Skaiw {"foo", "foo"}, 44260684Skaiw {"ba\\r", "ba\\r"}, 45260684Skaiw {"\\=ba\\-%z", "ba\\%z"}, 46260684Skaiw {"\\=ba\\--z", "ba\\-z"}, 47260684Skaiw {"=\\=", "=\\="}, 48260684Skaiw {"\\==\\|\\=", "=/\\="}, 49260684Skaiw {"\\|\\=", "/\\="}, 50260684Skaiw {"\\=ba\\!", "ba:"}, 51260684Skaiw {"\\|", "/"}, 52260684Skaiw {"\\", "\\"}, 53260684Skaiw {"\\\\%", "\\$"}, 54260684Skaiw {"\\\\", "\\\\"}, 55260684Skaiw {"\\=", ""} 56260684Skaiw 57260684Skaiw }; 58260684Skaiw 59260684Skaiw static final String DANGEROUS_CHARS = "\\/.;:$[]<>"; 60260684Skaiw static final String REPLACEMENT_CHARS = "-|,?!%{}^_"; 61260684Skaiw 62260684Skaiw static String[][] canonicalSamples() { 63260684Skaiw final int ndc = DANGEROUS_CHARS.length(); 64260684Skaiw final String[][] res = new String[2 * ndc][]; 65260684Skaiw for (int i = 0; i < ndc; i++) { 66260684Skaiw final char dc = DANGEROUS_CHARS.charAt(i); 67260684Skaiw final char rc = REPLACEMENT_CHARS.charAt(i); 68260684Skaiw if (dc == '\\') { 69260684Skaiw res[2 * i + 0] = new String[]{"\\-%", "\\%"}; 70260684Skaiw } else { 71260684Skaiw res[2 * i + 0] = new String[]{"\\" + rc, "" + dc}; 72260684Skaiw } 73260684Skaiw res[2 * i + 1] = new String[]{"" + rc, "" + rc}; 74260684Skaiw } 75260684Skaiw return res; 76260684Skaiw } 77260684Skaiw 78260684Skaiw @Test 79260684Skaiw public void testEncode() { 80260684Skaiw System.out.println("testEncode"); 81260684Skaiw testEncode(SAMPLES); 82260684Skaiw testEncode(canonicalSamples()); 83260684Skaiw } 84260684Skaiw 85260684Skaiw private void testEncode(final String[][] samples) { 86260684Skaiw for (final String[] sample : samples) { 87260684Skaiw final String s = sample[1]; 88260684Skaiw final String expResult = sample[0]; 89260684Skaiw final String result = NameCodec.encode(s); 90260684Skaiw if (!result.equals(expResult)) { 91260684Skaiw System.out.println(s + " => " + result + " != " + expResult); 92260684Skaiw } 93260684Skaiw assertEquals(expResult, result); 94260684Skaiw } 95260684Skaiw } 96260684Skaiw 97260684Skaiw @Test 98260684Skaiw public void testDecode() { 99260684Skaiw System.out.println("testDecode"); 100260684Skaiw testDecode(SAMPLES); 101260684Skaiw testDecode(canonicalSamples()); 102260684Skaiw } 103260684Skaiw 104260684Skaiw private void testDecode(final String[][] samples) { 105260684Skaiw for (final String[] sample : samples) { 106260684Skaiw final String s = sample[0]; 107260684Skaiw final String expResult = sample[1]; 108260684Skaiw final String result = NameCodec.decode(s); 109260684Skaiw assertEquals(expResult, result); 110260684Skaiw } 111260684Skaiw } 112260684Skaiw} 113260684Skaiw