001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.File;
022: import java.io.IOException;
023: import java.io.ObjectInputStream;
024: import java.io.ObjectOutputStream;
025:
026: import junit.framework.Test;
027: import junit.framework.TestSuite;
028: import junit.textui.TestRunner;
029:
030: import org.apache.commons.io.testtools.FileBasedTestCase;
031:
032: /**
033: * This is used to test IOCase for correctness.
034: *
035: * @author Stephen Colebourne
036: * @version $Id: IOCaseTestCase.java 437567 2006-08-28 06:39:07Z bayard $
037: */
038: public class IOCaseTestCase extends FileBasedTestCase {
039:
040: private static final boolean WINDOWS = (File.separatorChar == '\\');
041:
042: public static void main(String[] args) {
043: TestRunner.run(suite());
044: }
045:
046: public static Test suite() {
047: return new TestSuite(IOCaseTestCase.class);
048: }
049:
050: public IOCaseTestCase(String name) throws IOException {
051: super (name);
052: }
053:
054: protected void setUp() throws Exception {
055:
056: }
057:
058: protected void tearDown() throws Exception {
059: }
060:
061: //-----------------------------------------------------------------------
062: public void test_forName() throws Exception {
063: assertEquals(IOCase.SENSITIVE, IOCase.forName("Sensitive"));
064: assertEquals(IOCase.INSENSITIVE, IOCase.forName("Insensitive"));
065: assertEquals(IOCase.SYSTEM, IOCase.forName("System"));
066: try {
067: IOCase.forName("Blah");
068: fail();
069: } catch (IllegalArgumentException ex) {
070: }
071: try {
072: IOCase.forName(null);
073: fail();
074: } catch (IllegalArgumentException ex) {
075: }
076: }
077:
078: public void test_serialization() throws Exception {
079: assertSame(IOCase.SENSITIVE, serialize(IOCase.SENSITIVE));
080: assertSame(IOCase.INSENSITIVE, serialize(IOCase.INSENSITIVE));
081: assertSame(IOCase.SYSTEM, serialize(IOCase.SYSTEM));
082: }
083:
084: public void test_getName() throws Exception {
085: assertEquals("Sensitive", IOCase.SENSITIVE.getName());
086: assertEquals("Insensitive", IOCase.INSENSITIVE.getName());
087: assertEquals("System", IOCase.SYSTEM.getName());
088: }
089:
090: public void test_toString() throws Exception {
091: assertEquals("Sensitive", IOCase.SENSITIVE.toString());
092: assertEquals("Insensitive", IOCase.INSENSITIVE.toString());
093: assertEquals("System", IOCase.SYSTEM.toString());
094: }
095:
096: public void test_isCaseSensitive() throws Exception {
097: assertEquals(true, IOCase.SENSITIVE.isCaseSensitive());
098: assertEquals(false, IOCase.INSENSITIVE.isCaseSensitive());
099: assertEquals(!WINDOWS, IOCase.SYSTEM.isCaseSensitive());
100: }
101:
102: //-----------------------------------------------------------------------
103: public void test_checkEquals_functionality() throws Exception {
104: assertEquals(false, IOCase.SENSITIVE.checkEquals("ABC", ""));
105: assertEquals(false, IOCase.SENSITIVE.checkEquals("ABC", "A"));
106: assertEquals(false, IOCase.SENSITIVE.checkEquals("ABC", "AB"));
107: assertEquals(true, IOCase.SENSITIVE.checkEquals("ABC", "ABC"));
108: assertEquals(false, IOCase.SENSITIVE.checkEquals("ABC", "BC"));
109: assertEquals(false, IOCase.SENSITIVE.checkEquals("ABC", "C"));
110: assertEquals(false, IOCase.SENSITIVE.checkEquals("ABC", "ABCD"));
111: assertEquals(false, IOCase.SENSITIVE.checkEquals("", "ABC"));
112: assertEquals(true, IOCase.SENSITIVE.checkEquals("", ""));
113:
114: try {
115: IOCase.SENSITIVE.checkEquals("ABC", null);
116: fail();
117: } catch (NullPointerException ex) {
118: }
119: try {
120: IOCase.SENSITIVE.checkEquals(null, "ABC");
121: fail();
122: } catch (NullPointerException ex) {
123: }
124: try {
125: IOCase.SENSITIVE.checkEquals(null, null);
126: fail();
127: } catch (NullPointerException ex) {
128: }
129: }
130:
131: public void test_checkEquals_case() throws Exception {
132: assertEquals(true, IOCase.SENSITIVE.checkEquals("ABC", "ABC"));
133: assertEquals(false, IOCase.SENSITIVE.checkEquals("ABC", "Abc"));
134:
135: assertEquals(true, IOCase.INSENSITIVE.checkEquals("ABC", "ABC"));
136: assertEquals(true, IOCase.INSENSITIVE.checkEquals("ABC", "Abc"));
137:
138: assertEquals(true, IOCase.SYSTEM.checkEquals("ABC", "ABC"));
139: assertEquals(WINDOWS, IOCase.SYSTEM.checkEquals("ABC", "Abc"));
140: }
141:
142: //-----------------------------------------------------------------------
143: public void test_checkStartsWith_functionality() throws Exception {
144: assertEquals(true, IOCase.SENSITIVE.checkStartsWith("ABC", ""));
145: assertEquals(true, IOCase.SENSITIVE.checkStartsWith("ABC", "A"));
146: assertEquals(true, IOCase.SENSITIVE
147: .checkStartsWith("ABC", "AB"));
148: assertEquals(true, IOCase.SENSITIVE.checkStartsWith("ABC",
149: "ABC"));
150: assertEquals(false, IOCase.SENSITIVE.checkStartsWith("ABC",
151: "BC"));
152: assertEquals(false, IOCase.SENSITIVE
153: .checkStartsWith("ABC", "C"));
154: assertEquals(false, IOCase.SENSITIVE.checkStartsWith("ABC",
155: "ABCD"));
156: assertEquals(false, IOCase.SENSITIVE.checkStartsWith("", "ABC"));
157: assertEquals(true, IOCase.SENSITIVE.checkStartsWith("", ""));
158:
159: try {
160: IOCase.SENSITIVE.checkStartsWith("ABC", null);
161: fail();
162: } catch (NullPointerException ex) {
163: }
164: try {
165: IOCase.SENSITIVE.checkStartsWith(null, "ABC");
166: fail();
167: } catch (NullPointerException ex) {
168: }
169: try {
170: IOCase.SENSITIVE.checkStartsWith(null, null);
171: fail();
172: } catch (NullPointerException ex) {
173: }
174: }
175:
176: public void test_checkStartsWith_case() throws Exception {
177: assertEquals(true, IOCase.SENSITIVE
178: .checkStartsWith("ABC", "AB"));
179: assertEquals(false, IOCase.SENSITIVE.checkStartsWith("ABC",
180: "Ab"));
181:
182: assertEquals(true, IOCase.INSENSITIVE.checkStartsWith("ABC",
183: "AB"));
184: assertEquals(true, IOCase.INSENSITIVE.checkStartsWith("ABC",
185: "Ab"));
186:
187: assertEquals(true, IOCase.SYSTEM.checkStartsWith("ABC", "AB"));
188: assertEquals(WINDOWS, IOCase.SYSTEM
189: .checkStartsWith("ABC", "Ab"));
190: }
191:
192: //-----------------------------------------------------------------------
193: public void test_checkEndsWith_functionality() throws Exception {
194: assertEquals(true, IOCase.SENSITIVE.checkEndsWith("ABC", ""));
195: assertEquals(false, IOCase.SENSITIVE.checkEndsWith("ABC", "A"));
196: assertEquals(false, IOCase.SENSITIVE.checkEndsWith("ABC", "AB"));
197: assertEquals(true, IOCase.SENSITIVE.checkEndsWith("ABC", "ABC"));
198: assertEquals(true, IOCase.SENSITIVE.checkEndsWith("ABC", "BC"));
199: assertEquals(true, IOCase.SENSITIVE.checkEndsWith("ABC", "C"));
200: assertEquals(false, IOCase.SENSITIVE.checkEndsWith("ABC",
201: "ABCD"));
202: assertEquals(false, IOCase.SENSITIVE.checkEndsWith("", "ABC"));
203: assertEquals(true, IOCase.SENSITIVE.checkEndsWith("", ""));
204:
205: try {
206: IOCase.SENSITIVE.checkEndsWith("ABC", null);
207: fail();
208: } catch (NullPointerException ex) {
209: }
210: try {
211: IOCase.SENSITIVE.checkEndsWith(null, "ABC");
212: fail();
213: } catch (NullPointerException ex) {
214: }
215: try {
216: IOCase.SENSITIVE.checkEndsWith(null, null);
217: fail();
218: } catch (NullPointerException ex) {
219: }
220: }
221:
222: public void test_checkEndsWith_case() throws Exception {
223: assertEquals(true, IOCase.SENSITIVE.checkEndsWith("ABC", "BC"));
224: assertEquals(false, IOCase.SENSITIVE.checkEndsWith("ABC", "Bc"));
225:
226: assertEquals(true, IOCase.INSENSITIVE
227: .checkEndsWith("ABC", "BC"));
228: assertEquals(true, IOCase.INSENSITIVE
229: .checkEndsWith("ABC", "Bc"));
230:
231: assertEquals(true, IOCase.SYSTEM.checkEndsWith("ABC", "BC"));
232: assertEquals(WINDOWS, IOCase.SYSTEM.checkEndsWith("ABC", "Bc"));
233: }
234:
235: //-----------------------------------------------------------------------
236: public void test_checkRegionMatches_functionality()
237: throws Exception {
238: assertEquals(true, IOCase.SENSITIVE.checkRegionMatches("ABC",
239: 0, ""));
240: assertEquals(true, IOCase.SENSITIVE.checkRegionMatches("ABC",
241: 0, "A"));
242: assertEquals(true, IOCase.SENSITIVE.checkRegionMatches("ABC",
243: 0, "AB"));
244: assertEquals(true, IOCase.SENSITIVE.checkRegionMatches("ABC",
245: 0, "ABC"));
246: assertEquals(false, IOCase.SENSITIVE.checkRegionMatches("ABC",
247: 0, "BC"));
248: assertEquals(false, IOCase.SENSITIVE.checkRegionMatches("ABC",
249: 0, "C"));
250: assertEquals(false, IOCase.SENSITIVE.checkRegionMatches("ABC",
251: 0, "ABCD"));
252: assertEquals(false, IOCase.SENSITIVE.checkRegionMatches("", 0,
253: "ABC"));
254: assertEquals(true, IOCase.SENSITIVE.checkRegionMatches("", 0,
255: ""));
256:
257: assertEquals(true, IOCase.SENSITIVE.checkRegionMatches("ABC",
258: 1, ""));
259: assertEquals(false, IOCase.SENSITIVE.checkRegionMatches("ABC",
260: 1, "A"));
261: assertEquals(false, IOCase.SENSITIVE.checkRegionMatches("ABC",
262: 1, "AB"));
263: assertEquals(false, IOCase.SENSITIVE.checkRegionMatches("ABC",
264: 1, "ABC"));
265: assertEquals(true, IOCase.SENSITIVE.checkRegionMatches("ABC",
266: 1, "BC"));
267: assertEquals(false, IOCase.SENSITIVE.checkRegionMatches("ABC",
268: 1, "C"));
269: assertEquals(false, IOCase.SENSITIVE.checkRegionMatches("ABC",
270: 1, "ABCD"));
271: assertEquals(false, IOCase.SENSITIVE.checkRegionMatches("", 1,
272: "ABC"));
273: assertEquals(false, IOCase.SENSITIVE.checkRegionMatches("", 1,
274: ""));
275:
276: try {
277: IOCase.SENSITIVE.checkRegionMatches("ABC", 0, null);
278: fail();
279: } catch (NullPointerException ex) {
280: }
281: try {
282: IOCase.SENSITIVE.checkRegionMatches(null, 0, "ABC");
283: fail();
284: } catch (NullPointerException ex) {
285: }
286: try {
287: IOCase.SENSITIVE.checkRegionMatches(null, 0, null);
288: fail();
289: } catch (NullPointerException ex) {
290: }
291: try {
292: IOCase.SENSITIVE.checkRegionMatches("ABC", 1, null);
293: fail();
294: } catch (NullPointerException ex) {
295: }
296: try {
297: IOCase.SENSITIVE.checkRegionMatches(null, 1, "ABC");
298: fail();
299: } catch (NullPointerException ex) {
300: }
301: try {
302: IOCase.SENSITIVE.checkRegionMatches(null, 1, null);
303: fail();
304: } catch (NullPointerException ex) {
305: }
306: }
307:
308: public void test_checkRegionMatches_case() throws Exception {
309: assertEquals(true, IOCase.SENSITIVE.checkRegionMatches("ABC",
310: 0, "AB"));
311: assertEquals(false, IOCase.SENSITIVE.checkRegionMatches("ABC",
312: 0, "Ab"));
313:
314: assertEquals(true, IOCase.INSENSITIVE.checkRegionMatches("ABC",
315: 0, "AB"));
316: assertEquals(true, IOCase.INSENSITIVE.checkRegionMatches("ABC",
317: 0, "Ab"));
318:
319: assertEquals(true, IOCase.SYSTEM.checkRegionMatches("ABC", 0,
320: "AB"));
321: assertEquals(WINDOWS, IOCase.SYSTEM.checkRegionMatches("ABC",
322: 0, "Ab"));
323: }
324:
325: //-----------------------------------------------------------------------
326: private IOCase serialize(IOCase value) throws Exception {
327: ByteArrayOutputStream buf = new ByteArrayOutputStream();
328: ObjectOutputStream out = new ObjectOutputStream(buf);
329: out.writeObject(value);
330: out.flush();
331: out.close();
332:
333: ByteArrayInputStream bufin = new ByteArrayInputStream(buf
334: .toByteArray());
335: ObjectInputStream in = new ObjectInputStream(bufin);
336: return (IOCase) in.readObject();
337: }
338:
339: }
|