KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > openwire > DataFileGenerator


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.openwire;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.DataOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import junit.framework.Assert;
32
33 abstract public class DataFileGenerator extends Assert {
34     
35     static final File JavaDoc moduleBaseDir;
36     static final File JavaDoc controlDir;
37     static final File JavaDoc classFileDir;
38     
39     static {
40         moduleBaseDir = new File JavaDoc(System.getProperty("basedir", "."));
41         controlDir = new File JavaDoc(moduleBaseDir, "src/test/resources/openwire-control");
42         classFileDir = new File JavaDoc(moduleBaseDir, "src/test/java/org/apache/activemq/openwire");
43     }
44     
45     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
46         generateControlFiles();
47     }
48
49     /**
50      * @param srcdir
51      * @return
52      * @throws ClassNotFoundException
53      * @throws InstantiationException
54      * @throws IllegalAccessException
55      */

56     public static ArrayList JavaDoc getAllDataFileGenerators() throws Exception JavaDoc{
57 // System.out.println("Looking for generators in : "+classFileDir);
58
ArrayList JavaDoc l = new ArrayList JavaDoc();
59         File JavaDoc[] files = classFileDir.listFiles();
60         for (int i = 0; files!=null && i < files.length; i++) {
61             File JavaDoc file = files[i];
62             if( file.getName().endsWith("Data.java") ) {
63                 String JavaDoc cn = file.getName();
64                 cn = cn.substring(0, cn.length() - ".java".length());
65                 Class JavaDoc clazz = DataFileGenerator.class.getClassLoader().loadClass("org.apache.activemq.openwire."+cn);
66                 l.add((DataFileGenerator) clazz.newInstance());
67             }
68         }
69         return l;
70     }
71         
72     private static void generateControlFiles() throws Exception JavaDoc {
73         ArrayList JavaDoc generators = getAllDataFileGenerators();
74         for (Iterator JavaDoc iter = generators.iterator(); iter.hasNext();) {
75             DataFileGenerator object = (DataFileGenerator) iter.next();
76             try {
77 // System.out.println("Processing: "+object.getClass());
78
object.generateControlFile();
79             } catch (Exception JavaDoc e) {
80 // System.err.println("Error while processing: "+object.getClass() + ". Reason: " + e);
81
}
82         }
83     }
84
85     public void generateControlFile() throws Exception JavaDoc {
86         controlDir.mkdirs();
87         File JavaDoc dataFile = new File JavaDoc(controlDir, getClass().getName()+".bin");
88         
89         OpenWireFormat wf = new OpenWireFormat();
90         wf.setCacheEnabled(false);
91         wf.setStackTraceEnabled(false);
92         wf.setVersion(1);
93      
94         FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(dataFile);
95         DataOutputStream JavaDoc ds = new DataOutputStream JavaDoc(os);
96         wf.marshal(createObject(), ds);
97         ds.close();
98     }
99
100     public InputStream JavaDoc generateInputStream() throws Exception JavaDoc {
101         OpenWireFormat wf = new OpenWireFormat();
102         wf.setCacheEnabled(false);
103         wf.setStackTraceEnabled(false);
104         wf.setVersion(1);
105      
106         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
107         DataOutputStream JavaDoc ds = new DataOutputStream JavaDoc(os);
108         wf.marshal(createObject(), ds);
109         ds.close();
110         
111         return new ByteArrayInputStream JavaDoc(os.toByteArray());
112     }
113     
114     public static void assertAllControlFileAreEqual() throws Exception JavaDoc {
115         ArrayList JavaDoc generators = getAllDataFileGenerators();
116         for (Iterator JavaDoc iter = generators.iterator(); iter.hasNext();) {
117             DataFileGenerator object = (DataFileGenerator) iter.next();
118 // System.out.println("Processing: "+object.getClass());
119
object.assertControlFileIsEqual();
120         }
121     }
122
123     public void assertControlFileIsEqual() throws Exception JavaDoc {
124         File JavaDoc dataFile = new File JavaDoc(controlDir, getClass().getName()+".bin");
125         FileInputStream JavaDoc is1 = new FileInputStream JavaDoc(dataFile);
126         int pos=0;
127         try {
128             InputStream JavaDoc is2 = generateInputStream();
129             int a = is1.read();
130             int b = is2.read();
131             pos++;
132             assertEquals("Data does not match control file: "+dataFile+" at byte position "+pos,a,b);
133             while(a >= 0 && b>= 0) {
134                 a = is1.read();
135                 b = is2.read();
136                 pos++;
137                 assertEquals(a,b);
138             }
139             is2.close();
140         } finally {
141             is1.close();
142         }
143     }
144     
145     abstract protected Object JavaDoc createObject() throws IOException JavaDoc;
146 }
147
Popular Tags