KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > BaseTest


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
21 import java.io.*;
22 import java.util.*;
23
24 import org.w3c.dom.*;
25
26 import javax.xml.parsers.DocumentBuilder JavaDoc;
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28
29 import org.netbeans.modules.schema2beans.BaseBean;
30
31 public class BaseTest
32 {
33     String JavaDoc testName;
34     String JavaDoc testMsg;
35     String JavaDoc documentDir = null;
36
37     Document doc;
38     
39     OutputStream out;
40     
41     BaseTest()
42     {
43         this.testName = this.getClass().getName();
44         this.out = System.out;
45         this.doc = null;
46     }
47
48     String JavaDoc getDocumentName() {
49         return getClass().getName()+".xml";
50     }
51
52     String JavaDoc getFullDocumentName() {
53         if (documentDir == null)
54             return getDocumentName();
55         return documentDir + getDocumentName();
56     }
57
58     void setDocumentDir(String JavaDoc dir) {
59         documentDir = dir;
60     }
61
62     void readDocument()
63     {
64         this.readDocument(getDocumentName());
65     }
66         
67     void readDocument(String JavaDoc name)
68     {
69         /*
70          * The other way to create the object graph:
71          *
72          * DDFactory.register("book", "book.Book");
73          * book = DDFactory.create(in, "book");
74          *
75          */

76         if (documentDir != null)
77             name = documentDir + name;
78         try
79         {
80             FileInputStream in = new FileInputStream(name);
81             
82             out("creating the DOM document");
83             DocumentBuilderFactory JavaDoc dbf =
84                     DocumentBuilderFactory.newInstance();
85             DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
86             this.doc = db.parse(in);
87             in.close();
88         }
89         catch (Throwable JavaDoc t)
90         {
91             t.printStackTrace();
92             throw new RuntimeException JavaDoc("DD creation failed: " +
93                                        t.getMessage());
94         }
95
96         if (doc == null)
97         {
98             err("doc is null");
99             return;
100         }
101     }
102     
103     void setTest(String JavaDoc msg)
104     {
105         this.testMsg = msg;
106     }
107
108     void setOutputStream(OutputStream out)
109     {
110         this.out = out;
111     }
112
113     void print(String JavaDoc s) {
114         try {
115             this.out.write(s.getBytes());
116         } catch(Exception JavaDoc e) {
117             throw new RuntimeException JavaDoc("outputStream.write failed: " +
118                                        e.getMessage());
119         }
120     }
121
122     void println(String JavaDoc s) {
123         print(s);
124         print("\n");
125     }
126     
127     void err(String JavaDoc s)
128     {
129         println("### Error - " + s);
130     }
131     
132     void out(String JavaDoc s, String JavaDoc s2)
133     {
134         println(this.testName + " - " + s + "\n" + s2);
135     }
136     
137     void out(String JavaDoc s)
138     {
139         println(this.testName + " - " + s);
140     }
141
142     void out(int value)
143     {
144         println(this.testName + " - " + value);
145     }
146
147     void out(String JavaDoc[] s) {
148         out("{");
149         for (int i = 0; i < s.length; ++i)
150             out("\t"+s[i]);
151         out("}");
152     }
153
154     void out(BaseBean bean) {
155         bean.dumpXml();
156     }
157
158     void print(Object JavaDoc o) {
159         if (o == null) {
160             print("null");
161             return;
162         }
163         Class JavaDoc cls = o.getClass();
164         if (cls.isArray()) {
165             print("{");
166             Object JavaDoc[] arr = (Object JavaDoc[]) o;
167             for (int i = 0; i < arr.length; ++i) {
168                 if (i > 0)
169                     print(", ");
170                 print(arr[i]);
171             }
172             print("}");
173         } else {
174             print(""+o);
175         }
176     }
177     
178     void check(boolean success, String JavaDoc str)
179     {
180         if (success)
181             println(this.testName + " " + this.testMsg +
182                     ((str != null)?" "+str:"") + " -> OK");
183         else
184             println(this.testName + " " + this.testMsg +
185                     ((str != null)?" "+str:"") + " -> FAILED");
186     }
187     
188     void check(boolean success)
189     {
190         this.check(success, null);
191     }
192
193
194     int getKMemUsage() {
195     Runtime JavaDoc rt = Runtime.getRuntime();
196
197     try {
198         rt.gc();
199         Thread.sleep(1000L);
200         rt.gc();
201         Thread.sleep(1000L);
202         rt.gc();
203         Thread.sleep(1000L);
204         rt.gc();
205     } catch(Exception JavaDoc e) {
206     }
207
208     long used = rt.totalMemory()-rt.freeMemory();
209     return (int)(used/1024L);
210     }
211
212     void printMemUsage() {
213     out("Memory usage: " + getKMemUsage() + "k");
214     }
215
216
217     public void run()
218         throws Exception JavaDoc
219     {
220         throw new RuntimeException JavaDoc("This run() method should be subclassed");
221     }
222 }
223
224
Popular Tags