1
2
3
4
5
6
7
8 package org.codehaus.metaclass.io;
9
10 import java.io.ByteArrayInputStream;
11 import java.io.ByteArrayOutputStream;
12 import java.io.File;
13 import java.io.FileInputStream;
14 import java.io.FileNotFoundException;
15 import java.io.FileOutputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import junit.framework.TestCase;
19 import org.codehaus.metaclass.introspector.MetaClassException;
20 import org.codehaus.metaclass.model.Attribute;
21 import org.codehaus.metaclass.model.ClassDescriptor;
22 import org.codehaus.metaclass.model.FieldDescriptor;
23 import org.codehaus.metaclass.model.MethodDescriptor;
24
25 /***
26 * @author Peter Donald
27 * @version $Revision: 1.2 $ $Date: 2004/01/16 01:40:57 $
28 */
29 public class MetaClassIOASMTestCase
30 extends TestCase
31 {
32 public void testGetResourceName()
33 throws Exception
34 {
35 final MetaClassIOASM io = new MetaClassIOASM();
36 final String name = io.getResourceName( "com.biz.Foo" );
37 assertEquals( "name", "com" +
38 File.separatorChar +
39 "biz" +
40 File.separatorChar +
41 "Foo.class", name );
42 }
43
44 public void testSerializeThenDeserializeClass()
45 throws Exception
46 {
47 final ClassDescriptor descriptor =
48 new ClassDescriptor( "com.biz.Foo",
49 Attribute.EMPTY_SET,
50 Attribute.EMPTY_SET,
51 FieldDescriptor.EMPTY_SET,
52 MethodDescriptor.EMPTY_SET );
53 final InputStream original =
54 TestClass1.class.getResourceAsStream( "TestClass1.class" );
55 assertNotNull( "original", original );
56 final MetaClassIOASM io = new MetaClassIOASM();
57 final ByteArrayOutputStream output = new ByteArrayOutputStream();
58 io.serializeClass( original, output, descriptor );
59
60 final ByteArrayInputStream input =
61 new ByteArrayInputStream( output.toByteArray() );
62 final ClassDescriptor newDescriptor = io.deserializeClass( input );
63 assertEquals( "name", "com.biz.Foo", newDescriptor.getName() );
64 assertEquals( "attributes.length",
65 0,
66 newDescriptor.getAttributes().length );
67 assertEquals( "methods.length",
68 0,
69 newDescriptor.getMethods().length );
70 assertEquals( "fields.length",
71 0,
72 newDescriptor.getFields().length );
73 }
74
75 public void testDeserializeClassWithNoMetaData()
76 throws Exception
77 {
78 final InputStream input =
79 TestClass1.class.getResourceAsStream( "TestClass1.class" );
80 assertNotNull( "input", input );
81
82 final MetaClassIOASM io = new MetaClassIOASM();
83 try
84 {
85 io.deserializeClass( input );
86 }
87 catch( final MetaClassException mce )
88 {
89 return;
90 }
91 fail( "Expected to fail deserializing a class with no descriptor." );
92 }
93
94 public void testDeserializeClassWithIOException()
95 throws Exception
96 {
97 final InputStream original =
98 TestClass1.class.getResourceAsStream( "TestClass1.class" );
99 assertNotNull( "original", original );
100 final MockMetaClassIOASM io = new MockMetaClassIOASM();
101 try
102 {
103 io.deserializeClass( original );
104 }
105 catch( final MetaClassException mce )
106 {
107 return;
108 }
109 fail( "Expected to fail due to ioe exception reading descriptor" );
110 }
111
112 public void testSerializeDescriptorWithMissingFile()
113 throws Exception
114 {
115 final ClassDescriptor descriptor =
116 new ClassDescriptor( "com.biz.Foo",
117 Attribute.EMPTY_SET,
118 Attribute.EMPTY_SET,
119 FieldDescriptor.EMPTY_SET,
120 MethodDescriptor.EMPTY_SET );
121 final MockMetaClassIOASM io = new MockMetaClassIOASM();
122 final File input = new File( "NoExist.txt" );
123 final File output = new File( "IAlsoNoExist.txt" );
124 try
125 {
126 io.serializeDescriptor( input, output, descriptor );
127 }
128 catch( final FileNotFoundException fnfe )
129 {
130 return;
131 }
132 fail( "Expected to fail with file not found exception" );
133 }
134
135 public void testSerialize()
136 throws Exception
137 {
138 final File dir = generateDirectory();
139 final ClassDescriptor descriptor =
140 new ClassDescriptor( "com.biz.Foo",
141 Attribute.EMPTY_SET,
142 Attribute.EMPTY_SET,
143 FieldDescriptor.EMPTY_SET,
144 MethodDescriptor.EMPTY_SET );
145 final MetaClassIOASM io = new MetaClassIOASM();
146 final File target = new File( dir, "com/biz/Foo.class" );
147 target.getParentFile().mkdirs();
148
149 final InputStream input =
150 TestClass1.class.getResourceAsStream( "TestClass1.class" );
151 assertNotNull( "input", input );
152
153 final FileOutputStream fout = new FileOutputStream( target );
154
155 int ch = input.read();
156 while( -1 != ch )
157 {
158 fout.write( ch );
159 ch = input.read();
160 }
161 input.close();
162 fout.close();
163
164 io.writeDescriptor( dir, descriptor );
165
166 final FileInputStream fin = new FileInputStream( target );
167 final ClassDescriptor newDescriptor = io.deserializeClass( fin );
168 assertEquals( "name", "com.biz.Foo", newDescriptor.getName() );
169 assertEquals( "attributes.length",
170 0,
171 newDescriptor.getAttributes().length );
172 assertEquals( "methods.length",
173 0,
174 newDescriptor.getMethods().length );
175 assertEquals( "fields.length",
176 0,
177 newDescriptor.getFields().length );
178 }
179
180 public void testSerializeWithError()
181 throws Exception
182 {
183 final File dir = generateDirectory();
184 final ClassDescriptor descriptor =
185 new ClassDescriptor( "com.biz.Foo",
186 Attribute.EMPTY_SET,
187 Attribute.EMPTY_SET,
188 FieldDescriptor.EMPTY_SET,
189 MethodDescriptor.EMPTY_SET );
190 final MetaClassIOASM io = new MockMetaClassIOASM( new IOException() );
191 final File target = new File( dir, "com/biz/Foo.class" );
192 final File backup = new File( dir, "com/biz/Foo.class.bak" );
193 target.getParentFile().mkdirs();
194
195 final InputStream input =
196 TestClass1.class.getResourceAsStream( "TestClass1.class" );
197 assertNotNull( "input", input );
198
199 final FileOutputStream fout = new FileOutputStream( target );
200
201 int ch = input.read();
202 while( -1 != ch )
203 {
204 fout.write( ch );
205 ch = input.read();
206 }
207 input.close();
208 fout.close();
209
210 final long size = target.length();
211
212 try
213 {
214 io.writeDescriptor( dir, descriptor );
215 }
216 catch( Exception e )
217 {
218 assertEquals( "target.length()", size, target.length() );
219 return;
220 }
221 finally
222 {
223 assertTrue( "!backup.exists()", !backup.exists() );
224 }
225 }
226
227 public void testSerializeButNoClassFilePresent()
228 throws Exception
229 {
230 final File dir = generateDirectory();
231 final ClassDescriptor descriptor =
232 new ClassDescriptor( "com.biz.Foo",
233 Attribute.EMPTY_SET,
234 Attribute.EMPTY_SET,
235 FieldDescriptor.EMPTY_SET,
236 MethodDescriptor.EMPTY_SET );
237 final MetaClassIOASM io = new MetaClassIOASM();
238 try
239 {
240 io.writeDescriptor( dir, descriptor );
241 }
242 catch( Exception e )
243 {
244 return;
245 }
246 fail( "Expected to fail as no such .class file" );
247 }
248
249 private static final File generateDirectory()
250 throws IOException
251 {
252 final File baseDirectory = getBaseDirectory();
253 final File dir =
254 File.createTempFile( "mgtest", ".tmp", baseDirectory )
255 .getCanonicalFile();
256 dir.delete();
257 dir.mkdirs();
258 assertTrue( "dir.exists()", dir.exists() );
259 return dir;
260 }
261
262 private static final File getBaseDirectory()
263 {
264 final String tempDir = System.getProperty( "java.io.tmpdir" );
265 final String baseDir = System.getProperty( "basedir", tempDir );
266
267 final File base = new File( baseDir ).getAbsoluteFile();
268 final String pathname =
269 base + File.separator + "target" + File.separator + "test-data";
270 final File dir = new File( pathname );
271 dir.mkdirs();
272 return dir;
273 }
274 }