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.rowset.spi;
  24 
  25 import java.sql.SQLException;
  26 import javax.sql.rowset.spi.SyncFactoryException;
  27 import static org.testng.Assert.*;
  28 import org.testng.annotations.Test;
  29 import util.BaseTest;
  30 
  31 public class SyncFactoryExceptionTests extends BaseTest {
  32 
  33     /*
  34      * Create SyncFactoryException with no-arg constructor
  35      */
  36     @Test
  37     public void test01() {
  38         SyncFactoryException ex = new SyncFactoryException();
  39         assertTrue(ex.getMessage() == null
  40                 && ex.getSQLState() == null
  41                 && ex.getCause() == null
  42                 && ex.getErrorCode() == 0);
  43     }
  44 
  45     /*
  46      * Create SyncFactoryException with message
  47      */
  48     @Test
  49     public void test02() {
  50         SyncFactoryException ex = new SyncFactoryException(reason);
  51         assertTrue(ex.getMessage().equals(reason)
  52                 && ex.getSQLState() == null
  53                 && ex.getCause() == null
  54                 && ex.getErrorCode() == 0);
  55     }
  56 
  57     /*
  58      * Validate that the ordering of the returned Exceptions is correct using
  59      * for-each loop
  60      */
  61     @Test
  62     public void test03() {
  63         SyncFactoryException ex = new SyncFactoryException("Exception 1");
  64         ex.initCause(t1);
  65         SyncFactoryException ex1 = new SyncFactoryException("Exception 2");
  66         SyncFactoryException ex2 = new SyncFactoryException("Exception 3");
  67         ex2.initCause(t2);
  68         ex.setNextException(ex1);
  69         ex.setNextException(ex2);
  70         int num = 0;
  71         for (Throwable e : ex) {
  72             assertTrue(msgs[num++].equals(e.getMessage()));
  73         }
  74     }
  75 
  76     /*
  77      * Validate that the ordering of the returned Exceptions is correct using
  78      * traditional while loop
  79      */
  80     @Test
  81     public void test04() {
  82         SQLException ex = new SyncFactoryException("Exception 1");
  83         ex.initCause(t1);
  84         SyncFactoryException ex1 = new SyncFactoryException("Exception 2");
  85         SyncFactoryException ex2 = new SyncFactoryException("Exception 3");
  86         ex2.initCause(t2);
  87         ex.setNextException(ex1);
  88         ex.setNextException(ex2);
  89         int num = 0;
  90         while (ex != null) {
  91             assertTrue(msgs[num++].equals(ex.getMessage()));
  92             Throwable c = ex.getCause();
  93             while (c != null) {
  94                 assertTrue(msgs[num++].equals(c.getMessage()));
  95                 c = c.getCause();
  96             }
  97             ex = ex.getNextException();
  98         }
  99     }
 100 
 101     /*
 102      * Serialize a SyncFactoryException and make sure you can read it back properly
 103      */
 104     @Test
 105     public void test05() throws Exception {
 106         SyncFactoryException e = new SyncFactoryException(reason);
 107         SyncFactoryException ex1 = createSerializedException(e);
 108         assertTrue(ex1.getMessage().equals(reason)
 109                 && ex1.getSQLState() == null
 110                 && ex1.getCause() == null
 111                 && ex1.getErrorCode() == 0);
 112     }
 113 }