1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package org.jomc.tools.test;
32
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.OutputStream;
39 import org.apache.commons.io.IOUtils;
40 import org.jomc.model.Implementation;
41 import org.jomc.model.Module;
42 import org.jomc.model.Specification;
43 import org.jomc.tools.SourceFileProcessor;
44 import org.jomc.util.SectionEditor;
45 import org.junit.Test;
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertFalse;
48 import static org.junit.Assert.assertNotNull;
49 import static org.junit.Assert.assertTrue;
50 import static org.junit.Assert.fail;
51
52
53
54
55
56
57
58 public class SourceFileProcessorTest extends JomcToolTest
59 {
60
61
62 private static final String ABSOLUTE_RESOURCE_NAME_PREFIX = "/org/jomc/tools/test/";
63
64
65 public SourceFileProcessorTest()
66 {
67 super();
68 }
69
70
71 @Override
72 public SourceFileProcessor getJomcTool()
73 {
74 return (SourceFileProcessor) super.getJomcTool();
75 }
76
77
78 @Override
79 protected SourceFileProcessor newJomcTool()
80 {
81 return new SourceFileProcessor();
82 }
83
84 @Test
85 @SuppressWarnings( "deprecation" )
86 public final void testSourceFileProcessorNullPointerException() throws Exception
87 {
88 try
89 {
90 this.getJomcTool().getSourceFileType( (Specification) 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().getSourceFileType( (Implementation) 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().getSourceFilesType( (Specification) null );
111 fail( "Expected NullPointerException not thrown." );
112 }
113 catch ( final NullPointerException e )
114 {
115 assertNullPointerException( e );
116 }
117
118 try
119 {
120 this.getJomcTool().getSourceFilesType( (Implementation) 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().getSourceFileEditor( (Specification) null );
131 fail( "Expected NullPointerException not thrown." );
132 }
133 catch ( final NullPointerException e )
134 {
135 assertNullPointerException( e );
136 }
137
138 try
139 {
140 this.getJomcTool().getSourceFileEditor( (Implementation) 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().manageSourceFiles( null );
151 fail( "Expected NullPointerException not thrown." );
152 }
153 catch ( final NullPointerException e )
154 {
155 assertNullPointerException( e );
156 }
157
158 try
159 {
160 this.getJomcTool().manageSourceFiles( (Implementation) null, new File( "/" ) );
161 fail( "Expected NullPointerException not thrown." );
162 }
163 catch ( final NullPointerException e )
164 {
165 assertNullPointerException( e );
166 }
167
168 try
169 {
170 this.getJomcTool().manageSourceFiles( new Implementation(), null );
171 fail( "Expected NullPointerException not thrown." );
172 }
173 catch ( final NullPointerException e )
174 {
175 assertNullPointerException( e );
176 }
177
178 try
179 {
180 this.getJomcTool().manageSourceFiles( (Module) null, new File( "/" ) );
181 fail( "Expected NullPointerException not thrown." );
182 }
183 catch ( final NullPointerException e )
184 {
185 assertNullPointerException( e );
186 }
187
188
189 try
190 {
191 this.getJomcTool().manageSourceFiles( new Module(), null );
192 fail( "Expected NullPointerException not thrown." );
193 }
194 catch ( final NullPointerException e )
195 {
196 assertNullPointerException( e );
197 }
198
199 try
200 {
201 this.getJomcTool().manageSourceFiles( (Specification) null, new File( "/" ) );
202 fail( "Expected NullPointerException not thrown." );
203 }
204 catch ( final NullPointerException e )
205 {
206 assertNullPointerException( e );
207 }
208
209 try
210 {
211 this.getJomcTool().manageSourceFiles( new Specification(), null );
212 fail( "Expected NullPointerException not thrown." );
213 }
214 catch ( final NullPointerException e )
215 {
216 assertNullPointerException( e );
217 }
218 }
219
220 @Test
221 public final void testSourceFileProcessorNotNull() throws Exception
222 {
223 assertNotNull( this.getJomcTool().getSourceFilesType(
224 this.getJomcTool().getModules().getImplementation( "Implementation" ) ) );
225
226 assertNotNull( this.getJomcTool().getSourceFilesType(
227 this.getJomcTool().getModules().getSpecification( "Specification" ) ) );
228
229 assertNotNull( this.getJomcTool().getSourceFileEditor() );
230 }
231
232 @Test
233 public final void testManageSources() throws Exception
234 {
235 this.getJomcTool().setInputEncoding( this.getResourceEncoding() );
236 this.getJomcTool().setOutputEncoding( this.getResourceEncoding() );
237
238 final File nonExistingDirectory = this.getNextOutputDirectory();
239
240 try
241 {
242 this.getJomcTool().manageSourceFiles( nonExistingDirectory );
243 fail( "Expected IOException not thrown." );
244 }
245 catch ( final IOException e )
246 {
247 assertNotNull( e.getMessage() );
248 System.out.println( e );
249 }
250
251 try
252 {
253 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getModule( "Module" ),
254 nonExistingDirectory );
255
256 fail( "Expected IOException not thrown." );
257 }
258 catch ( final IOException e )
259 {
260 assertNotNull( e.getMessage() );
261 System.out.println( e );
262 }
263
264 try
265 {
266 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
267 nonExistingDirectory );
268
269 fail( "Expected IOException not thrown." );
270 }
271 catch ( final IOException e )
272 {
273 assertNotNull( e.getMessage() );
274 System.out.println( e );
275 }
276
277 try
278 {
279 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
280 nonExistingDirectory );
281
282 fail( "Expected IOException not thrown." );
283 }
284 catch ( final IOException e )
285 {
286 assertNotNull( e.getMessage() );
287 System.out.println( e );
288 }
289
290 File sourcesDirectory = this.getNextOutputDirectory();
291 assertTrue( sourcesDirectory.mkdirs() );
292 this.getJomcTool().manageSourceFiles( sourcesDirectory );
293 this.getJomcTool().manageSourceFiles( sourcesDirectory );
294
295 sourcesDirectory = this.getNextOutputDirectory();
296 assertTrue( sourcesDirectory.mkdirs() );
297 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getModule( "Module" ),
298 sourcesDirectory );
299
300 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getModule( "Module" ),
301 sourcesDirectory );
302
303 final File implementationDirectory = this.getNextOutputDirectory();
304 final File implementationSourceFile = new File( implementationDirectory, "Implementation.java" );
305 assertTrue( implementationDirectory.mkdirs() );
306 long implementationSourceFileLength;
307
308 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
309 implementationDirectory );
310
311 implementationSourceFileLength = implementationSourceFile.length();
312 assertTrue( implementationSourceFile.exists() );
313
314 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
315 implementationDirectory );
316
317 assertTrue( implementationSourceFile.exists() );
318 assertEquals( implementationSourceFileLength, implementationSourceFile.length() );
319
320 this.getJomcTool().getTemplateParameters().put( "with-javadoc", Boolean.FALSE );
321
322 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
323 implementationDirectory );
324
325 assertTrue( implementationSourceFile.exists() );
326 assertTrue( implementationSourceFile.length() < implementationSourceFileLength );
327
328 this.getJomcTool().getTemplateParameters().clear();
329
330 this.getJomcTool().manageSourceFiles(
331 this.getJomcTool().getModules().getImplementation( "ImplementationWithSourceFilesModel" ),
332 implementationDirectory );
333
334 this.getJomcTool().manageSourceFiles(
335 this.getJomcTool().getModules().getImplementation( "ImplementationWithSourceFilesModel" ),
336 implementationDirectory );
337
338 final File specificationDirectory = this.getNextOutputDirectory();
339 assertTrue( specificationDirectory.mkdirs() );
340 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
341 specificationDirectory );
342
343 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
344 specificationDirectory );
345
346 this.getJomcTool().manageSourceFiles(
347 this.getJomcTool().getModules().getSpecification( "SpecificationWithSourceFilesModel" ),
348 specificationDirectory );
349
350 this.getJomcTool().manageSourceFiles(
351 this.getJomcTool().getModules().getSpecification( "SpecificationWithSourceFilesModel" ),
352 specificationDirectory );
353
354 this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "IllegalImplementationSource.java.txt",
355 new File( implementationDirectory, "Implementation.java" ) );
356
357 this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "IllegalSpecificationSource.java.txt",
358 new File( specificationDirectory, "Specification.java" ) );
359
360 try
361 {
362 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
363 implementationDirectory );
364
365 fail( "Expected IOException not thrown." );
366 }
367 catch ( final IOException e )
368 {
369 assertNotNull( e.getMessage() );
370 System.out.println( e.toString() );
371 }
372
373 try
374 {
375 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
376 specificationDirectory );
377
378 fail( "Expected IOException not thrown." );
379 }
380 catch ( final IOException e )
381 {
382 assertNotNull( e.getMessage() );
383 System.out.println( e.toString() );
384 }
385
386 this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "EmptyImplementationSource.java.txt",
387 new File( implementationDirectory, "Implementation.java" ) );
388
389 this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "EmptySpecificationSource.java.txt",
390 new File( specificationDirectory, "Specification.java" ) );
391
392 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
393 implementationDirectory );
394
395 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
396 specificationDirectory );
397
398 this.getJomcTool().setTemplateProfile( "DOES_NOT_EXIST" );
399
400 sourcesDirectory = this.getNextOutputDirectory();
401 assertTrue( sourcesDirectory.mkdirs() );
402 this.getJomcTool().manageSourceFiles( sourcesDirectory );
403
404 sourcesDirectory = this.getNextOutputDirectory();
405 assertTrue( sourcesDirectory.mkdirs() );
406 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getModule( "Module" ), sourcesDirectory );
407
408 sourcesDirectory = this.getNextOutputDirectory();
409 assertTrue( sourcesDirectory.mkdirs() );
410 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
411 sourcesDirectory );
412
413 sourcesDirectory = this.getNextOutputDirectory();
414 assertTrue( sourcesDirectory.mkdirs() );
415 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
416 sourcesDirectory );
417
418 this.getJomcTool().setInputEncoding( null );
419 this.getJomcTool().setOutputEncoding( null );
420 }
421
422 @Test
423 public final void testMandatorySections() throws Exception
424 {
425 final SectionEditor editor = new SectionEditor();
426 final File specificationDirectory = this.getNextOutputDirectory();
427 final File implementationDirectory = this.getNextOutputDirectory();
428
429 assertTrue( specificationDirectory.mkdirs() );
430 assertTrue( implementationDirectory.mkdirs() );
431
432 this.getJomcTool().setInputEncoding( this.getResourceEncoding() );
433 this.getJomcTool().setOutputEncoding( this.getResourceEncoding() );
434
435 File f = new File( implementationDirectory, "Implementation.java" );
436 this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutAnnotationsSection.java.txt", f );
437 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
438 implementationDirectory );
439
440 String edited = this.toString( f );
441 editor.edit( edited );
442 assertTrue( editor.isSectionPresent( "Annotations" ) );
443
444 f = new File( implementationDirectory, "Implementation.java" );
445 this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutDependenciesSection.java.txt", f );
446 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
447 implementationDirectory );
448
449 edited = this.toString( f );
450 editor.edit( edited );
451 assertTrue( editor.isSectionPresent( "Dependencies" ) );
452
453 f = new File( implementationDirectory, "Implementation.java" );
454 this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutMessagesSection.java.txt", f );
455 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
456 implementationDirectory );
457
458 edited = this.toString( f );
459 editor.edit( edited );
460 assertTrue( editor.isSectionPresent( "Messages" ) );
461
462 f = new File( implementationDirectory, "Implementation.java" );
463 this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutPropertiesSection.java.txt", f );
464 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
465 implementationDirectory );
466
467 edited = this.toString( f );
468 editor.edit( edited );
469 assertTrue( editor.isSectionPresent( "Properties" ) );
470
471 f = new File( implementationDirectory, "ImplementationOfSpecification.java" );
472 this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX
473 + "ImplementationOfSpecificationWithoutConstructorsSection.java.txt", f );
474
475 this.getJomcTool().manageSourceFiles(
476 this.getJomcTool().getModules().getImplementation( "ImplementationOfSpecification" ),
477 implementationDirectory );
478
479 edited = this.toString( f );
480 editor.edit( edited );
481 assertTrue( editor.isSectionPresent( "Constructors" ) );
482
483 f = new File( specificationDirectory, "Specification.java" );
484 this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "SpecificationWithoutAnnotationsSection.java.txt", f );
485 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
486 specificationDirectory );
487
488 edited = this.toString( f );
489 editor.edit( edited );
490 assertTrue( editor.isSectionPresent( "Annotations" ) );
491
492 this.getJomcTool().setInputEncoding( null );
493 this.getJomcTool().setOutputEncoding( null );
494 }
495
496 @Test
497 public final void testOptionalSections() throws Exception
498 {
499 final SectionEditor editor = new SectionEditor();
500 final File implementationDirectory = this.getNextOutputDirectory();
501 final File specificationDirectory = this.getNextOutputDirectory();
502
503 assertTrue( specificationDirectory.mkdirs() );
504 assertTrue( implementationDirectory.mkdirs() );
505
506 this.getJomcTool().setInputEncoding( this.getResourceEncoding() );
507 this.getJomcTool().setOutputEncoding( this.getResourceEncoding() );
508
509 File f = new File( implementationDirectory, "Implementation.java" );
510 this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutConstructorsSection.java.txt", f );
511 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
512 implementationDirectory );
513
514 String edited = this.toString( f );
515 editor.edit( edited );
516 assertFalse( editor.isSectionPresent( "Constructors" ) );
517 this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutDefaultConstructorSection.java.txt", f );
518 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
519 implementationDirectory );
520
521 edited = this.toString( f );
522 editor.edit( edited );
523 assertTrue( editor.isSectionPresent( "Constructors" ) );
524 assertTrue( editor.isSectionPresent( "Default Constructor" ) );
525 this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutDocumentationSection.java.txt", f );
526 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
527 implementationDirectory );
528
529 edited = this.toString( f );
530 editor.edit( edited );
531 assertFalse( editor.isSectionPresent( "Documentation" ) );
532 this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "ImplementationWithoutLicenseSection.java.txt", f );
533 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getImplementation( "Implementation" ),
534 implementationDirectory );
535
536 edited = this.toString( f );
537 editor.edit( edited );
538 assertFalse( editor.isSectionPresent( "License Header" ) );
539
540 f = new File( specificationDirectory, "Specification.java" );
541 this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "SpecificationWithoutDocumentationSection.java.txt", f );
542 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
543 specificationDirectory );
544
545 edited = this.toString( f );
546 editor.edit( edited );
547 assertFalse( editor.isSectionPresent( "Documentation" ) );
548 this.copyResource( ABSOLUTE_RESOURCE_NAME_PREFIX + "SpecificationWithoutLicenseSection.java.txt", f );
549 this.getJomcTool().manageSourceFiles( this.getJomcTool().getModules().getSpecification( "Specification" ),
550 specificationDirectory );
551
552 edited = this.toString( f );
553 editor.edit( edited );
554 assertFalse( editor.isSectionPresent( "License Header" ) );
555
556 this.getJomcTool().setInputEncoding( null );
557 this.getJomcTool().setOutputEncoding( null );
558 }
559
560 @Test
561 public final void testCopyConstructor() throws Exception
562 {
563 try
564 {
565 new SourceFileProcessor( null );
566 fail( "Expected NullPointerException not thrown." );
567 }
568 catch ( final NullPointerException e )
569 {
570 assertNotNull( e.getMessage() );
571 System.out.println( e.toString() );
572 }
573
574 new SourceFileProcessor( this.getJomcTool() );
575 }
576
577 private void copyResource( final String resourceName, final File file ) throws IOException
578 {
579 assertTrue( resourceName.startsWith( "/" ) );
580
581 InputStream in = null;
582 boolean suppressExceptionOnClose = true;
583
584 try
585 {
586 in = this.getClass().getResourceAsStream( resourceName );
587 assertNotNull( "Resource '" + resourceName + "' not found.", in );
588 OutputStream out = null;
589
590 try
591 {
592 out = new FileOutputStream( file );
593 IOUtils.copy( in, out );
594 suppressExceptionOnClose = false;
595 }
596 finally
597 {
598 try
599 {
600 if ( out != null )
601 {
602 out.close();
603 }
604 }
605 catch ( final IOException e )
606 {
607 if ( !suppressExceptionOnClose )
608 {
609 throw e;
610 }
611 }
612 }
613 }
614 finally
615 {
616 try
617 {
618 if ( in != null )
619 {
620 in.close();
621 }
622 }
623 catch ( final IOException e )
624 {
625 if ( !suppressExceptionOnClose )
626 {
627 throw e;
628 }
629 }
630 }
631 }
632
633 private String toString( final File f ) throws IOException
634 {
635 InputStream in = null;
636 boolean suppressExceptionOnClose = true;
637
638 try
639 {
640 in = new FileInputStream( f );
641 final String str = IOUtils.toString( in, this.getResourceEncoding() );
642 suppressExceptionOnClose = false;
643 return str;
644 }
645 finally
646 {
647 try
648 {
649 if ( in != null )
650 {
651 in.close();
652 }
653 }
654 catch ( final IOException e )
655 {
656 if ( !suppressExceptionOnClose )
657 {
658 throw e;
659 }
660 }
661 }
662 }
663
664 }