View Javadoc
1   package org.synchronoss.cpo.jdbc.adapter;
2   
3   /*-
4    * [[
5    * jdbc
6    * ==
7    * Copyright (C) 2003 - 2026 Exaxis LLC, Synchronoss Technologies Inc
8    * ==
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ]]
23   */
24  
25  import static org.testng.Assert.*;
26  
27  import java.util.ArrayList;
28  import org.synchronoss.cpo.core.CpoAdapterFactoryManager;
29  import org.synchronoss.cpo.core.CpoException;
30  import org.synchronoss.cpo.core.CpoTrxAdapter;
31  import org.synchronoss.cpo.core.helper.ExceptionHelper;
32  import org.synchronoss.cpo.jdbc.ValueObject;
33  import org.testng.annotations.AfterClass;
34  import org.testng.annotations.BeforeClass;
35  import org.testng.annotations.Test;
36  
37  /**
38   * BlobTest is a test class for testing the JdbcAdapter class Constructors
39   *
40   * @author david berry
41   */
42  public class RollbackTrxTest {
43  
44    // unique id base matching the hardcoded ids in this class's meta rollback groups
45    private static final int IDB = 2100000;
46  
47    private CpoTrxAdapter trxAdapter = null;
48  
49    /** Creates a new RollbackTest object. */
50    public RollbackTrxTest() {}
51  
52    /**
53     * <code>setUp</code> Load the datasource from the properties in the property file
54     * jdbc_en_US.properties
55     */
56    @BeforeClass
57    public void setUp() {
58      String method = "setUp:";
59  
60      try {
61        trxAdapter = CpoAdapterFactoryManager.getCpoTrxAdapter(JdbcStatics.ADAPTER_CONTEXT_JDBC);
62        assertNotNull(trxAdapter, method + "trxAdapter is null");
63      } catch (Exception e) {
64        fail(method + e.getMessage());
65      }
66      ValueObject vo = ValueObjectFactory.createValueObject(IDB + 1);
67      vo.setAttrVarChar("Test");
68      try {
69        trxAdapter.insertBean(vo);
70        trxAdapter.commit();
71      } catch (Exception e) {
72        try {
73          trxAdapter.rollback();
74        } catch (Exception e1) {
75        }
76        fail(method + e.getMessage());
77      }
78    }
79  
80    /** DOCUMENT ME! */
81    @AfterClass
82    public void tearDown() {
83      ValueObject vo = ValueObjectFactory.createValueObject(IDB + 1);
84      try {
85        trxAdapter.deleteBean(vo);
86        trxAdapter.commit();
87      } catch (Exception e) {
88        try {
89          trxAdapter.rollback();
90        } catch (Exception e1) {
91        }
92      } finally {
93        try {
94          trxAdapter.close();
95        } catch (Exception e1) {
96        }
97        trxAdapter = null;
98      }
99    }
100 
101   /** DOCUMENT ME! */
102   @Test
103   public void testTrxRollbackProcessUpdateCollection() {
104     String method = "testTrxRollbackProcessUpdateCollection:";
105     ValueObject vo = ValueObjectFactory.createValueObject(IDB + 2);
106     ValueObject vo2 = ValueObjectFactory.createValueObject(IDB + 1);
107     ArrayList<ValueObject> al = new ArrayList<>();
108 
109     al.add(vo);
110     al.add(vo2);
111 
112     try {
113       trxAdapter.insertBeans(ValueObject.FG_CREATE_TESTROLLBACK, al);
114       trxAdapter.commit();
115       fail(method + "Insert should have thrown an exception");
116     } catch (Exception e) {
117       try {
118         trxAdapter.rollback();
119       } catch (CpoException ce) {
120         fail(method + "Rollback failed:" + ExceptionHelper.getLocalizedMessage(ce));
121       }
122       try {
123         ValueObject rvo = trxAdapter.retrieveBean(vo);
124         assertNull(rvo, method + "Value Object did not rollback");
125       } catch (Exception e2) {
126         fail(method + e.getMessage());
127       }
128     }
129   }
130 
131   /** DOCUMENT ME! */
132   @Test
133   public void testTrxSingleRollback() {
134     String method = "testTrxSingleRollback:";
135     ValueObject vo = ValueObjectFactory.createValueObject(IDB + 2);
136     try {
137       trxAdapter.insertBean("TestSingleRollbackTrx", vo);
138       trxAdapter.commit();
139       fail(method + "Insert should have thrown an exception");
140     } catch (Exception e) {
141       try {
142         trxAdapter.rollback();
143       } catch (CpoException ce) {
144         fail(method + "Rollback failed:" + ExceptionHelper.getLocalizedMessage(ce));
145       }
146       try {
147         ValueObject rvo = trxAdapter.retrieveBean(vo);
148         assertNull(rvo, method + "Value Object did not rollback");
149       } catch (Exception e2) {
150         fail(method + e.getMessage());
151       }
152     }
153   }
154 }