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