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 java.awt;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.io.ObjectInputStream;
024: import java.io.ObjectOutputStream;
025: import java.net.URL;
026:
027: import junit.framework.TestCase;
028:
029: public class SerializeTestCase extends TestCase {
030:
031: public static boolean SERIALIZATION_TEST = false;
032:
033: public String serializePath;
034:
035: public SerializeTestCase(String name) {
036: super (name);
037: if (SERIALIZATION_TEST) {
038: String classPath = "../resources/serialization/"
039: + Tools.getClasstPath(this .getClass());
040: URL url = ClassLoader.getSystemClassLoader().getResource(
041: classPath);
042: assertNotNull("Path not found " + classPath, url);
043: serializePath = url.getPath();
044: }
045: }
046:
047: public String objToStr(Object obj) {
048: return obj.toString();
049: }
050:
051: public void checkRead(Object obj) {
052: String file = serializePath + objToStr(obj) + ".ser";
053: Object golden = loadSerialized(file);
054: assertTrue("Non equals objects " + file, objToStr(golden)
055: .equals(objToStr(obj)));
056: }
057:
058: public void checkWrite(Object obj) {
059: String name = objToStr(obj);
060: String expected = serializePath + name + ".ser";
061: File actual = null;
062: try {
063: actual = File.createTempFile(name, ".actual", new File(
064: serializePath));
065: actual.deleteOnExit();
066: } catch (IOException e) {
067: fail("Can't create temp file");
068: }
069: saveSerialized(obj, actual.getPath());
070: assertTrue("Non equals files " + expected, compare(expected,
071: actual.getPath()));
072: }
073:
074: public Object loadSerialized(String file) {
075: try {
076: //System.out.println("load " + file);
077: FileInputStream fs = new FileInputStream(file);
078: ObjectInputStream os = new ObjectInputStream(fs);
079: Object obj = os.readObject();
080: os.close();
081: fs.close();
082: return obj;
083: } catch (Exception e) {
084: fail("Can''t read object from file " + file);
085: }
086: return null;
087: }
088:
089: public void saveSerialized(Object obj) {
090: saveSerialized(obj, serializePath + objToStr(obj));
091: }
092:
093: public void saveSerialized(Object obj, String file) {
094: try {
095: // System.out.println("save " + file);
096: FileOutputStream fs = new FileOutputStream(file);
097: ObjectOutputStream os = new ObjectOutputStream(fs);
098: os.writeObject(obj);
099: os.close();
100: fs.close();
101: } catch (Exception e) {
102: fail("Can''t write object to file " + file);
103: }
104: }
105:
106: boolean compare(String file1, String file2) {
107: boolean cmp = false;
108: try {
109: FileInputStream fs1 = new FileInputStream(file1);
110: FileInputStream fs2 = new FileInputStream(file2);
111:
112: byte[] buf1 = new byte[256];
113: byte[] buf2 = new byte[256];
114: int count1, count2;
115:
116: OUTER: while (true) {
117: count1 = fs1.read(buf1);
118: count2 = fs2.read(buf2);
119:
120: if (count1 != count2) {
121: break OUTER;
122: }
123: if (count1 == -1) {
124: cmp = true;
125: break;
126: }
127: for (int i = 0; i < count1; i++) {
128: if (buf1[i] != buf2[i]) {
129: break OUTER;
130: }
131: }
132: }
133:
134: fs1.close();
135: fs2.close();
136: } catch (Exception e) {
137: fail("Can''t compare files " + file1 + " and " + file2);
138: }
139: return cmp;
140: }
141:
142: }
|