View Javadoc

1   package org.apache.myfaces.custom.validators;
2   
3   import javax.faces.component.UIInput;
4   import javax.faces.validator.ValidatorException;
5   
6   import junit.framework.Test;
7   import junit.framework.TestSuite;
8   
9   import org.apache.myfaces.custom.equalvalidator.EqualValidator;
10  
11  public class EqualValidatorTestCase extends AbstractValidatorTestCase
12  {
13  
14    public EqualValidatorTestCase(String arg0) {
15      super(arg0);
16    }
17    
18    EqualValidator equalValidator;
19    
20    protected void setUp() throws Exception
21    {
22      super.setUp();
23      equalValidator = new EqualValidator();
24      
25    }
26  
27    protected void tearDown() throws Exception
28    {
29      super.tearDown();
30    }
31  
32    public static Test suite()
33    {
34      return new TestSuite(EqualValidatorTestCase.class);
35    }
36    
37    /**
38     * Test when context is set to null
39     */
40    public void testNullContext()
41    {
42  
43      doTestNullContext(component, equalValidator);
44    }
45    
46    public void testRightValue()
47    {
48      equalValidator.setFor("comp1");
49      
50      UIInput comp1 = new UIInput();
51      comp1.setValue("HANS");
52      comp1.setId("comp1");
53      facesContext.getViewRoot().getChildren().add(comp1);
54      
55      
56      UIInput comp2 = new UIInput ();
57      comp2.setId("comp2");
58      facesContext.getViewRoot().getChildren().add(comp2);
59  
60      equalValidator.validate(facesContext, comp2, "HANS");
61  
62    }
63  
64    public void testWrongValue()
65    {
66      try
67      {
68        equalValidator.setFor("comp1");
69        
70        UIInput comp1 = new UIInput();
71        comp1.setValue("HANS");
72        comp1.setId("comp1");
73        facesContext.getViewRoot().getChildren().add(comp1);
74        
75        
76        UIInput comp2 = new UIInput ();
77        comp2.setId("comp2");
78        facesContext.getViewRoot().getChildren().add(comp2);
79  
80        equalValidator.validate(facesContext, comp2, "BUUBA");
81        
82        fail("Expected ValidatorException");
83      }
84      catch (ValidatorException ve)
85      {
86        // if exception then fine.
87      }
88  
89    }
90  
91  }