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