test/java/sql/test/sql/DataTruncationTests.java

Print this page




   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 }


   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, "DataTruncation.ser");
 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 }