KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > system > metadata > test > AbstractMetaDataTest


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.system.metadata.test;
23
24 import java.io.IOException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.List JavaDoc;
29
30 import javax.management.ObjectName JavaDoc;
31
32 import junit.framework.AssertionFailedError;
33
34 import org.jboss.dependency.spi.ControllerState;
35 import org.jboss.mx.util.ObjectNameFactory;
36 import org.jboss.system.metadata.ServiceAttributeMetaData;
37 import org.jboss.system.metadata.ServiceConstructorMetaData;
38 import org.jboss.system.metadata.ServiceDependencyListValueMetaData;
39 import org.jboss.system.metadata.ServiceDependencyMetaData;
40 import org.jboss.system.metadata.ServiceDependencyValueMetaData;
41 import org.jboss.system.metadata.ServiceElementValueMetaData;
42 import org.jboss.system.metadata.ServiceInjectionValueMetaData;
43 import org.jboss.system.metadata.ServiceJBXBValueMetaData;
44 import org.jboss.system.metadata.ServiceJavaBeanValueMetaData;
45 import org.jboss.system.metadata.ServiceMetaData;
46 import org.jboss.system.metadata.ServiceTextValueMetaData;
47 import org.jboss.system.metadata.ServiceValueMetaData;
48 import org.jboss.test.AbstractSystemTest;
49 import org.jboss.test.AbstractTestDelegate;
50 import org.w3c.dom.Element JavaDoc;
51 import org.w3c.dom.Node JavaDoc;
52 import org.w3c.dom.NodeList JavaDoc;
53
54 /**
55  * A MetaDataTest.
56  *
57  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
58  * @version $Revision: 1.2 $
59  */

