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 org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.synchronoss.cpo.core.CpoAdapter;
30  import org.synchronoss.cpo.core.CpoAdapterFactoryManager;
31  import org.synchronoss.cpo.core.CpoException;
32  import org.synchronoss.cpo.core.CpoTrxAdapter;
33  import org.synchronoss.cpo.core.helper.ExceptionHelper;
34  import org.synchronoss.cpo.jdbc.ValueObject;
35  import org.synchronoss.cpo.jdbc.meta.JdbcCpoMetaDescriptor;
36  import org.testng.annotations.AfterClass;
37  import org.testng.annotations.BeforeClass;
38  import org.testng.annotations.Parameters;
39  import org.testng.annotations.Test;
40  
41  /**
42   * BlobTest is a test class for testing the JdbcAdapter class Constructors
43   *
44   * @author david berry
45   */
46  public class SelectForUpdateTest {
47  
48    // unique id base so this class's rows never collide with another test class's
49    private static final int IDB = 2200000;
50  
51    private static final Logger logger = LoggerFactory.getLogger(SelectForUpdateTest.class);
52    private CpoAdapter cpoAdapter = null;
53    private CpoTrxAdapter trxAdapter = null;
54    private JdbcCpoMetaDescriptor metaDescriptor = null;
55    private boolean isSupportsSelect4Update = true;
56  
57    public SelectForUpdateTest() {}
58  
59    /**
60     * <code>setUp</code> Load the datasource from the properties in the property file
61     * jdbc_en_US.properties
62     */
63    @Parameters({"db.select4update"})
64    @BeforeClass
65    public void setUp(boolean select4update) {
66      String method = "setUp:";
67      isSupportsSelect4Update = select4update;
68  
69      try {
70        cpoAdapter = CpoAdapterFactoryManager.getCpoAdapter(JdbcStatics.ADAPTER_CONTEXT_JDBC);
71        assertNotNull(cpoAdapter, method + "cpoAdapter is null");
72        trxAdapter = CpoAdapterFactoryManager.getCpoTrxAdapter(JdbcStatics.ADAPTER_CONTEXT_JDBC);
73        assertNotNull(trxAdapter, method + "trxAdapter is null");
74        metaDescriptor = (JdbcCpoMetaDescriptor) cpoAdapter.getCpoMetaDescriptor();
75      } catch (Exception e) {
76        logger.debug(ExceptionHelper.getLocalizedMessage(e));
77      }
78      ValueObject vo = ValueObjectFactory.createValueObject(IDB + 1);
79      ValueObject vo2 = ValueObjectFactory.createValueObject(IDB + 2);
80      try {
81        trxAdapter.insertBean(vo);
82        trxAdapter.insertBean(vo2);
83        trxAdapter.commit();
84      } catch (Exception e) {
85        try {
86          trxAdapter.rollback();
87        } catch (Exception e1) {
88        }
89        fail(method + e.getMessage());
90      }
91    }
92  
93    /** DOCUMENT ME! */
94    @AfterClass
95    public void tearDown() {
96      String method = "tearDown:";
97      ValueObject vo = ValueObjectFactory.createValueObject(IDB + 1);
98      ValueObject vo2 = ValueObjectFactory.createValueObject(IDB + 2);
99      try {
100       trxAdapter.deleteBean(vo);
101       trxAdapter.deleteBean(vo2);
102       trxAdapter.commit();
103     } catch (Exception e) {
104       try {
105         trxAdapter.rollback();
106       } catch (Exception e1) {
107       }
108       fail(method + e.getMessage());
109     } finally {
110       try {
111         trxAdapter.close();
112       } catch (Exception e1) {
113       }
114       trxAdapter = null;
115     }
116   }
117 
118   /** DOCUMENT ME! */
119   @Test
120   public void testSelect4UpdateSingleObject() {
121     if (isSupportsSelect4Update) {
122       String method = "testSelect4UpdateSingleObject:";
123       ValueObject vo2 = ValueObjectFactory.createValueObject(IDB + 1);
124 
125       try {
126         trxAdapter.retrieveBean(ValueObject.FG_RETRIEVE_SELECTFORUPDATE, vo2);
127       } catch (Exception e) {
128         fail(method + "Select For Update should work:" + ExceptionHelper.getLocalizedMessage(e));
129       }
130 
131       try {
132         trxAdapter.retrieveBean(ValueObject.FG_RETRIEVE_SELECTFORUPDATE, vo2);
133       } catch (Exception e) {
134         fail(method + "Select For Update should work:" + ExceptionHelper.getLocalizedMessage(e));
135       }
136 
137       try {
138         cpoAdapter.retrieveBean(ValueObject.FG_RETRIEVE_SELECT4UPDATENOWAIT, vo2);
139         fail(method + "SelectForUpdateNoWait should fail:");
140       } catch (Exception e) {
141         logger.debug(ExceptionHelper.getLocalizedMessage(e));
142       }
143 
144       try {
145         trxAdapter.commit();
146       } catch (Exception e) {
147         try {
148           trxAdapter.rollback();
149         } catch (CpoException ce) {
150           fail(method + "Rollback failed:" + ExceptionHelper.getLocalizedMessage(e));
151         }
152         fail(method + "Commit should have worked.");
153       }
154       try {
155         cpoAdapter.retrieveBean(ValueObject.FG_RETRIEVE_SELECT4UPDATENOWAIT, vo2);
156       } catch (Exception e) {
157         fail(
158             method
159                 + "SelectForUpdateNoWait should success:"
160                 + ExceptionHelper.getLocalizedMessage(e));
161       }
162     } else {
163       logger.error(cpoAdapter.getDataSourceName() + " does not support Select For Update");
164     }
165   }
166 
167   @Test
168   public void testSelect4UpdateExists() {
169     if (isSupportsSelect4Update) {
170       String method = "testSelect4UpdateExists:";
171       ValueObject vo2 = ValueObjectFactory.createValueObject(IDB + 1);
172 
173       try {
174         long count = trxAdapter.existsBean(ValueObject.FG_EXIST_SELECTFORUPDATEEXISTZERO, vo2);
175         assertEquals(0, count, "Zero objects should have been returned");
176       } catch (Exception e) {
177         fail(method + "Select For Update should work:" + ExceptionHelper.getLocalizedMessage(e));
178       }
179 
180       try {
181         long count = trxAdapter.existsBean(ValueObject.FG_EXIST_SELECTFORUPDATEEXISTSINGLE, vo2);
182         assertEquals(1, count, "One object should have been returned, got " + count);
183       } catch (Exception e) {
184         fail(method + "Select For Update should work:" + ExceptionHelper.getLocalizedMessage(e));
185       }
186 
187       try {
188         long count = trxAdapter.existsBean(ValueObject.FG_EXIST_SELECTFORUPDATEEXISTALL, vo2);
189         assertEquals(2, count, "Two objects should have been returned, got " + count);
190       } catch (Exception e) {
191         fail(method + "Select For Update should work:" + ExceptionHelper.getLocalizedMessage(e));
192       }
193 
194       try {
195         trxAdapter.commit();
196       } catch (Exception e) {
197         try {
198           trxAdapter.rollback();
199         } catch (CpoException ce) {
200           fail(method + "Rollback failed:" + ExceptionHelper.getLocalizedMessage(ce));
201         }
202         fail(method + "Commit should have worked.");
203       }
204     } else {
205       logger.error(cpoAdapter.getDataSourceName() + " does not support Select For Update");
206     }
207   }
208 }