001    /*
002     *   Copyright (C) Christian Schulte, 2005-206
003     *   All rights reserved.
004     *
005     *   Redistribution and use in source and binary forms, with or without
006     *   modification, are permitted provided that the following conditions
007     *   are met:
008     *
009     *     o Redistributions of source code must retain the above copyright
010     *       notice, this list of conditions and the following disclaimer.
011     *
012     *     o Redistributions in binary form must reproduce the above copyright
013     *       notice, this list of conditions and the following disclaimer in
014     *       the documentation and/or other materials provided with the
015     *       distribution.
016     *
017     *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018     *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019     *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020     *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021     *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022     *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023     *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024     *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025     *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026     *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027     *
028     *   $JOMC: ModelHelperTest.java 3851 2011-10-10 16:25:09Z schulte2005 $
029     *
030     */
031    package org.jomc.model.modlet.test;
032    
033    import org.jomc.model.ModelObject;
034    import org.jomc.model.Module;
035    import org.jomc.model.Modules;
036    import org.jomc.model.modlet.ModelHelper;
037    import org.jomc.modlet.Model;
038    import org.junit.Test;
039    import static org.junit.Assert.assertEquals;
040    import static org.junit.Assert.assertNotNull;
041    import static org.junit.Assert.assertNull;
042    import static org.junit.Assert.assertTrue;
043    import static org.junit.Assert.fail;
044    
045    /**
046     * Test cases for class {@code org.jomc.model.modlet.ModelHelper}.
047     *
048     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
049     * @version $JOMC: ModelHelperTest.java 3851 2011-10-10 16:25:09Z schulte2005 $
050     */
051    public class ModelHelperTest
052    {
053    
054        /** Creates a new {@code ModelHelperTest} instance. */
055        public ModelHelperTest()
056        {
057            super();
058        }
059    
060        @Test
061        public final void testModelHelper() throws Exception
062        {
063            try
064            {
065                ModelHelper.getModules( null );
066                fail( "Expected NullPointerException not thrown." );
067            }
068            catch ( final NullPointerException e )
069            {
070                assertNotNull( e.getMessage() );
071                System.out.println( e.toString() );
072            }
073    
074            try
075            {
076                ModelHelper.setModules( new Model(), null );
077                fail( "Expected NullPointerException not thrown." );
078            }
079            catch ( final NullPointerException e )
080            {
081                assertNotNull( e.getMessage() );
082                System.out.println( e.toString() );
083            }
084    
085            try
086            {
087                ModelHelper.setModules( null, new Modules() );
088                fail( "Expected NullPointerException not thrown." );
089            }
090            catch ( final NullPointerException e )
091            {
092                assertNotNull( e.getMessage() );
093                System.out.println( e.toString() );
094            }
095    
096            try
097            {
098                ModelHelper.addModules( new Model(), null );
099                fail( "Expected NullPointerException not thrown." );
100            }
101            catch ( final NullPointerException e )
102            {
103                assertNotNull( e.getMessage() );
104                System.out.println( e.toString() );
105            }
106    
107            try
108            {
109                ModelHelper.addModules( null, new Modules() );
110                fail( "Expected NullPointerException not thrown." );
111            }
112            catch ( final NullPointerException e )
113            {
114                assertNotNull( e.getMessage() );
115                System.out.println( e.toString() );
116            }
117    
118            try
119            {
120                ModelHelper.removeModules( null );
121                fail( "Expected NullPointerException not thrown." );
122            }
123            catch ( final NullPointerException e )
124            {
125                assertNotNull( e.getMessage() );
126                System.out.println( e.toString() );
127            }
128    
129            final Model model = new Model();
130            model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
131    
132            ModelHelper.setModules( model, new Modules() );
133    
134            assertNotNull( ModelHelper.getModules( model ) );
135            assertTrue( ModelHelper.getModules( model ).getModule().isEmpty() );
136    
137            try
138            {
139                ModelHelper.setModules( model, new Modules() );
140                fail( "Expected IllegalStateException not thrown." );
141            }
142            catch ( final IllegalStateException e )
143            {
144                assertNotNull( e.getMessage() );
145                System.out.println( e.toString() );
146            }
147    
148            final Module m = new Module();
149            m.setName( "TEST" );
150    
151            final Modules add = new Modules();
152            add.getModule().add( m );
153    
154            ModelHelper.addModules( model, add );
155            assertNotNull( ModelHelper.getModules( model ) );
156            assertEquals( 1, ModelHelper.getModules( model ).getModule().size() );
157    
158            ModelHelper.removeModules( model );
159            assertNull( ModelHelper.getModules( model ) );
160        }
161    
162    }