01: /*
02: * Copyright (C) The MX4J Contributors.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the MX4J License version 1.0.
06: * See the terms of the MX4J License in the documentation provided with this software.
07: */
08:
09: package test.javax.management.compliance;
10:
11: import java.io.IOException;
12: import java.net.MalformedURLException;
13: import java.util.ArrayList;
14: import java.util.Collections;
15: import java.util.Enumeration;
16: import java.util.jar.JarEntry;
17: import java.util.jar.JarFile;
18:
19: import test.MX4JTestCase;
20:
21: /**
22: * @version $Revision: 1.5 $
23: */
24: public abstract class ComplianceTestCase extends MX4JTestCase {
25: public ComplianceTestCase(String s) {
26: super (s);
27: }
28:
29: public void testCompliance() throws Exception {
30: ClassLoader loader = createClassLoader();
31:
32: JarFile jar = loadJar();
33:
34: Enumeration entries = jar.entries();
35: ArrayList nonExistingMethods = new ArrayList();
36: while (entries.hasMoreElements()) {
37: JarEntry entry = (JarEntry) entries.nextElement();
38:
39: // Skip directories
40: if (entry.isDirectory())
41: continue;
42:
43: // Skip Sun's implementation classes
44: String entryName = entry.getName();
45: if (entryName.startsWith("javax")) {
46: // Take the class
47: String fullClassName = entryName.replace('/', '.');
48: fullClassName = fullClassName.substring(0,
49: fullClassName.length() - ".class".length());
50:
51: if (skipClassName(fullClassName))
52: continue;
53:
54: Class cls = loader.loadClass(fullClassName);
55:
56: if (skipClass(cls))
57: continue;
58:
59: String name = fullClassName
60: .substring("javax.management".length());
61: name = name.replace('.', '_');
62: try {
63: // Verify that a method with this name exists
64: getClass().getMethod("test" + name, new Class[0]);
65: } catch (NoSuchMethodException x) {
66: nonExistingMethods.add(fullClassName);
67: }
68: }
69: }
70: Collections.sort(nonExistingMethods);
71: if (nonExistingMethods.size() > 0)
72: fail("Compliance test incomplete, missing classes are:\n"
73: + nonExistingMethods);
74: }
75:
76: protected abstract boolean skipClassName(String className);
77:
78: protected abstract boolean skipClass(Class cls);
79:
80: protected abstract void checkCompliance(String className)
81: throws Exception;
82:
83: protected abstract ClassLoader createClassLoader()
84: throws MalformedURLException;
85:
86: protected abstract JarFile loadJar() throws IOException;
87:
88: protected void check(String partialClassName) throws Exception {
89: ClassLoader loader = createClassLoader();
90: String fullName = "javax.management." + partialClassName;
91: if (skipClassName(fullName))
92: return;
93: if (skipClass(loader.loadClass(fullName)))
94: return;
95: checkCompliance(fullName);
96: }
97: }
|