1 /* 2 * Copyright (c) 2016, 2018, 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 package org.graalvm.compiler.debug.test; 26 27 import static org.junit.Assert.assertEquals; 28 29 import java.io.ByteArrayOutputStream; 30 import java.io.PrintStream; 31 import java.nio.charset.StandardCharsets; 32 import java.util.Arrays; 33 import java.util.Collection; 34 35 import org.junit.Test; 36 import org.junit.experimental.runners.Enclosed; 37 import org.junit.runner.RunWith; 38 import org.junit.runners.Parameterized; 39 import org.junit.runners.Parameterized.Parameter; 40 import org.junit.runners.Parameterized.Parameters; 41 42 import org.graalvm.compiler.debug.CSVUtil; 43 44 @RunWith(Enclosed.class) 45 public class CSVUtilTest { 46 47 @RunWith(Parameterized.class) 48 public static class FormatStringBuilder { 49 /** Some interesting values. */ 50 private static final Object[][] values = { 51 {"", ""}, 52 {"%s", "%s"}, 53 {"%s,%s", "%s;%s"}, 54 }; 55 56 @Parameters(name = " [{0}] to \"{1}\" ") 57 public static Collection<Object[]> data() { 58 return Arrays.asList(values); 59 } 60 61 @Parameter(value = 0) public String input; 62 @Parameter(value = 1) public String expected; 63 64 @Test 65 public void testBuildFormatString() { 66 assertEquals(expected, CSVUtil.buildFormatString(input.split(","))); 67 } 68 } 69 70 @RunWith(Parameterized.class) 71 public static class Escape { 72 73 /** Some interesting values. */ 74 private static final Object[][] values = { 75 {"XXX\"YYY", "\"XXX\\\"YYY\""}, 76 {"X\\XX\"YYY", "\"X\\\\XX\\\"YYY\""}, 77 }; 78 79 @Parameters(name = "''{0}'' to ''{1}''") 80 public static Collection<Object[]> data() { 81 return Arrays.asList(values); 82 } 83 84 @Parameter(value = 0) public String input; 85 @Parameter(value = 1) public String expected; 86 87 @Test 88 public void testEscape() { 89 assertEquals(expected, CSVUtil.Escape.escapeRaw(input)); 90 } 91 92 } 93 94 @RunWith(Parameterized.class) 95 public static class Formatter { 96 /** Some interesting values. */ 97 private static final Object[][] values = { 98 {"%s;%s", "XXX,YYY", "XXX;YYY"}, 99 {"%s;%s", "XXX,Y\"YY", "XXX;Y\"YY"}, 100 {"%s;%s", "XXX,Y;YY", "XXX;\"Y;YY\""}, 101 {"%s;%s", "XXX,Y\"Y;Y", "XXX;\"Y\\\"Y;Y\""}, 102 }; 103 104 @Parameters(name = "format=''{0}'' args=''{1}'' output=''{2}''") 105 public static Collection<Object[]> data() { 106 return Arrays.asList(values); 107 } 108 109 @Parameter(value = 0) public String format; 110 @Parameter(value = 1) public String args; 111 @Parameter(value = 2) public String expected; 112 113 @Test 114 public void testFormatter() { 115 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 116 // call the method under test 117 CSVUtil.Escape.println(new PrintStream(outputStream), format, toObjectArray(args)); 118 // get the actual string 119 String printedStream = new String(outputStream.toByteArray(), StandardCharsets.UTF_8); 120 // add newline to the expected string 121 assertEquals(expected + System.lineSeparator(), printedStream); 122 } 123 124 private static Object[] toObjectArray(String args) { 125 String[] split = args.split(","); 126 Object[] obj = new Object[split.length]; 127 for (int i = 0; i < split.length; i++) { 128 obj[i] = split[i]; 129 } 130 return obj; 131 } 132 133 } 134 135 }