1 17 package org.apache.geronimo.gbean; 18 19 import java.util.Properties ; 20 import java.util.HashSet ; 21 import java.util.Set ; 22 import java.util.LinkedHashMap ; 23 import java.util.Map ; 24 import java.util.Comparator ; 25 import java.rmi.MarshalledObject ; 26 27 import junit.framework.TestCase; 28 29 32 public class GBeanNameTest extends TestCase { 33 private Properties props; 34 35 public void testPropertyConstruction() { 36 String domain = "testDomain"; 37 Map props = new LinkedHashMap (); 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 () { 75 public int compare(Object o1, Object o2) { 76 return ((String )o1).compareTo(o2); 77 } 78 })); 79 assertEquals("testDomain:prop3=value3,prop2=value2,prop1=value1", name.toString(new Comparator () { 80 public int compare(Object o1, Object o2) { 81 return - ((String )o1).compareTo(o2); 82 } 83 })); 84 } 85 86 public void testInvalidNames() { 87 try { 88 new GBeanName((String ) null); 89 fail(); 90 } catch (NullPointerException e) { 91 } 92 try { 93 new GBeanName(""); 94 fail(); 95 } catch (IllegalArgumentException e) { 96 } 97 try { 98 new GBeanName("foo=bar"); 99 fail(); 100 } catch (IllegalArgumentException e) { 101 } 102 try { 103 new GBeanName("x:"); 104 fail(); 105 } catch (IllegalArgumentException e) { 106 } 107 try { 108 new GBeanName("x: "); 109 fail(); 110 } catch (IllegalArgumentException e) { 111 } 112 try { 113 new GBeanName("x:foo"); 114 fail(); 115 } catch (IllegalArgumentException e) { 116 } 117 try { 118 new GBeanName("x:x=x,foo"); 119 fail(); 120 } catch (IllegalArgumentException e) { 121 } 122 try { 123 new GBeanName("x:x=x,").toString(); 124 fail(); 125 } catch (IllegalArgumentException e) { 126 } 127 try { 128 new GBeanName("x:x=x, ").toString(); 129 fail(); 130 } catch (IllegalArgumentException e) { 131 } 132 try { 133 new GBeanName("x:,x=x"); 134 fail(); 135 } catch (IllegalArgumentException e) { 136 } 137 try { 138 new GBeanName("x:x=x,,y=y"); 139 fail(); 140 } catch (IllegalArgumentException e) { 141 } 142 try { 143 new GBeanName("x:x=x,x=x"); 144 fail(); 145 } catch (IllegalArgumentException 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 set = new HashSet (); 160 set.add(name); 161 set.add(name); 162 assertEquals(1, set.size()); 163 } 164 165 public void testSerialization() throws Exception { 166 GBeanName name = new GBeanName("testDomain:prop1=value1,prop2=value2"); 167 MarshalledObject o = new MarshalledObject (name); 168 GBeanName name2 = (GBeanName) o.get(); 169 assertEquals(name, name2); 170 } 171 172 protected void setUp() throws Exception { 173 props = new Properties (); 174 } 175 } 176 | Popular Tags |