001: // Copyright 2006 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.util;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
018:
019: import java.io.EOFException;
020: import java.io.InputStream;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023: import java.util.Map;
024:
025: import org.testng.Assert;
026: import org.testng.annotations.Test;
027:
028: /**
029: * Tests for {@link Base64InputStream} and {@link Base64OutputStream}, etc.
030: */
031: public class Base64Tests extends Assert {
032: @SuppressWarnings("unchecked")
033: @Test
034: public void round_trip_is_equal() throws Exception {
035: Map input = newMap();
036:
037: input.put("fred", "flintstone");
038: input.put("barney", "rubble");
039:
040: Base64OutputStream bos = new Base64OutputStream();
041: ObjectOutputStream oos = new ObjectOutputStream(bos);
042:
043: oos.writeObject(input);
044:
045: oos.close();
046:
047: String base64 = bos.toBase64();
048:
049: InputStream is = new Base64InputStream(base64);
050: ObjectInputStream ois = new ObjectInputStream(is);
051:
052: Map output = (Map) ois.readObject();
053:
054: assertEquals(output, input);
055: assertNotSame(output, input);
056: }
057:
058: @Test
059: @SuppressWarnings("unchecked")
060: public void round_trip_is_equal_via_object_wrappers()
061: throws Exception {
062: Map input = newMap();
063:
064: input.put("fred", "flintstone");
065: input.put("barney", "rubble");
066:
067: Base64ObjectOutputStream os = new Base64ObjectOutputStream();
068:
069: os.writeObject(input);
070:
071: os.close();
072:
073: String base64 = os.toBase64();
074:
075: ObjectInputStream ois = new Base64ObjectInputStream(base64);
076:
077: Map output = (Map) ois.readObject();
078:
079: assertEquals(output, input);
080: assertNotSame(output, input);
081: }
082:
083: @Test
084: public void checks_for_eof() throws Exception {
085: String[] values = { "fred", "barney", "wilma" };
086:
087: Base64ObjectOutputStream os = new Base64ObjectOutputStream();
088:
089: for (String value : values)
090: os.writeObject(value);
091:
092: os.close();
093:
094: String base64 = os.toBase64();
095:
096: ObjectInputStream ois = new Base64ObjectInputStream(base64);
097:
098: for (int i = 0; i < 3; i++) {
099: String value = (String) ois.readObject();
100:
101: assertEquals(value, values[i]);
102: }
103:
104: try {
105: ois.readObject();
106: fail("Unreachable.");
107: } catch (EOFException ex) {
108: // Expected.
109: }
110:
111: }
112: }
|