/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2006, by :
* Corporate:
* EADS Astrium SAS
* EADS CRC
* Individual:
* Claude Cazenave
*
* $Id: TestFileSerializer.java,v 1.2 2007/06/05 14:15:17 ogor Exp $
*
* Changes
* -------
* 4 janv. 2006 : Initial public release (CC);
*
*/
package simtools.util.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import simtools.data.DataSource;
import simtools.data.buffer.Buffer;
import simtools.util.FileSerializer;
import simtools.util.ImageSerializer;
/**
* A FileSerializer test
*
* @author cazenave_c
*
*/
public class TestFileSerializer {
FileSerializer fs;
public TestFileSerializer(FileSerializer s){
fs=s;
}
public void write(Class c, ObjectOutputStream oo, File referenceFile) throws IOException{
fs.write(oo,getFile(c),referenceFile);
}
public void read(Class c, ObjectInputStream io, File referenceFile, boolean noError) throws ClassNotFoundException, IOException{
File f=fs.read(io,referenceFile);
if(f.canRead()){
if(f.equals(getFile(c))){
System.out.println("Ok");
}
else{
System.err.println("Error "+f+" != "+getFile(c));
}
}
else{
if(!noError){
System.out.println("Invalid excepted file="+f);
}
else{
System.err.println("Invalid file="+f);
}
}
}
public static File getFile(Class c){
String name=c.getName();
name=name.substring(name.lastIndexOf('.')+1);
URL url = c.getResource(name+".class");
return new File(url.getFile());
}
public static void main(String[] args){
TestFileSerializer t=new TestFileSerializer(new FileSerializer());
File refFile=getFile(ImageSerializer.class);
ByteArrayOutputStream bo=new ByteArrayOutputStream();
ObjectOutputStream oo;
ObjectInputStream io;
try {
oo = new ObjectOutputStream(bo);
t.write(FileSerializer.class,oo,refFile);
t.write(FileSerializer.class,oo,null);
t.write(FileSerializer.class,oo,refFile);
t.write(DataSource.class,oo,refFile);
t.write(DataSource.class,oo,null);
t.write(DataSource.class,oo,refFile);
t.write(Buffer.class,oo,refFile);
t.write(Buffer.class,oo,null);
t.write(Buffer.class,oo,refFile);
//t.fs.addPreferredDirectory("classes", refFile.getParentFile().getParentFile().getParentFile());
t.write(FileSerializer.class,oo,null);
t.write(DataSource.class,oo,null);
t.write(Buffer.class,oo,null);
ByteArrayInputStream bi=new ByteArrayInputStream(bo.toByteArray());
io = new ObjectInputStream(bi);
t.read(FileSerializer.class,io,refFile,true);
t.read(FileSerializer.class,io,refFile,true);
t.read(FileSerializer.class,io,null,false);
t.read(DataSource.class,io,refFile,true);
t.read(DataSource.class,io,refFile,true);
t.read(DataSource.class,io,null,false);
t.read(Buffer.class,io,refFile,true);
t.read(Buffer.class,io,refFile,true);
t.read(Buffer.class,io,null,false);
t.read(FileSerializer.class,io,null,true);
t.read(DataSource.class,io,null,true);
//t.fs.removePreferredDirectory("classes");
t.read(Buffer.class,io,null,false);
}
catch(Exception e){
e.printStackTrace();
}
}
}