View Javadoc

1   // SECTION-START[License Header]
2   // <editor-fold defaultstate="collapsed" desc=" Generated License ">
3   /*
4    *   Copyright (c) 2010 The JOMC Project
5    *   Copyright (c) 2005 Christian Schulte <schulte2005@users.sourceforge.net>
6    *   All rights reserved.
7    *
8    *   Redistribution and use in source and binary forms, with or without
9    *   modification, are permitted provided that the following conditions
10   *   are met:
11   *
12   *     o Redistributions of source code must retain the above copyright
13   *       notice, this list of conditions and the following disclaimer.
14   *
15   *     o Redistributions in binary form must reproduce the above copyright
16   *       notice, this list of conditions and the following disclaimer in
17   *       the documentation and/or other materials provided with the
18   *       distribution.
19   *
20   *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
21   *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22   *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23   *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
24   *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25   *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26   *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27   *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29   *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30   *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31   *
32   *   $Id: SequenceOperationsTest.java 2246 2010-06-29 07:59:25Z schulte2005 $
33   *
34   */
35  // </editor-fold>
36  // SECTION-END
37  package org.jomc.sequences.it;
38  
39  import java.io.IOException;
40  import java.io.InputStream;
41  import java.net.URL;
42  import java.util.ArrayList;
43  import java.util.Arrays;
44  import java.util.List;
45  import java.util.logging.LogManager;
46  import org.jomc.sequences.Sequence;
47  import org.jomc.sequences.SequenceLimitException;
48  import org.jomc.sequences.SequenceNotFoundException;
49  import org.jomc.sequences.SequencesSystemException;
50  import org.junit.Test;
51  import org.junit.runner.JUnitCore;
52  import static org.junit.Assert.assertEquals;
53  import static org.junit.Assert.assertNotNull;
54  import static org.junit.Assert.fail;
55  
56  // SECTION-START[Documentation]
57  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
58  /**
59   * Testcase for SequenceOperations implementations.
60   * <p><b>Dependencies</b><ul>
61   * <li>"{@link #getSequenceDirectory SequenceDirectory}"<blockquote>
62   * Dependency on {@code 'org.jomc.sequences.SequenceDirectory'} {@code (org.jomc.sequences.SequenceDirectory)} at specification level 1.0 bound to an instance.</blockquote></li>
63   * <li>"{@link #getSequenceOperations SequenceOperations}"<blockquote>
64   * Dependency on {@code 'org.jomc.sequences.SequenceOperations'} {@code (org.jomc.sequences.SequenceOperations)} at specification level 1.0 bound to an instance.</blockquote></li>
65   * </ul></p>
66   *
67   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
68   * @version $Id: SequenceOperationsTest.java 2246 2010-06-29 07:59:25Z schulte2005 $
69   */
70  // </editor-fold>
71  // SECTION-END
72  // SECTION-START[Annotations]
73  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
74  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
75  // </editor-fold>
76  // SECTION-END
77  public class SequenceOperationsTest
78  {
79      // SECTION-START[SequenceOperationsTest]
80  
81      /** Increment value of the test sequence. */
82      private static final int TEST_INCREMENT = 10;
83  
84      /**
85       * Tests the {@link org.jomc.sequences.SequenceOperations#getNextSequenceValue(String)} and
86       * {@link org.jomc.sequences.SequenceOperations#getNextSequenceValues(String,int)} methods to handle illegal
87       * arguments correctly by throwing a {@code SequencesSystemException} with non-null message.
88       */
89      @Test public void testIllegalArguments() throws Exception
90      {
91          assert this.getSequenceOperations() != null;
92  
93          try
94          {
95              this.getSequenceOperations().getNextSequenceValue( null );
96              fail( "Expected SequencesSystemException not thrown." );
97          }
98          catch ( final SequencesSystemException e )
99          {
100             assertNotNull( e.getMessage() );
101             System.out.println( e.toString() );
102         }
103 
104         try
105         {
106             this.getSequenceOperations().getNextSequenceValues( null, 0 );
107             fail( "Expected SequencesSystemException not thrown." );
108         }
109         catch ( final SequencesSystemException e )
110         {
111             assertNotNull( e.getMessage() );
112             System.out.println( e.toString() );
113         }
114 
115         try
116         {
117             this.getSequenceOperations().getNextSequenceValues( "TEST", -1 );
118             fail( "Expected SequencesSystemException not thrown." );
119         }
120         catch ( final SequencesSystemException e )
121         {
122             assertNotNull( e.getMessage() );
123             System.out.println( e.toString() );
124         }
125 
126     }
127 
128     /**
129      * Tests that requesting sequence values for unknown sequences is prevented by throwing a corresponding
130      * {@code SequenceNotFoundException}.
131      */
132     @Test public void testSequenceNotFoundException() throws Exception
133     {
134         assert this.getSequenceOperations() != null;
135 
136         try
137         {
138             this.getSequenceOperations().getNextSequenceValue( "UNKNOWN" );
139             fail( "Expected SequenceNotFoundException not thrown." );
140         }
141         catch ( final SequenceNotFoundException e )
142         {
143             assertNotNull( e.getMessage() );
144             System.out.println( e.toString() );
145         }
146 
147         try
148         {
149             this.getSequenceOperations().getNextSequenceValues( "UNKNOWN", 100 );
150             fail( "Expected SequenceNotFoundException not thrown." );
151         }
152         catch ( final SequenceNotFoundException e )
153         {
154             assertNotNull( e.getMessage() );
155             System.out.println( e.toString() );
156         }
157     }
158 
159     /**
160      * Tests the {@link org.jomc.sequences.SequenceOperations#getNextSequenceValue(java.lang.String) } and
161      * {@link org.jomc.sequences.SequenceOperations#getNextSequenceValues(java.lang.String, int) } methods to throw a
162      * {@code SequenceLimitException} when a maximum value is reached.
163      */
164     @Test public void testSequenceLimitException() throws Exception
165     {
166         this.setupTestSequence();
167 
168         final long nextValue = this.getSequenceOperations().getNextSequenceValue( this.getClass().getName() );
169 
170         assertEquals( 10, nextValue );
171 
172         try
173         {
174             this.getSequenceOperations().getNextSequenceValue( this.getClass().getName() );
175             fail( "Expected SequenceLimitException not thrown." );
176         }
177         catch ( final SequenceLimitException e )
178         {
179             assertNotNull( e.getMessage() );
180             System.out.println( e.toString() );
181         }
182 
183         this.setupTestSequence();
184 
185         final long[] nextValues = this.getSequenceOperations().getNextSequenceValues( this.getClass().getName(), 1 );
186         assertEquals( 1, nextValues.length );
187         assertEquals( 10, nextValues[0] );
188 
189         try
190         {
191             this.getSequenceOperations().getNextSequenceValues( this.getClass().getName(), 1 );
192             fail( "Expected SequenceLimitException not thrown." );
193         }
194         catch ( final SequenceLimitException e )
195         {
196             assertNotNull( e.getMessage() );
197             System.out.println( e.toString() );
198         }
199     }
200 
201     /**
202      * Test runner entry point.
203      * <p>This method sets up the JDK's {@code LogManager} with properties found at classpath location
204      * {@code "/logging.properties"} and executes {@link JUnitCore#main} passing the given arguments with this classes
205      * name prepended.</p>
206      *
207      * @param args Command line arguments.
208      */
209     public static void main( final String... args )
210     {
211         try
212         {
213             final URL loggingProperties = SequenceOperationsTest.class.getResource( "/logging.properties" );
214             if ( loggingProperties != null )
215             {
216                 final InputStream in = loggingProperties.openStream();
217                 LogManager.getLogManager().readConfiguration( in );
218                 in.close();
219             }
220 
221             final List<String> l = new ArrayList<String>( Arrays.asList( args ) );
222             l.add( 0, SequenceOperationsTest.class.getName() );
223             JUnitCore.main( l.toArray( new String[ l.size() ] ) );
224         }
225         catch ( final IOException e )
226         {
227             e.printStackTrace();
228             System.exit( 1 );
229         }
230     }
231 
232     private void setupTestSequence() throws Exception
233     {
234         assert this.getSequenceDirectory() != null;
235 
236         Sequence s = this.getSequenceDirectory().getSequence( this.getClass().getName() );
237 
238         if ( s == null )
239         {
240             s = new Sequence();
241             s.setIncrement( TEST_INCREMENT );
242             s.setMaximum( TEST_INCREMENT );
243             s.setMinimum( 0 );
244             s.setName( this.getClass().getName() );
245             s.setValue( 0 );
246 
247             this.getSequenceDirectory().addSequence( s );
248         }
249         else
250         {
251             s.setValue( 0 );
252             this.getSequenceDirectory().editSequence( s.getName(), s.getRevision(), s );
253         }
254     }
255 
256     // SECTION-END
257     // SECTION-START[Constructors]
258     // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
259 
260     /** Creates a new {@code SequenceOperationsTest} instance. */
261     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
262     public SequenceOperationsTest()
263     {
264         // SECTION-START[Default Constructor]
265         super();
266         // SECTION-END
267     }
268     // </editor-fold>
269     // SECTION-END
270     // SECTION-START[Dependencies]
271     // <editor-fold defaultstate="collapsed" desc=" Generated Dependencies ">
272 
273     /**
274      * Gets the {@code SequenceDirectory} dependency.
275      * <p>This method returns any available object of the {@code 'org.jomc.sequences.SequenceDirectory'} {@code (org.jomc.sequences.SequenceDirectory)} specification at specification level 1.0.</p>
276      * <p>That specification applies to {@code Singleton} scope. The singleton object is returned whenever requested and bound to this instance.</p>
277      * @return The {@code SequenceDirectory} dependency.
278      * {@code null} if no object is available.
279      * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
280      */
281     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
282     private org.jomc.sequences.SequenceDirectory getSequenceDirectory()
283     {
284         return (org.jomc.sequences.SequenceDirectory) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "SequenceDirectory" );
285     }
286 
287     /**
288      * Gets the {@code SequenceOperations} dependency.
289      * <p>This method returns any available object of the {@code 'org.jomc.sequences.SequenceOperations'} {@code (org.jomc.sequences.SequenceOperations)} specification at specification level 1.0.</p>
290      * <p>That specification applies to {@code Singleton} scope. The singleton object is returned whenever requested and bound to this instance.</p>
291      * @return The {@code SequenceOperations} dependency.
292      * {@code null} if no object is available.
293      * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
294      */
295     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
296     private org.jomc.sequences.SequenceOperations getSequenceOperations()
297     {
298         return (org.jomc.sequences.SequenceOperations) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "SequenceOperations" );
299     }
300     // </editor-fold>
301     // SECTION-END
302     // SECTION-START[Properties]
303     // SECTION-END
304     // SECTION-START[Messages]
305     // SECTION-END
306 }