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.snmp;
023:
024: import java.util.SortedSet;
025: import java.util.TreeSet;
026:
027: import org.jboss.jmx.adaptor.snmp.agent.ComparableSnmpObjectId;
028:
029: import junit.framework.TestCase;
030:
031: /**
032: * Tests for the ComparableSnmpObjectId, a Subclass of SnmpObjectId from
033: * the joesnmp package. Most tests are trivial.
034: * @author <a href="mailto:hwr@pilhuhn.de">Heiko W. Rupp</a>
035: * @version $Revision: 57211 $
036: */
037: public class ComparableSnmpOidTestCase extends TestCase {
038:
039: /**
040: * Make sure, that the passed oid which does not end in .0
041: * is not detected as leaf.
042: */
043: public void testIsNotLeaf() {
044: ComparableSnmpObjectId coid = new ComparableSnmpObjectId(
045: ".1.2.3.4");
046: boolean res = coid.isLeaf();
047: assertFalse(res);
048: }
049:
050: /**
051: * Make sure that the passed oid ending in .0 is detected as leaf.
052: */
053: public void testIsLeaf() {
054: ComparableSnmpObjectId coid = new ComparableSnmpObjectId(
055: ".1.2.3.4.0");
056: boolean res = coid.isLeaf();
057: assertTrue(res);
058: }
059:
060: /**
061: * Make sure that the passed oid ending in .0 is detected as leaf.
062: */
063: public void testIsLeaf2() {
064: ComparableSnmpObjectId coid = new ComparableSnmpObjectId(
065: "1.2.3.4.0");
066: boolean res = coid.isLeaf();
067: assertTrue(res);
068: }
069:
070: /**
071: * See if the last part of an oid is correctly chopped of.
072: *
073: */
074: public void testRemoveLastPart() {
075: ComparableSnmpObjectId coid = new ComparableSnmpObjectId(
076: "1.2.3.4.0");
077: ComparableSnmpObjectId res = coid.removeLastPart();
078: assertEquals(".1.2.3.4", res.toString());
079: }
080:
081: /**
082: * See if compareTo from Comparable works as expected.
083: * This is needed for use of the ComparableSnmpObjectId in SortedSets etc.
084: * @see java.lang.Comparable
085: */
086: public void testCompareTo1() {
087: ComparableSnmpObjectId coid = new ComparableSnmpObjectId(
088: "1.2.3.4.0");
089: ComparableSnmpObjectId coid2 = new ComparableSnmpObjectId(
090: ".1.2.3.4.0");
091: int res = coid.compareTo(coid2);
092: assertEquals(0, res);
093: }
094:
095: /**
096: * See if compareTo from Comparable works as expected.
097: * This is needed for use of the ComparableSnmpObjectId in SortedSets etc.
098: * @see java.lang.Comparable
099: */
100: public void testCompareTo2() {
101: ComparableSnmpObjectId coid = new ComparableSnmpObjectId(
102: "1.2.3.4");
103: ComparableSnmpObjectId coid2 = new ComparableSnmpObjectId(
104: "1.2.3.4.0");
105: int res = coid.compareTo(coid2);
106: assertTrue(res != 0);
107: }
108:
109: /**
110: * See if compareTo from Comparable works as expected.
111: * This is needed for use of the ComparableSnmpObjectId in SortedSets etc.
112: * @see java.lang.Comparable
113: */
114: public void testCompareTo3() {
115: ComparableSnmpObjectId coid = new ComparableSnmpObjectId(
116: "1.2.3.4.1");
117: ComparableSnmpObjectId coid2 = new ComparableSnmpObjectId(
118: "1.2.3.4.2");
119: int res = coid.compareTo(coid2);
120: assertTrue(res < 0);
121: }
122:
123: /**
124: * See if compareTo from Comparable works as expected.
125: * This is needed for use of the ComparableSnmpObjectId in SortedSets etc.
126: * @see java.lang.Comparable
127: */
128: public void testCompareTo4() {
129: ComparableSnmpObjectId coid = new ComparableSnmpObjectId(
130: "1.2.3.4.2");
131: ComparableSnmpObjectId coid2 = new ComparableSnmpObjectId(
132: "1.2.3.4.1");
133: int res = coid.compareTo(coid2);
134: assertTrue(res > 0);
135: }
136:
137: /**
138: * See if compareTo from Comparable works as expected.
139: * This is needed for use of the ComparableSnmpObjectId in SortedSets etc.
140: * @see java.lang.Comparable
141: */
142: public void testCompareTo5() {
143: ComparableSnmpObjectId coid = new ComparableSnmpObjectId(
144: "1.2.3.4.2");
145: Object coid2 = new Object();
146: int res = coid.compareTo(coid2);
147: assertTrue(res == -1);
148: }
149:
150: public void testGetNextArc() {
151: ComparableSnmpObjectId coid = new ComparableSnmpObjectId(
152: "1.2.3.4");
153: ComparableSnmpObjectId res = coid.getNextArc();
154: assertEquals(".1.2.4", res.toString());
155: }
156:
157: public void testGetNextArc2() {
158: ComparableSnmpObjectId coid = new ComparableSnmpObjectId(
159: ".1.2.3.4.5");
160: ComparableSnmpObjectId res = coid.getNextArc();
161: assertEquals(".1.2.3.5", res.toString());
162: }
163:
164: public void testGetNextArc3() {
165: ComparableSnmpObjectId coid = new ComparableSnmpObjectId(
166: ".1.2.3.4.0");
167: ComparableSnmpObjectId res = coid.getNextArc();
168: assertEquals(".1.2.4", res.toString());
169: }
170:
171: public void testGetSubtree() {
172: SortedSet s = new TreeSet();
173: ComparableSnmpObjectId coid = new ComparableSnmpObjectId(
174: "1.2.3.4.0");
175: s.add(coid);
176: coid = new ComparableSnmpObjectId("1.2.3.5.0");
177: s.add(coid);
178: coid = new ComparableSnmpObjectId("1.2.3.6.0");
179: s.add(coid);
180:
181: ComparableSnmpObjectId c2 = new ComparableSnmpObjectId(
182: "1.2.3.4.1");
183: SortedSet subset = s.tailSet(c2);
184: assertEquals(2, subset.size());
185:
186: subset = s.headSet(c2);
187: assertEquals(1, subset.size());
188: }
189:
190: }
|