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