0001: /*
0002: * JBoss, Home of Professional Open Source.
0003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
0004: * as indicated by the @author tags. See the copyright.txt file in the
0005: * distribution for a full listing of individual contributors.
0006: *
0007: * This is free software; you can redistribute it and/or modify it
0008: * under the terms of the GNU Lesser General Public License as
0009: * published by the Free Software Foundation; either version 2.1 of
0010: * the License, or (at your option) any later version.
0011: *
0012: * This software is distributed in the hope that it will be useful,
0013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0015: * Lesser General Public License for more details.
0016: *
0017: * You should have received a copy of the GNU Lesser General Public
0018: * License along with this software; if not, write to the Free
0019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
0020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
0021: */
0022: package test.compliance.query;
0023:
0024: import java.util.HashSet;
0025: import java.util.Iterator;
0026: import java.util.Set;
0027:
0028: import javax.management.AttributeValueExp;
0029: import javax.management.MBeanServer;
0030: import javax.management.MBeanServerFactory;
0031: import javax.management.ObjectInstance;
0032: import javax.management.ObjectName;
0033: import javax.management.Query;
0034: import javax.management.QueryExp;
0035: import javax.management.ValueExp;
0036:
0037: import junit.framework.AssertionFailedError;
0038: import junit.framework.TestCase;
0039: import test.compliance.query.support.BooleanTest;
0040: import test.compliance.query.support.NumberTest;
0041: import test.compliance.query.support.StringTest;
0042: import test.compliance.query.support.Trivial;
0043:
0044: /**
0045: * Query unit tests
0046: *
0047: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
0048: */
0049: public class QueryTestCase extends TestCase {
0050: // Attributes ----------------------------------------------------------------
0051:
0052: // Constructor ---------------------------------------------------------------
0053:
0054: /**
0055: * Construct the test
0056: */
0057: public QueryTestCase(String s) {
0058: super (s);
0059: }
0060:
0061: // Tests ---------------------------------------------------------------------
0062:
0063: /**
0064: * Test a boolean
0065: */
0066: public void testBoolean() throws Exception {
0067: ValueExp one = Query.value(true);
0068: ValueExp two = Query.value(false);
0069: equalsTEST(one, two);
0070: attrTEST(new BooleanTest(true), Query.attr("Boolean"), one, two);
0071: attrTEST(new BooleanTest(true), Query.attr(BooleanTest.class
0072: .getName(), "Boolean"), one, two);
0073: try {
0074: // Test in first
0075: new QueryTEST(new MBean[] { new MBean(new Trivial(),
0076: "Domain1:type=instance1") }, new MBean[0], Query
0077: .in(one, new ValueExp[] { one, two, two })).test();
0078: // Test in last
0079: new QueryTEST(new MBean[] { new MBean(new Trivial(),
0080: "Domain1:type=instance1") }, new MBean[0], Query
0081: .in(one, new ValueExp[] { two, two, one })).test();
0082: // Test in not the first or last
0083: new QueryTEST(new MBean[] { new MBean(new Trivial(),
0084: "Domain1:type=instance1") }, new MBean[0], Query
0085: .in(one, new ValueExp[] { two, one, two })).test();
0086: // Test not in
0087: new QueryTEST(new MBean[0], new MBean[] { new MBean(
0088: new Trivial(), "Domain1:type=instance1") }, Query
0089: .in(one, new ValueExp[] { two, two, two })).test();
0090: } catch (AssertionFailedError e) {
0091: fail("FAILS IN RI: Query.in boolean");
0092: }
0093: }
0094:
0095: /**
0096: * Test a double
0097: */
0098: public void testDouble() throws Exception {
0099: ValueExp one = Query.value(10d);
0100: ValueExp two = Query.value(20d);
0101: ValueExp div = Query.value(2d);
0102: ValueExp minus = Query.value(-10d);
0103: ValueExp mult = Query.value(200d);
0104: ValueExp plus = Query.value(30d);
0105: equalsTEST(one, two);
0106: operationTEST(one, two, div, minus, mult, plus);
0107: comparisonTEST(one, two);
0108: betweenTEST(one, two, plus);
0109: attrTEST(new NumberTest(10d), Query.attr("Number"), one, two);
0110: attrTEST(new NumberTest(10d), Query.attr(NumberTest.class
0111: .getName(), "Number"), one, two);
0112: inTEST(one, two, div, minus, mult, plus);
0113: }
0114:
0115: /**
0116: * Test a Double
0117: */
0118: public void testDoubleObject() throws Exception {
0119: ValueExp one = Query.value(new Double(10d));
0120: ValueExp two = Query.value(new Double(20d));
0121: ValueExp div = Query.value(new Double(2d));
0122: ValueExp minus = Query.value(new Double(-10d));
0123: ValueExp mult = Query.value(new Double(200d));
0124: ValueExp plus = Query.value(new Double(30d));
0125: equalsTEST(one, two);
0126: operationTEST(one, two, div, minus, mult, plus);
0127: comparisonTEST(one, two);
0128: betweenTEST(one, two, plus);
0129: attrTEST(new NumberTest(new Double(10d)), Query.attr("Number"),
0130: one, two);
0131: attrTEST(new NumberTest(new Double(10d)), Query.attr(
0132: NumberTest.class.getName(), "Number"), one, two);
0133: inTEST(one, two, div, minus, mult, plus);
0134: }
0135:
0136: /**
0137: * Test a float
0138: */
0139: public void testFloat() throws Exception {
0140: ValueExp one = Query.value(10f);
0141: ValueExp two = Query.value(20f);
0142: ValueExp div = Query.value(2f);
0143: ValueExp minus = Query.value(-10f);
0144: ValueExp mult = Query.value(200f);
0145: ValueExp plus = Query.value(30f);
0146: equalsTEST(one, two);
0147: operationTEST(one, two, div, minus, mult, plus);
0148: comparisonTEST(one, two);
0149: betweenTEST(one, two, plus);
0150: attrTEST(new NumberTest(10f), Query.attr("Number"), one, two);
0151: attrTEST(new NumberTest(10f), Query.attr(NumberTest.class
0152: .getName(), "Number"), one, two);
0153: inTEST(one, two, div, minus, mult, plus);
0154: }
0155:
0156: /**
0157: * Test a Float
0158: */
0159: public void testFloatObject() throws Exception {
0160: ValueExp one = Query.value(new Float(10f));
0161: ValueExp two = Query.value(new Float(20f));
0162: ValueExp div = Query.value(new Double(2f));
0163: ValueExp minus = Query.value(new Double(-10f));
0164: ValueExp mult = Query.value(new Double(200f));
0165: ValueExp plus = Query.value(new Double(30f));
0166: equalsTEST(one, two);
0167: operationTEST(one, two, div, minus, mult, plus);
0168: comparisonTEST(one, two);
0169: betweenTEST(one, two, plus);
0170: attrTEST(new NumberTest(new Float(10f)), Query.attr("Number"),
0171: one, two);
0172: attrTEST(new NumberTest(new Float(10f)), Query.attr(
0173: NumberTest.class.getName(), "Number"), one, two);
0174: inTEST(one, two, div, minus, mult, plus);
0175: }
0176:
0177: /**
0178: * Test a int
0179: */
0180: public void testInteger() throws Exception {
0181: ValueExp one = Query.value(10);
0182: ValueExp two = Query.value(20);
0183: ValueExp div = Query.value(2);
0184: ValueExp minus = Query.value(-10);
0185: ValueExp mult = Query.value(200);
0186: ValueExp plus = Query.value(30);
0187: equalsTEST(one, two);
0188: operationTEST(one, two, div, minus, mult, plus);
0189: comparisonTEST(one, two);
0190: betweenTEST(one, two, plus);
0191: attrTEST(new NumberTest(10), Query.attr("Number"), one, two);
0192: attrTEST(new NumberTest(10), Query.attr(NumberTest.class
0193: .getName(), "Number"), one, two);
0194: inTEST(one, two, div, minus, mult, plus);
0195: }
0196:
0197: /**
0198: * Test a Integer
0199: */
0200: public void testIntegerObject() throws Exception {
0201: ValueExp one = Query.value(new Integer(10));
0202: ValueExp two = Query.value(new Integer(20));
0203: ValueExp div = Query.value(new Double(2));
0204: ValueExp minus = Query.value(new Double(-10));
0205: ValueExp mult = Query.value(new Double(200));
0206: ValueExp plus = Query.value(new Double(30));
0207: equalsTEST(one, two);
0208: operationTEST(one, two, div, minus, mult, plus);
0209: comparisonTEST(one, two);
0210: betweenTEST(one, two, plus);
0211: attrTEST(new NumberTest(new Integer(10)), Query.attr("Number"),
0212: one, two);
0213: attrTEST(new NumberTest(new Integer(10)), Query.attr(
0214: NumberTest.class.getName(), "Number"), one, two);
0215: inTEST(one, two, div, minus, mult, plus);
0216: }
0217:
0218: /**
0219: * Test a long
0220: */
0221: public void testLong() throws Exception {
0222: ValueExp one = Query.value(10l);
0223: ValueExp two = Query.value(20l);
0224: ValueExp div = Query.value(2l);
0225: ValueExp minus = Query.value(-10l);
0226: ValueExp mult = Query.value(200l);
0227: ValueExp plus = Query.value(30l);
0228: equalsTEST(one, two);
0229: operationTEST(one, two, div, minus, mult, plus);
0230: comparisonTEST(one, two);
0231: betweenTEST(one, two, plus);
0232: attrTEST(new NumberTest(10l), Query.attr("Number"), one, two);
0233: attrTEST(new NumberTest(10l), Query.attr(NumberTest.class
0234: .getName(), "Number"), one, two);
0235: inTEST(one, two, div, minus, mult, plus);
0236: }
0237:
0238: /**
0239: * Test a Long
0240: */
0241: public void testLongObject() throws Exception {
0242: ValueExp one = Query.value(new Long(10l));
0243: ValueExp two = Query.value(new Long(20l));
0244: ValueExp div = Query.value(new Double(2l));
0245: ValueExp minus = Query.value(new Double(-10l));
0246: ValueExp mult = Query.value(new Double(200l));
0247: ValueExp plus = Query.value(new Double(30l));
0248: equalsTEST(one, two);
0249: operationTEST(one, two, div, minus, mult, plus);
0250: comparisonTEST(one, two);
0251: betweenTEST(one, two, plus);
0252: attrTEST(new NumberTest(new Long(10l)), Query.attr("Number"),
0253: one, two);
0254: attrTEST(new NumberTest(new Long(10l)), Query.attr(
0255: NumberTest.class.getName(), "Number"), one, two);
0256: inTEST(one, two, div, minus, mult, plus);
0257: }
0258:
0259: /**
0260: * Test a String
0261: */
0262: public void testString() throws Exception {
0263: ValueExp one = Query.value("Hello");
0264: ValueExp two = Query.value("Goodbye");
0265: ValueExp cat = Query.value("HelloGoodbye");
0266: ValueExp three = Query.value("ZZZZZZ");
0267: ValueExp four = Query.value("Hi");
0268: ValueExp five = Query.value("See ya");
0269: ValueExp six = Query.value("Laytaz");
0270: equalsTEST(one, two);
0271: catTEST(one, two, cat);
0272: comparisonTEST(two, one);
0273: betweenTEST(two, one, three);
0274: attrTEST(new StringTest("Hello"), Query.attr("String"), one,
0275: two);
0276: attrTEST(new StringTest("Hello"), Query.attr(StringTest.class
0277: .getName(), "String"), one, two);
0278: inTEST(one, two, three, four, five, six);
0279: }
0280:
0281: /**
0282: * Test and is true both
0283: */
0284: public void testAndTrueBoth() throws Exception {
0285: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
0286: "Domain1:type=instance1") }, new MBean[0], Query.and(
0287: Query.eq(Query.value(10), Query.value(10)), Query.eq(
0288: Query.value("Hello"), Query.value("Hello"))))
0289: .test();
0290: }
0291:
0292: /**
0293: * Test and is false first parameter
0294: */
0295: public void testAndFalseFirst() throws Exception {
0296: new QueryTEST(new MBean[0], new MBean[] { new MBean(
0297: new NumberTest(0), "Domain1:type=instance1") },
0298: Query.and(Query.eq(Query.value(10), Query.value(20)),
0299: Query.eq(Query.value("Hello"), Query
0300: .value("Hello")))).test();
0301: }
0302:
0303: /**
0304: * Test and is false second parameter
0305: */
0306: public void testAndFalseSecond() throws Exception {
0307: new QueryTEST(new MBean[0], new MBean[] { new MBean(
0308: new NumberTest(0), "Domain1:type=instance1") }, Query
0309: .and(Query.eq(Query.value(10), Query.value(10)), Query
0310: .eq(Query.value("Hello"), Query
0311: .value("Goodbye")))).test();
0312: }
0313:
0314: /**
0315: * Test and is false both parameters
0316: */
0317: public void testAndFalseBoth() throws Exception {
0318: new QueryTEST(new MBean[0], new MBean[] { new MBean(
0319: new NumberTest(0), "Domain1:type=instance1") }, Query
0320: .and(Query.eq(Query.value(10), Query.value(20)), Query
0321: .eq(Query.value("Hello"), Query
0322: .value("Goodbye")))).test();
0323: }
0324:
0325: /**
0326: * Test or is true both
0327: */
0328: public void testOrTrueBoth() throws Exception {
0329: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
0330: "Domain1:type=instance1") }, new MBean[0], Query.or(
0331: Query.eq(Query.value(10), Query.value(10)), Query.eq(
0332: Query.value("Hello"), Query.value("Hello"))))
0333: .test();
0334: }
0335:
0336: /**
0337: * Test or is false first parameter
0338: */
0339: public void testOrFalseFirst() throws Exception {
0340: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
0341: "Domain1:type=instance1") }, new MBean[0], Query.or(
0342: Query.eq(Query.value(10), Query.value(20)), Query.eq(
0343: Query.value("Hello"), Query.value("Hello"))))
0344: .test();
0345: }
0346:
0347: /**
0348: * Test or is false second parameter
0349: */
0350: public void testOrFalseSecond() throws Exception {
0351: new QueryTEST(new MBean[0], new MBean[] { new MBean(
0352: new NumberTest(0), "Domain1:type=instance1") }, Query
0353: .and(Query.eq(Query.value(10), Query.value(10)), Query
0354: .eq(Query.value("Hello"), Query
0355: .value("Goodbye")))).test();
0356: }
0357:
0358: /**
0359: * Test or is false both parameters
0360: */
0361: public void testOrFalseBoth() throws Exception {
0362: new QueryTEST(new MBean[0], new MBean[] { new MBean(
0363: new NumberTest(0), "Domain1:type=instance1") }, Query
0364: .or(Query.eq(Query.value(10), Query.value(20)), Query
0365: .eq(Query.value("Hello"), Query
0366: .value("Goodbye")))).test();
0367: }
0368:
0369: /**
0370: * Test not
0371: */
0372: public void testNot() throws Exception {
0373: new QueryTEST(new MBean[0], new MBean[] { new MBean(
0374: new NumberTest(0), "Domain1:type=instance1") }, Query
0375: .not(Query.eq(Query.value("Hello"), Query
0376: .value("Hello")))).test();
0377: }
0378:
0379: /**
0380: * Test not not
0381: */
0382: public void testNotNot() throws Exception {
0383: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
0384: "Domain1:type=instance1") }, new MBean[0], Query
0385: .not(Query.eq(Query.value("Hello"), Query
0386: .value("Goodbye")))).test();
0387: }
0388:
0389: /**
0390: * Test class attribute
0391: */
0392: public void testClassAttribute() throws Exception {
0393: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
0394: "Domain1:type=instance1") }, new MBean[] { new MBean(
0395: new Trivial(), "Domain1:type=instance2") }, Query.eq(
0396: Query.classattr(), Query.value(NumberTest.class
0397: .getName()))).test();
0398: }
0399:
0400: /**
0401: * Test simple object name
0402: */
0403: public void testSimpleObjectName() throws Exception {
0404: new QueryTEST(new MBean[] { new MBean(new Trivial(),
0405: "Domain1:type=instance1") }, new MBean[] { new MBean(
0406: new Trivial(), "Domain1:type=instance2") },
0407: new ObjectName("Domain1:type=instance1")).test();
0408: }
0409:
0410: /**
0411: * Test domain pattern object name
0412: */
0413: public void testDomainPatternObjectName() throws Exception {
0414: new QueryTEST(new MBean[] { new MBean(new Trivial(),
0415: "Domain1:type=instance1") }, new MBean[] { new MBean(
0416: new Trivial(), "Domain1:type=instance2") },
0417: new ObjectName("*:type=instance1")).test();
0418: }
0419:
0420: /**
0421: * Test property pattern object name
0422: */
0423: public void testPropertyPatternObjectName() throws Exception {
0424: new QueryTEST(new MBean[] {
0425: new MBean(new Trivial(), "Domain1:type=instance1"),
0426: new MBean(new Trivial(), "Domain1:type=instance2") },
0427: new MBean[] { new MBean(new Trivial(),
0428: "Domain2:type=instance1") }, new ObjectName(
0429: "Domain1:*")).test();
0430: }
0431:
0432: /**
0433: * Test multiple property pattern object name
0434: */
0435: public void testMultiplePropertyPatternObjectName()
0436: throws Exception {
0437: new QueryTEST(new MBean[] { new MBean(new Trivial(),
0438: "Domain1:type=instance1,extra=true") },
0439: new MBean[] { new MBean(new Trivial(),
0440: "Domain1:type=instance2") }, new ObjectName(
0441: "Domain1:extra=true,*")).test();
0442: }
0443:
0444: /**
0445: * Test invalid name passed to ObjectName
0446: */
0447: public void testInvalidNamePassedToObjectName() throws Exception {
0448: if (new ObjectName("*:*").apply(new ObjectName(
0449: "*:type=patternNotAllowedHere")))
0450: fail("Patterns should not be matched");
0451: }
0452:
0453: /**
0454: * Test any substring
0455: */
0456: public void testAnySubstring() throws Exception {
0457: new QueryTEST(new MBean[] {
0458: new MBean(new StringTest("Hello"),
0459: "Domain1:type=instance1"),
0460: new MBean(new StringTest("ellbeginning"),
0461: "Domain1:type=instance2"),
0462: new MBean(new StringTest("endell"),
0463: "Domain1:type=instance3"),
0464: new MBean(new StringTest("ell"),
0465: "Domain1:type=instance4") }, new MBean[] {
0466: new MBean(new StringTest("Goodbye"),
0467: "Domain2:type=instance1"),
0468: new MBean(new StringTest("el"),
0469: "Domain2:type=instance2"),
0470: new MBean(new StringTest("ll"),
0471: "Domain2:type=instance3"),
0472: new MBean(new StringTest("e ll"),
0473: "Domain2:type=instance4"),
0474: new MBean(new StringTest("ELL"),
0475: "Domain2:type=instance5"),
0476: new MBean(new StringTest("Ell"),
0477: "Domain2:type=instance6") }, Query
0478: .anySubString(Query.attr("String"), Query.value("ell")))
0479: .test();
0480: }
0481:
0482: /**
0483: * Test final substring
0484: */
0485: public void testFinalSubstring() throws Exception {
0486: new QueryTEST(new MBean[] {
0487: new MBean(new StringTest("endell"),
0488: "Domain1:type=instance1"),
0489: new MBean(new StringTest("ell"),
0490: "Domain1:type=instance2") }, new MBean[] {
0491: new MBean(new StringTest("Hello"),
0492: "Domain2:type=instance1"),
0493: new MBean(new StringTest("ellbeginning"),
0494: "Domain2:type=instance2"),
0495: new MBean(new StringTest("Goodbye"),
0496: "Domain2:type=instance3"),
0497: new MBean(new StringTest("el"),
0498: "Domain2:type=instance4"),
0499: new MBean(new StringTest("ll"),
0500: "Domain2:type=instance5"),
0501: new MBean(new StringTest("e ll"),
0502: "Domain2:type=instance6"),
0503: new MBean(new StringTest("ELL"),
0504: "Domain2:type=instance7"),
0505: new MBean(new StringTest("Ell"),
0506: "Domain2:type=instance8") }, Query
0507: .finalSubString(Query.attr("String"), Query
0508: .value("ell"))).test();
0509: }
0510:
0511: /**
0512: * Test initial substring
0513: */
0514: public void testInitialSubstring() throws Exception {
0515: new QueryTEST(new MBean[] {
0516: new MBean(new StringTest("ellbeginning"),
0517: "Domain1:type=instance1"),
0518: new MBean(new StringTest("ell"),
0519: "Domain1:type=instance2") }, new MBean[] {
0520: new MBean(new StringTest("Hello"),
0521: "Domain2:type=instance1"),
0522: new MBean(new StringTest("endell"),
0523: "Domain2:type=instance2"),
0524: new MBean(new StringTest("Goodbye"),
0525: "Domain2:type=instance3"),
0526: new MBean(new StringTest("el"),
0527: "Domain2:type=instance4"),
0528: new MBean(new StringTest("ll"),
0529: "Domain2:type=instance5"),
0530: new MBean(new StringTest("e ll"),
0531: "Domain2:type=instance6"),
0532: new MBean(new StringTest("ELL"),
0533: "Domain2:type=instance7"),
0534: new MBean(new StringTest("Ell"),
0535: "Domain2:type=instance8") }, Query
0536: .initialSubString(Query.attr("String"), Query
0537: .value("ell"))).test();
0538: }
0539:
0540: /**
0541: * Test match asterisk beginning
0542: */
0543: public void testMatchAsteriskBeginning() throws Exception {
0544: new QueryTEST(new MBean[] {
0545: new MBean(new StringTest("endell"),
0546: "Domain1:type=instance1"),
0547: new MBean(new StringTest("ell"),
0548: "Domain1:type=instance2") }, new MBean[] {
0549: new MBean(new StringTest("Hello"),
0550: "Domain2:type=instance1"),
0551: new MBean(new StringTest("ellbeginning"),
0552: "Domain2:type=instance2"),
0553: new MBean(new StringTest("Goodbye"),
0554: "Domain2:type=instance3"),
0555: new MBean(new StringTest("el"),
0556: "Domain2:type=instance4"),
0557: new MBean(new StringTest("ll"),
0558: "Domain2:type=instance5"),
0559: new MBean(new StringTest("e ll"),
0560: "Domain2:type=instance6"),
0561: new MBean(new StringTest("ELL"),
0562: "Domain2:type=instance7"),
0563: new MBean(new StringTest("Ell"),
0564: "Domain2:type=instance8") }, Query.match(Query
0565: .attr("String"), Query.value("*ell"))).test();
0566: }
0567:
0568: /**
0569: * Test match asterisk end
0570: */
0571: public void testMatchAsteriskEnd() throws Exception {
0572: new QueryTEST(new MBean[] {
0573: new MBean(new StringTest("ellbeginning"),
0574: "Domain1:type=instance1"),
0575: new MBean(new StringTest("ell"),
0576: "Domain1:type=instance2") }, new MBean[] {
0577: new MBean(new StringTest("Hello"),
0578: "Domain2:type=instance1"),
0579: new MBean(new StringTest("beginningell"),
0580: "Domain2:type=instance2"),
0581: new MBean(new StringTest("Goodbye"),
0582: "Domain2:type=instance3"),
0583: new MBean(new StringTest("el"),
0584: "Domain2:type=instance4"),
0585: new MBean(new StringTest("ll"),
0586: "Domain2:type=instance5"),
0587: new MBean(new StringTest("e ll"),
0588: "Domain2:type=instance6"),
0589: new MBean(new StringTest("ELL"),
0590: "Domain2:type=instance7"),
0591: new MBean(new StringTest("Ell"),
0592: "Domain2:type=instance8") }, Query.match(Query
0593: .attr("String"), Query.value("ell*"))).test();
0594: }
0595:
0596: /**
0597: * Test any match asterisk beginning and end
0598: */
0599: public void testMatchAsteriskBeginningAndEnd() throws Exception {
0600: new QueryTEST(new MBean[] {
0601: new MBean(new StringTest("Hello"),
0602: "Domain1:type=instance1"),
0603: new MBean(new StringTest("ell"),
0604: "Domain1:type=instance2"),
0605: new MBean(new StringTest("endell"),
0606: "Domain1:type=instance3"),
0607: new MBean(new StringTest("beginningell"),
0608: "Domain1:type=instance4") }, new MBean[] {
0609: new MBean(new StringTest("Goodbye"),
0610: "Domain2:type=instance1"),
0611: new MBean(new StringTest("el"),
0612: "Domain2:type=instance2"),
0613: new MBean(new StringTest("ll"),
0614: "Domain2:type=instance3"),
0615: new MBean(new StringTest("e ll"),
0616: "Domain2:type=instance4"),
0617: new MBean(new StringTest("ELL"),
0618: "Domain2:type=instance5"),
0619: new MBean(new StringTest("Ell"),
0620: "Domain2:type=instance6") }, Query.match(Query
0621: .attr("String"), Query.value("*ell*"))).test();
0622: }
0623:
0624: /**
0625: * Test match asterisk embedded
0626: */
0627: public void testMatchAsteriskEmbedded() throws Exception {
0628: new QueryTEST(new MBean[] { new MBean(new StringTest("Hello"),
0629: "Domain1:type=instance1"), }, new MBean[] {
0630: new MBean(new StringTest("ell"),
0631: "Domain2:type=instance1"),
0632: new MBean(new StringTest("endell"),
0633: "Domain2:type=instance2"),
0634: new MBean(new StringTest("beginningell"),
0635: "Domain2:type=instance3"),
0636: new MBean(new StringTest("Goodbye"),
0637: "Domain2:type=instance4"),
0638: new MBean(new StringTest("el"),
0639: "Domain2:type=instance5"),
0640: new MBean(new StringTest("ll"),
0641: "Domain2:type=instance6"),
0642: new MBean(new StringTest("e ll"),
0643: "Domain4:type=instance7"),
0644: new MBean(new StringTest("ELL"),
0645: "Domain2:type=instance8"),
0646: new MBean(new StringTest("Ell"),
0647: "Domain2:type=instance9") }, Query.match(Query
0648: .attr("String"), Query.value("H*o"))).test();
0649: }
0650:
0651: /**
0652: * Test match question beginning
0653: */
0654: public void testMatchQuestionBeginning() throws Exception {
0655: new QueryTEST(new MBean[] {
0656: new MBean(new StringTest("Hello"),
0657: "Domain1:type=instance1"),
0658: new MBean(new StringTest("hello"),
0659: "Domain1:type=instance2"),
0660: new MBean(new StringTest(" ello"),
0661: "Domain1:type=instance3") }, new MBean[] {
0662: new MBean(new StringTest("ello"),
0663: "Domain2:type=instance1"),
0664: new MBean(new StringTest("Ello"),
0665: "Domain2:type=instance2"),
0666: new MBean(new StringTest("hhello"),
0667: "Domain2:type=instance3"), }, Query.match(Query
0668: .attr("String"), Query.value("?ello"))).test();
0669: }
0670:
0671: /**
0672: * Test match question end
0673: */
0674: public void testMatchQuestionEnd() throws Exception {
0675: new QueryTEST(new MBean[] {
0676: new MBean(new StringTest("Hello"),
0677: "Domain1:type=instance1"),
0678: new MBean(new StringTest("HellO"),
0679: "Domain1:type=instance2"),
0680: new MBean(new StringTest("Hell "),
0681: "Domain1:type=instance3") }, new MBean[] {
0682: new MBean(new StringTest("hell"),
0683: "Domain2:type=instance1"),
0684: new MBean(new StringTest("helL"),
0685: "Domain2:type=instance2"),
0686: new MBean(new StringTest("Helloo"),
0687: "Domain2:type=instance3"), }, Query.match(Query
0688: .attr("String"), Query.value("Hell?"))).test();
0689: }
0690:
0691: /**
0692: * Test match question beginning and end
0693: */
0694: public void testMatchQuestionBeginningEnd() throws Exception {
0695: new QueryTEST(new MBean[] {
0696: new MBean(new StringTest("Hello"),
0697: "Domain1:type=instance1"),
0698: new MBean(new StringTest("HellO"),
0699: "Domain1:type=instance2"),
0700: new MBean(new StringTest("hello"),
0701: "Domain1:type=instance3"),
0702: new MBean(new StringTest("hellO"),
0703: "Domain1:type=instance4"),
0704: new MBean(new StringTest(" ell "),
0705: "Domain1:type=instance5") }, new MBean[] {
0706: new MBean(new StringTest("hell"),
0707: "Domain2:type=instance1"),
0708: new MBean(new StringTest("helL"),
0709: "Domain2:type=instance2"),
0710: new MBean(new StringTest("ello"),
0711: "Domain2:type=instance3"),
0712: new MBean(new StringTest("Ello"),
0713: "Domain2:type=instance4"),
0714: new MBean(new StringTest("ell"),
0715: "Domain2:type=instance5"),
0716: new MBean(new StringTest("Helloo"),
0717: "Domain2:type=instance6"),
0718: new MBean(new StringTest("HHello"),
0719: "Domain2:type=instance7"),
0720: new MBean(new StringTest("HHelloo"),
0721: "Domain2:type=instance8"), }, Query.match(Query
0722: .attr("String"), Query.value("?ell?"))).test();
0723: }
0724:
0725: /**
0726: * Test match question embedded
0727: */
0728: public void testMatchQuestionEmbedded() throws Exception {
0729: new QueryTEST(new MBean[] {
0730: new MBean(new StringTest("Hello"),
0731: "Domain1:type=instance1"),
0732: new MBean(new StringTest("HeLlo"),
0733: "Domain1:type=instance2"),
0734: new MBean(new StringTest("He lo"),
0735: "Domain1:type=instance3") }, new MBean[] {
0736: new MBean(new StringTest("hell"),
0737: "Domain2:type=instance1"),
0738: new MBean(new StringTest("ello"),
0739: "Domain2:type=instance2"),
0740: new MBean(new StringTest("ell"),
0741: "Domain2:type=instance3"),
0742: new MBean(new StringTest("Helloo"),
0743: "Domain2:type=instance4"),
0744: new MBean(new StringTest("HHello"),
0745: "Domain2:type=instance5"),
0746: new MBean(new StringTest("HHelloo"),
0747: "Domain2:type=instance6"), }, Query.match(Query
0748: .attr("String"), Query.value("He?lo"))).test();
0749: }
0750:
0751: /**
0752: * Test match character set
0753: */
0754: public void testMatchCharacterSet() throws Exception {
0755: try {
0756: new QueryTEST(new MBean[] {
0757: new MBean(new StringTest("Hello"),
0758: "Domain1:type=instance1"),
0759: new MBean(new StringTest("HeLlo"),
0760: "Domain1:type=instance2"), }, new MBean[] {
0761: new MBean(new StringTest("hell"),
0762: "Domain2:type=instance1"),
0763: new MBean(new StringTest("ello"),
0764: "Domain2:type=instance2"),
0765: new MBean(new StringTest("ell"),
0766: "Domain2:type=instance3"),
0767: new MBean(new StringTest("Helloo"),
0768: "Domain2:type=instance4"),
0769: new MBean(new StringTest("HHello"),
0770: "Domain2:type=instance5"),
0771: new MBean(new StringTest("HHelloo"),
0772: "Domain2:type=instance6"), }, Query.match(
0773: Query.attr("String"), Query.value("He[lL]lo")))
0774: .test();
0775: } catch (AssertionFailedError e) {
0776: fail("FAILS IN RI: expected Hello to match He[lL]lo");
0777: }
0778: }
0779:
0780: /**
0781: * Test match character range
0782: */
0783: public void testMatchCharacterRange() throws Exception {
0784: try {
0785: new QueryTEST(new MBean[] {
0786: new MBean(new StringTest("Hello"),
0787: "Domain1:type=instance1"),
0788: new MBean(new StringTest("Hemlo"),
0789: "Domain1:type=instance2"), }, new MBean[] {
0790: new MBean(new StringTest("hell"),
0791: "Domain2:type=instance1"),
0792: new MBean(new StringTest("He lo"),
0793: "Domain2:type=instance2"),
0794: new MBean(new StringTest("Heklo"),
0795: "Domain2:type=instance3"),
0796: new MBean(new StringTest("Henlo"),
0797: "Domain2:type=instance4"),
0798: new MBean(new StringTest("HeLlo"),
0799: "Domain2:type=instance5"),
0800: new MBean(new StringTest("HeMlo"),
0801: "Domain2:type=instance6"), }, Query.match(
0802: Query.attr("String"), Query.value("He[l-m]lo")))
0803: .test();
0804: } catch (AssertionFailedError e) {
0805: fail("FAILS IN RI: didn't expected HeMlo to match He[l-m]lo");
0806: }
0807: }
0808:
0809: /**
0810: * Test match escaping question mark
0811: */
0812: public void testEscapingQuestion() throws Exception {
0813: new QueryTEST(new MBean[] { new MBean(new StringTest("Hello?"),
0814: "Domain1:type=instance1"), }, new MBean[] { new MBean(
0815: new StringTest("Helloz"), "Domain2:type=instance1"), },
0816: Query.match(Query.attr("String"), Query
0817: .value("Hello\\?"))).test();
0818: }
0819:
0820: /**
0821: * Test match escaping asterisk
0822: */
0823: public void testEscapingAsterisk() throws Exception {
0824: new QueryTEST(new MBean[] { new MBean(new StringTest("Hello*"),
0825: "Domain1:type=instance1"), }, new MBean[] { new MBean(
0826: new StringTest("Helloz"), "Domain2:type=instance1"), },
0827: Query.match(Query.attr("String"), Query
0828: .value("Hello\\*"))).test();
0829: }
0830:
0831: /**
0832: * Test match escaping open bracket
0833: */
0834: public void testEscapingOpenBracket() throws Exception {
0835: new QueryTEST(new MBean[] { new MBean(new StringTest(
0836: "Hello[ab]"), "Domain1:type=instance1"), },
0837: new MBean[] {
0838: new MBean(new StringTest("Helloa"),
0839: "Domain2:type=instance1"),
0840: new MBean(new StringTest("Hello\\a"),
0841: "Domain2:type=instance2"), }, Query
0842: .match(Query.attr("String"), Query
0843: .value("Hello\\[ab]"))).test();
0844: }
0845:
0846: /**
0847: * Test match minus in character set
0848: */
0849: public void testMinusInCharacterSet() throws Exception {
0850: try {
0851: new QueryTEST(new MBean[] { new MBean(new StringTest(
0852: "Hello-"), "Domain1:type=instance1"), },
0853: new MBean[] { new MBean(
0854: new StringTest("Hello[ab-]"),
0855: "Domain2:type=instance1"), }, Query.match(
0856: Query.attr("String"), Query
0857: .value("Hello[ab-]"))).test();
0858: } catch (AssertionFailedError e) {
0859: fail("FAILS IN RI: expected Hello- to match Hello[ab-]");
0860: }
0861: }
0862:
0863: /**
0864: * Test threading, tests that running the same query in multiple threads
0865: * works. This test might not catch a threading problem on every run.
0866: */
0867: public void testThreading() throws Exception {
0868: MBeanServer server1 = MBeanServerFactory
0869: .createMBeanServer("server1");
0870: MBeanServer server2 = MBeanServerFactory
0871: .createMBeanServer("server2");
0872: try {
0873: ObjectName name = new ObjectName("Domain1:type=instance1");
0874: NumberTest bean1 = new NumberTest(1);
0875: NumberTest bean2 = new NumberTest(2);
0876: server1.registerMBean(bean1, name);
0877: server2.registerMBean(bean2, name);
0878: QueryExp query = Query.eq(Query.attr("Number"), Query
0879: .value(2));
0880: QueryThread thread1 = new QueryThread(server1, query, 0);
0881: QueryThread thread2 = new QueryThread(server2, query, 1);
0882: thread1.start();
0883: thread2.start();
0884: thread1.join(10000);
0885: thread1.check();
0886: thread2.join(10000);
0887: thread2.check();
0888: } finally {
0889: MBeanServerFactory.releaseMBeanServer(server1);
0890: MBeanServerFactory.releaseMBeanServer(server2);
0891: }
0892: }
0893:
0894: /**
0895: * Test pathological
0896: */
0897: public void testPathological() throws Exception {
0898: try {
0899: new QueryTEST(new MBean[] { new MBean(new StringTest(
0900: "Hello(?:.)"), "Domain1:type=instance1"), },
0901: new MBean[] { new MBean(new StringTest("Hellox"),
0902: "Domain2:type=instance1"), }, Query.match(
0903: Query.attr("String"), Query
0904: .value("Hello(?:.)"))).test();
0905: } catch (AssertionFailedError e) {
0906: fail("FAILS IN JBossMX: expected Hello(?:.) to match Hello(?:.)");
0907: }
0908: }
0909:
0910: // Support ----------------------------------------------------------------
0911:
0912: private void equalsTEST(ValueExp value1, ValueExp value2)
0913: throws Exception {
0914: // Test equals
0915: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
0916: "Domain1:type=instance1") }, new MBean[0], Query.eq(
0917: value1, value1)).test();
0918: // Test not equals
0919: new QueryTEST(new MBean[0], new MBean[] { new MBean(
0920: new NumberTest(0), "Domain1:type=instance1") }, Query
0921: .eq(value1, value2)).test();
0922: }
0923:
0924: private void operationTEST(ValueExp value1, ValueExp value2,
0925: ValueExp div, ValueExp minus, ValueExp mult, ValueExp plus)
0926: throws Exception {
0927: // Test div
0928: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
0929: "Domain1:type=instance1") }, new MBean[0], Query.eq(
0930: Query.div(value2, value1), div)).test();
0931: // Test minus
0932: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
0933: "Domain1:type=instance1") }, new MBean[0], Query.eq(
0934: Query.minus(value1, value2), minus)).test();
0935: // Test mult
0936: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
0937: "Domain1:type=instance1") }, new MBean[0], Query.eq(
0938: Query.times(value1, value2), mult)).test();
0939: // Test plus
0940: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
0941: "Domain1:type=instance1") }, new MBean[0], Query.eq(
0942: Query.plus(value1, value2), plus)).test();
0943: }
0944:
0945: private void catTEST(ValueExp value1, ValueExp value2, ValueExp cat)
0946: throws Exception {
0947: // Test cat
0948: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
0949: "Domain1:type=instance1") }, new MBean[0], Query.eq(
0950: Query.plus(value1, value2), cat)).test();
0951: }
0952:
0953: private void comparisonTEST(ValueExp value1, ValueExp value2)
0954: throws Exception {
0955: // Test greater than or equals (really greater than)
0956: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
0957: "Domain1:type=instance1") }, new MBean[0], Query.geq(
0958: value2, value1)).test();
0959: // Test greater than or equals (really greater equals)
0960: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
0961: "Domain1:type=instance1") }, new MBean[0], Query.geq(
0962: value1, value1)).test();
0963: // Test not greater than or equals
0964: new QueryTEST(new MBean[0], new MBean[] { new MBean(
0965: new NumberTest(0), "Domain1:type=instance1") }, Query
0966: .geq(value1, value2)).test();
0967: // Test greater than
0968: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
0969: "Domain1:type=instance1") }, new MBean[0], Query.gt(
0970: value2, value1)).test();
0971: // Test not greater than
0972: new QueryTEST(new MBean[0], new MBean[] { new MBean(
0973: new NumberTest(0), "Domain1:type=instance1") }, Query
0974: .gt(value1, value2)).test();
0975: // Test greater than or equals (really greater than)
0976: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
0977: "Domain1:type=instance1") }, new MBean[0], Query.leq(
0978: value1, value2)).test();
0979: // Test greater than or equals (really greater equals)
0980: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
0981: "Domain1:type=instance1") }, new MBean[0], Query.leq(
0982: value1, value1)).test();
0983: // Test not greater than or equals
0984: new QueryTEST(new MBean[0], new MBean[] { new MBean(
0985: new NumberTest(0), "Domain1:type=instance1") }, Query
0986: .leq(value2, value1)).test();
0987: // Test greater than
0988: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
0989: "Domain1:type=instance1") }, new MBean[0], Query.lt(
0990: value1, value2)).test();
0991: // Test not greater than
0992: new QueryTEST(new MBean[0], new MBean[] { new MBean(
0993: new NumberTest(0), "Domain1:type=instance1") }, Query
0994: .lt(value2, value1)).test();
0995: }
0996:
0997: private void betweenTEST(ValueExp value1, ValueExp value2,
0998: ValueExp value3) throws Exception {
0999: // Test between (really between)
1000: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
1001: "Domain1:type=instance1") }, new MBean[0], Query
1002: .between(value2, value1, value3)).test();
1003: // Test between (equals first)
1004: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
1005: "Domain1:type=instance1") }, new MBean[0], Query
1006: .between(value2, value2, value3)).test();
1007: // Test between (equals second)
1008: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
1009: "Domain1:type=instance1") }, new MBean[0], Query
1010: .between(value2, value1, value2)).test();
1011: // Test between (equals both)
1012: new QueryTEST(new MBean[] { new MBean(new NumberTest(0),
1013: "Domain1:type=instance1") }, new MBean[0], Query
1014: .between(value2, value2, value2)).test();
1015: // Test not between (first)
1016: new QueryTEST(new MBean[0], new MBean[] { new MBean(
1017: new NumberTest(0), "Domain1:type=instance1") }, Query
1018: .between(value1, value2, value3)).test();
1019: // Test not between (second)
1020: new QueryTEST(new MBean[0], new MBean[] { new MBean(
1021: new NumberTest(0), "Domain1:type=instance1") }, Query
1022: .between(value3, value1, value2)).test();
1023: }
1024:
1025: private void attrTEST(Object mbean, AttributeValueExp attr,
1026: ValueExp value1, ValueExp value2) throws Exception {
1027: // Test true
1028: new QueryTEST(new MBean[] { new MBean(mbean,
1029: "Domain1:type=instance1") }, new MBean[] { new MBean(
1030: new Trivial(), "Domain1:type=instance2") }, Query.eq(
1031: attr, value1)).test();
1032: // Test false
1033: new QueryTEST(new MBean[0], new MBean[] { new MBean(mbean,
1034: "Domain1:type=instance1") }, Query.eq(attr, value2))
1035: .test();
1036: }
1037:
1038: private void inTEST(ValueExp value1, ValueExp value2,
1039: ValueExp value3, ValueExp value4, ValueExp value5,
1040: ValueExp value6) throws Exception {
1041: // Test in first
1042: new QueryTEST(new MBean[] { new MBean(new Trivial(),
1043: "Domain1:type=instance1") }, new MBean[0], Query.in(
1044: value1, new ValueExp[] { value1, value2, value3,
1045: value4, value5, value6 })).test();
1046: // Test in last
1047: new QueryTEST(new MBean[] { new MBean(new Trivial(),
1048: "Domain1:type=instance1") }, new MBean[0], Query.in(
1049: value6, new ValueExp[] { value1, value2, value3,
1050: value4, value5, value6 })).test();
1051: // Test in not the first or last
1052: new QueryTEST(new MBean[] { new MBean(new Trivial(),
1053: "Domain1:type=instance1") }, new MBean[0], Query.in(
1054: value3, new ValueExp[] { value1, value2, value3,
1055: value4, value5, value6 })).test();
1056: // Test not in
1057: new QueryTEST(new MBean[0], new MBean[] { new MBean(
1058: new Trivial(), "Domain1:type=instance1") }, Query.in(
1059: value1, new ValueExp[] { value2, value3, value4,
1060: value5, value6 })).test();
1061: }
1062:
1063: private class MBean {
1064: public Object mbean;
1065: public ObjectName objectName;
1066:
1067: public MBean(Object object, String name) throws Exception {
1068: this .mbean = object;
1069: this .objectName = new ObjectName(name);
1070: }
1071: }
1072:
1073: private class QueryTEST {
1074: HashSet expectedInstances = new HashSet();
1075: HashSet expectedNames = new HashSet();
1076: QueryExp queryExp;
1077: MBeanServer server;
1078:
1079: public QueryTEST(MBean[] expected, MBean[] others,
1080: QueryExp queryExp) throws Exception {
1081: this .queryExp = queryExp;
1082:
1083: server = MBeanServerFactory.createMBeanServer();
1084:
1085: for (int i = 0; i < expected.length; i++) {
1086: ObjectInstance instance = server.registerMBean(
1087: expected[i].mbean, expected[i].objectName);
1088: expectedInstances.add(instance);
1089: expectedNames.add(instance.getObjectName());
1090: }
1091: for (int i = 0; i < others.length; i++)
1092: server.registerMBean(others[i].mbean,
1093: others[i].objectName);
1094: }
1095:
1096: public void test() throws Exception {
1097: try {
1098: testQueryMBeans();
1099: testQueryNames();
1100: } finally {
1101: MBeanServerFactory.releaseMBeanServer(server);
1102: }
1103: }
1104:
1105: public void testQueryMBeans() throws Exception {
1106: Set result = server.queryMBeans(null, queryExp);
1107:
1108: Iterator iterator = result.iterator();
1109: while (iterator.hasNext()) {
1110: ObjectInstance instance = (ObjectInstance) iterator
1111: .next();
1112: Iterator iterator2 = expectedInstances.iterator();
1113: boolean found = false;
1114: while (iterator2.hasNext()) {
1115: if (iterator2.next().equals(instance)) {
1116: iterator2.remove();
1117: found = true;
1118: break;
1119: }
1120: }
1121: if (found == false
1122: && instance.getObjectName().getDomain().equals(
1123: "JMImplementation") == false)
1124: fail("Unexpected instance "
1125: + instance.getObjectName() + "\nfor query "
1126: + queryExp);
1127: }
1128:
1129: iterator = expectedInstances.iterator();
1130: if (iterator.hasNext()) {
1131: ObjectInstance instance = (ObjectInstance) iterator
1132: .next();
1133: fail("Expected instance " + instance.getObjectName()
1134: + "\nfor query " + queryExp);
1135: }
1136: }
1137:
1138: public void testQueryNames() throws Exception {
1139: Set result = server.queryNames(null, queryExp);
1140:
1141: Iterator iterator = result.iterator();
1142: while (iterator.hasNext()) {
1143: ObjectName name = (ObjectName) iterator.next();
1144: Iterator iterator2 = expectedNames.iterator();
1145: boolean found = false;
1146: while (iterator2.hasNext()) {
1147: if (iterator2.next().equals(name)) {
1148: iterator2.remove();
1149: found = true;
1150: break;
1151: }
1152: }
1153: if (found == false
1154: && name.getDomain().equals("JMImplementation") == false)
1155: fail("Unexpected name " + name + "\nfor query "
1156: + queryExp);
1157: }
1158:
1159: iterator = expectedNames.iterator();
1160: if (iterator.hasNext()) {
1161: fail("Expected instance " + iterator.next()
1162: + "\nfor query " + queryExp);
1163: }
1164: }
1165: }
1166:
1167: public class QueryThread extends Thread {
1168: MBeanServer server;
1169: QueryExp query;
1170: int expected;
1171: int result;
1172:
1173: public QueryThread(MBeanServer server, QueryExp query,
1174: int expected) {
1175: this .server = server;
1176: this .query = query;
1177: this .expected = expected;
1178: }
1179:
1180: public int getExpected() {
1181: return expected;
1182: }
1183:
1184: public void check() {
1185: assertEquals(expected, result);
1186: }
1187:
1188: public void run() {
1189: for (int i = 0; i < 1000; i++) {
1190: result = server.queryNames(null, query).size();
1191: if (result != expected)
1192: return;
1193: }
1194: }
1195: }
1196: }
|