KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > AbstractMethodsTest


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 package org.netbeans.modules.javacore.jmiimpl;
20
21 import java.io.PrintStream JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.lang.reflect.Modifier JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.TreeSet JavaDoc;
27 import javax.jmi.reflect.RefPackage;
28 import junit.textui.TestRunner;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.junit.NbTestSuite;
31 import org.netbeans.mdr.handlers.BaseObjectHandler;
32 import org.netbeans.mdr.persistence.StorageException;
33 import org.netbeans.mdr.storagemodel.StorableBaseObject;
34 import org.netbeans.mdr.storagemodel.StorableClass;
35 import org.netbeans.mdr.storagemodel.TransientStorableObject;
36 import org.netbeans.modules.javacore.api.JavaModel;
37
38 /** Test that goes through all the implementations generated from
39  * the JMI metamodel and checks whether they are
40  *
41  * @author Martin Matula
42  */

43 public class AbstractMethodsTest extends NbTestCase {
44     private Set JavaDoc methods;
45     
46     public static NbTestSuite suite() {
47         return new NbTestSuite(AbstractMethodsTest.class);
48     }
49     
50     /** Use for execution inside IDE */
51     public static void main(java.lang.String JavaDoc[] args) {
52         TestRunner.run(suite());
53     }
54
55     /** Creates a new instance of AbstractMethodsTest */
56     public AbstractMethodsTest(String JavaDoc name) {
57         super(name);
58     }
59     
60     public void setUp() {
61         methods = new TreeSet JavaDoc();
62     }
63     
64     public void testAllMethodsAreImplemented() {
65         checkMetamodel(JavaModel.getDefaultExtent());
66         if (!methods.isEmpty()) {
67             PrintStream JavaDoc log = getLog();
68             for (Iterator JavaDoc it = methods.iterator(); it.hasNext();) {
69                 log.println(it.next());
70             }
71             assertTrue(methods.size() + " abstract methods found. See log for details.", false);
72         }
73     }
74     
75     private void checkMetamodel(RefPackage pkg) {
76         try {
77             for (Iterator JavaDoc it = pkg.refAllPackages().iterator(); it.hasNext();) {
78                 Object JavaDoc obj = it.next();
79                 checkMetamodel((RefPackage) obj);
80                 checkObject(obj);
81             }
82             for (Iterator JavaDoc it = pkg.refAllAssociations().iterator(); it.hasNext();) {
83                 checkObject(it.next());
84             }
85             for (Iterator JavaDoc it = pkg.refAllClasses().iterator(); it.hasNext();) {
86                 Object JavaDoc obj = it.next();
87                 checkObject(obj);
88                 checkInstance(obj);
89             }
90         } catch (StorageException e) {
91             throw new RuntimeException JavaDoc(e);
92         }
93     }
94     
95     private void checkObject(Object JavaDoc obj) throws StorageException {
96         StorableBaseObject storable = ((BaseObjectHandler) obj)._getDelegate();
97         checkObject(storable);
98     }
99     
100     private void checkObject(StorableBaseObject storable) {
101         Class JavaDoc ifc = BaseObjectHandler.resolveClass(storable);
102         checkClass(BaseObjectHandler.getHandlerClass(ifc, storable));
103     }
104     
105     private void checkInstance(Object JavaDoc obj) throws StorageException {
106         StorableClass storable = (StorableClass) ((BaseObjectHandler) obj)._getDelegate();
107         
108         if (!storable.isAbstract()) {
109             checkObject(new TransientStorableObject(storable.getMdrStorage(), storable.getImmediatePackageId(), storable.getMetaObjectId(), storable.getMofId()));
110         }
111     }
112     
113     private void checkClass(Class JavaDoc cls) {
114         try {
115             Method JavaDoc[] methods = cls.getMethods();
116             for (int i = 0; i < methods.length; i++) {
117                 if (Modifier.isAbstract(methods[i].getModifiers())) {
118                     this.methods.add(cls.getName() + "." + getMethodSignature(methods[i]));
119                 }
120             }
121         } catch (SecurityException JavaDoc e) {
122             throw new RuntimeException JavaDoc(e);
123         }
124     }
125     
126     private static String JavaDoc getMethodSignature(Method JavaDoc method) {
127         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
128         result.append(method.getName() + '(');
129         Class JavaDoc[] paramTypes = method.getParameterTypes();
130         for (int i = 0; i < paramTypes.length; i++) {
131             result.append(paramTypes[i].getName());
132             if (i < paramTypes.length - 1) {
133                 result.append(", ");
134             }
135         }
136         result.append(')');
137         return result.toString();
138     }
139 }
140
Popular Tags