View Javadoc

1   package org.codehaus.modello.plugin.java.javasource;
2   
3   import java.io.File;
4   
5   import org.codehaus.modello.AbstractModelloGeneratorTest;
6   
7   public class JavaSourceTest
8       extends AbstractModelloGeneratorTest
9   {
10      public JavaSourceTest()
11      {
12          super( "javasource" );
13      }
14  
15      public void testJavaSource()
16      {
17          checkJClass();
18          checkJCompUnit();
19          checkJInterface();
20      }
21  
22      private void checkJClass()
23      {
24          JClass testClass = new JClass( "org.acme.JClassTest" );
25  
26          testClass.addSourceCode( "// source code" );
27          testClass.addSourceCode( "// source code 2" );
28  
29          testClass.addImport( "java.util.Vector" );
30          testClass.appendAnnotation( "@SuppressWarnings( \"all\" )" );
31          testClass.appendAnnotation( "@Deprecated" );
32          testClass.addMember( new JField( JType.INT, "x" ) );
33          JClass jcString = new JClass( "String" );
34  
35          JField field = null;
36          field = new JField( JType.INT, "_z" );
37          field.appendAnnotation( "@Deprecated" );
38          field.getModifiers().setStatic( true );
39          testClass.addField( field );
40  
41          testClass.getStaticInitializationCode().add( "_z = 75;" );
42  
43          field = new JField( jcString, "myString" );
44          field.getModifiers().makePrivate();
45          testClass.addMember( field );
46  
47          // generics test
48          JType type = new JCollectionType( "java.util.List", jcString, true );
49          field = new JField( type, "generics" );
50          testClass.addMember( field );
51  
52          type = new JCollectionType( "java.util.List", jcString, false );
53          field = new JField( type, "noGenerics" );
54          testClass.addMember( field );
55  
56          //-- create constructor
57          JConstructor cons = testClass.createConstructor();
58          cons.appendAnnotation( "@Deprecated" );
59          cons.getSourceCode().add( "this.x = 6;" );
60  
61          JMethod jMethod = new JMethod( "getX", JType.INT, null );
62          jMethod.appendAnnotation( "@Deprecated" );
63          jMethod.setSourceCode( "return this.x;" );
64          testClass.addMethod( jMethod );
65  
66          jMethod = new JMethod( "setX", null, null );
67          JParameter param = new JParameter( JType.INT, "x" );
68          jMethod.addParameter( param );
69          jMethod.setSourceCode( "this.x = x;" );
70          testClass.addMethod( jMethod );
71  
72          jMethod = new JMethod( "checkParameterAnotation", JType.INT, null );
73          param = new JParameter( JType.LONG, "param" );
74          param.appendAnnotation( "@Test" );
75          param.appendAnnotation( "@Test2" );
76          jMethod.addParameter( param );
77          jMethod.setSourceCode( "return param;" );
78          testClass.addMethod( jMethod );
79  
80          //-- create inner-class
81          JClass innerClass = testClass.createInnerClass( "Foo" );
82          innerClass.appendAnnotation( "@Deprecated" );
83          innerClass.addImport( "java.util.Hashtable" );
84          innerClass.addMember( new JField( JType.INT, "_type" ) );
85  
86          field = new JField( jcString, "_name" );
87          field.getModifiers().makePrivate();
88          innerClass.addMember( field );
89  
90          testClass.createInnerClass( "Bar" );
91  
92          //-- create constructor
93          cons = innerClass.createConstructor();
94          cons.getSourceCode().add( "_name = \"foo\";" );
95  
96          jMethod = new JMethod( "getName", jcString, null );
97          jMethod.setSourceCode( "return _name;" );
98          innerClass.addMethod( jMethod );
99  
100         testClass.print( getOutputDirectory().toString(), null );
101 
102         assertTrue( new File( getOutputDirectory(), "org/acme/JClassTest.java" ).exists() );
103     }
104 
105     private void checkJCompUnit()
106     {
107         JCompUnit unit = new JCompUnit( "com.acme", "JCompUnitTest.java" );
108 
109         JClass testClass = new JClass( "Test" );
110 
111         testClass.addImport( "java.util.Vector" );
112         testClass.addMember( new JField( JType.INT, "x" ) );
113 
114         JField field = null;
115         field = new JField( JType.INT, "_z" );
116         field.getModifiers().setStatic( true );
117         testClass.addField( field );
118 
119         testClass.getStaticInitializationCode().add( "_z = 75;" );
120 
121         JClass jcString = new JClass( "String" );
122         field = new JField( jcString, "myString" );
123         field.getModifiers().makePrivate();
124         testClass.addMember( field );
125 
126         // -- create constructor
127         JConstructor cons = testClass.createConstructor();
128         testClass.addConstructor( cons );
129         cons.getSourceCode().add( "this.x = 6;" );
130 
131         JMethod jMethod = new JMethod( "getX", JType.INT, null );
132         jMethod.setSourceCode( "return this.x;" );
133         testClass.addMethod( jMethod );
134 
135         unit.addClass( testClass );
136 
137         JClass fooClass = new JClass( "Foo" );
138         unit.addClass( fooClass );
139 
140         unit.print( getOutputDirectory().toString(), null );
141 
142         assertTrue( new File( getOutputDirectory(), "com/acme/JCompUnitTest.java" ).exists() );
143     }
144 
145     private void checkJInterface()
146     {
147         JInterface jInterface = new JInterface( "InterfaceTest" );
148 
149         // -- add an import
150         jInterface.addImport( "java.util.Vector" );
151         JClass jString = new JClass( "String" );
152 
153         // -- add an interface
154         jInterface.addInterface( "java.io.Serializable" );
155 
156         // -- add a static field
157         JField jField = new JField( new JClass( "java.lang.String" ), "TEST" );
158         jField.setInitString( "\"Test\"" );
159         jField.getModifiers().setStatic( true );
160         jField.getModifiers().makePublic();
161         jInterface.addField( jField );
162 
163         // -- add a method signature
164         JMethodSignature jMethodSig = new JMethodSignature( "getName", jString );
165         jInterface.addMethod( jMethodSig );
166         jInterface.print( getOutputDirectory().toString(), null );
167 
168         assertTrue( new File( getOutputDirectory(), "InterfaceTest.java" ).exists() );
169     }
170 }