0001: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
0002:
0003: package jodd.bean;
0004:
0005: import java.math.BigDecimal;
0006: import java.sql.Date;
0007: import java.sql.Time;
0008: import java.sql.Timestamp;
0009: import java.util.HashMap;
0010: import java.util.Map;
0011:
0012: import jodd.bean.modifier.TrimStringsBeanModifier;
0013: import jodd.bean.test.Abean;
0014: import jodd.bean.test.Bbean;
0015: import jodd.bean.test.Cbean;
0016: import jodd.bean.test.FooBean;
0017: import jodd.bean.test.FooBean2;
0018: import jodd.bean.test.FooBean3;
0019: import jodd.bean.test.FooBean4;
0020: import jodd.bean.test.FooBeanSlim;
0021: import jodd.bean.test.Gig;
0022: import jodd.bean.test.XBean;
0023: import junit.framework.TestCase;
0024:
0025: public class BeanUtilTest extends TestCase {
0026:
0027: public void testSimpleProperty() {
0028: FooBean fb = new FooBean();
0029:
0030: // read non initialized property (null)
0031: assertNull(BeanUtil.getSimpleProperty(fb, "fooInteger", false));
0032: assertTrue(BeanUtil.hasProperty(fb, "fooInteger"));
0033: // set property
0034: BeanUtil.setSimpleProperty(fb, "fooInteger", new Integer(173),
0035: false);
0036: // read initialized property
0037: assertEquals(new Integer(173), BeanUtil.getSimpleProperty(fb,
0038: "fooInteger", false));
0039:
0040: // read non-initialized simple property (zero)
0041: assertEquals(new Integer(0), BeanUtil.getSimpleProperty(fb,
0042: "fooint", false));
0043: assertTrue(BeanUtil.hasProperty(fb, "fooint"));
0044: assertFalse(BeanUtil.hasProperty(fb, "fooint-xxx"));
0045:
0046: // read forced non-initialized property (not null)
0047: assertTrue(BeanUtil.hasProperty(fb, "fooByte"));
0048: assertEquals(new Byte((byte) 0), BeanUtil
0049: .getSimplePropertyForced(fb, "fooByte", false));
0050:
0051: Map m = new HashMap();
0052: // set property in map
0053: BeanUtil.setSimpleProperty(m, "foo", new Integer(173), false);
0054: // read property from map
0055: assertTrue(BeanUtil.hasProperty(m, "foo"));
0056: assertEquals(new Integer(173), BeanUtil.getSimpleProperty(m,
0057: "foo", false));
0058:
0059: // read non-initialized map property
0060: assertTrue(BeanUtil.hasProperty(fb, "fooMap"));
0061: assertNull(BeanUtil.getSimpleProperty(fb, "fooMap", false));
0062: // read forced non-initialized map property
0063: assertNotNull(BeanUtil.getSimplePropertyForced(fb, "fooMap",
0064: false));
0065:
0066: // read non-initialized list property
0067: assertTrue(BeanUtil.hasProperty(fb, "fooList"));
0068: assertNull(BeanUtil.getSimpleProperty(fb, "fooList", false));
0069: // read forced non-initialized list property
0070: assertNotNull(BeanUtil.getSimplePropertyForced(fb, "fooList",
0071: false));
0072:
0073: // read non-initialized array (null)
0074: assertTrue(BeanUtil.hasProperty(fb, "fooStringA"));
0075: assertNull(BeanUtil.getSimpleProperty(fb, "fooStringA", false));
0076: String[] tmp = new String[10];
0077: tmp[2] = "foo";
0078: // set array property
0079: BeanUtil.setSimpleProperty(fb, "fooStringA", tmp, false);
0080: // read array property
0081: tmp = (String[]) BeanUtil.getSimpleProperty(fb, "fooStringA",
0082: false);
0083: assertEquals("foo", tmp[2]);
0084:
0085: fb.setFooStringA(null);
0086: // read non-initialized array property
0087: assertTrue(BeanUtil.hasProperty(fb, "fooStringA"));
0088: assertNull(BeanUtil.getSimpleProperty(fb, "fooStringA", false));
0089: // read forced non-initialized array property
0090: assertNotNull(BeanUtil.getSimplePropertyForced(fb,
0091: "fooStringA", false));
0092: }
0093:
0094: public void testSimplePropertySlimPrivate() {
0095: FooBeanSlim fb = new FooBeanSlim();
0096:
0097: // read non initialized property (null)
0098: assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooInteger"));
0099: assertNull(BeanUtil.getSimpleProperty(fb, "fooInteger", true));
0100: // set property
0101: BeanUtil.setSimpleProperty(fb, "fooInteger", new Integer(173),
0102: true);
0103: // read initialized property
0104: assertEquals(new Integer(173), BeanUtil.getSimpleProperty(fb,
0105: "fooInteger", true));
0106:
0107: // read non-initialized simple property (zero)
0108: assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooint"));
0109: assertEquals(new Integer(0), BeanUtil.getSimpleProperty(fb,
0110: "fooint", true));
0111:
0112: // read forced non-initialized property (not null)
0113: assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooByte"));
0114: assertEquals(new Byte((byte) 0), BeanUtil
0115: .getSimplePropertyForced(fb, "fooByte", true));
0116:
0117: Map m = new HashMap();
0118: // set property in map
0119: assertFalse(BeanUtil.hasDeclaredProperty(m, "foo"));
0120: BeanUtil.setSimpleProperty(m, "foo", new Integer(173), true);
0121: // read property from map
0122: assertTrue(BeanUtil.hasDeclaredProperty(m, "foo"));
0123: assertEquals(new Integer(173), BeanUtil.getSimpleProperty(m,
0124: "foo", true));
0125:
0126: // read non-initialized map property
0127: assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooMap"));
0128: assertNull(BeanUtil.getSimpleProperty(fb, "fooMap", true));
0129: // read forced non-initialized map property
0130: assertNotNull(BeanUtil.getSimplePropertyForced(fb, "fooMap",
0131: true));
0132:
0133: // read non-initialized list property
0134: assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooList"));
0135: assertNull(BeanUtil.getSimpleProperty(fb, "fooList", true));
0136: // read forced non-initialized list property
0137: assertNotNull(BeanUtil.getSimplePropertyForced(fb, "fooList",
0138: true));
0139:
0140: // read non-initialized array (null)
0141: assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooStringA"));
0142: assertNull(BeanUtil.getSimpleProperty(fb, "fooStringA", true));
0143: String[] tmp = new String[10];
0144: tmp[2] = "foo";
0145: // set array property
0146: BeanUtil.setSimpleProperty(fb, "fooStringA", tmp, true);
0147: // read array property
0148: tmp = (String[]) BeanUtil.getSimpleProperty(fb, "fooStringA",
0149: true);
0150: assertEquals("foo", tmp[2]);
0151:
0152: fb = new FooBeanSlim();
0153: // read non-initialized array property
0154: assertNull(BeanUtil.getSimpleProperty(fb, "fooStringA", true));
0155: // read forced non-initialized array property
0156: assertNotNull(BeanUtil.getSimplePropertyForced(fb,
0157: "fooStringA", true));
0158: }
0159:
0160: public void testIndexProperty() {
0161: FooBean fb = new FooBean();
0162:
0163: // read forced non-initialized array property
0164: assertNull(fb.getFooStringA());
0165: assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooStringA[0]"));
0166: try {
0167: BeanUtil.getIndexProperty(fb, "fooStringA[0]", false, true);
0168: fail();
0169: } catch (ArrayIndexOutOfBoundsException aioobex) {
0170: // ignore
0171: }
0172: assertNotNull(fb.getFooStringA());
0173: assertEquals(0, fb.getFooStringA().length);
0174:
0175: // set array property (non-forced)
0176: assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooStringA[7]"));
0177: try {
0178: BeanUtil.setIndexProperty(fb, "fooStringA[7]", "xxx",
0179: false, false);
0180: fail();
0181: } catch (ArrayIndexOutOfBoundsException aioobex) {
0182: // ignore
0183: }
0184:
0185: // set forced array property
0186: BeanUtil.setIndexProperty(fb, "fooStringA[40]", "zzz", false,
0187: true);
0188: assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooStringA[40]"));
0189: assertEquals("zzz", fb.getFooStringA()[40]);
0190: assertEquals(41, fb.getFooStringA().length);
0191:
0192: // set null
0193: assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooStringA[43]"));
0194: BeanUtil.setIndexProperty(fb, "fooStringA[43]", null, false,
0195: true);
0196: assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooStringA[43]"));
0197: assertNull(fb.getFooStringA()[43]);
0198: assertEquals(44, fb.getFooStringA().length);
0199:
0200: // get forced
0201: assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooStringA[15]"));
0202: assertNotNull(BeanUtil.getIndexProperty(fb, "fooStringA[15]",
0203: false, true));
0204: assertNull(fb.getFooStringA()[0]);
0205: assertNotNull(fb.getFooStringA()[15]);
0206:
0207: // set uninitialized array property
0208: fb.setFooStringA(null);
0209: BeanUtil.setIndexProperty(fb, "fooStringA[7]", "ccc", false,
0210: true);
0211: assertEquals("ccc", fb.getFooStringA()[7]);
0212:
0213: // read forced non-initialized list property
0214: assertNull(fb.getFooList());
0215: assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooList[1]"));
0216: try {
0217: BeanUtil.getIndexProperty(fb, "fooList[1]", false, true);
0218: fail();
0219: } catch (IndexOutOfBoundsException ioobex) {
0220: // ignore
0221: }
0222: assertNotNull(fb.getFooList());
0223:
0224: // set list property (non-forced)
0225: try {
0226: BeanUtil.setIndexProperty(fb, "fooList[1]", "xxx", false,
0227: false);
0228: fail();
0229: } catch (IndexOutOfBoundsException ioobex) {
0230: // ignore
0231: }
0232:
0233: // set forced list property
0234: assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooList[40]"));
0235: BeanUtil
0236: .setIndexProperty(fb, "fooList[40]", "zzz", false, true);
0237: assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooList[40]"));
0238: assertEquals("zzz", fb.getFooList().get(40));
0239: assertEquals(41, fb.getFooList().size());
0240:
0241: // set forced unitialized list property
0242: fb.setFooList(null);
0243: assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooList[1]"));
0244: BeanUtil.setIndexProperty(fb, "fooList[1]", "xxx", false, true);
0245: assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooList[1]"));
0246: assertEquals("xxx", fb.getFooList().get(1));
0247: assertEquals(2, fb.getFooList().size());
0248:
0249: // read forced non-initialized map property
0250: assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooMap[foo]"));
0251: assertNull(BeanUtil.getIndexProperty(fb, "fooMap[foo]", false,
0252: true));
0253: assertNotNull(fb.getFooMap());
0254: // set non-initialized map property
0255: fb.setFooMap(null);
0256: assertFalse(BeanUtil.hasDeclaredProperty(fb, "fooMap[foo]"));
0257: BeanUtil
0258: .setIndexProperty(fb, "fooMap[foo]", "xxx", false, true);
0259: assertTrue(BeanUtil.hasDeclaredProperty(fb, "fooMap[foo]"));
0260: assertEquals("xxx", fb.getFooMap().get("foo"));
0261: assertEquals(1, fb.getFooMap().size());
0262: }
0263:
0264: public void testIndexPropertySlimPrivate() {
0265: FooBeanSlim fb = new FooBeanSlim();
0266:
0267: // read forced non-initialized array property
0268: assertNull(fb.getStringA());
0269: try {
0270: BeanUtil.getIndexProperty(fb, "fooStringA[0]", true, true);
0271: fail();
0272: } catch (ArrayIndexOutOfBoundsException aioobex) {
0273: // ignore
0274: }
0275: assertNotNull(fb.getStringA());
0276: assertEquals(0, fb.getStringA().length);
0277:
0278: // set array property (non-forced)
0279: try {
0280: BeanUtil.setIndexProperty(fb, "fooStringA[7]", "xxx", true,
0281: false);
0282: fail();
0283: } catch (ArrayIndexOutOfBoundsException aioobex) {
0284: // ignore
0285: }
0286:
0287: // set forced array property
0288: BeanUtil.setIndexProperty(fb, "fooStringA[40]", "zzz", true,
0289: true);
0290: assertEquals("zzz", fb.getStringA()[40]);
0291: assertEquals(41, fb.getStringA().length);
0292:
0293: BeanUtil.setIndexProperty(fb, "fooStringA[43]", null, true,
0294: true);
0295: assertNull(fb.getStringA()[43]);
0296: assertEquals(44, fb.getStringA().length);
0297:
0298: // set uninitialized array property
0299: fb = new FooBeanSlim();
0300: assertNull(fb.getStringA());
0301: BeanUtil.setIndexProperty(fb, "fooStringA[7]", "ccc", true,
0302: true);
0303: assertNotNull(fb.getStringA());
0304: assertEquals("ccc", fb.getStringA()[7]);
0305:
0306: // read forced non-initialized list property
0307: assertNull(fb.getList());
0308: try {
0309: BeanUtil.getIndexProperty(fb, "fooList[1]", true, true);
0310: fail();
0311: } catch (IndexOutOfBoundsException ioobex) {
0312: // ignore
0313: }
0314: assertNotNull(fb.getList());
0315:
0316: // set list property (non-forced)
0317: try {
0318: BeanUtil.setIndexProperty(fb, "fooList[1]", "xxx", true,
0319: false);
0320: fail();
0321: } catch (IndexOutOfBoundsException ioobex) {
0322: // ignore
0323: }
0324:
0325: // set forced list property
0326: BeanUtil.setIndexProperty(fb, "fooList[40]", "zzz", true, true);
0327: assertEquals("zzz", fb.getList().get(40));
0328: assertEquals(41, fb.getList().size());
0329:
0330: // set forced unitialized list property
0331: fb = new FooBeanSlim();
0332: BeanUtil.setIndexProperty(fb, "fooList[1]", "xxx", true, true);
0333: assertEquals("xxx", fb.getList().get(1));
0334:
0335: // read forced non-initialized map property
0336: assertNull(fb.getMap());
0337: assertNull(BeanUtil.getIndexProperty(fb, "fooMap[foo]", true,
0338: true));
0339: assertNotNull(fb.getMap());
0340:
0341: // set non-initialized map property
0342: fb = new FooBeanSlim();
0343: assertNull(fb.getMap());
0344: BeanUtil.setIndexProperty(fb, "fooMap[foo]", "xxx", true, true);
0345: assertNotNull(fb.getMap());
0346: assertEquals("xxx", fb.getMap().get("foo"));
0347: }
0348:
0349: // ---------------------------------------------------------------- types
0350:
0351: public void testSetPropertyNumbers() {
0352: FooBean fb = new FooBean();
0353:
0354: // Integer
0355: String propName = "fooInteger";
0356: BeanUtil.setProperty(fb, propName, new Integer(1));
0357: assertEquals(1, fb.getFooInteger().intValue());
0358: BeanUtil.setProperty(fb, propName, null);
0359: assertNull(fb.getFooInteger());
0360: BeanUtil.setProperty(fb, propName, "2"); // valid string
0361: assertEquals(2, fb.getFooInteger().intValue());
0362: try {
0363: BeanUtil.setProperty(fb, propName, "x"); // invalid string - value stays the same
0364: fail();
0365: } catch (Exception ex) {
0366: }
0367: assertEquals(2, fb.getFooInteger().intValue());
0368:
0369: // int
0370: propName = "fooint";
0371: BeanUtil.setProperty(fb, propName, new Integer(1));
0372: assertEquals(1, fb.getFooint());
0373:
0374: try {
0375: BeanUtil.setProperty(fb, propName, null); // null is not an int
0376: fail();
0377: } catch (Exception ex) {
0378: }
0379: assertEquals(1, fb.getFooint());
0380:
0381: BeanUtil.setProperty(fb, propName, "2");
0382: assertEquals(2, fb.getFooint());
0383:
0384: try {
0385: BeanUtil.setProperty(fb, propName, "w"); // invalid string
0386: fail();
0387: } catch (Exception ex) {
0388: }
0389: assertEquals(2, fb.getFooint());
0390:
0391: // Long
0392: propName = "fooLong";
0393: BeanUtil.setProperty(fb, propName, new Long(1));
0394: assertEquals(1L, fb.getFooLong().longValue());
0395: BeanUtil.setProperty(fb, propName, new Integer(3));
0396: assertEquals(3L, fb.getFooLong().longValue());
0397: BeanUtil.setProperty(fb, propName, null);
0398: assertNull(fb.getFooLong());
0399: BeanUtil.setProperty(fb, propName, "2"); // valid string
0400: assertEquals(2L, fb.getFooLong().longValue());
0401:
0402: try {
0403: BeanUtil.setProperty(fb, propName, "x"); // invalid string - value stays the same
0404: fail();
0405: } catch (Exception ex) {
0406: }
0407: assertEquals(2L, fb.getFooLong().longValue());
0408:
0409: // long
0410: propName = "foolong";
0411: BeanUtil.setProperty(fb, propName, new Long(1));
0412: assertEquals(1L, fb.getFoolong());
0413: BeanUtil.setProperty(fb, propName, new Integer(3));
0414: assertEquals(3L, fb.getFoolong());
0415:
0416: try {
0417: BeanUtil.setProperty(fb, propName, null); // null is not a long
0418: fail();
0419: } catch (Exception ex) {
0420: }
0421:
0422: assertEquals(3L, fb.getFoolong());
0423: BeanUtil.setProperty(fb, propName, "2"); // valid string
0424: assertEquals(2L, fb.getFoolong());
0425: try {
0426: BeanUtil.setProperty(fb, propName, "w"); // invalid string
0427: fail();
0428: } catch (Exception ex) {
0429: }
0430: assertEquals(2L, fb.getFoolong());
0431:
0432: // Byte
0433: propName = "fooByte";
0434: BeanUtil.setProperty(fb, propName, new Byte((byte) 1));
0435: assertEquals(1, fb.getFooByte().byteValue());
0436: BeanUtil.setProperty(fb, propName, new Integer(3));
0437: assertEquals(3, fb.getFooByte().byteValue());
0438: BeanUtil.setProperty(fb, propName, new Integer(257));
0439: assertEquals(1, fb.getFooByte().byteValue()); // lower byte of 257
0440: BeanUtil.setProperty(fb, propName, null);
0441: assertNull(fb.getFooByte());
0442: BeanUtil.setProperty(fb, propName, "2"); // valid string
0443: assertEquals(2, fb.getFooByte().byteValue());
0444:
0445: try {
0446: BeanUtil.setProperty(fb, propName, "x"); // invalid string - value stays the same
0447: fail();
0448: } catch (Exception ex) {
0449: }
0450: assertEquals(2, fb.getFooByte().byteValue());
0451:
0452: // byte
0453: propName = "foobyte";
0454: BeanUtil.setProperty(fb, propName, new Byte((byte) 1));
0455: assertEquals(1, fb.getFoobyte());
0456: BeanUtil.setProperty(fb, propName, new Integer(3));
0457: assertEquals(3, fb.getFoobyte());
0458: BeanUtil.setProperty(fb, propName, new Integer(257));
0459: assertEquals(1, fb.getFoobyte());
0460: try {
0461: BeanUtil.setProperty(fb, propName, null); // null is not a byte
0462: fail();
0463: } catch (Exception ex) {
0464: }
0465: assertEquals(1, fb.getFoobyte());
0466: BeanUtil.setProperty(fb, propName, "2"); // valid string
0467: assertEquals(2, fb.getFoobyte());
0468: try {
0469: BeanUtil.setProperty(fb, propName, "x"); // invalid string - value stays the same
0470: fail();
0471: } catch (Exception ex) {
0472: }
0473: assertEquals(2, fb.getFoobyte());
0474:
0475: // Boolean
0476: propName = "fooBoolean";
0477: BeanUtil.setProperty(fb, propName, Boolean.TRUE);
0478: assertTrue(fb.getFooBoolean().booleanValue());
0479: BeanUtil.setProperty(fb, propName, Boolean.FALSE);
0480: assertFalse(fb.getFooBoolean().booleanValue());
0481: BeanUtil.setProperty(fb, propName, null);
0482: assertNull(fb.getFooBoolean());
0483: BeanUtil.setProperty(fb, propName, "yes");
0484: assertTrue(fb.getFooBoolean().booleanValue());
0485: BeanUtil.setProperty(fb, propName, "y");
0486: assertTrue(fb.getFooBoolean().booleanValue());
0487: BeanUtil.setProperty(fb, propName, "true");
0488: assertTrue(fb.getFooBoolean().booleanValue());
0489: BeanUtil.setProperty(fb, propName, "on");
0490: assertTrue(fb.getFooBoolean().booleanValue());
0491: BeanUtil.setProperty(fb, propName, "1");
0492: assertTrue(fb.getFooBoolean().booleanValue());
0493: BeanUtil.setProperty(fb, propName, "no");
0494: assertFalse(fb.getFooBoolean().booleanValue());
0495: BeanUtil.setProperty(fb, propName, "n");
0496: assertFalse(fb.getFooBoolean().booleanValue());
0497: BeanUtil.setProperty(fb, propName, "false");
0498: assertFalse(fb.getFooBoolean().booleanValue());
0499: BeanUtil.setProperty(fb, propName, "off");
0500: assertFalse(fb.getFooBoolean().booleanValue());
0501: BeanUtil.setProperty(fb, propName, "0");
0502: assertFalse(fb.getFooBoolean().booleanValue());
0503:
0504: // boolean
0505: propName = "fooboolean";
0506: BeanUtil.setProperty(fb, propName, Boolean.TRUE);
0507: assertTrue(fb.getFooboolean());
0508: BeanUtil.setProperty(fb, propName, Boolean.FALSE);
0509: assertFalse(fb.getFooboolean());
0510:
0511: try {
0512: BeanUtil.setProperty(fb, propName, null);
0513: fail();
0514: } catch (Exception ex) {
0515: }
0516:
0517: assertFalse(fb.getFooboolean());
0518: BeanUtil.setProperty(fb, propName, "yes");
0519: assertTrue(fb.getFooboolean());
0520: BeanUtil.setProperty(fb, propName, "y");
0521: assertTrue(fb.getFooboolean());
0522: BeanUtil.setProperty(fb, propName, "true");
0523: assertTrue(fb.getFooboolean());
0524: BeanUtil.setProperty(fb, propName, "on");
0525: assertTrue(fb.getFooboolean());
0526: BeanUtil.setProperty(fb, propName, "1");
0527: assertTrue(fb.getFooboolean());
0528: BeanUtil.setProperty(fb, propName, "no");
0529: assertFalse(fb.getFooboolean());
0530: BeanUtil.setProperty(fb, propName, "n");
0531: assertFalse(fb.getFooboolean());
0532: BeanUtil.setProperty(fb, propName, "false");
0533: assertFalse(fb.getFooboolean());
0534: BeanUtil.setProperty(fb, propName, "off");
0535: assertFalse(fb.getFooboolean());
0536: BeanUtil.setProperty(fb, propName, "0");
0537: assertFalse(fb.getFooboolean());
0538:
0539: // Float
0540: propName = "fooFloat";
0541: BeanUtil.setProperty(fb, propName, new Float(1.1));
0542: assertEquals(1.1, fb.getFooFloat().floatValue(), 0.0005);
0543: BeanUtil.setProperty(fb, propName, new Integer(3));
0544: assertEquals(3.0, fb.getFooFloat().floatValue(), 0.0005);
0545: BeanUtil.setProperty(fb, propName, null);
0546: assertNull(fb.getFooFloat());
0547: BeanUtil.setProperty(fb, propName, "2.2"); // valid string
0548: assertEquals(2.2, fb.getFooFloat().floatValue(), 0.0005);
0549: try {
0550: BeanUtil.setProperty(fb, propName, "x"); // invalid string - value stays the same
0551: fail();
0552: } catch (Exception ex) {
0553: }
0554: assertEquals(2.2, fb.getFooFloat().floatValue(), 0.0005);
0555:
0556: // float
0557: propName = "foofloat";
0558: BeanUtil.setProperty(fb, propName, new Float(1.1));
0559: assertEquals(1.1, fb.getFoofloat(), 0.0005);
0560: BeanUtil.setProperty(fb, propName, new Integer(3));
0561: assertEquals(3.0, fb.getFoofloat(), 0.0005);
0562: try {
0563: BeanUtil.setProperty(fb, propName, null); // null is not a long
0564: fail();
0565: } catch (Exception ex) {
0566: }
0567: assertEquals(3.0, fb.getFoofloat(), 0.0005);
0568: BeanUtil.setProperty(fb, propName, "2.2"); // valid string
0569:
0570: assertEquals(2.2, fb.getFoofloat(), 0.0005);
0571: try {
0572: BeanUtil.setProperty(fb, propName, "w"); // invalid string
0573: fail();
0574: } catch (Exception ex) {
0575: }
0576: assertEquals(2.2, fb.getFoofloat(), 0.0005);
0577:
0578: // Double
0579: propName = "fooDouble";
0580: BeanUtil.setProperty(fb, propName, new Double(1.1));
0581: assertEquals(1.1, fb.getFooDouble().doubleValue(), 0.0005);
0582: BeanUtil.setProperty(fb, propName, new Integer(3));
0583: assertEquals(3.0, fb.getFooDouble().doubleValue(), 0.0005);
0584: BeanUtil.setProperty(fb, propName, null);
0585: assertNull(fb.getFooDouble());
0586: BeanUtil.setProperty(fb, propName, "2.2"); // valid string
0587: assertEquals(2.2, fb.getFooDouble().doubleValue(), 0.0005);
0588: try {
0589: BeanUtil.setProperty(fb, propName, "x"); // invalid string - value stays the same
0590: fail();
0591: } catch (Exception ex) {
0592: }
0593: assertEquals(2.2, fb.getFooDouble().doubleValue(), 0.0005);
0594:
0595: // double
0596: propName = "foodouble";
0597: BeanUtil.setProperty(fb, propName, new Double(1.1));
0598: assertEquals(1.1, fb.getFoodouble(), 0.0005);
0599: BeanUtil.setProperty(fb, propName, new Integer(3));
0600: assertEquals(3.0, fb.getFoodouble(), 0.0005);
0601: try {
0602: BeanUtil.setProperty(fb, propName, null); // null is not a long
0603: fail();
0604: } catch (Exception ex) {
0605: }
0606: assertEquals(3.0, fb.getFoodouble(), 0.0005);
0607: BeanUtil.setProperty(fb, propName, "2.2"); // valid string
0608:
0609: assertEquals(2.2, fb.getFoodouble(), 0.0005);
0610: try {
0611: BeanUtil.setProperty(fb, propName, "w"); // invalid string
0612: fail();
0613: } catch (Exception ex) {
0614: }
0615: assertEquals(2.2, fb.getFoodouble(), 0.0005);
0616: }
0617:
0618: public void testSetPropertySql() {
0619: FooBean2 fb = new FooBean2();
0620:
0621: String propName = "fooTimestamp";
0622: //noinspection deprecation
0623: Timestamp ts = new Timestamp(101, 0, 17, 1, 2, 3, 4);
0624: BeanUtil.setProperty(fb, propName, ts);
0625: assertEquals("2001-01-17 01:02:03.000000004", fb
0626: .getFooTimestamp().toString());
0627:
0628: propName = "fooTime";
0629: //noinspection deprecation
0630: Time t = new Time(17, 13, 15);
0631: BeanUtil.setProperty(fb, propName, t);
0632:
0633: assertEquals("17:13:15", fb.getFooTime().toString());
0634:
0635: propName = "fooDate";
0636: //noinspection deprecation
0637: Date d = new Date(101, 1, 17);
0638: BeanUtil.setProperty(fb, propName, d);
0639: assertEquals("2001-02-17", fb.getFooDate().toString());
0640: }
0641:
0642: public void testSetPropertyMath() {
0643: FooBean2 fb = new FooBean2();
0644: String propName = "fooBigDecimal";
0645: BeanUtil.setProperty(fb, propName, new BigDecimal(1.2));
0646: assertEquals(1.2, fb.getFooBigDecimal().doubleValue(), 0.0005);
0647: }
0648:
0649: public void testSetPropertyString() {
0650: FooBean fb = new FooBean();
0651:
0652: // String
0653: String propName = "fooString";
0654: BeanUtil.setProperty(fb, propName, "string");
0655: assertEquals("string", fb.getFooString());
0656: BeanUtil.setProperty(fb, propName, null);
0657: assertNull(fb.getFooString());
0658:
0659: // String array
0660: propName = "fooStringA";
0661: String[] sa = new String[] { "one", "two", "three" };
0662: BeanUtil.setProperty(fb, propName, sa);
0663: assertEquals("one", fb.getFooStringA()[0]);
0664: assertEquals("two", fb.getFooStringA()[1]);
0665: assertEquals("three", fb.getFooStringA()[2]);
0666: BeanUtil.setProperty(fb, propName, "just a string");
0667: sa = (String[]) BeanUtil.getProperty(fb, propName);
0668: assertEquals(1, sa.length);
0669: assertEquals("just a string", sa[0]);
0670:
0671: // Character
0672: propName = "fooCharacter";
0673: BeanUtil.setProperty(fb, propName, new Character('a'));
0674: assertEquals('a', fb.getFooCharacter().charValue());
0675: BeanUtil.setProperty(fb, propName, "123");
0676: assertEquals('1', fb.getFooCharacter().charValue());
0677: BeanUtil.setProperty(fb, propName, new Integer(789));
0678: assertEquals('7', fb.getFooCharacter().charValue());
0679:
0680: // char
0681: propName = "foochar";
0682: BeanUtil.setProperty(fb, propName, new Character('a'));
0683: assertEquals('a', fb.getFoochar());
0684: BeanUtil.setProperty(fb, propName, "123");
0685: assertEquals('1', fb.getFoochar());
0686: BeanUtil.setProperty(fb, propName, new Integer(789));
0687: assertEquals('7', fb.getFoochar());
0688: }
0689:
0690: public void testLoaders() {
0691: HashMap map = new HashMap();
0692:
0693: map.put("fooInteger", new Integer(1));
0694: map.put("fooint", new Integer(2));
0695: map.put("fooLong", new Long(3));
0696: map.put("foolong", new Long(4));
0697: map.put("fooByte", new Byte((byte) 5));
0698: map.put("foobyte", new Byte((byte) 6));
0699: map.put("fooCharacter", new Character('7'));
0700: map.put("foochar", new Character('8'));
0701: map.put("fooBoolean", Boolean.TRUE);
0702: map.put("fooboolean", Boolean.FALSE);
0703: map.put("fooFloat", new Float(9.0));
0704: map.put("foofloat", new Float(10.0));
0705: map.put("fooDouble", new Double(11.0));
0706: map.put("foodouble", new Double(12.0));
0707: map.put("fooString", "13");
0708: map.put("fooStringA", new String[] { "14", "15" });
0709:
0710: FooBean fb = new FooBean();
0711: BeanUtil.load(fb, map);
0712:
0713: assertEquals(1, fb.getFooInteger().intValue());
0714: assertEquals(2, fb.getFooint());
0715: assertEquals(3, fb.getFooLong().longValue());
0716: assertEquals(4, fb.getFoolong());
0717: assertEquals(5, fb.getFooByte().byteValue());
0718: assertEquals(6, fb.getFoobyte());
0719: assertEquals('7', fb.getFooCharacter().charValue());
0720: assertEquals('8', fb.getFoochar());
0721: assertTrue(fb.getFooBoolean().booleanValue());
0722: assertFalse(fb.getFooboolean());
0723: assertEquals(9.0, fb.getFooFloat().floatValue(), 0.005);
0724: assertEquals(10.0, fb.getFoofloat(), 0.005);
0725: assertEquals(11.0, fb.getFooDouble().doubleValue(), 0.005);
0726: assertEquals(12.0, fb.getFoodouble(), 0.005);
0727: assertEquals("13", fb.getFooString());
0728: assertEquals("14", fb.getFooStringA()[0]);
0729: assertEquals("15", fb.getFooStringA()[1]);
0730:
0731: map.put("FooInteger", new Integer(1));
0732: map.put("Fooint", new Integer(2));
0733: map.put("FooLong", new Long(3));
0734: map.put("Foolong", new Long(4));
0735: map.put("FooByte", new Byte((byte) 5));
0736: map.put("Foobyte", new Byte((byte) 6));
0737: map.put("FooCharacter", new Character('7'));
0738: map.put("Foochar", new Character('8'));
0739: map.put("FooBoolean", Boolean.TRUE);
0740: map.put("Fooboolean", Boolean.FALSE);
0741: map.put("FooFloat", new Float(9.0));
0742: map.put("Foofloat", new Float(10.0));
0743: map.put("FooDouble", new Double(11.0));
0744: map.put("Foodouble", new Double(12.0));
0745: map.put("FooString", "13");
0746: map.put("FooStringA", new String[] { "14", "15" });
0747:
0748: fb = new FooBean();
0749: BeanUtil.load(fb, map);
0750:
0751: assertEquals(1, fb.getFooInteger().intValue());
0752: assertEquals(2, fb.getFooint());
0753: assertEquals(3, fb.getFooLong().longValue());
0754: assertEquals(4, fb.getFoolong());
0755: assertEquals(5, fb.getFooByte().byteValue());
0756: assertEquals(6, fb.getFoobyte());
0757: assertEquals('7', fb.getFooCharacter().charValue());
0758: assertEquals('8', fb.getFoochar());
0759: assertTrue(fb.getFooBoolean().booleanValue());
0760: assertFalse(fb.getFooboolean());
0761: assertEquals(9.0, fb.getFooFloat().floatValue(), 0.005);
0762: assertEquals(10.0, fb.getFoofloat(), 0.005);
0763: assertEquals(11.0, fb.getFooDouble().doubleValue(), 0.005);
0764: assertEquals(12.0, fb.getFoodouble(), 0.005);
0765: assertEquals("13", fb.getFooString());
0766: assertEquals("14", fb.getFooStringA()[0]);
0767: assertEquals("15", fb.getFooStringA()[1]);
0768:
0769: }
0770:
0771: public void testForEach() {
0772: FooBean fb = new FooBean();
0773: fb.setFooString(" xxx ");
0774: fb.setFooStringA(new String[] { " xxx ", " yy ", " z " });
0775: new TrimStringsBeanModifier().modify(fb);
0776: assertEquals("xxx", fb.getFooString());
0777: assertEquals("xxx", fb.getFooStringA()[0]);
0778: assertEquals("yy", fb.getFooStringA()[1]);
0779: assertEquals("z", fb.getFooStringA()[2]);
0780: }
0781:
0782: public void testGet() {
0783: FooBean fb = new FooBean();
0784: fb.setFooInteger(new Integer(101));
0785: fb.setFooint(102);
0786: fb.setFooLong(new Long(103));
0787: fb.setFoolong(104);
0788: fb.setFooByte(new Byte((byte) 105));
0789: fb.setFoobyte((byte) 106);
0790: fb.setFooCharacter(new Character('7'));
0791: fb.setFoochar('8');
0792: fb.setFooBoolean(Boolean.TRUE);
0793: fb.setFooboolean(false);
0794: fb.setFooFloat(new Float(109.0));
0795: fb.setFoofloat((float) 110.0);
0796: fb.setFooDouble(new Double(111.0));
0797: fb.setFoodouble(112.0);
0798: fb.setFooString("113");
0799: fb.setFooStringA(new String[] { "114", "115" });
0800:
0801: Integer v = (Integer) BeanUtil.getProperty(fb, "fooInteger");
0802: assertEquals(101, v.intValue());
0803: v = (Integer) BeanUtil.getProperty(fb, "fooint");
0804: assertEquals(102, v.intValue());
0805: Long vl = (Long) BeanUtil.getProperty(fb, "fooLong");
0806: assertEquals(103, vl.longValue());
0807: vl = (Long) BeanUtil.getProperty(fb, "foolong");
0808: assertEquals(104, vl.longValue());
0809: Byte vb = (Byte) BeanUtil.getProperty(fb, "fooByte");
0810: assertEquals(105, vb.intValue());
0811: vb = (Byte) BeanUtil.getProperty(fb, "foobyte");
0812: assertEquals(106, vb.intValue());
0813: Character c = (Character) BeanUtil.getProperty(fb,
0814: "fooCharacter");
0815: assertEquals('7', c.charValue());
0816: c = (Character) BeanUtil.getProperty(fb, "foochar");
0817: assertEquals('8', c.charValue());
0818: Boolean b = (Boolean) BeanUtil.getProperty(fb, "fooBoolean");
0819: assertTrue(b.booleanValue());
0820: b = (Boolean) BeanUtil.getProperty(fb, "fooboolean");
0821: assertFalse(b.booleanValue());
0822: Float f = (Float) BeanUtil.getProperty(fb, "fooFloat");
0823: assertEquals(109.0, f.floatValue(), 0.005);
0824: f = (Float) BeanUtil.getProperty(fb, "foofloat");
0825: assertEquals(110.0, f.floatValue(), 0.005);
0826: Double d = (Double) BeanUtil.getProperty(fb, "fooDouble");
0827: assertEquals(111.0, d.doubleValue(), 0.005);
0828: d = (Double) BeanUtil.getProperty(fb, "foodouble");
0829: assertEquals(112.0, d.doubleValue(), 0.005);
0830: String s = (String) BeanUtil.getProperty(fb, "fooString");
0831: assertEquals("113", s);
0832: String[] sa = (String[]) BeanUtil.getProperty(fb, "fooStringA");
0833: assertEquals(2, sa.length);
0834: assertEquals("114", sa[0]);
0835: assertEquals("115", sa[1]);
0836: }
0837:
0838: public void testNested() {
0839: Cbean cbean = new Cbean();
0840: String value = "testnest";
0841: String value2 = "nesttest";
0842: assertTrue(BeanUtil.hasDeclaredProperty(cbean,
0843: "bbean.abean.fooProp"));
0844: BeanUtil.setProperty(cbean, "bbean.abean.fooProp", value);
0845: assertEquals(value, (String) BeanUtil.getProperty(cbean,
0846: "bbean.abean.fooProp"));
0847: Bbean bbean = (Bbean) BeanUtil.getProperty(cbean, "bbean");
0848: assertTrue(BeanUtil.hasDeclaredProperty(bbean, "abean.fooProp"));
0849: assertEquals(value, (String) BeanUtil.getProperty(bbean,
0850: "abean.fooProp"));
0851: Abean abean = (Abean) BeanUtil.getProperty(bbean, "abean");
0852: assertEquals(value, (String) BeanUtil.getProperty(abean,
0853: "fooProp"));
0854: BeanUtil.setProperty(bbean, "abean.fooProp", value2);
0855: assertEquals(value2, (String) BeanUtil.getProperty(bbean,
0856: "abean.fooProp"));
0857: }
0858:
0859: public void testIster() {
0860: Abean abean = new Abean();
0861: Boolean b = (Boolean) BeanUtil.getProperty(abean, "something");
0862: assertTrue(b.booleanValue());
0863: try {
0864: BeanUtil.getProperty(abean, "Something");
0865: fail();
0866: } catch (BeanException bex) {
0867: // ignore
0868: }
0869:
0870: }
0871:
0872: public void testMap() {
0873: Cbean cbean = new Cbean();
0874: Abean abean = cbean.getBbean().getAbean();
0875: BeanUtil.setProperty(abean, "mval", new Integer(173));
0876: BeanUtil.setProperty(abean, "mval2", new Integer(1));
0877: assertEquals(173, (abean.get("mval")).intValue());
0878: assertEquals(173, ((Integer) BeanUtil
0879: .getProperty(abean, "mval")).intValue());
0880: assertEquals(1,
0881: ((Integer) BeanUtil.getProperty(abean, "mval2"))
0882: .intValue());
0883: assertTrue(BeanUtil.hasDeclaredProperty(cbean,
0884: "bbean.abean.mval"));
0885: BeanUtil.setProperty(cbean, "bbean.abean.mval", new Integer(3));
0886: assertEquals(3, ((Integer) BeanUtil.getProperty(abean, "mval"))
0887: .intValue());
0888: assertEquals(3, ((Integer) BeanUtil.getProperty(cbean,
0889: "bbean.abean.mval")).intValue());
0890: HashMap map = new HashMap();
0891: BeanUtil.setProperty(map, "val1", new Integer(173));
0892: assertEquals(173, ((Integer) map.get("val1")).intValue());
0893: Integer i = (Integer) BeanUtil.getProperty(map, "val1");
0894: assertEquals(173, i.intValue());
0895: }
0896:
0897: public void testMap3() {
0898: Map m = new HashMap();
0899: BeanUtil.setProperty(m, "Foo", "John");
0900: assertEquals("John", (String) m.get("Foo"));
0901: assertNull(m.get("foo"));
0902: assertFalse(BeanUtil.hasDeclaredProperty(m, "foo"));
0903: BeanUtil.setProperty(m, "foo", new HashMap());
0904: assertTrue(BeanUtil.hasDeclaredProperty(m, "foo"));
0905: assertFalse(BeanUtil.hasDeclaredProperty(m, "foo.Name"));
0906: BeanUtil.setProperty(m, "foo.Name", "Doe");
0907: assertEquals("John", (String) m.get("Foo"));
0908: assertEquals("Doe", ((HashMap) m.get("foo")).get("Name"));
0909: assertNull("Doe", ((HashMap) m.get("foo")).get("name"));
0910: assertEquals("John", (String) BeanUtil.getProperty(m, "Foo"));
0911: assertEquals("Doe", (String) BeanUtil
0912: .getProperty(m, "foo.Name"));
0913: try {
0914: assertNull(BeanUtil.getProperty(m, "foo.name"));
0915: fail();
0916: } catch (Exception e) {
0917: }
0918: }
0919:
0920: public void testNotDeclared() {
0921: FooBean3 fb = new FooBean3();
0922:
0923: try {
0924: BeanUtil.setProperty(fb, "pprotected", new Integer(1));
0925: fail();
0926: } catch (Exception ex) {
0927: }
0928: try {
0929: BeanUtil.getProperty(fb, "pprotected");
0930: fail();
0931: } catch (Exception ex) {
0932: }
0933:
0934: try {
0935: BeanUtil.setProperty(fb, "ppackage", new Integer(2));
0936: fail();
0937: } catch (Exception ex) {
0938: }
0939: try {
0940: BeanUtil.getProperty(fb, "ppackage");
0941: fail();
0942: } catch (Exception ex) {
0943: }
0944:
0945: try {
0946: BeanUtil.setProperty(fb, "pprivate", new Integer(3));
0947: fail();
0948: } catch (Exception ex) {
0949: }
0950: try {
0951: BeanUtil.getProperty(fb, "pprivate");
0952: fail();
0953: } catch (Exception ex) {
0954: }
0955: }
0956:
0957: public void testDeclared() {
0958: FooBean3 fb = new FooBean3();
0959:
0960: BeanUtil.setDeclaredProperty(fb, "pprotected", new Integer(1));
0961: Integer value = (Integer) BeanUtil.getDeclaredProperty(fb,
0962: "pprotected");
0963: assertNotNull(value);
0964: assertEquals(1, value.intValue());
0965:
0966: BeanUtil.setDeclaredProperty(fb, "ppackage", new Integer(2));
0967: value = (Integer) BeanUtil.getDeclaredProperty(fb, "ppackage");
0968: assertNotNull(value);
0969: assertEquals(2, value.intValue());
0970:
0971: BeanUtil.setDeclaredProperty(fb, "pprivate", new Integer(3));
0972: value = (Integer) BeanUtil.getDeclaredProperty(fb, "pprivate");
0973: assertNotNull(value);
0974: assertEquals(3, value.intValue());
0975: }
0976:
0977: static class Dummy {
0978: private FooBean4 fb = new FooBean4();
0979:
0980: public FooBean4 getFb() {
0981: return fb;
0982: }
0983:
0984: public void setFb(FooBean4 fb) {
0985: this .fb = fb;
0986: }
0987:
0988: /** @noinspection UnnecessaryBoxing*/
0989: private Integer[] data = new Integer[] {
0990: Integer.valueOf("173"), Integer.valueOf("2") };
0991:
0992: public Integer[] getData() {
0993: return data;
0994: }
0995: }
0996:
0997: public void testArrays() {
0998: FooBean4 fb4 = new FooBean4();
0999: Dummy dummy = new Dummy();
1000: assertTrue(BeanUtil.hasDeclaredProperty(fb4,
1001: "data[0].bbean.abean.fooProp"));
1002: assertEquals("xxx", BeanUtil.getProperty(fb4,
1003: "data[0].bbean.abean.fooProp"));
1004: assertTrue(BeanUtil.hasDeclaredProperty(fb4,
1005: "data[1].bbean.abean.fooProp"));
1006: assertEquals("yyy", BeanUtil.getProperty(fb4,
1007: "data[1].bbean.abean.fooProp"));
1008:
1009: assertTrue(BeanUtil.hasDeclaredProperty(fb4,
1010: "data[2].bbean.abean.fooProp"));
1011: assertEquals("zzz", BeanUtil.getProperty(fb4,
1012: "data[2].bbean.abean.fooProp"));
1013: BeanUtil.setProperty(fb4, "data[2].bbean.abean.fooProp", "ZZZ");
1014: assertEquals("ZZZ", BeanUtil.getProperty(fb4,
1015: "data[2].bbean.abean.fooProp"));
1016:
1017: assertTrue(BeanUtil.hasDeclaredProperty(fb4,
1018: "list[0].bbean.abean.fooProp"));
1019: assertEquals("LLL", BeanUtil.getProperty(fb4,
1020: "list[0].bbean.abean.fooProp"));
1021: BeanUtil.setProperty(fb4, "list[0].bbean.abean.fooProp", "EEE");
1022: assertEquals("EEE", BeanUtil.getProperty(fb4,
1023: "list[0].bbean.abean.fooProp"));
1024: assertEquals("lll", BeanUtil.getProperty(fb4, "list[1]"));
1025: BeanUtil.setProperty(fb4, "list[1]", "eee");
1026:
1027: assertFalse(BeanUtil.hasDeclaredProperty(fb4,
1028: "list[1].bbean.abean.fooProp"));
1029: assertTrue(BeanUtil.hasDeclaredProperty(fb4, "list[1]"));
1030: assertEquals("eee", BeanUtil.getProperty(fb4, "list[1]"));
1031: assertTrue(BeanUtil.hasDeclaredProperty(dummy,
1032: "fb.data[0].bbean.abean.fooProp"));
1033: assertTrue(BeanUtil.hasDeclaredProperty(dummy,
1034: "fb.data[1].bbean.abean.fooProp"));
1035: assertTrue(BeanUtil.hasDeclaredProperty(dummy,
1036: "fb.data[2].bbean.abean.fooProp"));
1037: assertEquals("xxx", BeanUtil.getProperty(dummy,
1038: "fb.data[0].bbean.abean.fooProp"));
1039: assertEquals("yyy", BeanUtil.getProperty(dummy,
1040: "fb.data[1].bbean.abean.fooProp"));
1041: assertEquals("zzz", BeanUtil.getProperty(dummy,
1042: "fb.data[2].bbean.abean.fooProp"));
1043:
1044: BeanUtil.setProperty(dummy, "fb.data[2].bbean.abean.fooProp",
1045: "ZZZ");
1046: assertEquals("ZZZ", BeanUtil.getProperty(dummy,
1047: "fb.data[2].bbean.abean.fooProp"));
1048: assertEquals(new Integer(173), BeanUtil.getProperty(dummy,
1049: "data[0]"));
1050:
1051: BeanUtil.setProperty(dummy, "data[0]", new Integer(-173));
1052: assertEquals(new Integer(-173), BeanUtil.getProperty(dummy,
1053: "data[0]"));
1054: }
1055:
1056: public void testForced() {
1057: XBean x = new XBean();
1058: assertTrue(BeanUtil.hasDeclaredProperty(x, "y"));
1059: assertFalse(BeanUtil.hasDeclaredProperty(x, "y.foo"));
1060: assertFalse(BeanUtil.hasDeclaredProperty(x, "y[23].foo"));
1061: try {
1062: BeanUtil.setProperty(x, "y.foo", "yyy");
1063: fail();
1064: } catch (Exception ex) {
1065: }
1066: assertNull(x.getY());
1067:
1068: BeanUtil.setPropertyForced(x, "y.foo", "yyy");
1069: assertTrue(BeanUtil.hasDeclaredProperty(x, "y.foo"));
1070: assertEquals("yyy", x.getY().getFoo());
1071:
1072: assertNotNull(x.getYy());
1073: assertFalse(BeanUtil.hasDeclaredProperty(x, "yy[2].foo"));
1074: try {
1075: BeanUtil.setProperty(x, "yy[2].foo", "yyy");
1076: fail();
1077: } catch (Exception ex) {
1078: }
1079: assertNull(x.getYy()[2]);
1080:
1081: BeanUtil.setPropertyForced(x, "yy[2].foo", "xxx");
1082: assertTrue(BeanUtil.hasDeclaredProperty(x, "yy[2].foo"));
1083: assertEquals("xxx", x.getYy()[2].getFoo());
1084:
1085: assertFalse(BeanUtil.hasDeclaredProperty(x, "yy[20].foo"));
1086: BeanUtil.setPropertyForced(x, "yy[20].foo", "zzz");
1087: assertTrue(BeanUtil.hasDeclaredProperty(x, "yy[20].foo"));
1088: assertEquals("zzz", x.getYy()[20].getFoo());
1089: }
1090:
1091: public void testSilent() {
1092: FooBean fb = new FooBean();
1093: assertFalse(BeanUtil.hasDeclaredProperty(fb, "notexisting"));
1094: try {
1095: BeanUtil.setProperty(fb, "notexisting", null);
1096: fail();
1097: } catch (Exception ex) {
1098: }
1099: assertFalse(BeanUtil.setPropertySilent(fb, "notexisting", null));
1100:
1101: }
1102:
1103: public void testGenerics() {
1104: Gig gig = new Gig();
1105:
1106: BeanUtil.setPropertyForced(gig, "listOfStrings[1]", "string");
1107: assertNull(gig.getListOfStrings().get(0));
1108: assertEquals("string", gig.getListOfStrings().get(1));
1109: assertEquals(2, gig.getListOfStrings().size());
1110:
1111: BeanUtil.setPropertyForced(gig, "listOfIntegers[1]", Integer
1112: .valueOf(1));
1113: assertNull(gig.getListOfIntegers().get(0));
1114: assertEquals(1, gig.getListOfIntegers().get(1).intValue());
1115: assertEquals(2, gig.getListOfStrings().size());
1116:
1117: BeanUtil.setPropertyForced(gig, "listOfIntegers[3]", "3");
1118: assertNull(gig.getListOfIntegers().get(0));
1119: assertEquals(1, gig.getListOfIntegers().get(1).intValue());
1120: assertNull(gig.getListOfIntegers().get(2));
1121: assertEquals(3, gig.getListOfIntegers().get(3).intValue());
1122: assertEquals(4, gig.getListOfIntegers().size());
1123:
1124: BeanUtil.setPropertyForced(gig, "listOfAbeans[1].fooProp",
1125: "xxx");
1126: assertNull(gig.getListOfAbeans().get(0));
1127: assertEquals("xxx", gig.getListOfAbeans().get(1).getFooProp());
1128: assertEquals(2, gig.getListOfAbeans().size());
1129:
1130: BeanUtil.setPropertyForced(gig, "mapOfIntegers[kkk]", "173");
1131: assertEquals(173, gig.getMapOfIntegers().get("kkk").intValue());
1132: assertEquals(1, gig.getMapOfIntegers().size());
1133:
1134: BeanUtil.setPropertyForced(gig, "mapOfAbeans[kkk].fooProp",
1135: "zzz");
1136: assertEquals("zzz", gig.getMapOfAbeans().get("kkk")
1137: .getFooProp());
1138: assertEquals(1, gig.getMapOfAbeans().size());
1139:
1140: }
1141:
1142: public void testNoGenerics() {
1143: Gig gig = new Gig();
1144:
1145: BeanUtil.setPropertyForced(gig, "listOfStrings2[1]", "string");
1146: assertNull(gig.getListOfStrings2().get(0));
1147: assertEquals("string", gig.getListOfStrings2().get(1));
1148: assertEquals(2, gig.getListOfStrings2().size());
1149:
1150: BeanUtil.setPropertyForced(gig, "listOfIntegers2[1]", Integer
1151: .valueOf(1));
1152: assertNull(gig.getListOfIntegers2().get(0));
1153: assertEquals(1, ((Integer) gig.getListOfIntegers2().get(1))
1154: .intValue());
1155: assertEquals(2, gig.getListOfStrings2().size());
1156:
1157: BeanUtil.setPropertyForced(gig, "listOfIntegers2[3]", "3");
1158: assertNull(gig.getListOfIntegers2().get(0));
1159: assertEquals(1, ((Integer) gig.getListOfIntegers2().get(1))
1160: .intValue());
1161: assertNull(gig.getListOfIntegers2().get(2));
1162: assertEquals("3", gig.getListOfIntegers2().get(3));
1163: assertEquals(4, gig.getListOfIntegers2().size());
1164:
1165: BeanUtil.setPropertyForced(gig, "listOfAbeans2[1].fooProp",
1166: "xxx");
1167: assertNull(gig.getListOfAbeans2().get(0));
1168: assertEquals("xxx", ((Map) gig.getListOfAbeans2().get(1))
1169: .get("fooProp"));
1170: assertEquals(2, gig.getListOfAbeans2().size());
1171:
1172: BeanUtil.setPropertyForced(gig, "mapOfIntegers2[kkk]", "173");
1173: assertEquals("173", gig.getMapOfIntegers2().get("kkk"));
1174: assertEquals(1, gig.getMapOfIntegers2().size());
1175:
1176: BeanUtil.setPropertyForced(gig, "mapOfAbeans2[kkk].fooProp",
1177: "zzz");
1178: assertEquals("zzz", ((Map) gig.getMapOfAbeans2().get("kkk"))
1179: .get("fooProp"));
1180: assertEquals(1, gig.getMapOfAbeans2().size());
1181:
1182: }
1183:
1184: /**
1185: * All exceptions.
1186: */
1187: public static void testExceptions() {
1188: Map map = new HashMap();
1189: Gig gig = new Gig();
1190:
1191: try {
1192: BeanUtil.getProperty(map, "xxx");
1193: fail();
1194: } catch (Exception e) {
1195: System.out.println(e.getMessage());
1196: }
1197:
1198: try {
1199: BeanUtil.getProperty(gig, "doo");
1200: fail();
1201: } catch (Exception e) {
1202: System.out.println(e.getMessage());
1203: }
1204:
1205: try {
1206: BeanUtil.setProperty(gig, "xxx", "value");
1207: fail();
1208: } catch (Exception e) {
1209: System.out.println(e.getMessage());
1210: }
1211:
1212: try {
1213: BeanUtil.getProperty(gig, "listOfAbeans[1].fooProp");
1214: fail();
1215: } catch (Exception e) {
1216: System.out.println(e.getMessage());
1217: }
1218:
1219: try {
1220: BeanUtil.setPropertyForced(gig,
1221: "listOfAbeans[xxx].fooProp", "123");
1222: fail();
1223: } catch (Exception e) {
1224: System.out.println(e.getMessage());
1225: }
1226:
1227: try {
1228: gig.setZoro("zoro");
1229: BeanUtil.getProperty(gig, "zoro[1].fooProp");
1230: fail();
1231: } catch (Exception e) {
1232: System.out.println(e.getMessage());
1233: }
1234:
1235: try {
1236: BeanUtil.setProperty(gig, "zoro[1]", "foo");
1237: fail();
1238: } catch (Exception e) {
1239: System.out.println(e.getMessage());
1240: }
1241:
1242: try {
1243: BeanUtil.setPropertySilent(gig, "zoro[1].doo", "foo");
1244: } catch (Exception e) {
1245: fail();
1246: System.out.println(e.getMessage());
1247: }
1248: }
1249:
1250: public void testGeneralMapOnly() {
1251: Map map = new HashMap();
1252: BeanUtil.setPropertyForced(map, "foo.lll", "value");
1253: assertNotNull(map.get("foo"));
1254: assertEquals("value", ((Map) map.get("foo")).get("lll"));
1255:
1256: map = new HashMap();
1257: BeanUtil.setPropertyForced(map, "foo.lll[2]", "value");
1258: assertNotNull(map.get("foo"));
1259: assertNotNull(((Map) map.get("foo")).get("lll"));
1260: assertEquals("value", ((Map) ((Map) map.get("foo")).get("lll"))
1261: .get("2"));
1262: }
1263: }
|