1
2
3
4
5
6
7
8 package org.codehaus.metaclass.tools.tasks;
9
10 import java.io.File;
11 import java.io.FileInputStream;
12 import java.io.FileOutputStream;
13 import java.io.IOException;
14 import junit.framework.TestCase;
15 import org.apache.tools.ant.DefaultLogger;
16 import org.apache.tools.ant.Project;
17 import org.apache.tools.ant.ProjectHelper;
18 import org.codehaus.metaclass.io.MetaClassIO;
19 import org.codehaus.metaclass.io.MetaClassIOBinary;
20 import org.codehaus.metaclass.model.ClassDescriptor;
21
22 /***
23 * @author Peter Donald
24 * @version $Revision: 1.3 $ $Date: 2004/01/16 02:11:22 $
25 */
26 public class AntIntegrationTestCase
27 extends TestCase
28 {
29 public void testSimpleRun()
30 throws Exception
31 {
32 final String source =
33 "package com.biz;\n" +
34 "\n" +
35 "/**\n" +
36 " * @dna.component\n" +
37 " */\n" +
38 "public class MyAntIntegrationClass\n" +
39 "{\n" +
40 "}\n";
41
42 final File sourceDirectory = generateDirectory();
43 final File destDirectory = generateDirectory();
44
45 final String sourceFilename = sourceDirectory +
46 File.separator +
47 "com" +
48 File.separator +
49 "biz" +
50 File.separator +
51 "MyAntIntegrationClass.java";
52 final File sourceFile = new File( sourceFilename );
53 sourceFile.getParentFile().mkdirs();
54 final FileOutputStream output = new FileOutputStream( sourceFile );
55 output.write( source.getBytes() );
56 output.close();
57 final String buildSource =
58 "<project default=\"main\" basedir=\".\">\n" +
59 " <target name=\"main\">\n" +
60 " <taskdef name=\"metaclass_register\"\n" +
61 " classname=\"org.codehaus.metaclass.tools.tasks.RegisterMetaClassLibTask\"/>\n" +
62 " <metaclass_register/>\n" +
63 "\n" +
64 " <metaclass_interceptorSet id=\"metaclass.interceptors\"/>\n" +
65 "\n" +
66 " <metaclass_interceptorSet id=\"test.interceptors\">\n" +
67 " <interceptor \n" +
68 " name=\"org.codehaus.metaclass.tools.qdox.DefaultQDoxAttributeInterceptor\"/>\n" +
69 " </metaclass_interceptorSet>\n" +
70 "\n" +
71 " <metaclass_addToInterceptorSet\n" +
72 " dest=\"metaclass.interceptors\"\n" +
73 " source=\"test.interceptors\"/>\n" +
74 "\n" +
75 " <metaclass_generate format=\"binary\" destDir=\"" +
76 destDirectory +
77 "\">\n" +
78 " <interceptorSet refid=\"metaclass.interceptors\"/>\n" +
79 " <fileset dir=\"" +
80 sourceDirectory +
81 "\">\n" +
82 " <include name=\"**/*.java\"/>\n" +
83 " </fileset>\n" +
84 " </metaclass_generate>\n" +
85 " </target>\n" +
86 "</project>\n";
87
88 final String buildSourceFilename =
89 sourceDirectory + File.separator + "build.xml";
90 final File buildSourceFile = new File( buildSourceFilename );
91 buildSourceFile.getParentFile().mkdirs();
92 final FileOutputStream buildOutput =
93 new FileOutputStream( buildSourceFile );
94 buildOutput.write( buildSource.getBytes() );
95 buildOutput.close();
96
97 final Project project = createProject( buildSourceFile );
98 project.executeTarget( "main" );
99
100 final String destFilename =
101 destDirectory +
102 File.separator +
103 "com" +
104 File.separator +
105 "biz" +
106 File.separator +
107 "MyAntIntegrationClass" +
108 MetaClassIOBinary.EXTENSION;
109 final File destFile = new File( destFilename );
110
111 assertTrue( "destFile.exists()", destFile.exists() );
112 final MetaClassIO io = MetaClassIOBinary.IO;
113 final FileInputStream input = new FileInputStream( destFile );
114 final ClassDescriptor descriptor = io.deserializeClass( input );
115 assertEquals( "descriptor.name",
116 "com.biz.MyAntIntegrationClass",
117 descriptor.getName() );
118 assertEquals( "descriptor.attributes.length",
119 1,
120 descriptor.getAttributes().length );
121 assertEquals( "descriptor.attributes[0].name",
122 "dna.component",
123 descriptor.getAttributes()[ 0 ].getName() );
124 assertEquals( "descriptor.methods.length",
125 0,
126 descriptor.getMethods().length );
127 assertEquals( "descriptor.fields.length",
128 0,
129 descriptor.getFields().length );
130 }
131
132 protected Project createProject( final File file )
133 {
134 final Project project = new Project();
135 project.init();
136 project.setUserProperty( "ant.file", file.getAbsolutePath() );
137 final DefaultLogger logger = new DefaultLogger();
138 logger.setOutputPrintStream( System.out );
139 logger.setErrorPrintStream( System.out );
140 logger.setMessageOutputLevel( Project.MSG_INFO );
141 project.addBuildListener( logger );
142 ProjectHelper.configureProject( project, file );
143 return project;
144 }
145
146 private static final File generateDirectory()
147 throws IOException
148 {
149 final File baseDirectory = getBaseDirectory();
150 final File dir =
151 File.createTempFile( "mgtest", ".tmp", baseDirectory )
152 .getCanonicalFile();
153 dir.delete();
154 dir.mkdirs();
155 assertTrue( "dir.exists()", dir.exists() );
156 return dir;
157 }
158
159 private static final File getBaseDirectory()
160 {
161 final String tempDir = System.getProperty( "java.io.tmpdir" );
162 final String baseDir = System.getProperty( "basedir", tempDir );
163
164 final File base = new File( baseDir ).getAbsoluteFile();
165 final String pathname =
166 base + File.separator + "target" + File.separator + "test-data";
167 final File dir = new File( pathname );
168 dir.mkdirs();
169 return dir;
170 }
171 }