View Javadoc
1   /*
2    *   Copyright (C) Christian Schulte <cs@schulte.it>, 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 5043 2015-05-27 07:03:39Z schulte $
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:cs@schulte.it">Christian Schulte</a>
50   * @version $JOMC: ResourceFileProcessorTest.java 5043 2015-05-27 07:03:39Z schulte $
51   */
52  public class ResourceFileProcessorTest extends JomcToolTest
53  {
54  
55      /**
56       * Creates a new {@code ResourceFileProcessorTest} instance.
57       */
58      public ResourceFileProcessorTest()
59      {
60          super();
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public ResourceFileProcessor getJomcTool()
68      {
69          return (ResourceFileProcessor) super.getJomcTool();
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      protected ResourceFileProcessor newJomcTool()
77      {
78          return new ResourceFileProcessor();
79      }
80  
81      @Test
82      public final void testResourceFileProcessorNullPointerException() throws Exception
83      {
84          try
85          {
86              this.getJomcTool().getResourceBundleResources( (Specification) null );
87              fail( "Expected NullPointerException not thrown." );
88          }
89          catch ( final NullPointerException e )
90          {
91              assertNullPointerException( e );
92          }
93  
94          try
95          {
96              this.getJomcTool().getResourceBundleResources( (Implementation) null );
97              fail( "Expected NullPointerException not thrown." );
98          }
99          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 }