blob: 35373cfabb8460fa936a79921f013a51b37856e7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
package com.itmill.toolkit.tests.testbench;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import com.itmill.toolkit.Application;
import com.itmill.toolkit.data.Property;
import com.itmill.toolkit.data.util.HierarchicalContainer;
import com.itmill.toolkit.terminal.Sizeable;
import com.itmill.toolkit.ui.Component;
import com.itmill.toolkit.ui.CustomComponent;
import com.itmill.toolkit.ui.ExpandLayout;
import com.itmill.toolkit.ui.Label;
import com.itmill.toolkit.ui.Panel;
import com.itmill.toolkit.ui.SplitPanel;
import com.itmill.toolkit.ui.Tree;
import com.itmill.toolkit.ui.Window;
/**
* TestBench finds out testable classes within given java packages and adds them
* to menu from where they can be executed. Class is considered testable if it
* is of class CustomComponent.
*
* Note: edit TestBench.testablePackages array
*
* @author IT Mill Ltd.
*
*/
public class TestBench extends com.itmill.toolkit.Application implements
Property.ValueChangeListener {
// Add here packages which are used for finding testable classes
String[] testablePackages = { "com.itmill.toolkit.demo.testbench" };
HierarchicalContainer testables = new HierarchicalContainer();
Window mainWindow = new Window("TestBench window");
// Main layout consists of tree menu and body layout
SplitPanel mainLayout = new SplitPanel(SplitPanel.ORIENTATION_HORIZONTAL);
Tree menu;
Panel bodyLayout = new Panel();
HashMap itemCaptions = new HashMap();
public void init() {
// Add testable classes to hierarchical container
for (int p = 0; p < testablePackages.length; p++) {
testables.addItem(testablePackages[p]);
try {
List testableClasses = getTestableClassesForPackage(testablePackages[p]);
for (Iterator it = testableClasses.iterator(); it.hasNext();) {
Class t = (Class) it.next();
// ignore TestBench itself
if (t.equals(TestBench.class)) {
continue;
}
try {
testables.addItem(t);
itemCaptions.put(t, t.getName());
testables.setParent(t, testablePackages[p]);
continue;
} catch (Exception e) {
try {
testables.addItem(t);
itemCaptions.put(t, t.getName());
testables.setParent(t, testablePackages[p]);
continue;
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
menu = new Tree("Testables", testables);
for (Iterator i = itemCaptions.keySet().iterator(); i.hasNext();) {
Class testable = (Class) i.next();
// simplify captions
String name = testable.getName().substring(
testable.getName().lastIndexOf('.') + 1);
menu.setItemCaption(testable, name);
}
// expand all root items
for (Iterator i = menu.rootItemIds().iterator(); i.hasNext();) {
menu.expandItemsRecursively(i.next());
}
menu.addListener(this);
menu.setImmediate(true);
mainLayout.addComponent(menu);
bodyLayout.addStyleName("light");
bodyLayout.setHeight(100);
bodyLayout.setHeightUnits(Sizeable.UNITS_PERCENTAGE);
bodyLayout.setLayout(new ExpandLayout());
mainLayout.addComponent(bodyLayout);
mainLayout.setSplitPosition(30);
mainWindow.setLayout(mainLayout);
setMainWindow(mainWindow);
}
private Component createTestable(Class c) {
try {
Application app = (Application) c.newInstance();
app.init();
return app.getMainWindow().getLayout();
} catch (Exception e) {
try {
CustomComponent cc = (CustomComponent) c.newInstance();
return cc;
} catch (Exception e1) {
e1.printStackTrace();
return new Label("Cannot create custom component: "
+ e1.toString());
}
}
}
// Handle menu selection and update body
public void valueChange(Property.ValueChangeEvent event) {
bodyLayout.removeAllComponents();
bodyLayout.setCaption(null);
Object o = menu.getValue();
if (o != null && o instanceof Class) {
Class c = (Class) o;
String title = c.getName();
bodyLayout.setCaption(title);
bodyLayout.addComponent(createTestable(c));
} else {
// NOP node selected or deselected tree item
}
}
/**
* Return all testable classes within given package. Class is considered
* testable if it's superclass is CustomComponent.
*
* @param packageName
* @return
* @throws ClassNotFoundException
*/
public static List getTestableClassesForPackage(String packageName)
throws Exception {
ArrayList directories = new ArrayList();
try {
ClassLoader cld = Thread.currentThread().getContextClassLoader();
if (cld == null) {
throw new ClassNotFoundException("Can't get class loader.");
}
String path = packageName.replace('.', '/');
// Ask for all resources for the path
Enumeration resources = cld.getResources(path);
while (resources.hasMoreElements()) {
URL url = (URL) resources.nextElement();
directories.add(new File(url.getFile()));
}
} catch (Exception x) {
throw new Exception(packageName
+ " does not appear to be a valid package.");
}
ArrayList classes = new ArrayList();
// For every directory identified capture all the .class files
for (Iterator it = directories.iterator(); it.hasNext();) {
File directory = (File) it.next();
if (directory.exists()) {
// Get the list of the files contained in the package
String[] files = directory.list();
for (int j = 0; j < files.length; j++) {
// we are only interested in .class files
if (files[j].endsWith(".class")) {
// removes the .class extension
String p = packageName + '.'
+ files[j].substring(0, files[j].length() - 6);
Class c = Class.forName(p);
if (c.getSuperclass() != null) {
if ((c.getSuperclass()
.equals(com.itmill.toolkit.ui.CustomComponent.class))) {
classes.add(c);
}
}
}
}
}
}
return classes;
}
}
|