KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > test > InterceptionTest


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.mdr.test;
20
21 import java.util.*;
22 import junit.framework.*;
23
24 import org.netbeans.api.mdr.*;
25
26 import javax.jmi.reflect.*;
27 import javax.jmi.model.*;
28
29 /**
30  *
31  * @author Dusan Balek
32  */

33 public class InterceptionTest extends MDRTestCase {
34
35     private static final String JavaDoc MODEL_XMI = "component.xml";
36     private static final String JavaDoc MODEL_PKG_NAME = "InterceptionTest";
37     private static final String JavaDoc PKG_ELEMENT = "Test";
38     private static final String JavaDoc PKG_NAME = "InterceptionExtent";
39
40     public static short componentPreSet = 0;
41     public static short componentPostSet = 0;
42     public static boolean componentPreSetVersion = false;
43     public static short servicePreSet = 0;
44     public static short servicePostSet = 0;
45     public static boolean providesPreAdd = false;
46     public static boolean providesPostAdd = false;
47     
48     /** Creates a new instance of InterceptionTest */
49     public InterceptionTest(String JavaDoc testName) {
50         super(testName);
51     }
52
53     public static Test suite() {
54         return new TestSuite(InterceptionTest.class);
55     }
56     
57     public static void main(String JavaDoc[] args) {
58         junit.textui.TestRunner.run(suite());
59     }
60     
61     public void test() {
62         // Init
63
ModelPackage pkg = this.loadMOFModel(MODEL_XMI, MODEL_PKG_NAME);
64         // Create extent
65
test.TestPackage testPkg = (test.TestPackage) createExtent(findMofPackage(pkg, PKG_ELEMENT), PKG_NAME);
66         if (testPkg == null)
67             fail("Extent creation failed.");
68
69         test.ComponentClass cClass = testPkg.getComponent();
70         test.Component comp = cClass.createComponent("A", 1);
71
72         if (comp == null)
73             fail("Object creation failed.");
74         
75         test.Component testComp = cClass.createComponent("A", 2);
76         if (testComp != null)
77             fail("Component creation interception failed.");
78         
79         ArrayList list = new ArrayList(2);
80         list.add("A");
81         list.add(new Integer JavaDoc(3));
82         testComp = (test.Component)cClass.refCreateInstance(list);
83         if (testComp != null)
84             fail("Component creation interception failed.");
85
86         list = new ArrayList(2);
87         list.add("A");
88         try {
89             cClass.refCreateInstance(list);
90         } catch (WrongSizeException tme) {
91             System.out.println("---> WrongSizeException is OK !!!");
92         }
93         list.add("B");
94         try {
95             cClass.refCreateInstance(list);
96         } catch (TypeMismatchException tme) {
97             System.out.println("---> TypeMismatchException is OK !!!");
98         }
99         
100
101         // Attribute test
102
comp.setName("X");
103         if (! comp.getName().equals("X"))
104             fail("Invalid attribute value");
105         comp.setName("TestObjectInterceptor");
106         if (! comp.getName().equals("X"))
107             fail("Setting component attribute interception failed.");
108         comp.refSetValue("name", "TestObjectInterceptor");
109         if (! comp.getName().equals("X"))
110             fail("Setting component attribute via reflective interface interception failed.");
111
112         // Association test
113
test.ServiceClass sClass = testPkg.getService();
114         test.Service serv = sClass.createService("TestAssociationInterceptor");
115         testPkg.getProvides().add(serv, comp);
116         Collection c = comp.getProvidedService();
117         if (c.size() != 0)
118             fail("Association interceptor failed (C->S)");
119         c = serv.getProvider();
120         if (c.size() != 0)
121             fail("Association interceptor failed (S->C)");
122         testPkg.getProvides().refAddLink(serv, comp);
123         c = comp.getProvidedService();
124         if (c.size() != 0)
125             fail("Association reflactive interceptor failed (C->S)");
126         c = serv.getProvider();
127         if (c.size() != 0)
128             fail("Association reflective interceptor failed (S->C)");
129         comp.getProvidedService().add(serv);
130         c = comp.getProvidedService();
131         if (c.size() != 1)
132             fail("Adding association failed (C->S)");
133         c = serv.getProvider();
134         if (c.size() != 1)
135             fail("Adding association failed (S->C)");
136         
137     }
138 }
139
Popular Tags