1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 package org.jomc.sequences.it;
38
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.math.BigInteger;
42 import java.net.URL;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.List;
46 import java.util.Set;
47 import java.util.logging.LogManager;
48 import org.jomc.sequences.ConcurrentModificationException;
49 import org.jomc.sequences.SequenceExistsException;
50 import org.jomc.sequences.SequenceVetoException;
51 import org.jomc.sequences.Sequence;
52 import org.jomc.sequences.SequenceNotFoundException;
53 import org.jomc.sequences.SequencesSystemException;
54 import org.junit.Test;
55 import org.junit.runner.JUnitCore;
56 import static org.junit.Assert.assertEquals;
57 import static org.junit.Assert.assertNotNull;
58 import static org.junit.Assert.fail;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
87
88
89 public class SequenceDirectoryTest
90 {
91
92
93
94
95
96
97
98 public static Sequence getTestSequence()
99 {
100 final Sequence legal = new Sequence();
101 legal.setIncrement( 1L );
102 legal.setMaximum( 10L );
103 legal.setMinimum( 0L );
104 legal.setName( "TEST" );
105 legal.setValue( 0L );
106
107 return legal;
108 }
109
110
111
112
113
114 @Test public void testIllegalArguments() throws Exception
115 {
116 assert this.getSequenceDirectory() != null;
117
118 try
119 {
120 this.getSequenceDirectory().addSequence( null );
121 fail( "Expected SequencesSystemException not thrown." );
122 }
123 catch ( final SequencesSystemException e )
124 {
125 assertNotNull( e.getMessage() );
126 System.out.println( e.toString() );
127 }
128
129 try
130 {
131 this.getSequenceDirectory().deleteSequence( null, 0L );
132 fail( "Expected SequencesSystemException not thrown." );
133 }
134 catch ( final SequencesSystemException e )
135 {
136 assertNotNull( e.getMessage() );
137 System.out.println( e.toString() );
138 }
139
140 try
141 {
142 this.getSequenceDirectory().editSequence( null, 0L, null );
143 fail( "Expected SequencesSystemException not thrown." );
144 }
145 catch ( final SequencesSystemException e )
146 {
147 assertNotNull( e.getMessage() );
148 System.out.println( e.toString() );
149 }
150
151 try
152 {
153 this.getSequenceDirectory().editSequence( "TEST", 0L, null );
154 fail( "Expected SequencesSystemException not thrown." );
155 }
156 catch ( final SequencesSystemException e )
157 {
158 assertNotNull( e.getMessage() );
159 System.out.println( e.toString() );
160 }
161
162 try
163 {
164 this.getSequenceDirectory().editSequence( null, 0L, new Sequence() );
165 fail( "Expected SequencesSystemException not thrown." );
166 }
167 catch ( final SequencesSystemException e )
168 {
169 assertNotNull( e.getMessage() );
170 System.out.println( e.toString() );
171 }
172
173 try
174 {
175 this.getSequenceDirectory().getSequence( null );
176 fail( "Expected SequencesSystemException not thrown." );
177 }
178 catch ( final SequencesSystemException e )
179 {
180 assertNotNull( e.getMessage() );
181 System.out.println( e.toString() );
182 }
183
184 }
185
186
187 @Test public void testAddEditSearchDeleteLegalSequence() throws Exception
188 {
189 assert this.getSequenceDirectory() != null;
190
191 this.clearDirectory();
192
193 Sequence legal = getTestSequence();
194
195 legal = this.getSequenceDirectory().addSequence( legal );
196
197 System.out.println( legal );
198
199 legal.setName( "TEST2" );
200
201 legal = this.getSequenceDirectory().editSequence( "TEST", legal.getRevision(), legal );
202
203 System.out.println( legal );
204
205 assertEquals( legal, this.getSequenceDirectory().getSequence( "TEST2" ) );
206
207 final Set<Sequence> result = this.getSequenceDirectory().searchSequences( "%TEST%" );
208
209 assertEquals( 1, result.size() );
210 assertEquals( legal, result.toArray()[0] );
211
212 this.getSequenceDirectory().deleteSequence( "TEST2", legal.getRevision() );
213
214 assertEquals( 0, this.getSequenceDirectory().searchSequences( null ).size() );
215 assertEquals( BigInteger.ZERO, this.getSequenceDirectory().getSequenceCount() );
216 }
217
218
219 @Test public void testConcurrentModificationException() throws Exception
220 {
221 assert this.getSequenceDirectory() != null;
222
223 this.clearDirectory();
224
225 final Sequence legal = getTestSequence();
226
227 final Sequence sequence = this.getSequenceDirectory().addSequence( legal );
228
229 try
230 {
231 this.getSequenceDirectory().editSequence( "TEST", sequence.getRevision() + 1L, sequence );
232 fail( "Expected ConcurrentModificationException not thrown." );
233 }
234 catch ( final ConcurrentModificationException e )
235 {
236 assertNotNull( e.getMessage() );
237 System.out.println( e.toString() );
238 }
239
240 try
241 {
242 this.getSequenceDirectory().deleteSequence( "TEST", sequence.getRevision() + 1L );
243 fail( "Expected ConcurrentModificationException not thrown." );
244 }
245 catch ( final ConcurrentModificationException e )
246 {
247 assertNotNull( e.getMessage() );
248 System.out.println( e.toString() );
249 }
250 }
251
252
253
254
255
256 @Test public void testSequenceVetoException() throws Exception
257 {
258 assert this.getSequenceDirectory() != null;
259
260 this.clearDirectory();
261
262 Sequence legal = getTestSequence();
263
264 final Sequence illegal = new Sequence();
265 char[] name = new char[ this.getSequenceNameMaxLength() + 1 ];
266 Arrays.fill( name, 'T' );
267
268 illegal.setName( String.valueOf( name ) );
269 illegal.setMinimum( 100L );
270 illegal.setMaximum( 1L );
271 illegal.setValue( 0L );
272 illegal.setIncrement( -1L );
273
274 try
275 {
276 this.getSequenceDirectory().addSequence( illegal );
277 fail( "Expected SequenceVetoException not thrown." );
278 }
279 catch ( final SequenceVetoException e )
280 {
281 assertNotNull( e.getMessage() );
282 System.out.println( e.toString() );
283 }
284
285 illegal.setName( null );
286
287 try
288 {
289 this.getSequenceDirectory().addSequence( illegal );
290 fail( "Expected SequenceVetoException not thrown." );
291 }
292 catch ( final SequenceVetoException e )
293 {
294 assertNotNull( e.getMessage() );
295 System.out.println( e.toString() );
296 }
297
298 if ( this.getSequenceNameMinLength() - 1 >= 0 )
299 {
300 try
301 {
302 name = new char[ this.getSequenceNameMinLength() - 1 ];
303 Arrays.fill( name, 'T' );
304 illegal.setName( String.valueOf( name ) );
305 this.getSequenceDirectory().addSequence( illegal );
306 fail( "Expected SequenceVetoException not thrown." );
307 }
308 catch ( final SequenceVetoException e )
309 {
310 assertNotNull( e.getMessage() );
311 System.out.println( e.toString() );
312 }
313 }
314
315 legal = this.getSequenceDirectory().addSequence( legal );
316
317 try
318 {
319 this.getSequenceDirectory().editSequence( legal.getName(), legal.getRevision(), illegal );
320 fail( "Expected SequenceVetoException not thrown." );
321 }
322 catch ( final SequenceVetoException e )
323 {
324 assertNotNull( e.getMessage() );
325 System.out.println( e.toString() );
326 }
327
328 this.getSequenceDirectory().deleteSequence( legal.getName(), legal.getRevision() );
329 }
330
331
332
333
334 @Test public void testSequenceExistsException() throws Exception
335 {
336 assert this.getSequenceDirectory() != null;
337
338 this.clearDirectory();
339
340 Sequence legal = getTestSequence();
341
342 legal = this.getSequenceDirectory().addSequence( legal );
343
344 try
345 {
346 this.getSequenceDirectory().addSequence( legal );
347 fail( "Expected SequenceExistsException not thrown." );
348 }
349 catch ( final SequenceExistsException e )
350 {
351 assertNotNull( e.getMessage() );
352 System.out.println( e.toString() );
353 }
354 }
355
356
357
358
359
360 @Test public void testSequenceNotFoundException() throws Exception
361 {
362 assert this.getSequenceDirectory() != null;
363
364 this.clearDirectory();
365
366 final Sequence legal = getTestSequence();
367
368 try
369 {
370 this.getSequenceDirectory().editSequence( "UNKNOWN", 0L, legal );
371 fail( "Expected SequenceNotFoundException not thrown." );
372 }
373 catch ( final SequenceNotFoundException e )
374 {
375 assertNotNull( e.getMessage() );
376 System.out.println( e.toString() );
377 }
378
379 try
380 {
381 this.getSequenceDirectory().deleteSequence( "UNKNOWN", 0L );
382 fail( "Expected SequenceNotFoundException not thrown." );
383 }
384 catch ( final SequenceNotFoundException e )
385 {
386 assertNotNull( e.getMessage() );
387 System.out.println( e.toString() );
388 }
389
390 this.clearDirectory();
391 }
392
393
394 @Test public void testAddEditDeleteMany() throws Exception
395 {
396 assert this.getSequenceDirectory() != null;
397
398 this.clearDirectory();
399
400 final int count = 15;
401 final List<Sequence> added = new ArrayList<Sequence>( count );
402 final List<Sequence> updated = new ArrayList<Sequence>( count );
403 for ( int i = 0; i < count; i++ )
404 {
405 final Sequence legal = getTestSequence();
406 legal.setName( legal.getName() + ' ' + i );
407 final Sequence a = this.getSequenceDirectory().addSequence( legal );
408 added.add( a );
409 System.out.println( "ADD: " + a );
410 }
411
412 for ( Sequence s : added )
413 {
414 final String oldName = s.getName();
415 s.setName( oldName + "_UPDATED" );
416
417 final Sequence u = this.getSequenceDirectory().editSequence( oldName, s.getRevision(), s );
418 updated.add( u );
419
420 System.out.println( "EDIT: " + u );
421 }
422
423 for ( Sequence s : updated )
424 {
425 final Sequence d = this.getSequenceDirectory().deleteSequence( s.getName(), s.getRevision() );
426 System.out.println( "DELETE: " + d );
427 }
428
429 assertEquals( BigInteger.ZERO, this.getSequenceDirectory().getSequenceCount() );
430 }
431
432
433 protected void clearDirectory() throws Exception
434 {
435
436 for ( Sequence sequence : this.getSequenceDirectory().searchSequences( null ) )
437 {
438 System.out.println( this.getSequenceDirectory().deleteSequence(
439 sequence.getName(), sequence.getRevision() ) );
440
441 }
442
443 assertEquals( BigInteger.ZERO, this.getSequenceDirectory().getSequenceCount() );
444 }
445
446
447
448
449
450
451
452
453
454 public static void main( final String... args )
455 {
456 try
457 {
458 final URL loggingProperties = SequenceDirectoryTest.class.getResource( "/logging.properties" );
459 if ( loggingProperties != null )
460 {
461 final InputStream in = loggingProperties.openStream();
462 LogManager.getLogManager().readConfiguration( in );
463 in.close();
464 }
465
466 final List<String> l = new ArrayList<String>( Arrays.asList( args ) );
467 l.add( 0, SequenceDirectoryTest.class.getName() );
468 JUnitCore.main( l.toArray( new String[ l.size() ] ) );
469 }
470 catch ( final IOException e )
471 {
472 e.printStackTrace();
473 System.exit( 1 );
474 }
475 }
476
477
478
479
480
481
482 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
483 public SequenceDirectoryTest()
484 {
485
486 super();
487
488 }
489
490
491
492
493
494
495
496
497
498
499
500
501
502 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
503 private org.jomc.sequences.SequenceDirectory getSequenceDirectory()
504 {
505 return (org.jomc.sequences.SequenceDirectory) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "SequenceDirectory" );
506 }
507
508
509
510
511
512
513
514
515
516
517 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
518 private int getSequenceNameMaxLength()
519 {
520 final java.lang.Integer _p = (java.lang.Integer) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "sequenceNameMaxLength" );
521 assert _p != null : "'sequenceNameMaxLength' property not found.";
522 return _p.intValue();
523 }
524
525
526
527
528
529
530 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
531 private int getSequenceNameMinLength()
532 {
533 final java.lang.Integer _p = (java.lang.Integer) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "sequenceNameMinLength" );
534 assert _p != null : "'sequenceNameMinLength' property not found.";
535 return _p.intValue();
536 }
537
538
539
540
541 }