001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jbossmx.compliance.objectname;
023:
024: import org.jboss.test.jbossmx.compliance.TestCase;
025:
026: import javax.management.MalformedObjectNameException;
027: import javax.management.ObjectName;
028: import java.util.Hashtable;
029:
030: public class BasicTestCase extends TestCase {
031: public static final String STD_DOMAIN = "domain";
032: public static final String WHITESPACE_DOMAIN = " ";
033: public static final String STD_KEYPROP_STRING = "key1=val1,key2=val2";
034: public static final String REV_KEYPROP_STRING = "key2=val2,key1=val1";
035: public static final String KEY1 = "key1";
036: public static final String KEY2 = "key2";
037: public static final String VAL1 = "val1";
038: public static final String VAL2 = "val2";
039:
040: public BasicTestCase(String s) {
041: super (s);
042: }
043:
044: public void testStringNoDomain() {
045: String nameArg = ":" + STD_KEYPROP_STRING;
046: try {
047: ObjectName name = new ObjectName(nameArg);
048: String domain = name.getDomain();
049: if (null == domain) {
050: fail("getDomain() should return empty string rather than null");
051: }
052: assertTrue("domain should have been zero size", domain
053: .length() == 0);
054: assertEquals("value for key: " + KEY1 + " should be: "
055: + VAL1, VAL1, name.getKeyProperty(KEY1));
056: assertEquals("value for key: " + KEY2 + " should be: "
057: + VAL2, VAL2, name.getKeyProperty(KEY2));
058: } catch (MalformedObjectNameException e) {
059: fail("spurious MalformedObjectNameException on ('"
060: + nameArg + "')");
061: }
062: }
063:
064: public void testStringWithDomain() {
065: String nameArg = STD_DOMAIN + ":" + STD_KEYPROP_STRING;
066: try {
067: ObjectName name = new ObjectName(nameArg);
068: assertEquals("domain should be equivalent", STD_DOMAIN,
069: name.getDomain());
070: assertEquals("value for key: " + KEY1 + " should be: "
071: + VAL1, VAL1, name.getKeyProperty(KEY1));
072: assertEquals("value for key: " + KEY2 + " should be: "
073: + VAL2, VAL2, name.getKeyProperty(KEY2));
074: } catch (MalformedObjectNameException e) {
075: fail("spurious MalformedObjectNameException on ('"
076: + nameArg + "')");
077: }
078: }
079:
080: public void testSingleKVP() {
081: try {
082: ObjectName name = new ObjectName(STD_DOMAIN, KEY1, VAL1);
083: assertEquals("domain should be equivalent", STD_DOMAIN,
084: name.getDomain());
085: assertEquals("value for key: " + KEY1 + " should be: "
086: + VAL1, VAL1, name.getKeyProperty(KEY1));
087: assertNull("should return NULL key property for: " + KEY2,
088: name.getKeyProperty(KEY2));
089:
090: String kplistString = name.getKeyPropertyListString();
091: if (null == kplistString) {
092: fail("key property list string was null;");
093: }
094: assertTrue("KeyPropertyListString should match",
095: kplistString.equals("key1=val1"));
096:
097: } catch (MalformedObjectNameException e) {
098: fail("spurious MalformedObjectNameException on ('"
099: + STD_DOMAIN + "','" + KEY1 + "','" + VAL1 + "')");
100: }
101: }
102:
103: public void testHashtable() {
104: try {
105: Hashtable properties = new Hashtable();
106: properties.put(KEY1, VAL1);
107: properties.put(KEY2, VAL2);
108: ObjectName name = new ObjectName(STD_DOMAIN, properties);
109: assertEquals("domain should be equivalent", STD_DOMAIN,
110: name.getDomain());
111: assertEquals("value for key: " + KEY1 + " should be: "
112: + VAL1, VAL1, name.getKeyProperty(KEY1));
113: assertEquals("value for key: " + KEY2 + " should be: "
114: + VAL2, VAL2, name.getKeyProperty(KEY2));
115:
116: String kplistString = name.getKeyPropertyListString();
117: if (null == kplistString) {
118: fail("key property list string was null;");
119: }
120: assertTrue(
121: "KeyPropertyListString should match",
122: (kplistString.equals(STD_KEYPROP_STRING) || kplistString
123: .equals(REV_KEYPROP_STRING)));
124: } catch (MalformedObjectNameException e) {
125: fail("spurious MalformedObjectNameException on ('"
126: + STD_DOMAIN + "','" + KEY1 + "','" + VAL1 + "')");
127: }
128: }
129:
130: public void testWhitespaceDomain() {
131: String wsDomain = " ";
132: String nameArg = wsDomain + ":" + STD_KEYPROP_STRING;
133: try {
134: ObjectName name = new ObjectName(nameArg);
135: assertEquals("domain should be equivalent", wsDomain, name
136: .getDomain());
137: } catch (MalformedObjectNameException e) {
138: fail("spurious MalformedObjectNameException on ('"
139: + nameArg + "')");
140: }
141: }
142:
143: public void testKeyPropertyList() {
144: String nameArg = ":" + STD_KEYPROP_STRING;
145: try {
146: ObjectName name = new ObjectName(nameArg);
147: String kplistString = name.getKeyPropertyListString();
148: if (null == kplistString) {
149: fail("key property list string was null;");
150: }
151: assertTrue(
152: "KeyPropertyListString should match",
153: (kplistString.equals(STD_KEYPROP_STRING) || kplistString
154: .equals(REV_KEYPROP_STRING)));
155:
156: } catch (MalformedObjectNameException e) {
157: fail("spurious MalformedObjectNameException on ('"
158: + nameArg + "')");
159: }
160: }
161:
162: public void testToString() {
163: String nameArg1 = ":key1=val1";
164: String nameArg2 = "domain:key1=val1";
165:
166: try {
167: ObjectName name1 = new ObjectName(nameArg1);
168: assertEquals("toString should match", nameArg1, name1
169: .toString());
170: } catch (MalformedObjectNameException e) {
171: fail("spurious MalformedObjectNameException on ('"
172: + nameArg1 + "')");
173: }
174:
175: try {
176: ObjectName name2 = new ObjectName(nameArg2);
177: assertEquals("toString should match", nameArg2, name2
178: .toString());
179: } catch (MalformedObjectNameException e) {
180: fail("spurious MalformedObjectNameException on ('"
181: + nameArg2 + "')");
182: }
183: }
184: }
|