KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > compatibility > test > SerialVersionUIDUnitTestCase


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.test.compatibility.test;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34
35 import org.jboss.tools.ClassVersionInfo;
36 import org.jboss.tools.SerialVersionUID;
37
38
39 /**
40  * Tests of serial version uid compatibility across jboss versions
41  *
42  * @author Scott.Stark@jboss.org
43  * @version $Revision: 56841 $
44  */

45 public class SerialVersionUIDUnitTestCase extends TestCase
46 {
47    static Map JavaDoc currentClassInfoMap;
48
49    public SerialVersionUIDUnitTestCase(String JavaDoc name)
50    {
51       super(name);
52    }
53
54    /** Validate the 4.0.1 serial version uids against the current build
55     * @throws Exception
56     */

57    public void test401Compatibility()
58          throws Exception JavaDoc
59    {
60       // The packages in jboss-4.0.x with known 4.0.1 serialization issues
61
String JavaDoc[] badPackages = {
62          // Ignore org.apache.* issues
63
"org.apache",
64          // Ignore jacorb packages org.jacorb.*, org.omg.*
65
"org.jacorb",
66          "org.omg",
67          /* Ignore the aop package for 4.0.1 since this will be in flux until
68             jboss5/ejb3 is stablized
69          */

70          "org.jboss.aop",
71          /* Ignore org.jboss.webservice for 4.0.1 since the org.apache.axis to
72             org.jboss.axis package name change breaks serialization
73          */

74          "org.jboss.webservice",
75          /* Ignore org.hsqldb as there are some utility classes that changed
76          in the upgrade to 1_8_0
77          */

78          "org.hsqldb",
79          /* Ignore javacc generated classes
80          */

81          "org.jboss.ejb.plugins.cmp.ejbql.TokenMgrError",
82          "org.jboss.mq.selectors.TokenMgrError",
83          "org.jboss.security.auth.login.TokenMgrError"
84       };
85
86       System.out.println("+++ test401Compatibility");
87       // load the 4.0.1 serialVersionUID database
88
String JavaDoc etc = System.getProperty("jbosstest.src.etc", "../src/etc");
89       File JavaDoc serFile = new File JavaDoc(etc, "serialVersionUID/401.ser");
90       FileInputStream JavaDoc fis = new FileInputStream JavaDoc(serFile);
91       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(fis);
92       Map JavaDoc classInfoMap = (Map JavaDoc) ois.readObject();
93       System.out.println("4.0.1 serial classes count: "+classInfoMap.size());
94
95       System.setProperty("org.jboss.j2ee.LegacySerialization", "true");
96       Map JavaDoc currentClassInfoMap = calcClassInfoMap();
97       int mismatchCount = compare(classInfoMap, currentClassInfoMap, "401", badPackages);
98       currentClassInfoMap.clear();
99       System.out.println("serialVersionUID mismatches = "+mismatchCount);
100       assertTrue("There are no serialVersionUID mismatches("+mismatchCount+")",
101          mismatchCount == 0);
102    }
103
104    /** Validate the J2EE 1.4.1 RI serial version uids against the current build
105     * @throws Exception
106     */

107    public void testJ2EERI141Compatibility()
108          throws Exception JavaDoc
109    {
110       // The packages in j2ee 1.4.1RI with known serialization issues
111
String JavaDoc[] badPackages = {
112          // The javax.mail binaries in the ri are not consistent with the javamail 1.3FCS
113
"javax.mail"
114       };
115       System.out.println("+++ testJ2EERI141Compatibility");
116       System.getProperties().remove("org.jboss.j2ee.LegacySerialization");
117       String JavaDoc etc = System.getProperty("jbosstest.src.etc", "../src/etc");
118       File JavaDoc serFile = new File JavaDoc(etc, "serialVersionUID/j2ee141.ser");
119       FileInputStream JavaDoc fis = new FileInputStream JavaDoc(serFile);
120       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(fis);
121       Map JavaDoc classInfoMap = (Map JavaDoc) ois.readObject();
122       System.out.println("J2EE RI serial classes count: "+classInfoMap.size());
123
124       Map JavaDoc currentClassInfoMap = calcClassInfoMap();
125       int mismatchCount = compare(classInfoMap, currentClassInfoMap, "J2EE1.4", badPackages);
126       currentClassInfoMap.clear();
127       System.out.println("serialVersionUID mismatches = "+mismatchCount);
128       assertTrue("There are no serialVersionUID mismatches("+mismatchCount+")",
129          mismatchCount == 0);
130    }
131
132    private int compare(Map JavaDoc classInfoMap, Map JavaDoc currentClassInfoMap,
133       String JavaDoc versionName, String JavaDoc[] badPackages)
134    {
135       int mismatchCount = 0;
136       Iterator JavaDoc iter = currentClassInfoMap.values().iterator();
137       while( iter.hasNext() )
138       {
139          ClassVersionInfo cvi = (ClassVersionInfo) iter.next();
140          String JavaDoc name = cvi.getName();
141          ClassVersionInfo cvi401 = (ClassVersionInfo) classInfoMap.get(name);
142          if( cvi401 != null && cvi.getSerialVersion() != cvi401.getSerialVersion() )
143          {
144             String JavaDoc msg = "serialVersionUID error for "+name
145                +", " + versionName + " " + cvi401.getSerialVersion()
146                +", current: "+cvi.getSerialVersion();
147             // Don't count classes from badPackages
148
boolean isInBadPkg = false;
149             for(int n = 0; n < badPackages.length; n ++)
150             {
151                String JavaDoc pkg = badPackages[n];
152                if( name.startsWith(pkg) )
153                {
154                   isInBadPkg = true;
155                   break;
156                }
157             }
158             if( isInBadPkg == false )
159             {
160                mismatchCount ++;
161                System.err.println(msg);
162             }
163             else
164             {
165                System.out.println(msg);
166             }
167          }
168       }
169       return mismatchCount;
170    }
171
172    static Map JavaDoc calcClassInfoMap()
173       throws IOException JavaDoc
174    {
175       String JavaDoc jbossDist = System.getProperty("jbosstest.dist");
176       File JavaDoc jbossHome = new File JavaDoc(jbossDist);
177       jbossHome = jbossHome.getCanonicalFile();
178       System.out.println("Calculating serialVersionUIDs for jbossHome: "+jbossHome);
179       Map JavaDoc classInfoMap = SerialVersionUID.generateJBossSerialVersionUIDReport(
180          jbossHome);
181       return classInfoMap;
182    }
183    
184    public static Test suite() throws Exception JavaDoc
185    {
186       // JBAS-3600, the execution order of tests in this test case is important
187
// so it must be defined explicitly when running under some JVMs
188
TestSuite suite = new TestSuite();
189       suite.addTest(new SerialVersionUIDUnitTestCase("test401Compatibility"));
190       suite.addTest(new SerialVersionUIDUnitTestCase("testJ2EERI141Compatibility"));
191
192       return suite;
193    }
194
195    public static void main(String JavaDoc[] args)
196    {
197       junit.textui.TestRunner.run(SerialVersionUIDUnitTestCase.class);
198    }
199 }
200
Popular Tags