KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > compliance > ComplianceTestCase


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.javax.management.compliance;
10
11 import java.io.IOException JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.util.jar.JarEntry JavaDoc;
17 import java.util.jar.JarFile JavaDoc;
18
19 import test.MX4JTestCase;
20
21 /**
22  * @version $Revision: 1.5 $
23  */

24 public abstract class ComplianceTestCase extends MX4JTestCase
25 {
26    public ComplianceTestCase(String JavaDoc s)
27    {
28       super(s);
29    }
30
31    public void testCompliance() throws Exception JavaDoc
32    {
33       ClassLoader JavaDoc loader = createClassLoader();
34
35       JarFile JavaDoc jar = loadJar();
36
37       Enumeration JavaDoc entries = jar.entries();
38       ArrayList JavaDoc nonExistingMethods = new ArrayList JavaDoc();
39       while (entries.hasMoreElements())
40       {
41          JarEntry JavaDoc entry = (JarEntry JavaDoc)entries.nextElement();
42
43          // Skip directories
44
if (entry.isDirectory()) continue;
45
46          // Skip Sun's implementation classes
47
String JavaDoc entryName = entry.getName();
48          if (entryName.startsWith("javax"))
49          {
50             // Take the class
51
String JavaDoc fullClassName = entryName.replace('/', '.');
52             fullClassName = fullClassName.substring(0, fullClassName.length() - ".class".length());
53
54             if (skipClassName(fullClassName)) continue;
55
56             Class JavaDoc cls = loader.loadClass(fullClassName);
57
58             if (skipClass(cls)) continue;
59
60             String JavaDoc name = fullClassName.substring("javax.management".length());
61             name = name.replace('.', '_');
62             try
63             {
64                // Verify that a method with this name exists
65
getClass().getMethod("test" + name, new Class JavaDoc[0]);
66             }
67             catch (NoSuchMethodException JavaDoc x)
68             {
69                nonExistingMethods.add(fullClassName);
70             }
71          }
72       }
73       Collections.sort(nonExistingMethods);
74       if (nonExistingMethods.size() > 0) fail("Compliance test incomplete, missing classes are:\n" + nonExistingMethods);
75    }
76
77    protected abstract boolean skipClassName(String JavaDoc className);
78
79    protected abstract boolean skipClass(Class JavaDoc cls);
80
81    protected abstract void checkCompliance(String JavaDoc className) throws Exception JavaDoc;
82
83    protected abstract ClassLoader JavaDoc createClassLoader() throws MalformedURLException JavaDoc;
84
85    protected abstract JarFile JavaDoc loadJar() throws IOException JavaDoc;
86
87    protected void check(String JavaDoc partialClassName) throws Exception JavaDoc
88    {
89       ClassLoader JavaDoc loader = createClassLoader();
90       String JavaDoc fullName = "javax.management." + partialClassName;
91       if (skipClassName(fullName)) return;
92       if (skipClass(loader.loadClass(fullName))) return;
93       checkCompliance(fullName);
94    }
95 }
96
Popular Tags