KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > diff > TestListBasedValidator


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.diff;
34
35 import java.io.*;
36
37 import junit.framework.*;
38
39 public class TestListBasedValidator extends TestCase {
40     public void testDefault() throws IOException {
41         Validator validator = new ListBasedValidator();
42
43         assertTrue("package", validator.isPackageAllowed("foobar"));
44         assertTrue("class", validator.isClassAllowed("foobar"));
45         assertTrue("class", validator.isClassAllowed("foobar.foobar"));
46         assertTrue("feature", validator.isFeatureAllowed("foobar"));
47         assertTrue("feature", validator.isFeatureAllowed("foobar.foobar"));
48         assertTrue("feature", validator.isFeatureAllowed("foobar.foobar.foobar"));
49
50         assertTrue("package", validator.isPackageAllowed("barfoo"));
51         assertTrue("class", validator.isClassAllowed("barfoo"));
52         assertTrue("class", validator.isClassAllowed("barfoo.barfoo"));
53         assertTrue("feature", validator.isFeatureAllowed("barfoo"));
54         assertTrue("feature", validator.isFeatureAllowed("barfoo.barfoo"));
55         assertTrue("feature", validator.isFeatureAllowed("barfoo.barfoo.barfoo"));
56     }
57
58     public void testNullFile() throws IOException {
59         Validator validator;
60
61         try {
62             validator = new ListBasedValidator((BufferedReader) null);
63             fail("Created PackageValidator with null");
64         } catch (NullPointerException JavaDoc ex) {
65             // Ignore
66
}
67
68         validator = new ListBasedValidator(new BufferedReader(new StringReader("")));
69
70         assertTrue("package", validator.isPackageAllowed("foobar"));
71         assertTrue("class", validator.isClassAllowed("foobar"));
72         assertTrue("class", validator.isClassAllowed("foobar.foobar"));
73         assertTrue("feature", validator.isFeatureAllowed("foobar"));
74         assertTrue("feature", validator.isFeatureAllowed("foobar.foobar"));
75         assertTrue("feature", validator.isFeatureAllowed("foobar.foobar.foobar"));
76
77         assertTrue("package", validator.isPackageAllowed("barfoo"));
78         assertTrue("class", validator.isClassAllowed("barfoo"));
79         assertTrue("class", validator.isClassAllowed("barfoo.barfoo"));
80         assertTrue("feature", validator.isFeatureAllowed("barfoo"));
81         assertTrue("feature", validator.isFeatureAllowed("barfoo.barfoo"));
82         assertTrue("feature", validator.isFeatureAllowed("barfoo.barfoo.barfoo"));
83     }
84
85     public void testConstructor() throws IOException {
86         Validator validator = new ListBasedValidator(new BufferedReader(new StringReader("foobar\n")));
87
88         assertTrue("package", validator.isPackageAllowed("foobar"));
89         assertTrue("class", validator.isClassAllowed("foobar"));
90         assertTrue("class", !validator.isClassAllowed("foobar.foobar"));
91         assertTrue("feature", validator.isFeatureAllowed("foobar"));
92         assertTrue("feature", !validator.isFeatureAllowed("foobar.foobar"));
93         assertTrue("feature", !validator.isFeatureAllowed("foobar.foobar.foobar"));
94
95         assertTrue("package", !validator.isPackageAllowed("barfoo"));
96         assertTrue("class", !validator.isClassAllowed("barfoo"));
97         assertTrue("class", !validator.isClassAllowed("barfoo.barfoo"));
98         assertTrue("feature", !validator.isFeatureAllowed("barfoo"));
99         assertTrue("feature", !validator.isFeatureAllowed("barfoo.barfoo"));
100         assertTrue("feature", !validator.isFeatureAllowed("barfoo.barfoo.barfoo"));
101     }
102
103     public void testMissingFile1() throws IOException {
104         Validator validator = new ListBasedValidator("no such file");
105
106         assertTrue("package", validator.isPackageAllowed("foobar"));
107         assertTrue("class", validator.isClassAllowed("foobar"));
108         assertTrue("class", validator.isClassAllowed("foobar.foobar"));
109         assertTrue("feature", validator.isFeatureAllowed("foobar"));
110         assertTrue("feature", validator.isFeatureAllowed("foobar.foobar"));
111         assertTrue("feature", validator.isFeatureAllowed("foobar.foobar.foobar"));
112
113         assertTrue("package", validator.isPackageAllowed("barfoo"));
114         assertTrue("class", validator.isClassAllowed("barfoo"));
115         assertTrue("class", validator.isClassAllowed("barfoo.barfoo"));
116         assertTrue("feature", validator.isFeatureAllowed("barfoo"));
117         assertTrue("feature", validator.isFeatureAllowed("barfoo.barfoo"));
118         assertTrue("feature", validator.isFeatureAllowed("barfoo.barfoo.barfoo"));
119     }
120
121     public void testMissingFile2() throws IOException {
122         File file = new File("no such file");
123         assertFalse("File exists", file.exists());
124         
125         Validator validator = new ListBasedValidator(file);
126
127         assertTrue("package", validator.isPackageAllowed("foobar"));
128         assertTrue("class", validator.isClassAllowed("foobar"));
129         assertTrue("class", validator.isClassAllowed("foobar.foobar"));
130         assertTrue("feature", validator.isFeatureAllowed("foobar"));
131         assertTrue("feature", validator.isFeatureAllowed("foobar.foobar"));
132         assertTrue("feature", validator.isFeatureAllowed("foobar.foobar.foobar"));
133
134         assertTrue("package", validator.isPackageAllowed("barfoo"));
135         assertTrue("class", validator.isClassAllowed("barfoo"));
136         assertTrue("class", validator.isClassAllowed("barfoo.barfoo"));
137         assertTrue("feature", validator.isFeatureAllowed("barfoo"));
138         assertTrue("feature", validator.isFeatureAllowed("barfoo.barfoo"));
139         assertTrue("feature", validator.isFeatureAllowed("barfoo.barfoo.barfoo"));
140     }
141
142     public void testMerge() throws IOException {
143         ListBasedValidator validator = new ListBasedValidator();
144
145         validator.load(new BufferedReader(new StringReader("foo\nbar")));
146
147         assertTrue("package foo", validator.isPackageAllowed("foo"));
148         assertTrue("package bar", validator.isPackageAllowed("bar"));
149         assertTrue("package foobar", !validator.isClassAllowed("foobar"));
150         assertTrue("package barfoo", !validator.isClassAllowed("barfoo"));
151
152         validator.load(new BufferedReader(new StringReader("foobar")));
153
154         assertTrue("package foo", validator.isPackageAllowed("foo"));
155         assertTrue("package bar", validator.isPackageAllowed("bar"));
156         assertTrue("package foobar", validator.isClassAllowed("foobar"));
157         assertTrue("package barfoo", !validator.isClassAllowed("barfoo"));
158     }
159
160     public void testConstructorWithSuffix1() throws IOException {
161         Validator validator = new ListBasedValidator(new BufferedReader(new StringReader("foobar [P]\n")));
162
163         assertTrue("package", validator.isPackageAllowed("foobar"));
164         assertTrue("class", validator.isClassAllowed("foobar"));
165         assertTrue("class", !validator.isClassAllowed("foobar.foobar"));
166         assertTrue("feature", validator.isFeatureAllowed("foobar"));
167         assertTrue("feature", !validator.isFeatureAllowed("foobar.foobar"));
168         assertTrue("feature", !validator.isFeatureAllowed("foobar.foobar.foobar"));
169
170         assertTrue("package", !validator.isPackageAllowed("barfoo"));
171         assertTrue("class", !validator.isClassAllowed("barfoo"));
172         assertTrue("class", !validator.isClassAllowed("barfoo.barfoo"));
173         assertTrue("feature", !validator.isFeatureAllowed("barfoo"));
174         assertTrue("feature", !validator.isFeatureAllowed("barfoo.barfoo"));
175         assertTrue("feature", !validator.isFeatureAllowed("barfoo.barfoo.barfoo"));
176     }
177
178     public void testConstructorWithSuffix2() throws IOException {
179         Validator validator = new ListBasedValidator(new BufferedReader(new StringReader("foobar [C]\n")));
180
181         assertTrue("package", validator.isPackageAllowed("foobar"));
182         assertTrue("class", validator.isClassAllowed("foobar"));
183         assertTrue("class", !validator.isClassAllowed("foobar.foobar"));
184         assertTrue("feature", validator.isFeatureAllowed("foobar"));
185         assertTrue("feature", !validator.isFeatureAllowed("foobar.foobar"));
186         assertTrue("feature", !validator.isFeatureAllowed("foobar.foobar.foobar"));
187
188         assertTrue("package", !validator.isPackageAllowed("barfoo"));
189         assertTrue("class", !validator.isClassAllowed("barfoo"));
190         assertTrue("class", !validator.isClassAllowed("barfoo.barfoo"));
191         assertTrue("feature", !validator.isFeatureAllowed("barfoo"));
192         assertTrue("feature", !validator.isFeatureAllowed("barfoo.barfoo"));
193         assertTrue("feature", !validator.isFeatureAllowed("barfoo.barfoo.barfoo"));
194     }
195
196     public void testMergeWithSuffix() throws IOException {
197         ListBasedValidator validator = new ListBasedValidator();
198
199         validator.load(new BufferedReader(new StringReader("foo [P]\nbar [P]")));
200
201         assertTrue("package foo", validator.isPackageAllowed("foo"));
202         assertTrue("package bar", validator.isPackageAllowed("bar"));
203         assertTrue("package foobar", !validator.isClassAllowed("foobar"));
204         assertTrue("package barfoo", !validator.isClassAllowed("barfoo"));
205
206         validator.load(new BufferedReader(new StringReader("foobar [P]")));
207
208         assertTrue("package foo", validator.isPackageAllowed("foo"));
209         assertTrue("package bar", validator.isPackageAllowed("bar"));
210         assertTrue("package foobar", validator.isClassAllowed("foobar"));
211         assertTrue("package barfoo", !validator.isClassAllowed("barfoo"));
212     }
213 }
214
Popular Tags