1 22 package org.jboss.test.jbossmx.compliance.objectname; 23 24 import junit.framework.Test; 25 import junit.framework.TestSuite; 26 import org.jboss.test.jbossmx.compliance.TestCase; 27 28 import javax.management.MalformedObjectNameException ; 29 import javax.management.ObjectName ; 30 import java.util.Hashtable ; 31 32 43 public class MalformedTestCase 44 extends TestSuite 45 { 46 public static final String GOOD_DOMAIN = "domain"; 47 public static final String GOOD_KEY = "key1"; 48 public static final String GOOD_VALUE = "val1"; 49 50 public static final String [] BAD_KEYVALS = { 52 "som:thing", "som?thing", "som*thing", "som,thing", "som=thing", }; 58 59 public static final String [] BAD_DOMAINS = { 61 "doma:in", }; 63 64 public static final String [] BAD_FULLNAMES = { 66 "domain:key=val,key=val2", "domain:=,foo=bar", "domain:key=val,,foo=bar", "domain:,key=val,foo=bar", "domain:key=val,foo=bar,", "domain:key=val, ,foo=bar", }; 73 74 public MalformedTestCase(String s) 75 { 76 super(s); 77 } 78 79 public static Test suite() 80 { 81 TestSuite suite = new TestSuite("All Malformed Tests"); 82 83 suite.addTest(new DomainKeyValueTEST(null, null, null)); 85 suite.addTest(new DomainKeyValueTEST(null, "key1", "val1")); 86 suite.addTest(new DomainKeyValueTEST("domain", null, "val1")); 87 suite.addTest(new DomainKeyValueTEST("domain", "key1", null)); 88 suite.addTest(new DomainKeyValueTEST("domain", null, null)); 89 suite.addTest(new DomainHashtableTEST(null, "key1", "val1")); 90 91 suite.addTestSuite(DomainHashtableExtraTEST.class); 93 94 for (int i = 0; i < BAD_DOMAINS.length; i++) 96 { 97 suite.addTest(new FullNameTEST(BAD_DOMAINS[i] + ":" + GOOD_KEY + "=" + GOOD_VALUE)); 98 suite.addTest(new DomainKeyValueTEST(BAD_DOMAINS[i], GOOD_KEY, GOOD_VALUE)); 99 suite.addTest(new DomainHashtableTEST(BAD_DOMAINS[i], GOOD_KEY, GOOD_VALUE)); 100 } 101 102 for (int i = 0; i < BAD_KEYVALS.length; i++) 104 { 105 suite.addTest(new FullNameTEST(GOOD_DOMAIN + ":" + BAD_KEYVALS[i] + "=" + GOOD_VALUE)); 106 suite.addTest(new FullNameTEST(GOOD_DOMAIN + ":" + GOOD_KEY + "=" + BAD_KEYVALS[i])); 107 suite.addTest(new DomainKeyValueTEST(GOOD_DOMAIN, BAD_KEYVALS[i], GOOD_VALUE)); 108 suite.addTest(new DomainKeyValueTEST(GOOD_DOMAIN, GOOD_KEY, BAD_KEYVALS[i])); 109 suite.addTest(new DomainHashtableTEST(GOOD_DOMAIN, BAD_KEYVALS[i], GOOD_VALUE)); 110 suite.addTest(new DomainHashtableTEST(GOOD_DOMAIN, GOOD_KEY, BAD_KEYVALS[i])); 111 } 112 113 for (int i = 0; i < BAD_FULLNAMES.length; i++) 115 { 116 suite.addTest(new FullNameTEST(BAD_FULLNAMES[i])); 117 } 118 119 return suite; 120 } 121 122 public static class FullNameTEST extends TestCase 123 { 124 private String fullName; 125 126 public FullNameTEST(String fullName) 127 { 128 super("testMalformed"); 129 this.fullName = fullName; 130 } 131 132 public void testMalformed() 133 { 134 try 135 { 136 ObjectName name = new ObjectName (fullName); 137 } 138 catch (MalformedObjectNameException e) 139 { 140 if (fullName != null) 141 { 142 return; 143 } 144 } 145 catch (NullPointerException e) 146 { 147 if (fullName == null) 148 { 149 return; 150 } 151 } 152 fail("invalid object name: " + fullName); 153 } 154 } 155 156 public static class DomainKeyValueTEST extends TestCase 157 { 158 private String domain; 159 private String key; 160 private String value; 161 162 public DomainKeyValueTEST(String domain, String key, String value) 163 { 164 super("testMalformed"); 165 this.domain = domain; 166 this.key = key; 167 this.value = value; 168 } 169 170 public void testMalformed() 171 { 172 try 173 { 174 ObjectName name = new ObjectName (domain, key, value); 175 } 176 catch (MalformedObjectNameException e) 177 { 178 if (domain != null && key != null && value != null) 179 { 180 return; 181 } 182 } 183 catch (NullPointerException e) 184 { 185 if (domain == null || key == null || value == null) 186 { 187 return; 188 } 189 } 190 fail("invalid object name: " + domain + ":" + key + "=" + value); 191 } 192 } 193 194 public static class DomainHashtableTEST extends TestCase 195 { 196 private String domain; 197 private String key; 198 private String value; 199 200 public DomainHashtableTEST(String domain, String key, String value) 201 { 202 super("testMalformed"); 203 this.domain = domain; 204 this.key = key; 205 this.value = value; 206 } 207 208 public void testMalformed() 209 { 210 try 211 { 212 Hashtable h = new Hashtable (); 213 h.put(key, value); 214 ObjectName name = new ObjectName (domain, h); 215 } 216 catch (MalformedObjectNameException e) 217 { 218 if (domain != null && key != null && value != null) 219 { 220 return; 221 } 222 } 223 catch (NullPointerException e) 224 { 225 if (domain == null || key == null || value == null) 226 { 227 return; 228 } 229 } 230 fail("invalid object name: " + domain + ":" + key + "=" + value); 231 } 232 } 233 234 public static class DomainHashtableExtraTEST extends TestCase 235 { 236 public DomainHashtableExtraTEST(String s) 237 { 238 super(s); 239 } 240 241 public void testNullHashtable() 242 { 243 doCheck(GOOD_DOMAIN, null, "<null>"); 244 } 245 246 public void testEmptyHashtable() 247 { 248 doCheck(GOOD_DOMAIN, new Hashtable (), "<empty_hashtable>"); 249 } 250 251 public void testNonStringKey() 252 { 253 Hashtable h = new Hashtable (); 254 h.put(new Object (), GOOD_VALUE); 255 doCheck(GOOD_DOMAIN, h, "<non_string_key>=" + GOOD_VALUE); 256 } 257 258 public void testNonStringValue() 259 { 260 Hashtable h = new Hashtable (); 261 h.put(GOOD_KEY, new Object ()); 262 doCheck(GOOD_DOMAIN, h, GOOD_KEY + "=<non_string_value>"); 263 } 264 265 private void doCheck(String domain, Hashtable table, String failureHint) 266 { 267 try 268 { 269 ObjectName name = new ObjectName (domain, table); 270 } 271 catch (MalformedObjectNameException e) 272 { 273 if (domain != null && table != null) 274 { 275 return; 276 } 277 } 278 catch (NullPointerException e) 279 { 280 if (domain == null || table == null) 281 { 282 return; 283 } 284 } 285 fail("invalid object name: " + domain + ":" + failureHint); 286 } 287 } 288 } 289 | Popular Tags |