KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > servermgmt > RepositoryNameValidatorTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * $Id: RepositoryNameValidatorTest.java,v 1.3 2005/12/25 03:44:11 tcfujii Exp $
26  */

27
28 package com.sun.enterprise.admin.servermgmt;
29
30 import java.util.Random JavaDoc;
31
32 //junit imports
33
import junit.framework.*;
34 import junit.textui.TestRunner;
35
36 public class RepositoryNameValidatorTest extends TestCase
37 {
38     static final char[] INVALID_CHARS =
39         {'/', '\\', ':', '*', '?', '"', '<', '>', '|', ',', '=', 'î', ' ', '&', ';', '[', ']', '{', '}', '(', ')', '%', '$', '^', '!'};
40
41     static final char[] VALID_CHARS = {'a', '0', '-', '_', '.'};
42
43     static final int ITERATIONS = 100;
44
45     Validator validator;
46     Random JavaDoc random;
47
48     public void testNull()
49     {
50         testInvalid(null);
51     }
52
53     public void testZeroLength()
54     {
55         testInvalid("");
56     }
57
58     public void testInvalidChar()
59     {
60         for (int i = 0; i < INVALID_CHARS.length; i++)
61         {
62             testInvalid("" + INVALID_CHARS[i]);
63         }
64     }
65
66     public void testInvalidStr()
67     {
68         for (int i = 0; i < ITERATIONS; i++)
69         {
70             testInvalid("" +
71                 INVALID_CHARS[random.nextInt(INVALID_CHARS.length)] +
72                 INVALID_CHARS[random.nextInt(INVALID_CHARS.length)]);
73         }
74     }
75
76     public void testValidChar()
77     {
78         for (int i = 0; i < VALID_CHARS.length; i++)
79         {
80             testValid("" + VALID_CHARS[i]);
81         }
82     }
83
84     public void testValidStr()
85     {
86         for (int i = 0; i < ITERATIONS; i++)
87         {
88             testValid("" +
89                 VALID_CHARS[random.nextInt(VALID_CHARS.length)] +
90                 VALID_CHARS[random.nextInt(VALID_CHARS.length)]);
91         }
92     }
93
94     public void testCombination()
95     {
96         for (int i = 0; i < ITERATIONS; i++)
97         {
98             testInvalid("" +
99                 VALID_CHARS[random.nextInt(VALID_CHARS.length)] +
100                 INVALID_CHARS[random.nextInt(INVALID_CHARS.length)]);
101         }
102     }
103
104     public RepositoryNameValidatorTest(String JavaDoc name) throws Exception JavaDoc
105     {
106         super(name);
107     }
108
109     protected void setUp()
110     {
111         validator = new RepositoryNameValidator("repository name");
112         random = new Random JavaDoc();
113     }
114
115     protected void tearDown()
116     {
117         validator = null;
118         random = null;
119     }
120
121     public static junit.framework.Test suite()
122     {
123         TestSuite suite = new TestSuite(RepositoryNameValidatorTest.class);
124         return suite;
125     }
126
127     public static void main(String JavaDoc args[]) throws Exception JavaDoc
128     {
129         final TestRunner runner= new TestRunner();
130         final TestResult result = runner.doRun(
131             RepositoryNameValidatorTest.suite(), false);
132         System.exit(result.errorCount() + result.failureCount());
133     }
134
135     void testInvalid(String JavaDoc invalid)
136     {
137         try
138         {
139             validator.validate(invalid);
140             System.out.println(invalid);
141             Assert.assertTrue(false);
142         }
143         catch (Exception JavaDoc e)
144         {
145             //ok
146
}
147     }
148
149     void testValid(String JavaDoc valid)
150     {
151         try
152         {
153             validator.validate(valid);
154         }
155         catch (Exception JavaDoc e)
156         {
157             Assert.assertTrue(false);
158         }
159     }
160 }
161
Popular Tags