001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.object.tools;
006:
007: import com.tc.object.BaseDSOTestCase;
008: import com.tc.object.Portability;
009: import com.tc.object.PortabilityImpl;
010: import com.tc.object.config.DSOClientConfigHelper;
011: import com.tc.object.config.TransparencyClassSpec;
012: import com.tc.object.config.TransparencyClassSpecImpl;
013: import com.tc.util.Assert;
014: import com.tc.util.TCAssertionError;
015:
016: import java.io.File;
017: import java.util.HashSet;
018: import java.util.Iterator;
019: import java.util.Set;
020:
021: public class BootJarTest extends BaseDSOTestCase {
022:
023: private final static String STRING_CLASS = "java.lang.String";
024: private final ClassBytesProvider bytesProvider = new ClassLoaderBytesProvider(
025: getClass().getClassLoader());
026:
027: public BootJarTest() {
028: //
029: }
030:
031: public void testInvalidBootJarVersion() throws Exception {
032: File jarFile = this .getTempFile("dso-boot.jar");
033: BootJar bootJar = BootJar.getBootJarForWriting(jarFile,
034: "bogus_jvm_signature_666");
035: bootJar.loadClassIntoJar(STRING_CLASS, bytesProvider
036: .getBytesForClass(STRING_CLASS), false);
037: bootJar.close();
038: try {
039: BootJar.getBootJarForReading(jarFile);
040: fail("The test was expecting an InvalidJVMVersionException, but it was not thrown.");
041: } catch (InvalidJVMVersionException e) {
042: // we expect this to happen since the vm signature won't
043: // match the running VM signature)
044: }
045: }
046:
047: public void testFindBootJar() throws Exception {
048: // use absolute file to make sure the path is fully qualified on windows
049: // (ie. has drive letter)
050: File bootJar = new File("/path/to/dso-boot/dso-boot.jar")
051: .getAbsoluteFile();
052:
053: String origBootClassPath = System
054: .getProperty("sun.boot.class.path");
055:
056: System.setProperty("sun.boot.class.path", bootJar
057: .getAbsolutePath()
058: + System.getProperty("path.separator")
059: + origBootClassPath);
060:
061: assertEquals(bootJar, BootJar.findBootJar());
062:
063: System.setProperty("sun.boot.class.path", origBootClassPath);
064:
065: bootJar = new File(
066: "/path/to/dso-boot/dso-boot-hotspot_win32_150.jar")
067: .getAbsoluteFile();
068:
069: System.setProperty("sun.boot.class.path", bootJar
070: .getAbsolutePath()
071: + System.getProperty("path.separator")
072: + origBootClassPath);
073:
074: assertEquals(bootJar, BootJar.findBootJar());
075:
076: System.setProperty("sun.boot.class.path", origBootClassPath);
077: }
078:
079: public void testAllSuperClassesPresentInBootJar() throws Exception {
080: Portability portability = new PortabilityImpl(this
081: .configHelper());
082: BootJar bootJar = BootJar.getDefaultBootJarForReading();
083: if (bootJar == null) {
084: throw new TCAssertionError("Boot Jar Not Found !");
085: }
086: Set allClasses = bootJar.getAllPreInstrumentedClasses();
087: Set missingClasses = new HashSet();
088: for (Iterator iter = allClasses.iterator(); iter.hasNext();) {
089: String classname = (String) iter.next();
090: Class clazz = Class.forName(classname);
091: while (clazz != null && clazz != Object.class) {
092: clazz = clazz.getSuperclass();
093: if (!portability.isInstrumentationNotNeeded(clazz
094: .getName())
095: && !allClasses.contains(clazz.getName())) {
096: System.err
097: .println(" Class "
098: + classname
099: + " is in the bootjar, but its super class "
100: + clazz.getName()
101: + " not in the bootjar !");
102: missingClasses.add(clazz.getName());
103: }
104: }
105: }
106: if (!missingClasses.isEmpty()) {
107: System.err
108: .println("\n\nThe Following classes should go into bootjar !");
109: printClasses(missingClasses);
110: }
111: Assert.assertTrue(missingClasses.isEmpty());
112: }
113:
114: private void printClasses(Set classes) {
115: for (Iterator iter = classes.iterator(); iter.hasNext();) {
116: String className = (String) iter.next();
117: System.err.println(" " + className);
118: }
119: }
120:
121: protected boolean cleanTempDir() {
122: return false;
123: }
124:
125: public void tests() throws Exception {
126: String vmSig = "vm signature";
127: DSOClientConfigHelper config = configHelper();
128:
129: File jar = getTempFile("dso-boot.jar");
130:
131: BootJar bootJar = BootJar.getBootJarForWriting(jar, vmSig);
132:
133: // load a regular class into the jar.
134: bootJar.loadClassIntoJar(STRING_CLASS, bytesProvider
135: .getBytesForClass(STRING_CLASS), false);
136: // load an "instrumented" class into the jar.
137: String classname = Boolean.class.getName();
138: TransparencyClassSpec spec = new TransparencyClassSpecImpl(
139: classname, config);
140: spec.markPreInstrumented();
141: byte[] classBytes = bytesProvider.getBytesForClass(classname);
142: assertNotNull(classBytes);
143: bootJar.loadClassIntoJar(spec.getClassName(), classBytes, spec
144: .isPreInstrumented());
145: bootJar.close();
146:
147: bootJar = BootJar.getBootJarForReading(jar,
148: new BootJarSignature(vmSig));
149:
150: Set allPreInstrumentedClasses = bootJar
151: .getAllPreInstrumentedClasses();
152: assertEquals(1, allPreInstrumentedClasses.size());
153: assertTrue(allPreInstrumentedClasses.contains(classname));
154: assertFalse(allPreInstrumentedClasses
155: .contains(java.lang.String.class.getName()));
156:
157: bootJar.close();
158: }
159:
160: }
|