KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > projects > XMLSettingsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /**
35  * Benchmark measuring how fast .settings files can be recognized and probed for instances.
36  * Uses FolderInstance as the means of collecting them together.
37  * @author Jesse Glick
38  */

39 public class XMLSettingsTest extends Benchmark {
40     
41     public static void main(String JavaDoc[] args) {
42         simpleRun(XMLSettingsTest.class);
43     }
44     
45     public XMLSettingsTest(String JavaDoc name) {
46         // Each arg is an Integer[2] of # of unmodified & modified .settings files to make
47
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 JavaDoc b = new StringBuffer JavaDoc();
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 JavaDoc b = new StringBuffer JavaDoc();
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 JavaDoc {
106         NbTopManager.get();
107         int count = getIterationCount();
108         int[] x = (int[])getArgument();
109         int unmodified = x[0];
110         int modified = x[1];
111         //System.out.println("setUp: count=" + count + " unmodified=" + unmodified + " modified=" + modified);
112
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 JavaDoc)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 JavaDoc)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 JavaDoc {
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 JavaDoc {
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                 /*
152                 System.out.println("folder=" + folders[i]);
153                 DataObject[] kids = folders[i].getChildren();
154                 System.out.println("folder.children=" + Arrays.asList(kids));
155                 InstanceCookie ic = (InstanceCookie)kids[0].getCookie(InstanceCookie.class);
156                 System.out.println("folder.children[0].instanceCreate=" + (ic == null ? "null" : ic.instanceCreate()));
157                  */

158                 InstanceCookie cf = new ClazzFolder(folders[i]);
159                 /*
160                 FolderInstance cf = new ClazzFolder(folders[i]);
161                 cf.run();
162                 cf.waitFinished();
163                  */

164                 SuperClazz[] clazzes = (SuperClazz[])cf.instanceCreate();
165                 //System.out.println("clazzes=" + Arrays.asList(clazzes));
166
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                 // 0, 1, 3, 6, 10, 15, ...
178
assertEquals((modified * (modified + 1)) / 2, modifiedCount);
179             } catch (Exception JavaDoc e) {
180                 e.printStackTrace(System.out);
181                 throw e;
182             } catch (Error JavaDoc 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 JavaDoc createInstance(InstanceCookie[] cookies) throws IOException, ClassNotFoundException JavaDoc {
194             //System.out.println("createInstance: " + Arrays.asList(cookies));
195
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 JavaDoc {
202             if (cookie instanceof InstanceCookie.Of) {
203                 if (((InstanceCookie.Of)cookie).instanceOf(SuperClazz.class)) {
204                     //System.out.println("acceptCookie: OK");
205
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 JavaDoc toString() {return "Clazz[" + x + "]";}
224     }
225     
226 }
227
Popular Tags