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  
19  // Junit imports
20  import junit.framework.Test;
21  import junit.framework.TestSuite;
22  
23  // Jetspeed imports
24  import org.apache.jetspeed.test.JetspeedTestCase;
25  
26  /***
27   * Command Line Test Validation routines.
28   *
29   * @author <a href="mailto:ben.woodward@bbc.co.uk">Ben Woodward</a>
30   * @version $Id: TestValidation.java,v 1.1 2004/04/07 22:02:42 jford Exp $
31   */
32  
33  public class TestValidation extends JetspeedTestCase
34  {
35      /***
36       * Defines the testcase name for JUnit.
37       *
38       * @param name the testcase's name.
39       */
40      public TestValidation( 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[] { TestValidation.class.getName() } );
51      }
52  
53      public void setup() {
54          System.out.println("Setup: Testing Validation");
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( TestValidation.class );
61      }
62  
63  
64        ///////////////////////////////////////////////////////////////////////////
65  
66      public void testAlphaNumeric() throws Exception
67      {
68          String goodString = "The quick brown fox jumped over the lazy dog 0123456789";
69          String badString = "><£$$!&.*";
70  
71          assertTrue(ValidationHelper.isAlphaNumeric(goodString, false));        
72          assertTrue(!ValidationHelper.isAlphaNumeric(badString, false));        
73      }
74      
75      public void testLooseAlphaNumeric() throws Exception
76      {
77          //String goodString = "The quick brown fox jumped over the lazy dog 0123456789 .";
78          //String goodString = "www.www.www-/()+";
79          //String badString = "><£$$!&*.";
80          
81          String goodString[] = {"a","a.a","aaa.aaa","a-a","aaa-aaa", "(aaa) aaa", "+aa-aaa.aa", "555-4545", "555,4545"};
82          String badString[] = {"><£$$!&*."};   
83          
84          
85          for (int ia=0; ia < goodString.length; ia++)
86          {
87              assertTrue(ValidationHelper.isLooseAlphaNumeric(goodString[ia], false));
88              System.out.println(goodString[ia]+" is Good: "+ValidationHelper.isLooseAlphaNumeric(goodString[ia], false));
89          }
90          
91          for (int ib=0; ib < badString.length; ib++)
92          {
93              assertTrue(!ValidationHelper.isLooseAlphaNumeric(badString[ib], false));
94              System.out.println(badString[ib]+" is Bad: "+!ValidationHelper.isLooseAlphaNumeric(badString[ib], false));
95          }      
96      }
97      
98      public void testDecimal() throws Exception
99      {
100         
101         String goodString[] = {"1","1.1","11.1","1.11","11.11"};
102         String badString[] = {"a","1.a","1-a","1..1","1.1.1"};      
103         
104         for (int ia=0; ia < goodString.length; ia++)
105         {
106             assertTrue(ValidationHelper.isDecimal(goodString[ia], false));
107         }
108 
109         for (int ib=0; ib < badString.length; ib++)
110         {
111             assertTrue(!ValidationHelper.isDecimal(badString[ib], false));
112         }
113     }
114     
115     public void testInteger() throws Exception
116     {
117         
118         String goodString[] = {"1","11","111"};
119         String badString[] = {"a","1.1","1.a","1-a","1..1","1.1.1"};      
120 
121         for (int ia=0; ia < goodString.length; ia++)
122         {
123             assertTrue(ValidationHelper.isInteger(goodString[ia], false));
124         }
125 
126         for (int ib=0; ib < badString.length; ib++)
127         {
128             assertTrue(!ValidationHelper.isInteger(badString[ib], false));
129         }
130     }
131     
132     
133     public void testEmailAddress() throws Exception
134     {
135 
136         String goodString[] = {"a@b.c","a.b@c.d","aa@b.c","aaa@b.c"};
137         String badString[] = {"*@b.c","a","a@","@a",".@a","a@b.","a@b","a@@b.c","a@b@c.d","aaa@b^.c"};      
138 
139         for (int ia=0; ia < goodString.length; ia++)
140         {
141             assertTrue(ValidationHelper.isEmailAddress(goodString[ia], false));
142         }
143 
144         for (int ib=0; ib < badString.length; ib++)
145         {
146             assertTrue(!ValidationHelper.isEmailAddress(badString[ib], false));
147         }
148     }
149     
150     public void testURL() throws Exception
151     {
152         String goodString = "http://www.apache.org";
153         String badString = "me.";
154         
155         assertTrue(ValidationHelper.isURL(goodString, false));        
156         assertTrue(!ValidationHelper.isURL(badString, false));        
157     }
158 
159 
160     /*
161     Configuration object to run Turbine outside a servlet container
162     ( uses turbine.properties )
163     private static TurbineConfig config = null;
164     
165     static
166     {
167     try
168     {
169     config = new TurbineConfig( "./bin", "/WEB-INF/conf/TurbineResources.properties");
170     config.init();
171     }
172     catch (Exception e)
173     {
174     //fail(StringUtils.stackTrace(e));
175     System.out.println(StringUtils.stackTrace(e));
176     }
177     }
178      */
179 }
180 
181 
182