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 
  24 /**
  25  * @test
  26  * @modules java.sql.rowset/com.sun.rowset
  27  *          java.sql.rowset/com.sun.rowset.internal
  28  *          java.sql.rowset/com.sun.rowset.providers
  29  */
  30 
  31 package test.rowset.spi;
  32 
  33 import com.sun.rowset.internal.SyncResolverImpl;
  34 import java.sql.SQLException;
  35 import javax.sql.rowset.spi.SyncProviderException;
  36 import static org.testng.Assert.*;
  37 import org.testng.annotations.AfterClass;
  38 import org.testng.annotations.BeforeClass;
  39 import org.testng.annotations.Test;
  40 import util.BaseTest;
  41 import util.StubSyncResolver;
  42 
  43 public class SyncProviderExceptionTests extends BaseTest {
  44     @BeforeClass
  45     public static void setUpClass() throws Exception {
  46         System.out.println(System.getProperty("java.naming.factory.initial"));
  47     }
  48 
  49     @AfterClass
  50     public static void tearDownClass() throws Exception {
  51     }
  52     /*
  53      * Create SyncProviderException with no-arg constructor
  54      */
  55     @Test
  56     public void test() {
  57         SyncProviderException ex = new SyncProviderException();
  58         assertTrue(ex.getMessage() == null
  59                 && ex.getSQLState() == null
  60                 && ex.getCause() == null
  61                 && ex.getErrorCode() == 0
  62                 && ex.getSyncResolver() instanceof SyncResolverImpl);
  63     }
  64 
  65     /*
  66      * Create SyncProviderException with no-arg constructor and
  67      * call setSyncResolver to indicate the SyncResolver to use
  68      */
  69     @Test
  70     public void test01() {
  71         SyncProviderException ex = new SyncProviderException();
  72         ex.setSyncResolver(new StubSyncResolver());
  73         assertTrue(ex.getMessage() == null
  74                 && ex.getSQLState() == null
  75                 && ex.getCause() == null
  76                 && ex.getErrorCode() == 0
  77                 && ex.getSyncResolver() instanceof StubSyncResolver);
  78     }
  79 
  80     /*
  81      * Create SyncProviderException with message
  82      */
  83     @Test
  84     public void test02() {
  85         SyncProviderException ex = new SyncProviderException(reason);
  86         assertTrue(ex.getMessage().equals(reason)
  87                 && ex.getSQLState() == null
  88                 && ex.getCause() == null
  89                 && ex.getErrorCode() == 0
  90                 && ex.getSyncResolver() instanceof SyncResolverImpl);
  91     }
  92 
  93     /*
  94      * Create SyncProviderException with message and
  95      * call setSyncResolver to indicate the SyncResolver to use
  96      */
  97     @Test
  98     public void test03() {
  99         SyncProviderException ex = new SyncProviderException(reason);
 100         ex.setSyncResolver(new StubSyncResolver());
 101 
 102         assertTrue(ex.getMessage().equals(reason)
 103                 && ex.getSQLState() == null
 104                 && ex.getCause() == null
 105                 && ex.getErrorCode() == 0
 106                 && ex.getSyncResolver() instanceof StubSyncResolver);
 107     }
 108 
 109     /*
 110      * Create SyncProviderException with and specify the SyncResolver to use
 111      */
 112     @Test
 113     public void test04() {
 114         SyncProviderException ex = new SyncProviderException(new StubSyncResolver());
 115         assertTrue(ex.getMessage() == null
 116                 && ex.getSQLState() == null
 117                 && ex.getCause() == null
 118                 && ex.getErrorCode() == 0
 119                 && ex.getSyncResolver() instanceof StubSyncResolver);
 120     }
 121 
 122     /*
 123      * Validate that the ordering of the returned Exceptions is correct using
 124      * for-each loop
 125      */
 126     @Test
 127     public void test05() {
 128         SyncProviderException ex = new SyncProviderException("Exception 1");
 129         ex.initCause(t1);
 130         SyncProviderException ex1 = new SyncProviderException("Exception 2");
 131         SyncProviderException ex2 = new SyncProviderException("Exception 3");
 132         ex2.initCause(t2);
 133         ex.setNextException(ex1);
 134         ex.setNextException(ex2);
 135         int num = 0;
 136         for (Throwable e : ex) {
 137             assertTrue(msgs[num++].equals(e.getMessage()));
 138         }
 139     }
 140 
 141     /*
 142      * Validate that the ordering of the returned Exceptions is correct using
 143      * traditional while loop
 144      */
 145     @Test
 146     public void test06() {
 147         SQLException ex = new SyncProviderException("Exception 1");
 148         ex.initCause(t1);
 149         SyncProviderException ex1 = new SyncProviderException("Exception 2");
 150         SyncProviderException ex2 = new SyncProviderException("Exception 3");
 151         ex2.initCause(t2);
 152         ex.setNextException(ex1);
 153         ex.setNextException(ex2);
 154         int num = 0;
 155         while (ex != null) {
 156             assertTrue(msgs[num++].equals(ex.getMessage()));
 157             Throwable c = ex.getCause();
 158             while (c != null) {
 159                 assertTrue(msgs[num++].equals(c.getMessage()));
 160                 c = c.getCause();
 161             }
 162             ex = ex.getNextException();
 163         }
 164     }
 165 
 166     /*
 167      * Serialize a SyncProviderException and make sure you can read it back properly
 168      */
 169     @Test
 170     public void test07() throws Exception {
 171         SyncProviderException e = new SyncProviderException(reason);
 172         SyncProviderException ex1 = createSerializedException(e);
 173         assertTrue(ex1.getMessage().equals(reason)
 174                 && ex1.getSQLState() == null
 175                 && ex1.getCause() == null
 176                 && ex1.getErrorCode() == 0
 177                 && ex1.getSyncResolver() instanceof SyncResolverImpl, ex1.getSyncResolver().getClass().getName());
 178     }
 179 
 180     /*
 181      * Serialize a SyncProviderException and make sure you can read it back properly
 182      */
 183     @Test
 184     public void test08() throws Exception {
 185         SyncProviderException e = new SyncProviderException(reason);
 186         e.setSyncResolver(new StubSyncResolver());
 187 
 188         SyncProviderException ex1 = createSerializedException(e);
 189         assertTrue(ex1.getMessage().equals(reason)
 190                 && ex1.getSQLState() == null
 191                 && ex1.getCause() == null
 192                 && ex1.getErrorCode() == 0
 193                 && ex1.getSyncResolver() instanceof StubSyncResolver);
 194     }
 195 }