KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmx > compliance > objectname > BasicTestCase


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.jbossmx.compliance.objectname;
23
24 import org.jboss.test.jbossmx.compliance.TestCase;
25
26 import javax.management.MalformedObjectNameException JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import java.util.Hashtable JavaDoc;
29
30 public class BasicTestCase
31    extends TestCase
32 {
33    public static final String JavaDoc STD_DOMAIN = "domain";
34    public static final String JavaDoc WHITESPACE_DOMAIN = " ";
35    public static final String JavaDoc STD_KEYPROP_STRING = "key1=val1,key2=val2";
36    public static final String JavaDoc REV_KEYPROP_STRING = "key2=val2,key1=val1";
37    public static final String JavaDoc KEY1 = "key1";
38    public static final String JavaDoc KEY2 = "key2";
39    public static final String JavaDoc VAL1 = "val1";
40    public static final String JavaDoc VAL2 = "val2";
41
42    public BasicTestCase(String JavaDoc s)
43    {
44       super(s);
45    }
46
47    public void testStringNoDomain()
48    {
49       String JavaDoc nameArg = ":" + STD_KEYPROP_STRING;
50       try
51       {
52          ObjectName JavaDoc name = new ObjectName JavaDoc(nameArg);
53          String JavaDoc domain = name.getDomain();
54          if (null == domain)
55          {
56             fail("getDomain() should return empty string rather than null");
57          }
58          assertTrue("domain should have been zero size", domain.length() == 0);
59          assertEquals("value for key: " + KEY1 + " should be: " + VAL1, VAL1, name.getKeyProperty(KEY1));
60          assertEquals("value for key: " + KEY2 + " should be: " + VAL2, VAL2, name.getKeyProperty(KEY2));
61       }
62       catch (MalformedObjectNameException JavaDoc e)
63       {
64          fail("spurious MalformedObjectNameException on ('" + nameArg + "')");
65       }
66    }
67
68    public void testStringWithDomain()
69    {
70       String JavaDoc nameArg = STD_DOMAIN + ":" + STD_KEYPROP_STRING;
71       try
72       {
73          ObjectName JavaDoc name = new ObjectName JavaDoc(nameArg);
74          assertEquals("domain should be equivalent", STD_DOMAIN, name.getDomain());
75          assertEquals("value for key: " + KEY1 + " should be: " + VAL1, VAL1, name.getKeyProperty(KEY1));
76          assertEquals("value for key: " + KEY2 + " should be: " + VAL2, VAL2, name.getKeyProperty(KEY2));
77       }
78       catch (MalformedObjectNameException JavaDoc e)
79       {
80          fail("spurious MalformedObjectNameException on ('" + nameArg + "')");
81       }
82    }
83
84    public void testSingleKVP()
85    {
86       try
87       {
88          ObjectName JavaDoc name = new ObjectName JavaDoc(STD_DOMAIN, KEY1, VAL1);
89          assertEquals("domain should be equivalent", STD_DOMAIN, name.getDomain());
90          assertEquals("value for key: " + KEY1 + " should be: " + VAL1, VAL1, name.getKeyProperty(KEY1));
91          assertNull("should return NULL key property for: " + KEY2, name.getKeyProperty(KEY2));
92
93          String JavaDoc kplistString = name.getKeyPropertyListString();
94          if (null == kplistString)
95          {
96             fail("key property list string was null;");
97          }
98          assertTrue("KeyPropertyListString should match",
99                     kplistString.equals("key1=val1"));
100
101       }
102       catch (MalformedObjectNameException JavaDoc e)
103       {
104          fail("spurious MalformedObjectNameException on ('" + STD_DOMAIN + "','" + KEY1 + "','" + VAL1 + "')");
105       }
106    }
107
108    public void testHashtable()
109    {
110       try
111       {
112          Hashtable JavaDoc properties = new Hashtable JavaDoc();
113          properties.put(KEY1, VAL1);
114          properties.put(KEY2, VAL2);
115          ObjectName JavaDoc name = new ObjectName JavaDoc(STD_DOMAIN, properties);
116          assertEquals("domain should be equivalent", STD_DOMAIN, name.getDomain());
117          assertEquals("value for key: " + KEY1 + " should be: " + VAL1, VAL1, name.getKeyProperty(KEY1));
118          assertEquals("value for key: " + KEY2 + " should be: " + VAL2, VAL2, name.getKeyProperty(KEY2));
119
120          String JavaDoc kplistString = name.getKeyPropertyListString();
121          if (null == kplistString)
122          {
123             fail("key property list string was null;");
124          }
125          assertTrue("KeyPropertyListString should match",
126                     (kplistString.equals(STD_KEYPROP_STRING) || kplistString.equals(REV_KEYPROP_STRING)));
127       }
128       catch (MalformedObjectNameException JavaDoc e)
129       {
130          fail("spurious MalformedObjectNameException on ('" + STD_DOMAIN + "','" + KEY1 + "','" + VAL1 + "')");
131       }
132    }
133
134    public void testWhitespaceDomain()
135    {
136       String JavaDoc wsDomain = " ";
137       String JavaDoc nameArg = wsDomain + ":" + STD_KEYPROP_STRING;
138       try
139       {
140          ObjectName JavaDoc name = new ObjectName JavaDoc(nameArg);
141          assertEquals("domain should be equivalent", wsDomain, name.getDomain());
142       }
143       catch (MalformedObjectNameException JavaDoc e)
144       {
145          fail("spurious MalformedObjectNameException on ('" + nameArg + "')");
146       }
147    }
148
149    public void testKeyPropertyList()
150    {
151       String JavaDoc nameArg = ":" + STD_KEYPROP_STRING;
152       try
153       {
154          ObjectName JavaDoc name = new ObjectName JavaDoc(nameArg);
155          String JavaDoc kplistString = name.getKeyPropertyListString();
156          if (null == kplistString)
157          {
158             fail("key property list string was null;");
159          }
160          assertTrue("KeyPropertyListString should match",
161                     (kplistString.equals(STD_KEYPROP_STRING) || kplistString.equals(REV_KEYPROP_STRING)));
162
163       }
164       catch (MalformedObjectNameException JavaDoc e)
165       {
166          fail("spurious MalformedObjectNameException on ('" + nameArg + "')");
167       }
168    }
169
170    public void testToString()
171    {
172       String JavaDoc nameArg1 = ":key1=val1";
173       String JavaDoc nameArg2 = "domain:key1=val1";
174
175       try
176       {
177          ObjectName JavaDoc name1 = new ObjectName JavaDoc(nameArg1);
178          assertEquals("toString should match", nameArg1, name1.toString());
179       }
180       catch (MalformedObjectNameException JavaDoc e)
181       {
182          fail("spurious MalformedObjectNameException on ('" + nameArg1 + "')");
183       }
184
185       try
186       {
187          ObjectName JavaDoc name2 = new ObjectName JavaDoc(nameArg2);
188          assertEquals("toString should match", nameArg2, name2.toString());
189       }
190       catch (MalformedObjectNameException JavaDoc e)
191       {
192          fail("spurious MalformedObjectNameException on ('" + nameArg2 + "')");
193       }
194    }
195 }
196
Popular Tags