60 public class AbstractMetaDataTest extends AbstractSystemTest
61 {
62    protected static ObjectName JavaDoc testBasicMBeanName = ObjectNameFactory.create("jboss.test:type=BasicMBeanName");
63    protected static String JavaDoc testBasicMBeanCode = "BasicMBeanCode";
64    protected static String JavaDoc testBasicMBeanInterface = "BasicMBeanInterface";
65    protected static ObjectName JavaDoc TEST1 = ObjectNameFactory.create("test:test=1");
66    protected static ObjectName JavaDoc TEST2 = ObjectNameFactory.create("test:test=2");
67    protected static ObjectName JavaDoc[] NO_OBJECT_NAMES = new ObjectName JavaDoc[0];
68    
69    /**
70     * Create a new ContainerTest.
71     *
72     * @param name the test name
73     */

74    public AbstractMetaDataTest(String JavaDoc name)
75    {
76       super(name);
77    }
78    
79    /**
80     * Default setup with security manager enabled
81     *
82     * @param clazz the class
83     * @return the delegate
84     * @throws Exception for any error
85     */

86    public static AbstractTestDelegate getDelegate(Class JavaDoc clazz) throws Exception JavaDoc
87    {
88       MetaDataTestDelegate delegate = new MetaDataTestDelegate(clazz);
89       delegate.enableSecurity = true;
90       return delegate;
91    }
92    
93    /**
94     * Unmarshal some xml
95     *
96     * @param name the name
97     * @return the list of services
98     * @throws Exception for any error
99     */

100    protected List JavaDoc<ServiceMetaData> unmarshal(String JavaDoc name) throws Exception JavaDoc
101    {
102       URL JavaDoc url = findXML(name);
103       return getMetaDataDelegate().unmarshal(url);
104    }
105    
106    /**
107     * Unmarshal a single mbean
108     *
109     * @return the single service
110     * @throws Exception for any error
111     */

112    protected ServiceMetaData unmarshalSingleMBean() throws Exception JavaDoc
113    {
114       String JavaDoc name = getName();
115       name = name.substring(4) + ".xml";
116       return unmarshalSingleMBean(name);
117    }
118    
119    /**
120     * Unmarshal a single mbean
121     *
122     * @param name the name
123     * @return the single service
124     * @throws Exception for any error
125     */

126    protected ServiceMetaData unmarshalSingleMBean(String JavaDoc name) throws Exception JavaDoc
127    {
128       URL JavaDoc url = findXML(name);
129       List JavaDoc<ServiceMetaData> services = getMetaDataDelegate().unmarshal(url);
130       assertFalse(url + " should contain an mbean ", services.isEmpty());
131       ServiceMetaData service = services.get(services.size()-1);
132       assertNotNull(service);
133       return service;
134    }
135    
136    protected void assertFailUnmarshal(Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
137    {
138       String JavaDoc name = getName();
139       name = name.substring(4) + ".xml";
140       assertFailUnmarshal(name, expected);
141    }
142    
143    protected void assertFailUnmarshal(String JavaDoc name, Class JavaDoc<? extends Throwable JavaDoc> expected) throws Exception JavaDoc
144    {
145       try
146       {
147          unmarshal(name);
148          fail("Should fail to unmarshal " + name);
149       }
150       catch (AssertionFailedError e)
151       {
152          throw e;
153       }
154       catch (Throwable JavaDoc error)
155       {
156          AbstractSystemTest.checkThrowableDeep(expected, error);
157       }
158    }
159    
160    /**
161     * Find the xml
162     *
163     * @param name the name
164     * @return the url of the xml
165     */

166    protected URL JavaDoc findXML(String JavaDoc name) throws Exception JavaDoc
167    {
168       URL JavaDoc url = getResource(name);
169       if (url == null)
170          throw new IOException JavaDoc(name + " not found");
171       return url;
172    }
173
174    protected MetaDataTestDelegate getMetaDataDelegate()
175    {
176       return (MetaDataTestDelegate) getDelegate();
177    }
178    
179    protected void assertDefaultConstructor(ServiceMetaData metaData) throws Exception JavaDoc
180    {
181       ServiceConstructorMetaData constructor = metaData.getConstructor();
182       assertNotNull(constructor);
183       String JavaDoc[] signature = constructor.getSignature();
184       assertNotNull(signature);
185       assertEquals(0, signature.length);
186       String JavaDoc[] params = constructor.getParams();
187       assertNotNull(params);
188       assertEquals(0, params.length);
189    }
190    
191    protected void assertConstructor(String JavaDoc[] expectedSignature, String JavaDoc[] expectedParams, ServiceMetaData metaData) throws Exception JavaDoc
192    {
193       ServiceConstructorMetaData constructor = metaData.getConstructor();
194       assertNotNull(constructor);
195       String JavaDoc[] signature = constructor.getSignature();
196       assertNotNull(signature);
197       assertEquals(expectedSignature, signature);
198       String JavaDoc[] params = constructor.getParams();
199       assertNotNull(params);
200       assertEquals(expectedParams, params);
201    }
202    
203    protected void assertNoAttributes(ServiceMetaData metaData) throws Exception JavaDoc
204    {
205       List JavaDoc<ServiceAttributeMetaData> attributes = metaData.getAttributes();
206       assertNotNull(attributes);
207       assertEquals(0, attributes.size());
208    }
209
210    protected void assertAttributeName(ServiceAttributeMetaData attribute, String JavaDoc name) throws Exception JavaDoc
211    {
212       assertNotNull(attribute);
213       String JavaDoc result = attribute.getName();
214       assertNotNull(result);
215       assertEquals("Expected attribute with name " + name + " got " + result, name, result);
216    }
217
218    protected void assertAttribute(ServiceMetaData metaData, String JavaDoc name) throws Exception JavaDoc
219    {
220       assertNotNull(metaData);
221       List JavaDoc<ServiceAttributeMetaData> attributes = metaData.getAttributes();
222       assertNotNull(attributes);
223       assertEquals(1, attributes.size());
224       ServiceAttributeMetaData attribute = attributes.get(0);
225       assertAttributeName(attribute, name);
226    }
227
228    protected void assertAttributes(ServiceMetaData metaData, String JavaDoc[] names) throws Exception JavaDoc
229    {
230       assertNotNull(names);
231       assertNotNull(metaData);
232       List JavaDoc<ServiceAttributeMetaData> attributes = metaData.getAttributes();
233       assertNotNull(attributes);
234       assertEquals(names.length, attributes.size());
235       ServiceAttributeMetaData attribute = attributes.get(0);
236       for (String JavaDoc name : names)
237          assertAttributeName(attribute, name);
238    }
239    
240    protected void assertAttribute(ServiceAttributeMetaData attribute, boolean trim, boolean replace, String JavaDoc name) throws Exception JavaDoc
241    {
242       assertNotNull(attribute);
243       String JavaDoc result = attribute.getName();
244       assertNotNull(result);
245       assertEquals(name, result);
246       assertEquals(trim, attribute.isTrim());
247       assertEquals(replace, attribute.isReplace());
248    }
249
250    protected void assertTextAttribute(ServiceAttributeMetaData attribute) throws Exception JavaDoc
251    {
252       assertTextAttribute(attribute, true, true, "Attribute", "value");
253    }
254
255    protected void assertTextAttribute(ServiceAttributeMetaData attribute, String JavaDoc value) throws Exception JavaDoc
256    {
257       assertTextAttribute(attribute, true, true, "Attribute", value);
258    }
259
260    protected void assertTextAttribute(ServiceAttributeMetaData attribute, String JavaDoc name, String JavaDoc value) throws Exception JavaDoc
261    {
262       assertTextAttribute(attribute, true, true, name, value);
263    }
264
265    protected void assertTextAttribute(ServiceAttributeMetaData attribute, boolean trim, boolean replace) throws Exception JavaDoc
266    {
267       assertTextAttribute(attribute, trim, replace, "Attribute", "value");
268    }
269
270    protected void assertTextAttribute(ServiceAttributeMetaData attribute, boolean trim, boolean replace, String JavaDoc name, String JavaDoc value) throws Exception JavaDoc
271    {
272       assertAttribute(attribute, trim, replace, name);
273       ServiceValueMetaData actual = attribute.getValue();
274       assertNotNull(actual);
275       assertTextValue(actual, value);
276    }
277
278    protected void assertDependsAttribute(ServiceAttributeMetaData attribute) throws Exception JavaDoc
279    {
280       assertDependsAttribute(attribute, false, false, "Attribute", TEST1);
281    }
282
283    protected void assertDependsAttribute(ServiceAttributeMetaData attribute, String JavaDoc name, ObjectName JavaDoc value) throws Exception JavaDoc
284    {
285       assertDependsAttribute(attribute, false, false, name, value);
286    }
287
288    protected void assertDependsAttribute(ServiceAttributeMetaData attribute, boolean trim, boolean replace, String JavaDoc name, ObjectName JavaDoc value) throws Exception JavaDoc
289    {
290       assertAttribute(attribute, trim, replace, name);
291       ServiceValueMetaData actual = attribute.getValue();
292       assertNotNull(actual);
293       assertDependencyValue(actual, value);
294    }
295
296    protected void assertDependsListAttribute(ServiceAttributeMetaData attribute) throws Exception JavaDoc
297    {
298       assertDependsListAttribute(attribute, false, false, "Attribute", TEST1);
299    }
300
301    protected void assertDependsListAttribute(ServiceAttributeMetaData attribute, String JavaDoc name, ObjectName JavaDoc value) throws Exception JavaDoc
302    {
303       assertDependsListAttribute(attribute, false, false, name, value);
304    }
305
306    protected void assertDependsListAttribute(ServiceAttributeMetaData attribute, boolean trim, boolean replace, String JavaDoc name, ObjectName JavaDoc value) throws Exception JavaDoc
307    {
308       assertDependsListAttribute(attribute, trim, replace, name, new ObjectName JavaDoc[] { value });
309    }
310
311    protected void assertDependsListAttribute(ServiceAttributeMetaData attribute, boolean trim, boolean replace, String JavaDoc name, ObjectName JavaDoc[] value) throws Exception JavaDoc
312    {
313       assertAttribute(attribute, trim, replace, name);
314       ServiceValueMetaData actual = attribute.getValue();
315       assertNotNull(actual);
316       assertDependencyListValue(actual, value);
317    }
318
319    protected void assertDependsListAttributeEmpty(ServiceAttributeMetaData attribute) throws Exception JavaDoc
320    {
321       assertDependsListAttribute(attribute, false, false, "Attribute", NO_OBJECT_NAMES);
322    }
323    
324    protected void assertTextValue(ServiceValueMetaData actual, String JavaDoc expected) throws Exception JavaDoc
325    {
326       assertNotNull(expected);
327       assertNotNull(actual);
328       
329       ServiceTextValueMetaData value = assertInstanceOf(ServiceTextValueMetaData.class, actual);
330       
331       assertEquals(expected, value.getText());
332    }
333    
334    protected void assertDependencyValue(ServiceValueMetaData actual, ObjectName JavaDoc expected) throws Exception JavaDoc
335    {
336       assertDependencyValue(actual, expected, null);
337    }
338    
339    protected void assertDependencyValue(ServiceValueMetaData actual, ObjectName JavaDoc expected, String JavaDoc proxyType) throws Exception JavaDoc
340    {
341       assertNotNull(expected);
342       assertNotNull(actual);
343       
344       ServiceDependencyValueMetaData value = assertInstanceOf(ServiceDependencyValueMetaData.class, actual);
345       
346       assertEquals(expected, value.getObjectName());
347       assertEquals(proxyType, value.getProxyType());
348    }
349    
350    protected void assertDependencyListValueEmpty(ServiceValueMetaData actual) throws Exception JavaDoc
351    {
352       assertDependencyListValue(actual, NO_OBJECT_NAMES );
353    }
354    
355    protected void assertDependencyListValue(ServiceValueMetaData actual, ObjectName JavaDoc expected) throws Exception JavaDoc
356    {
357       assertDependencyListValue(actual, new ObjectName JavaDoc[] { expected } );
358    }
359    
360    protected void assertDependencyListValue(ServiceValueMetaData actual, ObjectName JavaDoc[] expected) throws Exception JavaDoc
361    {
362       assertNotNull(expected);
363       assertNotNull(actual);
364       
365       ServiceDependencyListValueMetaData value = assertInstanceOf(ServiceDependencyListValueMetaData.class, actual);
366       
367       List JavaDoc<ObjectName JavaDoc> list = value.getObjectNames();
368       assertNotNull(list);
369       ObjectName JavaDoc[] test = list.toArray(new ObjectName JavaDoc[list.size()]);
370       
371       assertTrue("Expected " + Arrays.asList(expected) + " got " + Arrays.asList(test), Arrays.equals(expected, test));
372    }
373    
374    protected void assertElementValue(ServiceValueMetaData actual, String JavaDoc expected) throws Exception JavaDoc
375    {
376       assertNotNull(expected);
377       assertNotNull(actual);
378       
379       ServiceElementValueMetaData value = assertInstanceOf(ServiceElementValueMetaData.class, actual);
380       
381       Element JavaDoc element = value.getElement();
382       assertNotNull(element);
383       
384       String JavaDoc name = element.getTagName();
385       assertNotNull(name);
386       
387       assertEquals(expected, name);
388    }
389    
390    protected void assertJavaBeanValue(ServiceValueMetaData actual, String JavaDoc expected) throws Exception JavaDoc
391    {
392       assertNotNull(actual);
393
394       ServiceJavaBeanValueMetaData value = assertInstanceOf(ServiceJavaBeanValueMetaData.class, actual);
395       
396       Element JavaDoc element = value.getElement();
397       assertChildOfAttribute(element, expected);
398    }
399    
400    protected void assertJBXBValue(ServiceValueMetaData actual, String JavaDoc expected) throws Exception JavaDoc
401    {
402       assertNotNull(actual);
403
404       ServiceJBXBValueMetaData value = assertInstanceOf(ServiceJBXBValueMetaData.class, actual);
405       
406       Element JavaDoc element = value.getElement();
407       assertChildOfAttribute(element, expected);
408    }
409    
410    protected void assertInjectValue(ServiceValueMetaData actual, Object JavaDoc dependency, String JavaDoc property) throws Exception JavaDoc
411    {
412       assertInjectValue(actual, dependency, property, ControllerState.INSTALLED);
413    }
414    
415    protected void assertInjectValue(ServiceValueMetaData actual, Object JavaDoc dependency, String JavaDoc property, ControllerState requiredState) throws Exception JavaDoc
416    {
417       assertNotNull(actual);
418
419       ServiceInjectionValueMetaData value = assertInstanceOf(ServiceInjectionValueMetaData.class, actual);
420       
421       assertEquals(dependency, value.getDependency());
422       assertEquals(property, value.getProperty());
423       assertEquals(requiredState, value.getDependentState());
424    }
425    
426    protected void assertChildOfAttribute(Element JavaDoc element, String JavaDoc expected) throws Exception JavaDoc
427    {
428       assertNotNull(element);
429       
430       String JavaDoc name = element.getTagName();
431       assertEquals("attribute", name);
432       
433       NodeList JavaDoc children = element.getChildNodes();
434       assertEquals(1, children.getLength());
435       Node JavaDoc node = children.item(0);
436       element = assertInstanceOf(Element JavaDoc.class, node);
437       name = element.getTagName();
438       assertEquals(expected, name);
439    }
440    
441    protected void assertNoDependencies(ServiceMetaData metaData) throws Exception JavaDoc
442    {
443       List JavaDoc<ServiceDependencyMetaData> dependencies = metaData.getDependencies();
444       assertNotNull(dependencies);
445       assertEquals(0, dependencies.size());
446    }
447    
448    protected void assertDependencies(ServiceMetaData metaData, ObjectName JavaDoc[] expected) throws Exception JavaDoc
449    {
450       List JavaDoc<ServiceDependencyMetaData> dependencies = metaData.getDependencies();
451       assertNotNull(dependencies);
452       assertEquals(expected.length, dependencies.size());
453       HashSet JavaDoc<ObjectName JavaDoc> expectedSet = new HashSet JavaDoc<ObjectName JavaDoc>();
454       for (ObjectName JavaDoc expect : expected)
455          expectedSet.add(expect);
456       HashSet JavaDoc<ObjectName JavaDoc> actual = new HashSet JavaDoc<ObjectName JavaDoc>();
457       for (ServiceDependencyMetaData depends : dependencies)
458          actual.add(depends.getIDependOnObjectName());
459       assertEquals(expectedSet, actual);
460    }
461    
462    protected void assertNoXMBean(ServiceMetaData metaData) throws Exception JavaDoc
463    {
464       assertNull(metaData.getXMBeanDD());
465       assertEquals(ServiceMetaData.XMBEAN_CODE, metaData.getXMBeanCode());
466       assertNull(metaData.getXMBeanDescriptor());
467    }
468 }
469
Popular Tags