KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > types > TestId


1 /*
2  * Copyright 2001-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
17 package test.types;
18
19 import junit.framework.TestCase;
20 import org.apache.axis.types.Id;
21
22 /**
23  * Test validation of types.Id
24  */

25 public class TestId extends TestCase {
26   // This needs to be extended to test ID specific things
27

28     public TestId(String JavaDoc name) {
29         super(name);
30     }
31
32     /**
33      * Run a failure test. value should be invalid.
34      */

35     private void runFailTest(String JavaDoc value) throws Exception JavaDoc {
36         Id oToken = null;
37         try {
38             oToken = new Id(value);
39         }
40         catch (Exception JavaDoc e) { // catch the validation exception
41
}
42         assertNull(
43                 "Id validation restriction failed. did not restrict bad value [" +
44                    value + "] did not restrict bad value", oToken);
45     }
46
47     /**
48      * Run a successful test. value should be valid.
49      */

50     private void runPassTest(String JavaDoc value) throws Exception JavaDoc {
51         Id oToken = null;
52         try {
53             oToken = new Id(value);
54         }
55         catch (Exception JavaDoc e) { // catch the validation exception
56
}
57         assertEquals("Id strings not equal. orig value:" + value, oToken.toString(), value);
58     }
59
60     /**
61      * Test a simple string.
62      */

63     public void testSimpleString() throws Exception JavaDoc {
64         runPassTest("Atlanta");
65     }
66
67     /**
68      * Test a simple string with allowed punctuation.
69      */

70     public void testPunctuationString() throws Exception JavaDoc {
71         runPassTest("Atlanta_Braves.Home-Team10");
72     }
73
74
75     /**
76      * Test a start character '_'
77      */

78     public void testStartUnderscore() throws Exception JavaDoc {
79         runPassTest("_Braves");
80     }
81
82     /**
83      * Test a simple string with allowed punctuation.
84      */

85     public void testMidColon() throws Exception JavaDoc {
86         runFailTest("Atlanta:_Braves.Home-Team10");
87     }
88
89     /**
90      * Test a start Digit
91      */

92     public void testStartDigit() throws Exception JavaDoc {
93         runFailTest("1_Braves");
94     }
95
96
97     /**
98      * Test a start character ':'
99      */

100     public void testStartColon() throws Exception JavaDoc {
101         runFailTest(":_Braves.Home-Team:1");
102     }
103
104     /**
105      * this is to differentiate from normalized string which cannot accept a \n
106      */

107     public void testLineFeed() throws Exception JavaDoc {
108         runFailTest("line one\n line two");
109     }
110
111     /**
112      * this is to differentiate from normalized string which cannot accept a \t
113      */

114     public void testStringWithTabs() throws Exception JavaDoc {
115         runFailTest("this has \t a tab");
116     }
117
118     /**
119      * this is to differentiate from normalized string which cannot accept leading spaces.
120      */

121     public void testStringWithLeadingSpaces() throws Exception JavaDoc {
122         runFailTest(" a failure case");
123     }
124
125     /**
126      * this is to differentiate from normalized string which cannot accept trailing spaces.
127      */

128     public void testStringWithTrailingSpaces() throws Exception JavaDoc {
129         runFailTest("this is a ");
130     }
131
132     /**
133      * this is to differentiate from normalized string which cannot accept
134      * leading and trailing spaces.
135      */

136     public void testStringWithLeadingAndTrailingSpaces() throws Exception JavaDoc {
137         runFailTest(" centered ");
138     }
139
140     /**
141      * this is to differentiate from normalized string which cannot accept double spaces.
142      */

143     public void testDoubleSpace() throws Exception JavaDoc {
144         runFailTest("a B"); // note: \r fails
145
}
146 }
147
Popular Tags