1 16 17 package org.springframework.beans.factory.config; 18 19 import java.sql.Connection ; 20 21 import junit.framework.TestCase; 22 23 27 public class FieldRetrievingFactoryBeanTests extends TestCase { 28 29 public void testStaticField() throws Exception { 30 FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); 31 fr.setStaticField("java.sql.Connection.TRANSACTION_SERIALIZABLE"); 32 fr.afterPropertiesSet(); 33 assertEquals(new Integer (Connection.TRANSACTION_SERIALIZABLE), fr.getObject()); 34 } 35 36 public void testStaticFieldWithWhitespace() throws Exception { 37 FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); 38 fr.setStaticField(" java.sql.Connection.TRANSACTION_SERIALIZABLE "); 39 fr.afterPropertiesSet(); 40 assertEquals(new Integer (Connection.TRANSACTION_SERIALIZABLE), fr.getObject()); 41 } 42 43 public void testStaticFieldViaClassAndFieldName() throws Exception { 44 FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); 45 fr.setTargetClass(Connection .class); 46 fr.setTargetField("TRANSACTION_SERIALIZABLE"); 47 fr.afterPropertiesSet(); 48 assertEquals(new Integer (Connection.TRANSACTION_SERIALIZABLE), fr.getObject()); 49 } 50 51 public void testNonStaticField() throws Exception { 52 FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); 53 PublicFieldHolder target = new PublicFieldHolder(); 54 fr.setTargetObject(target); 55 fr.setTargetField("publicField"); 56 fr.afterPropertiesSet(); 57 assertEquals(target.publicField, fr.getObject()); 58 } 59 60 public void testNothingButBeanName() throws Exception { 61 FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); 62 fr.setBeanName("java.sql.Connection.TRANSACTION_SERIALIZABLE"); 63 fr.afterPropertiesSet(); 64 assertEquals(new Integer (Connection.TRANSACTION_SERIALIZABLE), fr.getObject()); 65 } 66 67 public void testJustTargetField() throws Exception { 68 FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); 69 fr.setTargetField("TRANSACTION_SERIALIZABLE"); 70 try { 71 fr.afterPropertiesSet(); 72 } 73 catch (IllegalArgumentException ex) { 74 } 76 } 77 78 public void testJustTargetClass() throws Exception { 79 FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); 80 fr.setTargetClass(Connection .class); 81 try { 82 fr.afterPropertiesSet(); 83 } 84 catch (IllegalArgumentException ex) { 85 } 87 } 88 89 public void testJustTargetObject() throws Exception { 90 FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); 91 fr.setTargetObject(new PublicFieldHolder()); 92 try { 93 fr.afterPropertiesSet(); 94 } 95 catch (IllegalArgumentException ex) { 96 } 98 } 99 100 101 private static class PublicFieldHolder { 102 103 public String publicField = "test"; 104 } 105 106 } 107 | Popular Tags |