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