1 package org.jbpm.instantiation; 2 3 import org.dom4j.*; 4 import org.jbpm.instantiation.FieldInstantiator; 5 6 import junit.framework.*; 7 8 public class FieldInstantiatorTest extends TestCase { 9 10 public FieldInstantiator fieldInstantiator = new FieldInstantiator(); 11 12 public static class ClassWithLotsOfFields { 13 private String s = null; 14 private Integer i = null; 15 private int ii = -1; 16 private Long l = null; 17 private long ll = -1; 18 private Float f = null; 19 private float ff = -1; 20 private Double d = null; 21 private double dd = -1; 22 private Boolean b = null; 23 private boolean bb = false; 24 private Character c = null; 25 private char cc = ' '; 26 private Short sh = null; 27 private short shsh = -1; 28 private Byte by = null; 29 private byte byby = -1; 30 } 31 32 public void testBasicTypes() { 33 String configuration = 34 "<s>hello</s>" + 35 "<i>1</i>" + 36 "<ii>2</ii>" + 37 "<l>3</l>" + 38 "<ll>4</ll>" + 39 "<f>5.5</f>" + 40 "<ff>6.6</ff>" + 41 "<d>7.7</d>" + 42 "<dd>8.8</dd>" + 43 "<b>TRUE</b>" + 44 "<bb>true</bb>" + 45 "<c>a</c>" + 46 "<cc>b</cc>" + 47 "<sh>9</sh>" + 48 "<shsh>10</shsh>" + 49 "<by>11</by>" + 50 "<byby>12</byby>"; 51 52 ClassWithLotsOfFields c = (ClassWithLotsOfFields) fieldInstantiator.instantiate(ClassWithLotsOfFields.class, configuration); 53 54 assertEquals( "hello", c.s ); 55 assertEquals( new Integer (1), c.i ); 56 assertEquals( 2, c.ii ); 57 assertEquals( new Long (3), c.l ); 58 assertEquals( 4, c.ll ); 59 assertEquals( new Float (5.5), c.f ); 60 assertEquals( (float)6.6, c.ff, 0 ); 61 assertEquals( new Double (7.7), c.d ); 62 assertEquals( 8.8, c.dd, 0 ); 63 assertEquals( Boolean.TRUE, c.b ); 64 assertEquals( true, c.bb ); 65 assertEquals( new Character ('a'), c.c ); 66 assertEquals( 'b', c.cc ); 67 assertEquals( new Short ((short) 9), c.sh ); 68 assertEquals( 10, c.shsh ); 69 assertEquals( new Byte ((byte) 11), c.by ); 70 assertEquals( 12, c.byby ); 71 } 72 73 public static class ClassWithStringConstructorType { 74 private RuntimeException e; 75 } 76 77 public void testStringConstructorType() { 78 String configuration = "<e>i want yoghurt</e>"; 79 ClassWithStringConstructorType c = (ClassWithStringConstructorType) fieldInstantiator.instantiate(ClassWithStringConstructorType.class, configuration); 80 assertEquals("i want yoghurt", c.e.getMessage()); 81 } 82 83 public static class ClassWithDom4jField{ 84 private Element structuredElement; 85 } 86 87 public void testStructuredElement() { 88 String configuration = 89 "<structuredElement>" + 90 " <surfboard length=\"270\" />" + 91 " <mast length=\"475\" />" + 92 " <boom length=\"160\" />" + 93 " <sail size=\"5.7\" />" + 94 "</structuredElement>"; 95 ClassWithDom4jField c = (ClassWithDom4jField) fieldInstantiator.instantiate(ClassWithDom4jField.class, configuration); 96 assertEquals(4, c.structuredElement.elements().size()); 97 Element firstElement = (Element) c.structuredElement.elementIterator().next(); 98 assertEquals("surfboard", firstElement.getName()); 99 assertEquals("270", firstElement.attributeValue("length")); 100 } 101 102 public static class ClassWithOneField { 103 private String onlyMember = null; 104 } 105 106 public void testEmptyConfiguration() { 107 ClassWithOneField c = (ClassWithOneField) fieldInstantiator.instantiate(ClassWithOneField.class, null); 108 assertNull(c.onlyMember); 109 c = (ClassWithOneField) fieldInstantiator.instantiate(ClassWithOneField.class, ""); 110 assertNull(c.onlyMember); 111 } 112 113 public void testNonMatchingConfiguration() { 114 String configuration = "<unexistingMember>bullshit</unexistingMember>"; 115 ClassWithOneField c = (ClassWithOneField) fieldInstantiator.instantiate(ClassWithOneField.class, configuration); 116 assertNull(c.onlyMember); 117 } 118 } 119 120 | Popular Tags |