1   /**
2    * This file is part of the equanda project.
3    *
4    * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7    *
8    * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
9    * ANY KIND, either express or implied. See the License for the specific language governing rights and
10   * limitations under the License.
11   *
12   * Alternatively, the contents of this file may be used under the terms of
13   * either the GNU General Public License Version 2 or later (the "GPL"), or
14   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
15   * in which case the provisions of the GPL or the LGPL are applicable instead
16   * of those above. If you wish to allow use of your version of this file only
17   * under the terms of either the GPL or the LGPL, and not to allow others to
18   * use your version of this file under the terms of the MPL, indicate your
19   * decision by deleting the provisions above and replace them with the notice
20   * and other provisions required by the GPL or the LGPL. If you do not delete
21   * the provisions above, a recipient may use your version of this file under
22   * the terms of any one of the MPL, the GPL or the LGPL.
23   */
24  
25  package org.equanda.test.xejb;
26  
27  import org.equanda.persistence.EquandaConstraintViolation;
28  import org.equanda.persistence.EquandaPersistenceException;
29  import org.equanda.persistence.ExceptionCodes;
30  import org.equanda.test.dm.client.Compare;
31  import org.equanda.test.dm.client.CompareEJB;
32  import org.equanda.test.dm.client.EquandaGlobal;
33  import org.equanda.test.dm.client.Vehicle;
34  import org.equanda.test.dm.client.constants.VehicleConstants;
35  
36  import java.sql.Date;
37  import java.util.ArrayList;
38  import java.util.Calendar;
39  import java.util.GregorianCalendar;
40  import java.util.List;
41  
42  /**
43   * test case for compare
44   *
45   * @author <a href="mailto:joachim@progs.be">Joachim Van der Auwera</a>
46   * @author NetRom team
47   */
48  public class CompareTest
49      extends TestAdapter
50  {
51      public void testCompareFailureIntField()
52          throws Exception
53      {
54          CompareEJB obj = EquandaGlobal.getCompare();
55          obj.init( 1, 1 );
56          try
57          {
58              obj.setInt1( 101 );
59              fail( "compare ignored" );
60          }
61          catch ( EquandaConstraintViolation ucv )
62          {
63          }
64      }
65  
66      public void testCompareFailureIntGlobal()
67          throws Exception
68      {
69          CompareEJB obj = EquandaGlobal.getCompare();
70          obj.init( 1, 1 );
71          obj.setInt1( 1 );
72          try
73          {
74              obj.setInt2( 2 );
75              fail( "compare ignored" );
76          }
77          catch ( EquandaConstraintViolation ucv2 )
78          {
79          }
80      }
81  
82      public void testCompareFailureBooleanField()
83          throws Exception
84      {
85          CompareEJB obj = EquandaGlobal.getCompare();
86          obj.init( 1, 1 );
87          try
88          {
89              obj.setBool1( true );
90              fail( "compare ignored" );
91          }
92          catch ( EquandaConstraintViolation ucv )
93          {
94          }
95      }
96  
97      public void testCompareFailureBooleanGlobal()
98          throws Exception
99      {
100         CompareEJB obj = EquandaGlobal.getCompare();
101         obj.init( 1, 1 );
102         obj.setBool1( false );
103         try
104         {
105             obj.setBool2( true );
106             fail( "compare ignored" );
107         }
108         catch ( EquandaConstraintViolation ucv2 )
109         {
110         }
111     }
112 
113     public void testCompareFailureDoubleField()
114         throws Exception
115     {
116         CompareEJB obj = EquandaGlobal.getCompare();
117         obj.init( 1, 1 );
118         try
119         {
120             obj.setDouble1( 1.1 );
121             fail( "compare ignored" );
122         }
123         catch ( EquandaConstraintViolation ucv )
124         {
125         }
126     }
127 
128     public void testCompareFailureDoubleGlobal()
129         throws Exception
130     {
131         CompareEJB obj = EquandaGlobal.getCompare();
132         obj.init( 1, 1 );
133         obj.setDouble1( 1.2 );
134         try
135         {
136             obj.setDouble2( 0.1 );
137             fail( "compare ignored" );
138         }
139         catch ( EquandaConstraintViolation ucv2 )
140         {
141         }
142     }
143 
144     public void testCompareFailureStringField()
145         throws Exception
146     {
147         CompareEJB obj = EquandaGlobal.getCompare();
148         obj.init( 1, 1 );
149         try
150         {
151             obj.setStr1( "aaa" );
152             fail( "compare ignored" );
153         }
154         catch ( EquandaConstraintViolation ucv )
155         {
156         }
157     }
158 
159     public void testCompareFailureStringGlobal()
160         throws Exception
161     {
162         CompareEJB obj = EquandaGlobal.getCompare();
163         obj.init( 1, 1 );
164         obj.setStr1( "bbb" );
165         try
166         {
167             obj.setStr2( "ccc" );
168             fail( "compare ignored" );
169         }
170         catch ( EquandaConstraintViolation ucv2 )
171         {
172         }
173     }
174 
175     public void testCompareFailureDateField()
176         throws Exception
177     {
178         CompareEJB obj = EquandaGlobal.getCompare();
179         obj.init( 1, 1 );
180         java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
181         cal.set( Calendar.YEAR, 2000 );
182         try
183         {
184             obj.setDat1( new java.sql.Date( cal.getTime().getTime() ) );
185             fail( "compare ignored" );
186         }
187         catch ( EquandaConstraintViolation ucv )
188         {
189         }
190     }
191 
192     public void testCompareFailureDateGlobal()
193         throws Exception
194     {
195         CompareEJB obj = EquandaGlobal.getCompare();
196         obj.init( 1, 1 );
197         GregorianCalendar cal = new GregorianCalendar();
198         cal.clear();
199         cal.set( 2005, 1, 1 );
200         Date date = new Date( cal.getTime().getTime() );
201         obj.setDat1( date );
202         try
203         {
204             obj.setDat2( date );
205             fail( "compare ignored" );
206         }
207         catch ( EquandaConstraintViolation ucv2 )
208         {
209         }
210     }
211 
212     public void testCompareFailureTimestampField()
213         throws Exception
214     {
215         CompareEJB obj = EquandaGlobal.getCompare();
216         obj.init( 1, 1 );
217         java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
218         cal.set( Calendar.YEAR, 2020 );
219         try
220         {
221             obj.setTime1( new java.sql.Timestamp( cal.getTime().getTime() ) );
222             fail( "compare ignored" );
223         }
224         catch ( EquandaConstraintViolation ucv )
225         {
226         }
227     }
228 
229     public void testCompareFailureTimestampGlobal()
230         throws Exception
231     {
232         CompareEJB obj = EquandaGlobal.getCompare();
233         obj.init( 1, 1 );
234         java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
235         cal.set( Calendar.YEAR, 2005 );
236         obj.setTime1( new java.sql.Timestamp( cal.getTime().getTime() ) );
237         try
238         {
239             obj.setTime2( new java.sql.Timestamp( cal.getTime().getTime() ) );
240             fail( "compare ignored" );
241         }
242         catch ( EquandaConstraintViolation ucv2 )
243         {
244         }
245     }
246 
247     public void testCompareFailureDateBuiltIn()
248         throws Exception
249     {
250         CompareEJB obj = EquandaGlobal.getCompare();
251         obj.init( 1, 1 );
252         try
253         {
254             obj.setDateFuture( new java.sql.Date( System.currentTimeMillis() ) );
255             fail( "compare ignored" );
256         }
257         catch ( EquandaConstraintViolation ucv )
258         {
259         }
260     }
261 
262     public void testCompareFailureTimestampBuiltIn()
263         throws Exception
264     {
265         CompareEJB obj = EquandaGlobal.getCompare();
266         obj.init( 1, 1 );
267         try
268         {
269             obj.setTimePast( new java.sql.Timestamp( System.currentTimeMillis() + 1000 ) );
270             fail( "compare ignored" );
271         }
272         catch ( EquandaConstraintViolation ucv )
273         {
274         }
275     }
276 
277     public void testCompareSuccessDateBuiltIn()
278         throws Exception
279     {
280         CompareEJB obj = EquandaGlobal.getCompare();
281         obj.init( 1, 1 );
282         try
283         {
284             obj.setDateFuture( new java.sql.Date( System.currentTimeMillis() + 3600 * 25 * 1000 ) );
285         }
286         catch ( EquandaConstraintViolation ucv )
287         {
288             fail( "compare failed for no reason" );
289         }
290     }
291 
292     public void testCompareSuccessTimestampBuiltIn()
293         throws Exception
294     {
295         CompareEJB obj = EquandaGlobal.getCompare();
296         obj.init( 1, 1 );
297         try
298         {
299             obj.setTimePast( new java.sql.Timestamp( System.currentTimeMillis() - 1000 ) );
300         }
301         catch ( EquandaConstraintViolation ucv )
302         {
303             fail( "compare failed for no reason" );
304         }
305     }
306 
307     public void testCompareImmutableField()
308         throws Exception
309     {
310         try
311         {
312             CompareEJB obj = EquandaGlobal.getCompare();
313             obj.init( 100, 1 );
314             obj.remove();
315             fail( "compare ignored" );
316         }
317         catch ( EquandaPersistenceException ucv )
318         {/*ignore*/}
319     }
320 
321     public void testCompareLinkTypeSingleAllowed()
322         throws Exception
323     {
324         Vehicle vehicle = Vehicle.equandaCreate( VehicleConstants.TYPE_VehicleWithWheels );
325         vehicle.setName( "CompareLinkTypeSingleAllowed" );
326         Compare obj = Compare.equandaCreate();
327         obj.setTestImmutableDouble( 1.0 );
328         obj.setDouble1( 1.2 );
329         obj.setWheeledVehicle( vehicle );
330         obj.equandaUpdate();
331     }
332 
333     public void testCompareLinkTypeSingleDisallowed()
334         throws Exception
335     {
336         Vehicle vehicle = Vehicle.equandaCreate( VehicleConstants.TYPE_Vehicle );
337         vehicle.setName( "CompareLinkTypeSingleDisallowed" );
338         Compare obj = Compare.equandaCreate();
339         obj.setTestImmutableDouble( 1.0 );
340         obj.setDouble1( 1.2 );
341         obj.setWheeledVehicle( vehicle );
342         try
343         {
344             obj.equandaUpdate();
345         }
346         catch ( EquandaPersistenceException epe )
347         {
348             assertEquals( ExceptionCodes.ECV_COMPARE_TYPE_CONSTRAINT_NOT_SATISFIED, epe.getExceptionCode() );
349             return;
350         }
351         fail( "type compare constraint not adhered for single linked fields" );
352     }
353 
354     public void testCompareLinkTypeMultipleAllowed()
355         throws Exception
356     {
357         Vehicle vehicle;
358         List<Vehicle> vehicles = new ArrayList<Vehicle>();
359         vehicle = Vehicle.equandaCreate( VehicleConstants.TYPE_VehicleWithWheels );
360         vehicle.setName( "CompareLinkTypeMultipleAllowed1" );
361         vehicles.add( vehicle );
362         vehicle = Vehicle.equandaCreate( VehicleConstants.TYPE_VehicleWithWheels );
363         vehicle.setName( "CompareLinkTypeMultipleAllowed2" );
364         vehicles.add( vehicle );
365         Compare obj = Compare.equandaCreate();
366         obj.setTestImmutableDouble( 1.0 );
367         obj.setDouble1( 1.2 );
368         obj.setWheeledVehicles( vehicles );
369         obj.equandaUpdate();
370     }
371 
372     public void testCompareLinkTypeMultipleDisallowed()
373         throws Exception
374     {
375         Vehicle vehicle;
376         List<Vehicle> vehicles = new ArrayList<Vehicle>();
377         vehicle = Vehicle.equandaCreate( VehicleConstants.TYPE_VehicleWithWheels );
378         vehicle.setName( "CompareLinkTypeMultipleDisallowed1" );
379         vehicles.add( vehicle );
380         vehicle = Vehicle.equandaCreate( VehicleConstants.TYPE_Vehicle );
381         vehicle.setName( "CompareLinkTypeMultipleDisallowed2" );
382         vehicles.add( vehicle );
383         Compare obj = Compare.equandaCreate();
384         obj.setTestImmutableDouble( 1.0 );
385         obj.setDouble1( 1.2 );
386         obj.setWheeledVehicles( vehicles );
387         try
388         {
389             obj.equandaUpdate();
390         }
391         catch ( EquandaPersistenceException epe )
392         {
393             assertEquals( ExceptionCodes.ECV_COMPARE_TYPE_CONSTRAINT_NOT_SATISFIED, epe.getExceptionCode() );
394             return;
395         }
396         fail( "type compare constraint not adhered for multiple linked fields" );
397     }
398 }