View Javadoc

1   /*
2    *   Copyright (C) Christian Schulte, 2005-206
3    *   All rights reserved.
4    *
5    *   Redistribution and use in source and binary forms, with or without
6    *   modification, are permitted provided that the following conditions
7    *   are met:
8    *
9    *     o Redistributions of source code must retain the above copyright
10   *       notice, this list of conditions and the following disclaimer.
11   *
12   *     o Redistributions in binary form must reproduce the above copyright
13   *       notice, this list of conditions and the following disclaimer in
14   *       the documentation and/or other materials provided with the
15   *       distribution.
16   *
17   *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18   *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19   *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20   *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21   *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   *
28   *   $JOMC: ResourceFileProcessorTest.java 4579 2012-06-03 00:28:14Z schulte2005 $
29   *
30   */
31  package org.jomc.tools.test;
32  
33  import java.io.File;
34  import java.io.IOException;
35  import org.jomc.model.Implementation;
36  import org.jomc.model.Module;
37  import org.jomc.model.Specification;
38  import org.jomc.modlet.Model;
39  import org.jomc.tools.ResourceFileProcessor;
40  import org.junit.Test;
41  import static org.junit.Assert.assertNotNull;
42  import static org.junit.Assert.assertNull;
43  import static org.junit.Assert.assertTrue;
44  import static org.junit.Assert.fail;
45  
46  /**
47   * Test cases for class {@code org.jomc.tools.ResourceFileProcessor}.
48   *
49   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
50   * @version $JOMC: ResourceFileProcessorTest.java 4579 2012-06-03 00:28:14Z schulte2005 $
51   */
52  public class ResourceFileProcessorTest extends JomcToolTest
53  {
54  
55      /** Creates a new {@code ResourceFileProcessorTest} instance. */
56      public ResourceFileProcessorTest()
57      {
58          super();
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public ResourceFileProcessor getJomcTool()
64      {
65          return (ResourceFileProcessor) super.getJomcTool();
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      protected ResourceFileProcessor newJomcTool()
71      {
72          return new ResourceFileProcessor();
73      }
74  
75      @Test
76      public final void testResourceFileProcessorNullPointerException() throws Exception
77      {
78          try
79          {
80              this.getJomcTool().getResourceBundleResources( (Specification) null );
81              fail( "Expected NullPointerException not thrown." );
82          }
83          catch ( final NullPointerException e )
84          {
85              assertNullPointerException( e );
86          }
87  
88          try
89          {
90              this.getJomcTool().getResourceBundleResources( (Implementation) null );
91              fail( "Expected NullPointerException not thrown." );
92          }
93          catch ( final NullPointerException e )
94          {
95              assertNullPointerException( e );
96          }
97  
98          try
99          {
100             this.getJomcTool().writeResourceBundleResourceFiles( null );
101             fail( "Expected NullPointerException not thrown." );
102         }
103         catch ( final NullPointerException e )
104         {
105             assertNullPointerException( e );
106         }
107 
108         try
109         {
110             this.getJomcTool().writeResourceBundleResourceFiles( (Module) null, new File( "/" ) );
111             fail( "Expected NullPointerException not thrown." );
112         }
113         catch ( final NullPointerException e )
114         {
115             assertNullPointerException( e );
116         }
117 
118         try
119         {
120             this.getJomcTool().writeResourceBundleResourceFiles( new Module(), null );
121             fail( "Expected NullPointerException not thrown." );
122         }
123         catch ( final NullPointerException e )
124         {
125             assertNullPointerException( e );
126         }
127 
128         try
129         {
130             this.getJomcTool().writeResourceBundleResourceFiles( (Specification) null, new File( "/" ) );
131             fail( "Expected NullPointerException not thrown." );
132         }
133         catch ( final NullPointerException e )
134         {
135             assertNullPointerException( e );
136         }
137 
138         try
139         {
140             this.getJomcTool().writeResourceBundleResourceFiles( new Specification(), null );
141             fail( "Expected NullPointerException not thrown." );
142         }
143         catch ( final NullPointerException e )
144         {
145             assertNullPointerException( e );
146         }
147 
148         try
149         {
150             this.getJomcTool().writeResourceBundleResourceFiles( (Implementation) null, new File( "/" ) );
151             fail( "Expected NullPointerException not thrown." );
152         }
153         catch ( final NullPointerException e )
154         {
155             assertNullPointerException( e );
156         }
157 
158         try
159         {
160             this.getJomcTool().writeResourceBundleResourceFiles( new Implementation(), null );
161             fail( "Expected NullPointerException not thrown." );
162         }
163         catch ( final NullPointerException e )
164         {
165             assertNullPointerException( e );
166         }
167     }
168 
169     @Test
170     public final void testResourceFileProcessorNotNull() throws Exception
171     {
172         assertNotNull( this.getJomcTool().getResourceBundleDefaultLocale() );
173         assertNotNull( this.getJomcTool().getResourceBundleResources(
174             this.getJomcTool().getModules().getSpecification( "Specification" ) ) );
175 
176         assertNotNull( this.getJomcTool().getResourceBundleResources(
177             this.getJomcTool().getModules().getImplementation( "Implementation" ) ) );
178 
179     }
180 
181     @Test
182     public final void testResourceBundleDefaultLocale() throws Exception
183     {
184         this.getJomcTool().setResourceBundleDefaultLocale( null );
185         assertNotNull( this.getJomcTool().getResourceBundleDefaultLocale() );
186         this.getJomcTool().setResourceBundleDefaultLocale( null );
187     }
188 
189     @Test
190     public final void testWriteResourceBundleResourceFiles() throws Exception
191     {
192         final File nonExistentDirectory = this.getNextOutputDirectory();
193 
194         try
195         {
196             this.getJomcTool().writeResourceBundleResourceFiles( nonExistentDirectory );
197             fail( "Expected IOException not thrown." );
198         }
199         catch ( final IOException e )
200         {
201             assertNotNull( e.getMessage() );
202             System.out.println( e );
203         }
204 
205         try
206         {
207             this.getJomcTool().writeResourceBundleResourceFiles(
208                 this.getJomcTool().getModules().getModule( "Module" ), nonExistentDirectory );
209 
210             fail( "Expected IOException not thrown." );
211         }
212         catch ( final IOException e )
213         {
214             assertNotNull( e.getMessage() );
215             System.out.println( e );
216         }
217 
218         try
219         {
220             this.getJomcTool().writeResourceBundleResourceFiles(
221                 this.getJomcTool().getModules().getSpecification( "Specification" ), nonExistentDirectory );
222 
223             fail( "Expected IOException not thrown." );
224         }
225         catch ( final IOException e )
226         {
227             assertNotNull( e.getMessage() );
228             System.out.println( e );
229         }
230 
231         try
232         {
233             this.getJomcTool().writeResourceBundleResourceFiles(
234                 this.getJomcTool().getModules().getImplementation( "Implementation" ), nonExistentDirectory );
235 
236             fail( "Expected IOException not thrown." );
237         }
238         catch ( final IOException e )
239         {
240             assertNotNull( e.getMessage() );
241             System.out.println( e );
242         }
243 
244         File resourcesDirectory = this.getNextOutputDirectory();
245         assertTrue( resourcesDirectory.mkdirs() );
246         this.getJomcTool().writeResourceBundleResourceFiles( resourcesDirectory );
247 
248         resourcesDirectory = this.getNextOutputDirectory();
249         assertTrue( resourcesDirectory.mkdirs() );
250         this.getJomcTool().writeResourceBundleResourceFiles(
251             this.getJomcTool().getModules().getModule( "Module" ), resourcesDirectory );
252 
253         resourcesDirectory = this.getNextOutputDirectory();
254         assertTrue( resourcesDirectory.mkdirs() );
255         this.getJomcTool().writeResourceBundleResourceFiles(
256             this.getJomcTool().getModules().getSpecification( "Specification" ), resourcesDirectory );
257 
258         resourcesDirectory = this.getNextOutputDirectory();
259         assertTrue( resourcesDirectory.mkdirs() );
260         this.getJomcTool().writeResourceBundleResourceFiles(
261             this.getJomcTool().getModules().getImplementation( "Implementation" ), resourcesDirectory );
262 
263     }
264 
265     @Test
266     public final void testCopyConstructor() throws Exception
267     {
268         try
269         {
270             new ResourceFileProcessor( null );
271             fail( "Expected NullPointerException not thrown." );
272         }
273         catch ( final NullPointerException e )
274         {
275             assertNotNull( e.getMessage() );
276             System.out.println( e.toString() );
277         }
278 
279         new ResourceFileProcessor( this.getJomcTool() );
280     }
281 
282     @Test
283     public final void testResourceFileProcessorModelObjectsNotFound() throws Exception
284     {
285         final File tmpDir = new File( System.getProperty( "java.io.tmpdir", "/tmp" ) );
286         final Module m = new Module();
287         m.setName( "DOES_NOT_EXIST" );
288 
289         final Specification s = new Specification();
290         s.setIdentifier( "DOES_NOT_EXIST)" );
291 
292         final Implementation i = new Implementation();
293         i.setIdentifier( "DOES_NOT_EXIST" );
294 
295         final Model oldModel = this.getJomcTool().getModel();
296         this.getJomcTool().setModel( null );
297 
298         assertNull( this.getJomcTool().getResourceBundleResources( i ) );
299         assertNull( this.getJomcTool().getResourceBundleResources( s ) );
300 
301         this.getJomcTool().writeResourceBundleResourceFiles( tmpDir );
302         this.getJomcTool().writeResourceBundleResourceFiles( m, tmpDir );
303         this.getJomcTool().writeResourceBundleResourceFiles( s, tmpDir );
304         this.getJomcTool().writeResourceBundleResourceFiles( i, tmpDir );
305 
306         this.getJomcTool().setModel( oldModel );
307     }
308 
309 }