1 /*
   2  * Copyright (c) 2015, 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.transaction;
  24 
  25 import java.io.ByteArrayInputStream;
  26 import java.io.ObjectInputStream;
  27 import javax.transaction.xa.XAException;
  28 import static org.testng.Assert.*;
  29 import org.testng.annotations.Test;
  30 import util.SerializedTransactionExceptions;
  31 
  32 public class XAExceptionTests {
  33 
  34     protected final String reason = "reason";
  35 
  36     /**
  37      * Create XAException with no-arg constructor
  38      */
  39     @Test
  40     public void test1() {
  41         XAException ex = new XAException();
  42         assertTrue(ex.getMessage() == null
  43                 && ex.getCause() == null
  44                 && ex.errorCode == 0);
  45     }
  46 
  47     /**
  48      * Create XAException with message
  49      */
  50     @Test
  51     public void test2() {
  52         XAException ex = new XAException(reason);
  53         assertTrue(ex.getMessage().equals(reason)
  54                 && ex.getCause() == null
  55                 && ex.errorCode == 0);
  56     }
  57 
  58     /**
  59      * De-Serialize a XAException from JDBC 4.0 and make sure you can read it
  60      * back properly
  61      */
  62     @Test
  63     public void test3() throws Exception {
  64 
  65         ObjectInputStream ois = new ObjectInputStream(
  66                 new ByteArrayInputStream(SerializedTransactionExceptions.XAE_DATA));
  67         XAException ex = (XAException) ois.readObject();
  68         assertTrue(reason.equals(ex.getMessage())
  69                 && ex.getCause() == null
  70                 && ex.errorCode == 0);
  71     }
  72 
  73     /**
  74      * Create TransactionRolledbackException specifying an error code
  75      */
  76     @Test
  77     public void test4() {
  78         int error = 21;
  79         XAException ex = new XAException(error);
  80         assertTrue(ex.getMessage() == null
  81                 && ex.getCause() == null
  82                 && ex.errorCode == error);
  83     }
  84 
  85     /**
  86      * Create TransactionRolledbackException specifying an error code
  87      */
  88     @Test
  89     public void test5() {
  90         int error = 21;
  91         XAException ex = new XAException(error);
  92         ex.errorCode = error;
  93         assertTrue(ex.getMessage() == null
  94                 && ex.getCause() == null
  95                 && ex.errorCode == error);
  96     }
  97 
  98 }