/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This 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 software 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 software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.serial.substitutiontest;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.OutputStream;
import junit.framework.TestCase;
import org.jboss.serial.io.JBossObjectOutputStream;
import org.jboss.serial.objectmetamodel.DataContainer;
import org.jboss.serial.objectmetamodel.ObjectSubstitutionInterface;
import org.jboss.serial.util.StringUtil;
/**
* $Id: ObjectSubstitutionTestCase.java 294 2006-05-20 01:56:07Z csuconic $
*
* @author <a href="mailto:clebert.suconic@jboss.com">Clebert Suconic</a>
*/
public class ObjectSubstitutionTestCase extends TestCase {
class Substitution implements ObjectSubstitutionInterface
{
public Object replaceObject(Object obj) throws IOException {
if (obj instanceof String)
{
return "modified";
} else
{
return obj;
}
}
}
public void testSubstitution() throws Exception
{
Level1 level1 = new Level1();
DataContainer container = new DataContainer(this.getClass().getClassLoader(),new Substitution(),false,null);
ObjectOutput output =container.getOutput();
output.writeObject(level1);
output.flush();
Level1 newLevel1 = (Level1)container.getInput().readObject();
assertEquals("modified",newLevel1.getValue());
assertEquals("modified",newLevel1.getLevel2().getValue());
assertEquals("modified",newLevel1.getLevel2().getLevel3().getValue());
}
public void testSubstitutionOnStream() throws Exception
{
Level1 level1 = new Level1();
TestObjectOutputStream outTest = new TestObjectOutputStream(null,true);
Level1 newLevel1 = (Level1)outTest.smartClone(level1);
assertEquals("modified",newLevel1.getValue());
assertEquals("modified",newLevel1.getLevel2().getValue());
assertEquals("modified",newLevel1.getLevel2().getLevel3().getValue());
}
public void testSubstitutionOnStream2() throws Exception
{
Level1 level1 = new Level1();
TestObjectOutputStream outTest = new TestObjectOutputStream(null,false);
Level1 newLevel1 = (Level1)outTest.smartClone(level1);
assertEquals("original",newLevel1.getValue());
assertEquals("original",newLevel1.getLevel2().getValue());
assertEquals("original",newLevel1.getLevel2().getLevel3().getValue());
}
static class TestObjectOutputStream extends JBossObjectOutputStream
{
public TestObjectOutputStream(OutputStream output, boolean enable) throws IOException
{
super(output);
this.enableReplaceObject(enable);
}
protected Object replaceObject(Object obj) throws IOException
{
if (obj instanceof String)
{
return "modified";
} else
{
return obj;
}
}
}
}