1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.jetspeed.util;
17  
18  // Junit imports
19  import junit.framework.Test;
20  import junit.framework.TestSuite;
21  
22  import org.apache.jetspeed.test.HeadlessBaseTest;
23  import org.apache.jetspeed.util.parser.ValidationParameterParser;
24  
25  /***
26   * Command Line Test ValidationParameterParser routines.
27   *
28   * @author <a href="mailto:ben.woodward@bbc.co.uk">Ben Woodward</a>
29   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
30   * @version $Id: TestValidationParameterParser.java,v 1.1 2004/04/07 22:02:42 jford Exp $
31   */
32  
33  public class TestValidationParameterParser extends HeadlessBaseTest
34  {
35      /***
36       * Defines the testcase name for JUnit.
37       *
38       * @param name the testcase's name.
39       */
40      public TestValidationParameterParser( String name ) {
41          super( name );
42      }
43  
44      /***
45       * Start the tests.
46       *
47       * @param args the arguments. Not used
48       */
49      public static void main(String args[]) {
50          junit.awtui.TestRunner.main( new String[] { TestValidationParameterParser.class.getName() } );
51      }
52  
53      public void setup() {
54          System.out.println("Setup: Testing ValidationParameterParser");
55       }
56  
57      public static Test suite()
58      {
59          // All methods starting with "test" will be executed in the test suite.
60          return new TestSuite( TestValidationParameterParser.class );
61      }
62  
63  
64        ///////////////////////////////////////////////////////////////////////////
65  
66      public void testGood() throws Exception
67      {
68          try
69          {
70              Dummy dummy = new Dummy();
71              ValidationParameterParser parser = new ValidationParameterParser();
72              parser.add("Name", "David");
73              parser.add("Age", "41");
74              parser.add("Email", "david@bluesunrise.com");           
75              parser.setProperties(dummy);
76          }
77          catch (Throwable t)
78          {
79              t.printStackTrace();
80          }
81      }
82  
83      public void testFail() throws Exception
84      {
85          boolean failed = false;
86          try
87          {
88              Dummy dummy = new Dummy();
89              ValidationParameterParser parser = new ValidationParameterParser();
90              parser.add("Name", "David");
91              parser.add("Age", "1041");
92              parser.add("Email", "david@bluesunrise.com");           
93              parser.setProperties(dummy);
94          }
95          catch (Throwable t)
96          {
97              failed = true;
98          }
99          assertTrue(failed == true);
100     }
101 
102     public class Dummy
103     {
104         private String name;
105         private String email;
106         private int age;
107  
108         public String getName()
109         {
110             return name;
111         }
112 
113         public String getEmail()
114         {
115             return email;
116         }
117 
118         public int getAge()
119         {
120             return age;
121         }
122 
123         public void setName(String name)
124         {
125             this.name = name;
126         }
127 
128         public void setEmail(String email)
129         {
130             this.email = email;
131         }
132 
133         public void setAge(int age)
134         {
135             this.age = age;
136         }
137 
138         public boolean validateAge(int age)
139         {
140             if (age > 0 && age < 120)
141             {
142                 return true;
143             }
144             return false;
145         }            
146 
147         public boolean validateEmail(String email)
148         {
149             return ValidationHelper.isEmailAddress(email, false);
150         }            
151 
152         public boolean validateName(String name)
153         {
154             return ValidationHelper.isLooseAlphaNumeric(name, false);
155         }            
156 
157         public String validate()
158         {
159             if (name.equals("David"))
160             {
161                 if (email.equals("david@bluesunrise.com"))
162                 {
163                     return null;
164                 }
165                 else
166                 {
167                     return "Bad Email/Name combination";                    
168                 }
169             }
170             else
171             {
172                 return "Bad Name/Email combination";
173             }
174         }
175     }
176         
177 }