1 /* 2 * Copyright (c) 2015, 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 test.sql; 24 25 import java.sql.SQLException; 26 import static org.testng.Assert.assertEquals; 27 import org.testng.annotations.BeforeMethod; 28 import org.testng.annotations.DataProvider; 29 import org.testng.annotations.Test; 30 import util.BaseTest; 31 import util.StubStatement; 32 33 public class StatementTests extends BaseTest { 34 35 protected StubStatement stmt; 36 37 @BeforeMethod 38 public void setUpMethod() throws Exception { 39 stmt = new StubStatement(); 40 } 41 42 /* 43 * Verify that enqouteLiteral creates a valid literal and converts every 44 * single quote to two single quotes 45 */ 46 @Test(dataProvider = "validEnquotedLiteralValues") 47 public void test00(String s, String expected) { 48 assertEquals(stmt.enquoteLiteral(s), expected); 49 } 50 51 /* 52 * Validate a NullPointerException is thrown is the string passed to 53 * enquoteLiteral is null 54 */ 55 @Test(expectedExceptions = NullPointerException.class) 56 public void test01() { 57 stmt.enquoteLiteral(null); 58 59 } 60 61 /* 62 * Validate that enquoteIdentifier returns the expected value 63 */ 64 @Test(dataProvider = "validIdentifierValues") 65 public void test02(String s, boolean alwaysQuote, String expected) throws SQLException { 66 assertEquals(stmt.enquoteIdentifier(s, alwaysQuote), expected); 67 68 } 69 70 /* 71 * Validate that a SQLException is thrown for values that are not valid 72 * for a SQL identifier 73 */ 74 @Test(dataProvider = "invalidIdentifierValues", 75 expectedExceptions = SQLException.class) 76 public void test03(String s, boolean alwaysQuote, String expected) throws SQLException { 77 assertEquals(stmt.enquoteIdentifier(s, alwaysQuote), expected); 78 79 } 80 81 /* 82 * Validate a NullPointerException is thrown is the string passed to 83 * enquoteIdentiifer is null 84 */ 85 @Test(dataProvider = "trueFalse", 86 expectedExceptions = NullPointerException.class) 87 public void test03(boolean alwaysQuote) throws SQLException { 88 stmt.enquoteIdentifier(null, alwaysQuote); 89 90 } 91 92 /* 93 * DataProvider used to provide strings that will be used to validate 94 * that enqouteLiteral converts a string to a literal and every instance of 95 * a single quote will be converted into two single quotes in the literal. 96 */ 97 @DataProvider(name = "validEnquotedLiteralValues") 98 protected Object[][] validEnquotedLiteralValues() { 99 return new Object[][]{ 100 {"Hello", "'Hello'"}, 101 {"G'Day", "'G''Day'"}, 102 {"'G''Day'", "'''G''''Day'''"}, 103 {"I'''M", "'I''''''M'"}, 104 {"The Dark Knight", "'The Dark Knight'"} 105 106 }; 107 } 108 109 /* 110 * DataProvider used to provide strings that will be used to validate 111 * that enqouteIdentifier returns a simple SQL Identifier or a double 112 * quoted identifier 113 */ 114 @DataProvider(name = "validIdentifierValues") 115 protected Object[][] validEnquotedIdentifierValues() { 116 return new Object[][]{ 117 {"Hello", false, "Hello"}, 118 {"Hello", true, "\"Hello\""}, 119 {"G'Day", false, "\"G'Day\""}, 120 {"G'Day", true, "\"G'Day\""}, 121 {"Bruce Wayne", false, "\"Bruce Wayne\""}, 122 {"Bruce Wayne", true, "\"Bruce Wayne\""}, 123 {"GoodDay$", false, "\"GoodDay$\""}, 124 {"GoodDay$", true, "\"GoodDay$\""},}; 125 } 126 127 /* 128 * DataProvider used to provide strings are invalid for enquoteIdentifier 129 * resulting in a SQLException being thrown 130 */ 131 @DataProvider(name = "invalidIdentifierValues") 132 protected Object[][] invalidEnquotedIdentifierValues() { 133 int invalidLen = 129; 134 StringBuilder s = new StringBuilder(invalidLen); 135 for (int i = 0; i < invalidLen; i++) { 136 s.append('a'); 137 } 138 return new Object[][]{ 139 {"Hel\"lo", false, "Hello"}, 140 {"\"Hel\"lo\"", true, "\"Hello\""}, 141 {"Hello" + '\0', false, "\"G'Day\""}, 142 {"", false, "Hello"}, 143 {s.toString(), false, "Hello"},}; 144 } 145 }