1 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 ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.Set ; 18 19 public class BootJarTest extends BaseDSOTestCase { 20 21 private final static String STRING_CLASS = "java.lang.String"; 22 private final ClassBytesProvider bytesProvider = new ClassLoaderBytesProvider(getClass().getClassLoader()); 23 24 public BootJarTest() { 25 } 27 28 public void testBootJarVersion() throws Exception { 29 File 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 } 41 } 42 43 public void testFindBootJar() throws Exception { 44 File bootJar = new File ("/path/to/dso-boot/dso-boot.jar").getAbsoluteFile(); 47 48 String 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 ("/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 { 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 allClasses = bootJar.getAllPreInstrumentedClasses(); 72 Set missingClasses = new HashSet (); 73 for (Iterator iter = allClasses.iterator(); iter.hasNext();) { 74 String classname = (String ) iter.next(); 75 Class clazz = Class.forName(classname); 76 while (clazz != null && clazz != Object .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 classes) { 93 for (Iterator iter = classes.iterator(); iter.hasNext();) { 94 String className = (String ) 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 { 104 String vmSig = "vm signature"; 105 DSOClientConfigHelper config = configHelper(); 106 107 File jar = getTempFile("dso-boot.jar"); 108 109 BootJar bootJar = BootJar.getBootJarForWriting(jar, vmSig); 110 111 bootJar.loadClassIntoJar(STRING_CLASS, bytesProvider.getBytesForClass(STRING_CLASS), false); 113 String classname = Boolean .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 allPreInstrumentedClasses = bootJar.getAllPreInstrumentedClasses(); 125 assertEquals(1, allPreInstrumentedClasses.size()); 126 assertTrue(allPreInstrumentedClasses.contains(classname)); 127 assertFalse(allPreInstrumentedClasses.contains(java.lang.String .class.getName())); 128 129 bootJar.close(); 130 } 131 132 } 133 | Popular Tags |