1 /* 2 * Copyright (c) 2014, 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.DataTruncation; 26 import java.sql.SQLException; 27 import static org.testng.Assert.*; 28 import org.testng.annotations.Test; 29 import util.BaseTest; 30 31 public class DataTruncationTests extends BaseTest { 32 33 private final String READ_TRUNCATION = "01004"; 34 private final String WRITE_TRUNCATION = "22001"; 35 private final String dtReason = "Data truncation"; 36 private final int dterrorCode = 0; 37 private final String[] dtmsgs = {dtReason, "cause 1", dtReason, 38 dtReason, "cause 2"}; 39 private boolean onRead = false; 40 private final boolean parameter = false; 41 private final int index = 21; 42 private final int dataSize = 25; 43 private final int transferSize = 10; 44 45 /** 46 * Create DataTruncation object indicating a truncation on read 47 */ 48 @Test 49 public void test() { 50 onRead = true; 51 DataTruncation e = new DataTruncation(index, parameter, onRead, 52 dataSize, transferSize); 53 assertTrue(e.getMessage().equals(dtReason) 54 && e.getSQLState().equals(READ_TRUNCATION) 55 && e.getCause() == null 56 && e.getErrorCode() == dterrorCode 57 && e.getParameter() == parameter 58 && e.getRead() == onRead 59 && e.getDataSize() == dataSize 60 && e.getTransferSize() == transferSize 61 && e.getIndex() == index); 62 } 63 64 /** 65 * Create DataTruncation object indicating a truncation on write 66 */ 67 @Test 68 public void test1() { 69 onRead = false; 70 DataTruncation e = new DataTruncation(index, parameter, onRead, 71 dataSize, transferSize); 72 assertTrue(e.getMessage().equals(dtReason) 73 && e.getSQLState().equals(WRITE_TRUNCATION) 74 && e.getCause() == null 75 && e.getErrorCode() == dterrorCode 76 && e.getParameter() == parameter 77 && e.getRead() == onRead 78 && e.getDataSize() == dataSize 79 && e.getTransferSize() == transferSize 80 && e.getIndex() == index); 81 } 82 83 /** 84 * Create DataTruncation object indicating a truncation on read with a 85 * Throwable 86 */ 87 @Test 88 public void test2() { 89 onRead = true; 90 DataTruncation e = new DataTruncation(index, parameter, onRead, 91 dataSize, transferSize, t); 92 assertTrue(e.getMessage().equals(dtReason) 93 && e.getSQLState().equals(READ_TRUNCATION) 94 && cause.equals(e.getCause().toString()) 95 && e.getErrorCode() == dterrorCode 96 && e.getParameter() == parameter 97 && e.getRead() == onRead 98 && e.getDataSize() == dataSize 99 && e.getTransferSize() == transferSize 100 && e.getIndex() == index); 101 } 102 103 /** 104 * Create DataTruncation object indicating a truncation on read with null 105 * specified for the Throwable 106 */ 107 @Test 108 public void test3() { 109 onRead = true;; 110 DataTruncation e = new DataTruncation(index, parameter, onRead, 111 dataSize, transferSize, null); 112 assertTrue(e.getMessage().equals(dtReason) 113 && e.getSQLState().equals(READ_TRUNCATION) 114 && e.getCause() == null 115 && e.getErrorCode() == dterrorCode 116 && e.getParameter() == parameter 117 && e.getRead() == onRead 118 && e.getDataSize() == dataSize 119 && e.getTransferSize() == transferSize 120 && e.getIndex() == index); 121 } 122 123 /** 124 * Create DataTruncation object indicating a truncation on read and you can 125 * pass a -1 for the index 126 */ 127 @Test 128 public void test4() { 129 onRead = true; 130 int negIndex = -1; 131 DataTruncation e = new DataTruncation(negIndex, parameter, onRead, 132 dataSize, transferSize); 133 assertTrue(e.getMessage().equals(dtReason) 134 && e.getSQLState().equals(READ_TRUNCATION) 135 && e.getCause() == null 136 && e.getErrorCode() == dterrorCode 137 && e.getParameter() == parameter 138 && e.getRead() == onRead 139 && e.getDataSize() == dataSize 140 && e.getTransferSize() == transferSize 141 && e.getIndex() == negIndex); 142 } 143 144 /** 145 * Serialize a DataTruncation and make sure you can read it back properly 146 */ 147 @Test 148 public void test5() throws Exception { 149 DataTruncation e = new DataTruncation(index, parameter, onRead, 150 dataSize, transferSize); 151 DataTruncation ex1 = createSerializedException(e); 152 assertTrue(e.getMessage().equals(dtReason) 153 && e.getSQLState().equals(READ_TRUNCATION) 154 && e.getCause() == null 155 && e.getErrorCode() == dterrorCode 156 && e.getParameter() == parameter 157 && e.getRead() == onRead 158 && e.getDataSize() == dataSize 159 && e.getTransferSize() == transferSize 160 && e.getIndex() == index); 161 } 162 163 /** 164 * Validate that the ordering of the returned Exceptions is correct using 165 * for-each loop 166 */ 167 @Test 168 public void test11() { 169 DataTruncation ex = new DataTruncation(index, parameter, onRead, 170 dataSize, transferSize, t1); 171 DataTruncation ex1 = new DataTruncation(index, parameter, onRead, 172 dataSize, transferSize); 173 DataTruncation ex2 = new DataTruncation(index, parameter, onRead, 174 dataSize, transferSize, t2); 175 ex.setNextException(ex1); 176 ex.setNextException(ex2); 177 int num = 0; 178 for (Throwable e : ex) { 179 assertTrue(dtmsgs[num++].equals(e.getMessage())); 180 } 181 } 182 183 /** 184 * Validate that the ordering of the returned Exceptions is correct using 185 * traditional while loop 186 */ 187 @Test 188 public void test12() { 189 DataTruncation ex = new DataTruncation(index, parameter, onRead, 190 dataSize, transferSize, t1); 191 DataTruncation ex1 = new DataTruncation(index, parameter, onRead, 192 dataSize, transferSize); 193 DataTruncation ex2 = new DataTruncation(index, parameter, onRead, 194 dataSize, transferSize, t2); 195 ex.setNextException(ex1); 196 ex.setNextException(ex2); 197 int num = 0; 198 SQLException sqe = ex; 199 while (sqe != null) { 200 assertTrue(dtmsgs[num++].equals(sqe.getMessage())); 201 Throwable c = sqe.getCause(); 202 while (c != null) { 203 assertTrue(dtmsgs[num++].equals(c.getMessage())); 204 c = c.getCause(); 205 } 206 sqe = sqe.getNextException(); 207 } 208 } 209 }