|
From: Leandro R. S. C. <bas...@us...> - 2004-03-22 21:34:24
|
Update of /cvsroot/xingu/xingu/exintake/src/test/br/com/ibnetwork/xingu/exintake In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31708/exintake/src/test/br/com/ibnetwork/xingu/exintake Added Files: IntakeInvocationHandlerTest.java RuleTest.java IntakeTest.java PresentationTest.java Log Message: adding Extensible input taker --- NEW FILE: IntakeTest.java --- package br.com.ibnetwork.xingu.exintake; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import br.com.ibnetwork.xingu.AbstractTestCase; import br.com.ibnetwork.xingu.exintake.model.Field; import br.com.ibnetwork.xingu.exintake.model.Group; import br.com.ibnetwork.xingu.exintake.model.presentation.Presentation; import br.com.ibnetwork.xingu.exintake.model.rule.RuleSet; import br.com.ibnetwork.xingu.exintake.test.GroupOneBean; /** * @author neto * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class IntakeTest extends AbstractTestCase { Intake intake; public void setUp() throws Exception { intake = (Intake) manager.lookup(Intake.ROLE); } public void tearDown() { intake = null; } public void testGetGroupByName() throws Exception { Group groupOne = intake.get("GroupOne"); //asserts assertEquals("GroupOne",groupOne.getName()); assertEquals("G1-G",groupOne.getKey()); assertEquals("br.com.ibnetwork.xingu.exintake.test.GroupOneBean",groupOne.getMapToObject()); List fieldNames = groupOne.getFieldNames(); assertEquals(7,fieldNames.size()); for (Iterator iter = fieldNames.iterator(); iter.hasNext();) { String element = (String) iter.next(); assertTrue(element.startsWith("f")); } } public void testGetFieldByName() throws Exception { Group groupOne = intake.get("GroupOne"); Field f1 = groupOne.get("f1"); assertEquals("f1",f1.getName()); assertEquals("G1-G_f1",f1.getFullName()); assertEquals("int",f1.getType()); assertEquals("0",f1.getDefaultValue()); } public void testCreateObjectFromParameters() throws Exception { Group groupOne = intake.get("GroupOne"); String mapToObject = groupOne.getMapToObject(); Map map = new HashMap(); map.put("f1",new Integer(10)); map.put("f2","efe dois"); Date rightNow = new Date(); map.put("f3", rightNow); Object obj = groupOne.createObject(map); assertEquals(mapToObject,obj.getClass().getName()); GroupOneBean bean = (GroupOneBean) obj; assertEquals(10,bean.getF1()); assertEquals("efe dois",bean.getF2()); assertEquals(rightNow,bean.getF3()); } public void testNullImpl() throws IntakeException { Group groupOne = intake.get("GroupOne"); //test null presentation Presentation presentation = (Presentation) groupOne.get("f7"); //test null ruleSet RuleSet ruleSet = (RuleSet) groupOne.get("f7"); assertNotNull(ruleSet.getRuleByType(this.getClass())); assertTrue(ruleSet.isValid(null)); } public void testAlternateFile() throws Exception { Group especialGroup = intake.get("EspecialGroup"); assertNotNull(especialGroup); } public void testGroupNameCase() throws Exception { Group groupOne = intake.get("groupone"); assertNotNull(groupOne); } public void testFieldConvertion() throws Exception { Group two = intake.get("GroupTwo"); java.util.Calendar cal = java.util.Calendar.getInstance(); Date dataTest = new Date("01/01/2001"); cal.setTime(dataTest); short s = 1; byte b = 1; char c = 't'; assertEquals(Boolean.TRUE,two.convert("f1","true")); assertEquals(Boolean.FALSE,two.convert("f1","false")); assertEquals(1,((Integer)two.convert("f2","1")).intValue()); assertEquals("teste",two.convert("f3","teste")); assertEquals(dataTest,((Date)two.convert("f4","01/01/2001"))); assertEquals(new Float(1),((Float)two.convert("f5","1"))); assertEquals(new Integer(1),((Integer)two.convert("f6","1"))); assertEquals(new Double(1),((Double)two.convert("f7","1"))); assertEquals(new Long(1),((Long)two.convert("f8","1"))); assertEquals(new Byte(b),((Byte)two.convert("f9","1"))); assertEquals(new Short(s),((Short)two.convert("f10","1"))); assertEquals(new Character(c),((Character)two.convert("f11","t"))); } } --- NEW FILE: IntakeInvocationHandlerTest.java --- /* * Created on Dec 23, 2003 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package br.com.ibnetwork.xingu.exintake; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; import java.util.HashMap; import java.util.Map; import org.apache.commons.logging.LogFactory; import br.com.ibnetwork.xingu.exintake.model.Field; import br.com.ibnetwork.xingu.exintake.model.IntakeInvocationHandler; import br.com.ibnetwork.xingu.exintake.model.impl.FieldDefaultImpl; import br.com.ibnetwork.xingu.exintake.model.presentation.Presentation; import br.com.ibnetwork.xingu.exintake.model.presentation.impl.PresentationDefaultImpl; import junit.framework.TestCase; /** * @author <a href="mailto:le...@ib...">Leandro Rodrigo Saad Cruz</a> * */ public class IntakeInvocationHandlerTest extends TestCase { /** * */ public IntakeInvocationHandlerTest() { super(); } /** * @param arg0 */ public IntakeInvocationHandlerTest(String testName) { super(testName); } public void testMethodDelegation() { ClassLoader cl = this.getClass().getClassLoader(); IntakeInvocationHandler handler = new IntakeInvocationHandler(LogFactory.getLog(IntakeInvocationHandler.class)); Proxy proxy = (Proxy) Proxy.newProxyInstance(cl,new Class[]{Field.class},handler); //cast to Field handler.registerMethods(Field.class, new FieldDefaultImpl(null)); Field field = (Field) proxy; field.getName(); field.getType(); //cast to Presentation proxy = (Proxy) Proxy.newProxyInstance(cl,new Class[]{Presentation.class},handler); handler.registerMethods(Presentation.class, new PresentationDefaultImpl()); Presentation presentation = (Presentation) proxy; presentation.getDisplayName(); } } --- NEW FILE: RuleTest.java --- package br.com.ibnetwork.xingu.exintake; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.commons.beanutils.MethodUtils; import br.com.ibnetwork.xingu.AbstractTestCase; import br.com.ibnetwork.xingu.exintake.model.Group; import br.com.ibnetwork.xingu.exintake.model.rule.Rule; import br.com.ibnetwork.xingu.exintake.model.rule.RuleSet; import br.com.ibnetwork.xingu.exintake.model.rule.impl.FormatRule; import br.com.ibnetwork.xingu.exintake.model.rule.impl.MaskRule; import br.com.ibnetwork.xingu.exintake.model.rule.impl.MaxLengthRule; import br.com.ibnetwork.xingu.exintake.model.rule.impl.RequiredRule; import br.com.ibnetwork.xingu.exintake.test.GroupOneBean; /** * @author neto * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class RuleTest extends AbstractTestCase { Intake intake; public void setUp() throws Exception { intake = (Intake) manager.lookup(Intake.ROLE); } public void tearDown() { intake = null; } public void testRuleSetAndValid() throws IntakeException { Group groupOne = intake.get("GroupOne"); RuleSet ruleSet = (RuleSet) groupOne.get("f1"); assertEquals(2,ruleSet.getRules().size()); assertEquals(false,ruleSet.isValid(null)); assertEquals("f1",ruleSet.getFieldName()); } public void testEmptyRuleSet() throws IntakeException { Group groupOne = intake.get("GroupOne"); RuleSet ruleSet = (RuleSet) groupOne.get("f4"); assertEquals(0,ruleSet.getRules().size()); } public void testRuleInstance() throws IntakeException { Group groupOne = intake.get("GroupOne"); RuleSet ruleSet = (RuleSet) groupOne.get("f2"); Rule rule = (Rule) ruleSet.getRules().get(0); assertTrue( rule instanceof MaskRule); MaskRule maskRule = (MaskRule) rule; assertEquals("[a-z]", maskRule.getMask()); assertEquals("Please use only chars",maskRule.getMessage()); assertEquals("f2",rule.getFieldName()); } public void testRequiredRuleInstance() throws IntakeException { Group groupOne = intake.get("GroupOne"); RuleSet ruleSet = (RuleSet) groupOne.get("f2"); Rule rule = (Rule) ruleSet.getRules().get(1); assertTrue( rule instanceof RequiredRule); RequiredRule requiredRule = (RequiredRule) rule; assertEquals("true", requiredRule.getRequired()); assertEquals("Please fill out this field",requiredRule.getMessage()); } public void testFormatRuleInstance() throws IntakeException { Group groupOne = intake.get("GroupOne"); RuleSet ruleSet = (RuleSet) groupOne.get("f2"); Rule rule = (Rule) ruleSet.getRules().get(2); assertTrue( rule instanceof FormatRule); FormatRule formatRule = (FormatRule) rule; assertEquals("dd/MM/yyyy", formatRule.getFormat()); assertEquals("Please enter with date format dd/MM/yyyy",formatRule.getMessage()); } public void testGetRuleByName() throws Exception { Group groupOne = intake.get("GroupOne"); RuleSet ruleSet = (RuleSet) groupOne.get("f2"); Rule rule = ruleSet.getRuleByName("FormatRule"); assertTrue( rule instanceof FormatRule); } public void testMaxLengthRuleInstance() throws IntakeException { Group groupOne = intake.get("GroupOne"); RuleSet ruleSet = (RuleSet) groupOne.get("f2"); Rule rule = (Rule) ruleSet.getRules().get(3); assertTrue( rule instanceof MaxLengthRule); MaxLengthRule maxLengthRule = (MaxLengthRule) rule; assertEquals("128", maxLengthRule.getMaxLength()); assertEquals("Please enter with value smaller of 128",maxLengthRule.getMessage()); } public void testIsValid() throws Exception { Group groupOne = intake.get("GroupOne"); Map map = new HashMap(); //map.put("f1",new Integer(10)); map.put("f2","efe dois"); Date rightNow = new Date(); map.put("f3", rightNow); GroupOneBean bean = (GroupOneBean) groupOne.createObject(map); List fieldNames = groupOne.getFieldNames(); for (Iterator iter = fieldNames.iterator(); iter.hasNext();) { String fieldName = (String) iter.next(); RuleSet ruleSet = (RuleSet) groupOne.get(fieldName); List rules = ruleSet.getRules(); for (Iterator iterator = rules.iterator(); iterator.hasNext();) { Rule rule = (Rule) iterator.next(); assertEquals(fieldName,rule.getFieldName()); //System.out.println("Field : " + fieldName+ " rule " + rule + " valid ? " + rule.isValid(bean) ); } } } public void testIsValidIntField() throws Exception { Group groupOne = intake.get("GroupOne"); Map map = new HashMap(); map.put("f1",new Integer(10)); GroupOneBean bean = (GroupOneBean) groupOne.createObject(map); //test f1 RuleSet ruleSet = (RuleSet) groupOne.get("f1"); Rule requiredRule = ruleSet.getRuleByType(RequiredRule.class); assertTrue(requiredRule.isValid(bean)); } public void testIsValidStringField() throws Exception { Group groupOne = intake.get("GroupOne"); Map map = new HashMap(); map.put("f2","efe dois"); GroupOneBean bean = (GroupOneBean) groupOne.createObject(map); //test f2 RuleSet ruleSet = (RuleSet) groupOne.get("f2"); Rule requiredRule = ruleSet.getRuleByType(RequiredRule.class); assertTrue(requiredRule.isValid(bean)); bean.setF2(null); assertFalse(requiredRule.isValid(bean)); } public void testIsValidDateField() throws Exception { Group groupOne = intake.get("GroupOne"); Map map = new HashMap(); map.put("f6",new Date()); GroupOneBean bean = (GroupOneBean) groupOne.createObject(map); //test f6 RuleSet ruleSet = (RuleSet) groupOne.get("f6"); Rule requiredRule = ruleSet.getRuleByType(RequiredRule.class); assertTrue(requiredRule.isValid(bean)); bean.setF6(null); assertFalse(requiredRule.isValid(bean)); } public void testDateFormat() throws Exception { Group groupOne = intake.get("GroupOne"); Map map = new HashMap(); map.put("f6",new Date()); GroupOneBean bean = (GroupOneBean) groupOne.createObject(map); //test f6 RuleSet ruleSet = (RuleSet) groupOne.get("f6"); FormatRule rule = (FormatRule) ruleSet.getRuleByType(FormatRule.class); Date date = (Date) rule.parse("01/01/2004"); Date tmp = new Date(104,0,1); assertEquals("dd/MM/yyyy",rule.getFormat()); assertEquals(tmp ,date); assertEquals("01/01/2004", rule.format(tmp)); } public void testIsValidNoRuleSet() throws Exception { Group groupOne = intake.get("GroupOne"); RuleSet ruleSet = (RuleSet) groupOne.get("f7"); Rule requiredRule = ruleSet.getRuleByType(RequiredRule.class); assertTrue(ruleSet.isValid(null)); assertTrue(requiredRule.isValid(null)); } } --- NEW FILE: PresentationTest.java --- package br.com.ibnetwork.xingu.exintake; import br.com.ibnetwork.xingu.AbstractTestCase; import br.com.ibnetwork.xingu.exintake.model.Group; import br.com.ibnetwork.xingu.exintake.model.presentation.Presentation; /** * @author neto * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class PresentationTest extends AbstractTestCase { Intake intake; public void setUp() throws Exception { intake = (Intake) manager.lookup(Intake.ROLE); } public void tearDown() { intake = null; } public void testGetPresentationByName() throws IntakeException { Group groupOne = intake.get("GroupOne"); Presentation presentation = (Presentation) groupOne.get("f3"); assertEquals("F3 Date",presentation.getDisplayName()); assertEquals("text",presentation.getInputType()); assertEquals("10",presentation.getMaxLength()); assertEquals("10",presentation.getSize()); } public void testNullDisplayNamePresentation() throws IntakeException { Group groupOne = intake.get("GroupOne"); Presentation presentation = (Presentation) groupOne.get("f5"); assertNull(presentation.getDisplayName()); } public void testTextAreaAttributes() throws Exception { Group groupOne = intake.get("GroupOne"); Presentation presentation = (Presentation) groupOne.get("f6"); assertEquals("textarea",presentation.getInputType()); assertEquals("5",presentation.getCols()); assertEquals("10",presentation.getRows()); } // public void testEmptyPresentation() // throws IntakeException // { // // Group groupOne = intake.get("groupOne"); // Presentation presentation = (Presentation) groupOne.get("f6"); // assertNull(presentation.getDisplayName()); // // } } |