KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.netbeans.mdr.test;
21
22 import java.io.*;
23 import java.util.*;
24 import java.net.*;
25
26 import junit.extensions.*;
27 import junit.framework.*;
28
29 import org.netbeans.api.mdr.*;
30 import org.openide.util.Lookup;
31
32 import org.netbeans.mdr.util.*;
33 import org.netbeans.mdr.NBMDRepositoryImpl;
34 import org.netbeans.lib.jmi.xmi.*;
35 import org.netbeans.lib.jmi.mapping.*;
36
37 import javax.jmi.reflect.*;
38 import javax.jmi.model.*;
39
40 import staticfeatures.*;
41
42 /**
43  * Multivalued index test case.
44  */

45 public class StaticFeaturesTest extends MDRTestCase {
46
47     public StaticFeaturesTest(String JavaDoc testName) {
48         super (testName);
49     }
50     
51     public static void main (String JavaDoc[] args) {
52         junit.textui.TestRunner.run (suite ());
53     }
54     
55     public static Test suite () {
56         TestSuite suite = new TestSuite ();
57         suite.addTestSuite (StaticFeaturesTest.class);
58         
59         TestSetup setup = new TestSetup (suite) {
60             public void setUp () {
61             }
62             public void tearDown () {
63             }
64         };
65         return setup;
66     }
67     
68     // classifier-level attribute test
69
public void test_1 () {
70         ModelPackage modelPackage = loadMOFModel ("staticFeatures.xml", "model");
71         StaticFeaturesPackage pkg = (StaticFeaturesPackage) createExtent (findMofPackage (modelPackage, "StaticFeatures"), "staticFeatures");
72         
73         int x, y;
74         final int MAX = 100;
75         final int INT_VALUE = 678;
76         Random random = new Random (0);
77         
78         ClassAClass proxyA = pkg.getClassA (); // staticFieldA (String), staticFieldB (int)
79
ClassBClass proxyB = pkg.getClassB ();
80         ClassCClass proxyC = pkg.getClassC (); // staticFieldC (int), op. returnParam
81
ClassDClass proxyD = pkg.getClassD (); // staticFieldD (String), staticDerivedField (int), op. compute
82

83         if (proxyD.getStaticFieldD() != null)
84             fail ();
85         proxyD.setStaticFieldD("123");
86         proxyB.setStaticFieldA("abcdef");
87         
88         for (x = 0; x < MAX; x++) {
89             int value = random.nextInt ();
90             proxyD.setStaticFieldB (value);
91             if ((proxyA.getStaticFieldB () != value) || (proxyD.getStaticFieldB () != value))
92                 fail ();
93         }
94                 
95         proxyD.setStaticFieldC (INT_VALUE);
96         
97         if (!proxyD.getStaticFieldD().equals("123"))
98             fail();
99         if (!proxyB.getStaticFieldA().equals("abcdef"))
100             fail();
101         if (proxyD.getStaticFieldC() != INT_VALUE)
102             fail();
103                 
104         proxyD.setStaticFieldC (22);
105         if (!proxyB.getStaticFieldA().equals("abcdef"))
106             fail();
107         
108         pkg.refDelete ();
109         modelPackage.refDelete ();
110     }
111
112     // static operator test
113
public void test_2 () {
114         ModelPackage modelPackage = loadMOFModel ("staticFeatures.xml", "model");
115         StaticFeaturesPackage pkg = (StaticFeaturesPackage) createExtent (findMofPackage (modelPackage, "StaticFeatures"), "staticFeatures");
116                 
117         ClassAClass proxyA = pkg.getClassA (); // staticFieldA (String), staticFieldB (int)
118
ClassBClass proxyB = pkg.getClassB ();
119         ClassCClass proxyC = pkg.getClassC (); // staticFieldC (int), op. returnParam
120
ClassDClass proxyD = pkg.getClassD (); // staticFieldD (String), staticDerivedField (int), op. compute
121

122         ClassA objA = proxyA.createClassA ();
123         ClassB objB = proxyB.createClassB ();
124         ClassD objD = proxyD.createClassD ();
125         
126         if (!(proxyC.returnParam (objA) == objA)) // proxyC.returnParam (param) always returns 'param'
127
fail ();
128         if (!(proxyC.returnParam (objB) == objB))
129             fail ();
130         if (!(proxyC.returnParam (objD) == objD))
131             fail ();
132         
133         if (!(proxyD.returnParam (objA) == objA))
134             fail ();
135         if (!(proxyD.returnParam (objB) == objB))
136             fail ();
137         if (!(proxyD.returnParam (objD) == objD))
138             fail ();
139         
140         List params = new LinkedList ();
141         params.add (objD);
142         try {
143             proxyD.refInvokeOperation ("compute", params);
144         } catch (RefException e) {
145             fail();
146         }
147         
148         if (proxyD.compute(objD) != 333) // proxyD.compute always returns 333
149
fail();
150         
151         pkg.refDelete ();
152         modelPackage.refDelete ();
153     }
154     
155     // derived classifier-level attribute test
156
public void test_3 () {
157         ModelPackage modelPackage = loadMOFModel ("staticFeatures.xml", "model");
158         StaticFeaturesPackage pkg = (StaticFeaturesPackage) createExtent (findMofPackage (modelPackage, "StaticFeatures"), "staticFeatures");
159                         
160         ClassDClass proxyD = pkg.getClassD (); // staticFieldD (String), staticDerivedField (int), op. compute
161

162         if (proxyD.getStaticDerivedField() != 123)
163             fail();
164         
165         pkg.refDelete ();
166         modelPackage.refDelete ();
167     }
168     
169     // classifier-level attribute test using reflective methods
170
public void test_reflective () {
171         ModelPackage modelPackage = loadMOFModel ("staticFeatures.xml", "model");
172         StaticFeaturesPackage pkg = (StaticFeaturesPackage) createExtent (findMofPackage (modelPackage, "StaticFeatures"), "staticFeatures");
173         
174         int x, y;
175         final int MAX = 100;
176         final int INT_VALUE = 678;
177         Random random = new Random (0);
178         
179         RefClass proxyA = pkg.getClassA (); // staticFieldA (String), staticFieldB (int)
180
RefClass proxyB = pkg.getClassB ();
181         RefClass proxyC = pkg.getClassC (); // staticFieldC (int), op. returnParam
182
RefClass proxyD = pkg.getClassD (); // staticFieldD (String), staticDerivedField (int), op. compute
183

184         if (proxyD.refGetValue("staticFieldD") != null)
185             fail ();
186         proxyD.refSetValue("staticFieldD", "123");
187         proxyB.refSetValue("staticFieldA", "abcdef");
188         
189         for (x = 0; x < MAX; x++) {
190             int value = random.nextInt ();
191             proxyD.refSetValue("staticFieldB", new Integer JavaDoc(value));
192             if ( (((Integer JavaDoc)proxyA.refGetValue("staticFieldB")).intValue() != value) || (((Integer JavaDoc)proxyD.refGetValue("staticFieldB")).intValue() != value))
193                 fail ();
194         }
195                 
196         proxyD.refSetValue("staticFieldC", new Integer JavaDoc(INT_VALUE));
197         
198         if (!proxyD.refGetValue("staticFieldD").equals("123"))
199             fail();
200         if (!proxyB.refGetValue("staticFieldA").equals("abcdef"))
201             fail();
202         if (((Integer JavaDoc)proxyD.refGetValue("staticFieldC")).intValue() != INT_VALUE)
203             fail();
204                 
205         proxyD.refSetValue("staticFieldC", new Integer JavaDoc(22));
206         if (!proxyB.refGetValue("staticFieldA").equals("abcdef"))
207             fail();
208         
209         pkg.refDelete ();
210         modelPackage.refDelete ();
211     }
212 }
213
Popular Tags