001/*
002 *   Copyright (C) 2005 Christian Schulte <cs@schulte.it>
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: ResourceFileProcessorTest.java 5135 2016-04-08 13:53:07Z schulte $
029 *
030 */
031package org.jomc.tools.test;
032
033import java.io.File;
034import java.io.IOException;
035import org.jomc.model.Implementation;
036import org.jomc.model.Module;
037import org.jomc.model.Specification;
038import org.jomc.modlet.Model;
039import org.jomc.tools.ResourceFileProcessor;
040import org.junit.Test;
041import static org.junit.Assert.assertNotNull;
042import static org.junit.Assert.assertNull;
043import static org.junit.Assert.assertTrue;
044import static org.junit.Assert.fail;
045
046/**
047 * Test cases for class {@code org.jomc.tools.ResourceFileProcessor}.
048 *
049 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
050 * @version $JOMC: ResourceFileProcessorTest.java 5135 2016-04-08 13:53:07Z schulte $
051 */
052public class ResourceFileProcessorTest extends JomcToolTest
053{
054
055    /**
056     * Creates a new {@code ResourceFileProcessorTest} instance.
057     */
058    public ResourceFileProcessorTest()
059    {
060        super();
061    }
062
063    /**
064     * {@inheritDoc}
065     */
066    @Override
067    public ResourceFileProcessor getJomcTool()
068    {
069        return (ResourceFileProcessor) super.getJomcTool();
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    protected ResourceFileProcessor newJomcTool()
077    {
078        return new ResourceFileProcessor();
079    }
080
081    @Test
082    public final void testResourceFileProcessorNullPointerException() throws Exception
083    {
084        try
085        {
086            this.getJomcTool().getResourceBundleResources( (Specification) null );
087            fail( "Expected NullPointerException not thrown." );
088        }
089        catch ( final NullPointerException e )
090        {
091            assertNullPointerException( e );
092        }
093
094        try
095        {
096            this.getJomcTool().getResourceBundleResources( (Implementation) null );
097            fail( "Expected NullPointerException not thrown." );
098        }
099        catch ( final NullPointerException e )
100        {
101            assertNullPointerException( e );
102        }
103
104        try
105        {
106            this.getJomcTool().writeResourceBundleResourceFiles( null );
107            fail( "Expected NullPointerException not thrown." );
108        }
109        catch ( final NullPointerException e )
110        {
111            assertNullPointerException( e );
112        }
113
114        try
115        {
116            this.getJomcTool().writeResourceBundleResourceFiles( (Module) null, new File( "/" ) );
117            fail( "Expected NullPointerException not thrown." );
118        }
119        catch ( final NullPointerException e )
120        {
121            assertNullPointerException( e );
122        }
123
124        try
125        {
126            this.getJomcTool().writeResourceBundleResourceFiles( new Module(), null );
127            fail( "Expected NullPointerException not thrown." );
128        }
129        catch ( final NullPointerException e )
130        {
131            assertNullPointerException( e );
132        }
133
134        try
135        {
136            this.getJomcTool().writeResourceBundleResourceFiles( (Specification) null, new File( "/" ) );
137            fail( "Expected NullPointerException not thrown." );
138        }
139        catch ( final NullPointerException e )
140        {
141            assertNullPointerException( e );
142        }
143
144        try
145        {
146            this.getJomcTool().writeResourceBundleResourceFiles( new Specification(), null );
147            fail( "Expected NullPointerException not thrown." );
148        }
149        catch ( final NullPointerException e )
150        {
151            assertNullPointerException( e );
152        }
153
154        try
155        {
156            this.getJomcTool().writeResourceBundleResourceFiles( (Implementation) null, new File( "/" ) );
157            fail( "Expected NullPointerException not thrown." );
158        }
159        catch ( final NullPointerException e )
160        {
161            assertNullPointerException( e );
162        }
163
164        try
165        {
166            this.getJomcTool().writeResourceBundleResourceFiles( new Implementation(), null );
167            fail( "Expected NullPointerException not thrown." );
168        }
169        catch ( final NullPointerException e )
170        {
171            assertNullPointerException( e );
172        }
173    }
174
175    @Test
176    public final void testResourceFileProcessorNotNull() throws Exception
177    {
178        assertNotNull( this.getJomcTool().getResourceBundleDefaultLocale() );
179        assertNotNull( this.getJomcTool().getResourceBundleResources(
180            this.getJomcTool().getModules().getSpecification( "Specification" ) ) );
181
182        assertNotNull( this.getJomcTool().getResourceBundleResources(
183            this.getJomcTool().getModules().getImplementation( "Implementation" ) ) );
184
185    }
186
187    @Test
188    public final void testResourceBundleDefaultLocale() throws Exception
189    {
190        this.getJomcTool().setResourceBundleDefaultLocale( null );
191        assertNotNull( this.getJomcTool().getResourceBundleDefaultLocale() );
192        this.getJomcTool().setResourceBundleDefaultLocale( null );
193    }
194
195    @Test
196    public final void testWriteResourceBundleResourceFiles() throws Exception
197    {
198        final File nonExistentDirectory = this.getNextOutputDirectory();
199
200        try
201        {
202            this.getJomcTool().writeResourceBundleResourceFiles( nonExistentDirectory );
203            fail( "Expected IOException not thrown." );
204        }
205        catch ( final IOException e )
206        {
207            assertNotNull( e.getMessage() );
208            System.out.println( e );
209        }
210
211        try
212        {
213            this.getJomcTool().writeResourceBundleResourceFiles(
214                this.getJomcTool().getModules().getModule( "Module" ), nonExistentDirectory );
215
216            fail( "Expected IOException not thrown." );
217        }
218        catch ( final IOException e )
219        {
220            assertNotNull( e.getMessage() );
221            System.out.println( e );
222        }
223
224        try
225        {
226            this.getJomcTool().writeResourceBundleResourceFiles(
227                this.getJomcTool().getModules().getSpecification( "Specification" ), nonExistentDirectory );
228
229            fail( "Expected IOException not thrown." );
230        }
231        catch ( final IOException e )
232        {
233            assertNotNull( e.getMessage() );
234            System.out.println( e );
235        }
236
237        try
238        {
239            this.getJomcTool().writeResourceBundleResourceFiles(
240                this.getJomcTool().getModules().getImplementation( "Implementation" ), nonExistentDirectory );
241
242            fail( "Expected IOException not thrown." );
243        }
244        catch ( final IOException e )
245        {
246            assertNotNull( e.getMessage() );
247            System.out.println( e );
248        }
249
250        File resourcesDirectory = this.getNextOutputDirectory();
251        assertTrue( resourcesDirectory.mkdirs() );
252        this.getJomcTool().writeResourceBundleResourceFiles( resourcesDirectory );
253
254        resourcesDirectory = this.getNextOutputDirectory();
255        assertTrue( resourcesDirectory.mkdirs() );
256        this.getJomcTool().writeResourceBundleResourceFiles(
257            this.getJomcTool().getModules().getModule( "Module" ), resourcesDirectory );
258
259        resourcesDirectory = this.getNextOutputDirectory();
260        assertTrue( resourcesDirectory.mkdirs() );
261        this.getJomcTool().writeResourceBundleResourceFiles(
262            this.getJomcTool().getModules().getSpecification( "Specification" ), resourcesDirectory );
263
264        resourcesDirectory = this.getNextOutputDirectory();
265        assertTrue( resourcesDirectory.mkdirs() );
266        this.getJomcTool().writeResourceBundleResourceFiles(
267            this.getJomcTool().getModules().getImplementation( "Implementation" ), resourcesDirectory );
268
269    }
270
271    @Test
272    public final void testCopyConstructor() throws Exception
273    {
274        try
275        {
276            new ResourceFileProcessor( null );
277            fail( "Expected NullPointerException not thrown." );
278        }
279        catch ( final NullPointerException e )
280        {
281            assertNotNull( e.getMessage() );
282            System.out.println( e.toString() );
283        }
284
285        new ResourceFileProcessor( this.getJomcTool() );
286    }
287
288    @Test
289    public final void testResourceFileProcessorModelObjectsNotFound() throws Exception
290    {
291        final File tmpDir = new File( System.getProperty( "java.io.tmpdir", "/tmp" ) );
292        final Module m = new Module();
293        m.setName( "DOES_NOT_EXIST" );
294
295        final Specification s = new Specification();
296        s.setIdentifier( "DOES_NOT_EXIST)" );
297
298        final Implementation i = new Implementation();
299        i.setIdentifier( "DOES_NOT_EXIST" );
300
301        final Model oldModel = this.getJomcTool().getModel();
302        this.getJomcTool().setModel( null );
303
304        assertNull( this.getJomcTool().getResourceBundleResources( i ) );
305        assertNull( this.getJomcTool().getResourceBundleResources( s ) );
306
307        this.getJomcTool().writeResourceBundleResourceFiles( tmpDir );
308        this.getJomcTool().writeResourceBundleResourceFiles( m, tmpDir );
309        this.getJomcTool().writeResourceBundleResourceFiles( s, tmpDir );
310        this.getJomcTool().writeResourceBundleResourceFiles( i, tmpDir );
311
312        this.getJomcTool().setModel( oldModel );
313    }
314
315}