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: ResourceFileProcessorTest.java 4579 2012-06-03 00:28:14Z schulte2005 $ 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:schulte2005@users.sourceforge.net">Christian Schulte</a> 050 * @version $JOMC: ResourceFileProcessorTest.java 4579 2012-06-03 00:28:14Z schulte2005 $ 051 */ 052public class ResourceFileProcessorTest extends JomcToolTest 053{ 054 055 /** Creates a new {@code ResourceFileProcessorTest} instance. */ 056 public ResourceFileProcessorTest() 057 { 058 super(); 059 } 060 061 /** {@inheritDoc} */ 062 @Override 063 public ResourceFileProcessor getJomcTool() 064 { 065 return (ResourceFileProcessor) super.getJomcTool(); 066 } 067 068 /** {@inheritDoc} */ 069 @Override 070 protected ResourceFileProcessor newJomcTool() 071 { 072 return new ResourceFileProcessor(); 073 } 074 075 @Test 076 public final void testResourceFileProcessorNullPointerException() throws Exception 077 { 078 try 079 { 080 this.getJomcTool().getResourceBundleResources( (Specification) null ); 081 fail( "Expected NullPointerException not thrown." ); 082 } 083 catch ( final NullPointerException e ) 084 { 085 assertNullPointerException( e ); 086 } 087 088 try 089 { 090 this.getJomcTool().getResourceBundleResources( (Implementation) null ); 091 fail( "Expected NullPointerException not thrown." ); 092 } 093 catch ( final NullPointerException e ) 094 { 095 assertNullPointerException( e ); 096 } 097 098 try 099 { 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}