KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > tools > BootJarTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.tools;
5
6 import com.tc.object.BaseDSOTestCase;
7 import com.tc.object.Portability;
8 import com.tc.object.PortabilityImpl;
9 import com.tc.object.config.DSOClientConfigHelper;
10 import com.tc.object.config.TransparencyClassSpec;
11 import com.tc.util.Assert;
12 import com.tc.util.TCAssertionError;
13
14 import java.io.File JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Set JavaDoc;
18
19 public class BootJarTest extends BaseDSOTestCase {
20
21   private final static String JavaDoc STRING_CLASS = "java.lang.String";
22   private final ClassBytesProvider bytesProvider = new ClassLoaderBytesProvider(getClass().getClassLoader());
23
24   public BootJarTest() {
25     //
26
}
27
28   public void testBootJarVersion() throws Exception JavaDoc {
29     File JavaDoc jarFile = this.getTempFile("dso-boot.jar");
30
31     BootJar bootJar = BootJar.getBootJarForWriting(jarFile, "hotspot_win32_150");
32     bootJar.loadClassIntoJar(STRING_CLASS, bytesProvider.getBytesForClass(STRING_CLASS), false);
33     bootJar.close();
34
35     try {
36       BootJar.getBootJarForReading(jarFile);
37       fail("Expected InvalidJVMVersionException!");
38     } catch (InvalidJVMVersionException e) {
39       // expected (since vm signature won't match the running VM signature)
40
}
41   }
42
43   public void testFindBootJar() throws Exception JavaDoc {
44     // use absolute file to make sure the path is fully qualified on windows
45
// (ie. has drive letter)
46
File JavaDoc bootJar = new File JavaDoc("/path/to/dso-boot/dso-boot.jar").getAbsoluteFile();
47
48     String JavaDoc origBootClassPath = System.getProperty("sun.boot.class.path");
49
50     System.setProperty("sun.boot.class.path", bootJar.getAbsolutePath() + System.getProperty("path.separator")
51                                               + origBootClassPath);
52
53     assertEquals(bootJar, BootJar.findBootJar());
54
55     System.setProperty("sun.boot.class.path", origBootClassPath);
56
57     bootJar = new File JavaDoc("/path/to/dso-boot/dso-boot-hotspot_win32_150.jar").getAbsoluteFile();
58
59     System.setProperty("sun.boot.class.path", bootJar.getAbsolutePath() + System.getProperty("path.separator")
60                                               + origBootClassPath);
61
62     assertEquals(bootJar, BootJar.findBootJar());
63
64     System.setProperty("sun.boot.class.path", origBootClassPath);
65   }
66
67   public void testAllSuperClassesPresentInBootJar() throws Exception JavaDoc {
68     Portability portability = new PortabilityImpl(this.configHelper());
69     BootJar bootJar = BootJar.getDefaultBootJarForReading();
70     if (bootJar == null) { throw new TCAssertionError("Boot Jar Not Found !"); }
71     Set JavaDoc allClasses = bootJar.getAllPreInstrumentedClasses();
72     Set JavaDoc missingClasses = new HashSet JavaDoc();
73     for (Iterator JavaDoc iter = allClasses.iterator(); iter.hasNext();) {
74       String JavaDoc classname = (String JavaDoc) iter.next();
75       Class JavaDoc clazz = Class.forName(classname);
76       while (clazz != null && clazz != Object JavaDoc.class) {
77         clazz = clazz.getSuperclass();
78         if (!portability.isInstrumentationNotNeeded(clazz.getName()) && !allClasses.contains(clazz.getName())) {
79           System.err.println(" Class " + classname + " is in the bootjar, but its super class " + clazz.getName()
80                              + " not in the bootjar !");
81           missingClasses.add(clazz.getName());
82         }
83       }
84     }
85     if (!missingClasses.isEmpty()) {
86       System.err.println("\n\nThe Following classes should go into bootjar !");
87       printClasses(missingClasses);
88     }
89     Assert.assertTrue(missingClasses.isEmpty());
90   }
91
92   private void printClasses(Set JavaDoc classes) {
93     for (Iterator JavaDoc iter = classes.iterator(); iter.hasNext();) {
94       String JavaDoc className = (String JavaDoc) iter.next();
95       System.err.println(" " + className);
96     }
97   }
98
99   protected boolean cleanTempDir() {
100     return false;
101   }
102
103   public void tests() throws Exception JavaDoc {
104     String JavaDoc vmSig = "vm signature";
105     DSOClientConfigHelper config = configHelper();
106
107     File JavaDoc jar = getTempFile("dso-boot.jar");
108
109     BootJar bootJar = BootJar.getBootJarForWriting(jar, vmSig);
110
111     // load a regular class into the jar.
112
bootJar.loadClassIntoJar(STRING_CLASS, bytesProvider.getBytesForClass(STRING_CLASS), false);
113     // load an "instrumented" class into the jar.
114
String JavaDoc classname = Boolean JavaDoc.class.getName();
115     TransparencyClassSpec spec = new TransparencyClassSpec(classname, config);
116     spec.markPreInstrumented();
117     byte[] classBytes = bytesProvider.getBytesForClass(classname);
118     assertNotNull(classBytes);
119     bootJar.loadClassIntoJar(spec.getClassName(), classBytes, spec.isPreInstrumented());
120     bootJar.close();
121
122     bootJar = BootJar.getBootJarForReading(jar, new BootJarSignature(vmSig));
123
124     Set JavaDoc allPreInstrumentedClasses = bootJar.getAllPreInstrumentedClasses();
125     assertEquals(1, allPreInstrumentedClasses.size());
126     assertTrue(allPreInstrumentedClasses.contains(classname));
127     assertFalse(allPreInstrumentedClasses.contains(java.lang.String JavaDoc.class.getName()));
128
129     bootJar.close();
130   }
131
132 }
133
Popular Tags