1 19 20 package org.netbeans.core.projects; 21 22 import java.io.*; 23 import java.util.*; 24 25 import org.netbeans.performance.Benchmark; 26 27 import org.openide.loaders.*; 28 29 import org.netbeans.core.NbTopManager; 30 import org.netbeans.core.projects.FixedFileSystem; 31 import org.openide.cookies.InstanceCookie; 32 import org.openide.filesystems.Repository; 33 34 39 public class XMLSettingsTest extends Benchmark { 40 41 public static void main(String [] args) { 42 simpleRun(XMLSettingsTest.class); 43 } 44 45 public XMLSettingsTest(String name) { 46 super(name, new int[][] { 48 {10, 0}, 49 {100, 0}, 50 {1000, 0}, 51 {5, 5}, 52 {50, 50}, 53 {500, 500}, 54 }); 55 } 56 57 private static byte[] unmodifiedData = null; 58 private static byte[] getUnmodifiedData() { 59 if (unmodifiedData == null) { 60 StringBuffer b = new StringBuffer (); 61 b.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 62 b.append("<!DOCTYPE settings PUBLIC \"-//NetBeans//DTD Session settings 1.0//EN\" \"http://www.netbeans.org/dtds/sessionsettings-1_0.dtd\">\n"); 63 b.append("<settings version=\"1.0\">\n"); 64 b.append(" <instanceof class=\"org.netbeans.core.projects.XMLSettingsTest$SuperClazz\"/>\n"); 65 b.append(" <instanceof class=\"org.netbeans.core.projects.XMLSettingsTest$Clazz\"/>\n"); 66 b.append(" <instance class=\"org.netbeans.core.projects.XMLSettingsTest$Clazz\"/>\n"); 67 b.append("</settings>\n"); 68 unmodifiedData = b.toString().getBytes(); 69 } 70 return unmodifiedData; 71 } 72 73 private static List modifiedData = new ArrayList(1000); 74 private static char[] HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 75 private static byte[] getModifiedData(int i) throws IOException { 76 while (modifiedData.size() <= i) modifiedData.add(null); 77 if (modifiedData.get(i) == null) { 78 StringBuffer b = new StringBuffer (); 79 b.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 80 b.append("<!DOCTYPE settings PUBLIC \"-//NetBeans//DTD Session settings 1.0//EN\" \"http://www.netbeans.org/dtds/sessionsettings-1_0.dtd\">\n"); 81 b.append("<settings version=\"1.0\">\n"); 82 b.append(" <instanceof class=\"org.netbeans.core.projects.XMLSettingsTest$SuperClazz\"/>\n"); 83 b.append(" <instanceof class=\"org.netbeans.core.projects.XMLSettingsTest$Clazz\"/>\n"); 84 b.append(" <serialdata class=\"org.netbeans.core.projects.XMLSettingsTest$Clazz\">\n"); 85 b.append(" "); 86 ByteArrayOutputStream baos = new ByteArrayOutputStream(1000); 87 ObjectOutputStream oos = new ObjectOutputStream(baos); 88 oos.writeObject(new Clazz(i)); 89 oos.close(); 90 byte[] ser = baos.toByteArray(); 91 for (int j = 0; j < ser.length; j++) { 92 int x = ser[j] + 256; 93 b.append(HEX[(x & 0xF0) >> 4]); 94 b.append(HEX[x & 0x0F]); 95 } 96 b.append("\n"); 97 b.append(" </serialdata>\n"); 98 b.append("</settings>\n"); 99 modifiedData.set(i, b.toString().getBytes()); 100 } 101 return (byte[])modifiedData.get(i); 102 } 103 104 private DataFolder[] folders; 105 protected void setUp() throws Exception { 106 NbTopManager.get(); 107 int count = getIterationCount(); 108 int[] x = (int[])getArgument(); 109 int unmodified = x[0]; 110 int modified = x[1]; 111 folders = new DataFolder[count]; 113 for (int i = 0; i < count; i++) { 114 FixedFileSystem ffs = FixedFileSystem.getDefault(); 115 for (int j = 0; j < unmodified; j++) { 116 FixedFileSystem.Instance inst = new FixedFileSystem.Instance(false, "text/xml", getUnmodifiedData(), null, (String )null); 117 ffs.add("folder" + i + "/unmodified" + j + ".settings", inst); 118 } 119 for (int j = 0; j < modified; j++) { 120 FixedFileSystem.Instance inst = new FixedFileSystem.Instance(false, "text/xml", getModifiedData(j + 1), null, (String )null); 121 ffs.add("folder" + i + "/modified" + j + ".settings", inst); 122 } 123 folders[i] = DataFolder.findFolder(Repository.getDefault().getDefaultFileSystem().findResource("folder" + i)); 124 } 125 } 126 protected void tearDown() throws Exception { 127 folders = null; 128 int count = getIterationCount(); 129 int[] x = (int[])getArgument(); 130 int unmodified = x[0]; 131 int modified = x[1]; 132 for (int i = 0; i < count; i++) { 133 FixedFileSystem ffs = FixedFileSystem.getDefault(); 134 for (int j = 0; j < unmodified; j++) { 135 ffs.remove("folder" + i + "/unmodified" + j + ".settings"); 136 } 137 for (int j = 0; j < modified; j++) { 138 ffs.remove("folder" + i + "/modified" + j + ".settings"); 139 } 140 ffs.remove("folder" + i); 141 } 142 } 143 144 public void testSettings() throws Exception { 145 int count = getIterationCount(); 146 int[] x = (int[])getArgument(); 147 int unmodified = x[0]; 148 int modified = x[1]; 149 for (int i = 0; i < count; i++) { 150 try { 151 158 InstanceCookie cf = new ClazzFolder(folders[i]); 159 164 SuperClazz[] clazzes = (SuperClazz[])cf.instanceCreate(); 165 int unmodifiedCount = 0; 167 int modifiedCount = 0; 168 for (int j = 0; j < clazzes.length; j++) { 169 int number = clazzes[j].x(); 170 if (number == 0) { 171 unmodifiedCount++; 172 } else { 173 modifiedCount += number; 174 } 175 } 176 assertEquals(unmodified, unmodifiedCount); 177 assertEquals((modified * (modified + 1)) / 2, modifiedCount); 179 } catch (Exception e) { 180 e.printStackTrace(System.out); 181 throw e; 182 } catch (Error e) { 183 e.printStackTrace(System.out); 184 throw e; 185 } 186 } 187 } 188 189 private static final class ClazzFolder extends FolderInstance { 190 public ClazzFolder(DataFolder f) { 191 super(f); 192 } 193 protected Object createInstance(InstanceCookie[] cookies) throws IOException, ClassNotFoundException { 194 SuperClazz[] clazzes = new SuperClazz[cookies.length]; 196 for (int i = 0; i < clazzes.length; i++) { 197 clazzes[i] = (SuperClazz)cookies[i].instanceCreate(); 198 } 199 return clazzes; 200 } 201 protected InstanceCookie acceptCookie(InstanceCookie cookie) throws IOException, ClassNotFoundException { 202 if (cookie instanceof InstanceCookie.Of) { 203 if (((InstanceCookie.Of)cookie).instanceOf(SuperClazz.class)) { 204 return cookie; 206 } 207 System.out.println("acceptCookie: not IC.Of"); 208 } 209 System.out.println("acceptCookie: strange class: " + cookie.instanceName()); 210 return null; 211 } 212 } 213 214 public static abstract class SuperClazz implements Serializable { 215 public abstract int x(); 216 } 217 public static final class Clazz extends SuperClazz { 218 private static final long serialVersionUID = 24356298473569L; 219 private final int x; 220 public Clazz() {x = 0;} 221 public Clazz(int _x) {x = _x;} 222 public final int x() {return x;} 223 public String toString() {return "Clazz[" + x + "]";} 224 } 225 226 } 227 | Popular Tags |