KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gbean > GBeanNameTest


1 /**
2  *
3  * Copyright 2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.gbean;
18
19 import java.util.Properties JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.LinkedHashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.rmi.MarshalledObject JavaDoc;
26
27 import junit.framework.TestCase;
28
29 /**
30  * @version $Rev$ $Date$
31  */

32 public class GBeanNameTest extends TestCase {
33     private Properties JavaDoc props;
34
35     public void testPropertyConstruction() {
36         String JavaDoc domain = "testDomain";
37         Map JavaDoc props = new LinkedHashMap JavaDoc();
38         props.put("prop1", "value1");
39         props.put("prop2", "value2");
40         GBeanName name = new GBeanName(domain, props);
41         assertEquals("testDomain:prop1=value1,prop2=value2", name.toString());
42     }
43
44     public void testNameConstruction() {
45         GBeanName name = new GBeanName("testDomain:prop1=value1,prop2=value2");
46         assertEquals("testDomain:prop1=value1,prop2=value2", name.toString());
47
48         name = new GBeanName("testDomain:prop2=value2,prop1=value1");
49         assertEquals("testDomain:prop2=value2,prop1=value1", name.toString());
50     }
51
52     public void testMatches() {
53         GBeanName name = new GBeanName("testDomain:prop1=value1,prop2=value2");
54         assertTrue(name.matches(null, null));
55         assertTrue(name.matches(null, props));
56         assertTrue(name.matches("testDomain", null));
57         assertTrue(name.matches("testDomain", props));
58
59         assertFalse(name.matches("test", null));
60         assertFalse(name.matches("test", props));
61
62         props.setProperty("prop1", "value2");
63         assertFalse(name.matches("testDomain", props));
64         props.setProperty("prop1", "value1");
65         assertTrue(name.matches("testDomain", props));
66         props.setProperty("prop2", "value2");
67         assertTrue(name.matches("testDomain", props));
68         props.setProperty("prop3", "value3");
69         assertFalse(name.matches("testDomain", props));
70     }
71
72     public void testStringForms() {
73         GBeanName name = new GBeanName("testDomain:prop2=value2,prop3=value3,prop1=value1");
74         assertEquals("testDomain:prop1=value1,prop2=value2,prop3=value3", name.toString(new Comparator JavaDoc() {
75             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
76                 return ((String JavaDoc)o1).compareTo(o2);
77             }
78         }));
79         assertEquals("testDomain:prop3=value3,prop2=value2,prop1=value1", name.toString(new Comparator JavaDoc() {
80             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
81                 return - ((String JavaDoc)o1).compareTo(o2);
82             }
83         }));
84     }
85
86     public void testInvalidNames() {
87         try {
88             new GBeanName((String JavaDoc) null);
89             fail();
90         } catch (NullPointerException JavaDoc e) {
91         }
92         try {
93             new GBeanName("");
94             fail();
95         } catch (IllegalArgumentException JavaDoc e) {
96         }
97         try {
98             new GBeanName("foo=bar");
99             fail();
100         } catch (IllegalArgumentException JavaDoc e) {
101         }
102         try {
103             new GBeanName("x:");
104             fail();
105         } catch (IllegalArgumentException JavaDoc e) {
106         }
107         try {
108             new GBeanName("x: ");
109             fail();
110         } catch (IllegalArgumentException JavaDoc e) {
111         }
112         try {
113             new GBeanName("x:foo");
114             fail();
115         } catch (IllegalArgumentException JavaDoc e) {
116         }
117         try {
118             new GBeanName("x:x=x,foo");
119             fail();
120         } catch (IllegalArgumentException JavaDoc e) {
121         }
122         try {
123             new GBeanName("x:x=x,").toString();
124             fail();
125         } catch (IllegalArgumentException JavaDoc e) {
126         }
127         try {
128             new GBeanName("x:x=x, ").toString();
129             fail();
130         } catch (IllegalArgumentException JavaDoc e) {
131         }
132         try {
133             new GBeanName("x:,x=x");
134             fail();
135         } catch (IllegalArgumentException JavaDoc e) {
136         }
137         try {
138             new GBeanName("x:x=x,,y=y");
139             fail();
140         } catch (IllegalArgumentException JavaDoc e) {
141         }
142         try {
143             new GBeanName("x:x=x,x=x");
144             fail();
145         } catch (IllegalArgumentException JavaDoc e) {
146         }
147     }
148
149     public void testEquals() {
150         GBeanName name = new GBeanName("testDomain:prop1=value1,prop2=value2");
151         assertEquals(name, name);
152         assertEquals(new GBeanName("testDomain:prop2=value2,prop1=value1"), name);
153         assertFalse(name.equals(new GBeanName("foo:prop1=value1,prop2=value2")));
154         assertFalse(name.equals(new GBeanName("testDomain:prop1=value1")));
155         assertFalse(name.equals(new GBeanName("testDomain:prop2=value2")));
156         assertFalse(name.equals(new GBeanName("testDomain:prop2=value2")));
157         assertFalse(name.equals(new GBeanName("testDomain:prop1=value1,prop2=value2,prop3=value3")));
158
159         Set JavaDoc set = new HashSet JavaDoc();
160         set.add(name);
161         set.add(name);
162         assertEquals(1, set.size());
163     }
164
165     public void testSerialization() throws Exception JavaDoc {
166         GBeanName name = new GBeanName("testDomain:prop1=value1,prop2=value2");
167         MarshalledObject JavaDoc o = new MarshalledObject JavaDoc(name);
168         GBeanName name2 = (GBeanName) o.get();
169         assertEquals(name, name2);
170     }
171
172     protected void setUp() throws Exception JavaDoc {
173         props = new Properties JavaDoc();
174     }
175 }
176
Popular Tags