001    // SECTION-START[License Header]
002    // <editor-fold defaultstate="collapsed" desc=" Generated License ">
003    /*
004     *   Copyright (c) 2010 The JOMC Project
005     *   Copyright (c) 2005 Christian Schulte <schulte2005@users.sourceforge.net>
006     *   All rights reserved.
007     *
008     *   Redistribution and use in source and binary forms, with or without
009     *   modification, are permitted provided that the following conditions
010     *   are met:
011     *
012     *     o Redistributions of source code must retain the above copyright
013     *       notice, this list of conditions and the following disclaimer.
014     *
015     *     o Redistributions in binary form must reproduce the above copyright
016     *       notice, this list of conditions and the following disclaimer in
017     *       the documentation and/or other materials provided with the
018     *       distribution.
019     *
020     *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
021     *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022     *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023     *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
024     *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025     *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026     *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027     *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028     *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
029     *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
030     *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031     *
032     *   $Id: SequenceOperationsTest.java 2246 2010-06-29 07:59:25Z schulte2005 $
033     *
034     */
035    // </editor-fold>
036    // SECTION-END
037    package org.jomc.sequences.it;
038    
039    import java.io.IOException;
040    import java.io.InputStream;
041    import java.net.URL;
042    import java.util.ArrayList;
043    import java.util.Arrays;
044    import java.util.List;
045    import java.util.logging.LogManager;
046    import org.jomc.sequences.Sequence;
047    import org.jomc.sequences.SequenceLimitException;
048    import org.jomc.sequences.SequenceNotFoundException;
049    import org.jomc.sequences.SequencesSystemException;
050    import org.junit.Test;
051    import org.junit.runner.JUnitCore;
052    import static org.junit.Assert.assertEquals;
053    import static org.junit.Assert.assertNotNull;
054    import static org.junit.Assert.fail;
055    
056    // SECTION-START[Documentation]
057    // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
058    /**
059     * Testcase for SequenceOperations implementations.
060     * <p><b>Dependencies</b><ul>
061     * <li>"{@link #getSequenceDirectory SequenceDirectory}"<blockquote>
062     * Dependency on {@code 'org.jomc.sequences.SequenceDirectory'} {@code (org.jomc.sequences.SequenceDirectory)} at specification level 1.0 bound to an instance.</blockquote></li>
063     * <li>"{@link #getSequenceOperations SequenceOperations}"<blockquote>
064     * Dependency on {@code 'org.jomc.sequences.SequenceOperations'} {@code (org.jomc.sequences.SequenceOperations)} at specification level 1.0 bound to an instance.</blockquote></li>
065     * </ul></p>
066     *
067     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
068     * @version $Id: SequenceOperationsTest.java 2246 2010-06-29 07:59:25Z schulte2005 $
069     */
070    // </editor-fold>
071    // SECTION-END
072    // SECTION-START[Annotations]
073    // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
074    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
075    // </editor-fold>
076    // SECTION-END
077    public class SequenceOperationsTest
078    {
079        // SECTION-START[SequenceOperationsTest]
080    
081        /** Increment value of the test sequence. */
082        private static final int TEST_INCREMENT = 10;
083    
084        /**
085         * Tests the {@link org.jomc.sequences.SequenceOperations#getNextSequenceValue(String)} and
086         * {@link org.jomc.sequences.SequenceOperations#getNextSequenceValues(String,int)} methods to handle illegal
087         * arguments correctly by throwing a {@code SequencesSystemException} with non-null message.
088         */
089        @Test public void testIllegalArguments() throws Exception
090        {
091            assert this.getSequenceOperations() != null;
092    
093            try
094            {
095                this.getSequenceOperations().getNextSequenceValue( null );
096                fail( "Expected SequencesSystemException not thrown." );
097            }
098            catch ( final SequencesSystemException e )
099            {
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    }