[jboss-svn-commits] JBoss Portal SVN: r5253 - in trunk/portlet: . src/main/org/jboss/portal/portlet/impl/state src/main/org/jboss/portal/portlet/state src/main/org/jboss/portal/test/portlet/state
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Sep 25 20:39:46 EDT 2006
Author: julien at jboss.com
Date: 2006-09-25 20:39:39 -0400 (Mon, 25 Sep 2006)
New Revision: 5253
Added:
trunk/portlet/src/main/org/jboss/portal/portlet/impl/state/StateConverterV0.java
trunk/portlet/src/main/org/jboss/portal/portlet/state/StateConversionException.java
trunk/portlet/src/main/org/jboss/portal/test/portlet/state/StateConverterV0TestCase.java
Modified:
trunk/portlet/build.xml
trunk/portlet/src/main/org/jboss/portal/portlet/state/SimplePropertyMap.java
trunk/portlet/src/main/org/jboss/portal/portlet/state/StateConverter.java
Log:
- addded implementation V0 of the StateConverter interface that relies on DataInputStream and DataOutputStream to convert the ProducerState from/to a byte array
- corresponding test case
Modified: trunk/portlet/build.xml
===================================================================
--- trunk/portlet/build.xml 2006-09-25 21:58:23 UTC (rev 5252)
+++ trunk/portlet/build.xml 2006-09-26 00:39:39 UTC (rev 5253)
@@ -568,7 +568,7 @@
<!--<test todir="${test.reports}" name="org.jboss.portal.test.portlet.jsr168.ext.dispatcher.DispatcherTestSuite"/>-->
<!--<test todir="${test.reports}" name="org.jboss.portal.test.portlet.jsr168.ext.portletrequests.PortletRequestTestSuite"/>-->
- <test todir="${test.reports}" name="org.jboss.portal.test.portlet.ParametersTestCase"/>
+ <test todir="${test.reports}" name="org.jboss.portal.test.portlet.state.StateConverterV0TestCase"/>
</x-test>
<x-classpath>
Added: trunk/portlet/src/main/org/jboss/portal/portlet/impl/state/StateConverterV0.java
===================================================================
--- trunk/portlet/src/main/org/jboss/portal/portlet/impl/state/StateConverterV0.java 2006-09-25 21:58:23 UTC (rev 5252)
+++ trunk/portlet/src/main/org/jboss/portal/portlet/impl/state/StateConverterV0.java 2006-09-26 00:39:39 UTC (rev 5253)
@@ -0,0 +1,148 @@
+/*
+* 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.portal.portlet.impl.state;
+
+import org.jboss.portal.portlet.state.StateConverter;
+import org.jboss.portal.portlet.state.PropertyMap;
+import org.jboss.portal.portlet.state.SimplePropertyMap;
+import org.jboss.portal.portlet.state.StateConversionException;
+import org.jboss.portal.portlet.state.producer.ProducerState;
+import org.jboss.portal.jems.as.system.AbstractJBossService;
+import org.jboss.portal.common.value.StringValue;
+
+import java.io.DataOutputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class StateConverterV0 extends AbstractJBossService implements StateConverter
+{
+
+ /** . */
+ private static final int MAGIC_VALUE = 0xBE57A515;
+
+ /** . */
+ private static final byte VERSION_ID = 0;
+
+ public byte[] marshall(ProducerState state) throws StateConversionException
+ {
+ if (state == null)
+ {
+ throw new IllegalArgumentException("No null state");
+ }
+ try
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ DataOutputStream dos = new DataOutputStream(baos);
+
+ //
+ dos.writeInt(MAGIC_VALUE);
+ dos.write(VERSION_ID);
+ dos.writeUTF(state.getPortletId());
+ PropertyMap map = state.getProperties();
+ dos.writeInt(map.size());
+ for (Iterator i = map.entrySet().iterator(); i.hasNext();)
+ {
+ Map.Entry entry = (Map.Entry)i.next();
+ String key = (String)entry.getKey();
+ StringValue value = (StringValue)entry.getValue();
+ String[] strings = (String[])value.asObjectArray();
+ dos.writeUTF(key);
+ dos.writeInt(strings.length);
+ for (int j = 0; j < strings.length; j++)
+ {
+ String string = strings[j];
+ if (string == null)
+ {
+ dos.writeBoolean(true);
+ }
+ else
+ {
+ dos.writeBoolean(false);
+ dos.writeUTF(string);
+ }
+ }
+ }
+ dos.close();
+ return baos.toByteArray();
+ }
+ catch (IOException e)
+ {
+ throw new StateConversionException(e);
+ }
+ }
+
+ public ProducerState unmarshall(byte[] marshalledState) throws StateConversionException
+ {
+ if (marshalledState == null)
+ {
+ throw new IllegalArgumentException("No null bytes");
+ }
+ try
+ {
+ ByteArrayInputStream bais = new ByteArrayInputStream(marshalledState);
+ DataInputStream dis = new DataInputStream(bais);
+ int magicValue = dis.readInt();
+ if (magicValue != MAGIC_VALUE)
+ {
+ throw new StateConversionException("Bad magic value " + Integer.toHexString(magicValue));
+ }
+ byte versionId = dis.readByte();
+ if (versionId > 0)
+ {
+ throw new StateConversionException("Bad version id " + versionId);
+ }
+ String portletId = dis.readUTF();
+ int size = dis.readInt();
+ PropertyMap properties = new SimplePropertyMap(size);
+ while (size-- > 0)
+ {
+ String key = dis.readUTF();
+ int length = dis.readInt();
+ String[] strings = new String[length];
+ for (int i = 0; i < strings.length; i++)
+ {
+ boolean isNull = dis.readBoolean();
+ if (isNull == false)
+ {
+ String string = dis.readUTF();
+ strings[i] = string;
+ }
+ }
+ StringValue value = new StringValue(strings);
+ properties.setProperty(key, value);
+ }
+ return new ProducerState(portletId, properties);
+ }
+ catch (IOException e)
+ {
+ throw new StateConversionException(e);
+ }
+ }
+}
Modified: trunk/portlet/src/main/org/jboss/portal/portlet/state/SimplePropertyMap.java
===================================================================
--- trunk/portlet/src/main/org/jboss/portal/portlet/state/SimplePropertyMap.java 2006-09-25 21:58:23 UTC (rev 5252)
+++ trunk/portlet/src/main/org/jboss/portal/portlet/state/SimplePropertyMap.java 2006-09-26 00:39:39 UTC (rev 5253)
@@ -45,6 +45,11 @@
/** . */
protected final Map values;
+ public SimplePropertyMap(int size)
+ {
+ this(new HashMap(size));
+ }
+
public SimplePropertyMap()
{
this(new HashMap());
Added: trunk/portlet/src/main/org/jboss/portal/portlet/state/StateConversionException.java
===================================================================
--- trunk/portlet/src/main/org/jboss/portal/portlet/state/StateConversionException.java 2006-09-25 21:58:23 UTC (rev 5252)
+++ trunk/portlet/src/main/org/jboss/portal/portlet/state/StateConversionException.java 2006-09-26 00:39:39 UTC (rev 5253)
@@ -0,0 +1,48 @@
+/*
+* 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.portal.portlet.state;
+
+/**
+ * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class StateConversionException extends RuntimeException
+{
+ public StateConversionException()
+ {
+ }
+
+ public StateConversionException(String message)
+ {
+ super(message);
+ }
+
+ public StateConversionException(String message, Throwable cause)
+ {
+ super(message, cause);
+ }
+
+ public StateConversionException(Throwable cause)
+ {
+ super(cause);
+ }
+}
Modified: trunk/portlet/src/main/org/jboss/portal/portlet/state/StateConverter.java
===================================================================
--- trunk/portlet/src/main/org/jboss/portal/portlet/state/StateConverter.java 2006-09-25 21:58:23 UTC (rev 5252)
+++ trunk/portlet/src/main/org/jboss/portal/portlet/state/StateConverter.java 2006-09-26 00:39:39 UTC (rev 5253)
@@ -29,7 +29,23 @@
*/
public interface StateConverter
{
- byte[] marshall(ProducerState state);
+ /**
+ * Marshall the producer state as a byte array.
+ *
+ * @param state the producer state
+ * @return the marshalled state
+ * @throws StateConversionException if marshalling cannot be performed
+ * @throws IllegalArgumentException if the state is null
+ */
+ byte[] marshall(ProducerState state) throws StateConversionException, IllegalArgumentException;
- ProducerState unmarshall(byte[] marshalledState);
+ /**
+ * Unmarshall the producer state from a byte array.
+ *
+ * @param marshalledState the marshalled state
+ * @return the producer state
+ * @throws StateConversionException if unmarshalling cannot be performed
+ * @throws IllegalArgumentException if the argument is null
+ */
+ ProducerState unmarshall(byte[] marshalledState) throws StateConversionException, IllegalArgumentException;
}
Added: trunk/portlet/src/main/org/jboss/portal/test/portlet/state/StateConverterV0TestCase.java
===================================================================
--- trunk/portlet/src/main/org/jboss/portal/test/portlet/state/StateConverterV0TestCase.java 2006-09-25 21:58:23 UTC (rev 5252)
+++ trunk/portlet/src/main/org/jboss/portal/test/portlet/state/StateConverterV0TestCase.java 2006-09-26 00:39:39 UTC (rev 5253)
@@ -0,0 +1,121 @@
+/*
+* 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.portal.test.portlet.state;
+
+import org.jboss.portal.portlet.state.producer.ProducerState;
+import org.jboss.portal.portlet.state.StateConverter;
+import org.jboss.portal.portlet.state.PropertyMap;
+import org.jboss.portal.portlet.state.SimplePropertyMap;
+import org.jboss.portal.portlet.state.StateConversionException;
+import org.jboss.portal.portlet.impl.state.StateConverterV0;
+import org.jboss.portal.portlet.test.ValueMapAssert;
+import org.jboss.portal.common.value.StringValue;
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class StateConverterV0TestCase extends TestCase
+{
+
+ private final StateConverter converter = new StateConverterV0();
+
+ public void testIAE()
+ {
+ try
+ {
+ converter.marshall(null);
+ fail("Was expecting an IAE");
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ try
+ {
+ converter.unmarshall(null);
+ fail("Was expecting an IAE");
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ }
+
+ public void testAlteredMagic()
+ {
+ byte[] bytes = converter.marshall(new ProducerState("foo"));
+ bytes[0] = (byte)0xCA;
+ bytes[1] = (byte)0xFE;
+ bytes[2] = (byte)0xBA;
+ bytes[3] = (byte)0xBE;
+ try
+ {
+ converter.unmarshall(bytes);
+ fail("Was expecting a state conversion exception");
+ }
+ catch (StateConversionException expected)
+ {
+ }
+ }
+
+ public void testBadVersionNumber()
+ {
+ byte[] bytes = converter.marshall(new ProducerState("foo"));
+ bytes[4] = (byte)0x01;
+ try
+ {
+ converter.unmarshall(bytes);
+ fail("Was expecting a state conversion exception");
+ }
+ catch (StateConversionException expected)
+ {
+ }
+ }
+
+ public void testWorks() throws Exception
+ {
+ assertWorks(new ProducerState("foo"));
+
+ //
+ PropertyMap props = new SimplePropertyMap();
+ props.setProperty("ab", new StringValue("cd"));
+ props.setProperty("ef", new StringValue(new String[0]));
+ props.setProperty("gh", new StringValue(new String[]{""}));
+ props.setProperty("ij", new StringValue(new String[]{"kl"}));
+ props.setProperty("mn", new StringValue(new String[]{"op",null}));
+ props.setProperty("qr", new StringValue(new String[]{null,"st"}));
+ props.setProperty("uv", new StringValue(new String[]{"wx",null,"yz"}));
+ assertWorks(new ProducerState("bar", props));
+ }
+
+ private void assertWorks(ProducerState expectedState) throws Exception
+ {
+ byte[] bytes = converter.marshall(expectedState);
+ assertNotNull(bytes);
+ ProducerState state = converter.unmarshall(bytes);
+ assertNotNull(state);
+
+ //
+ assertEquals(expectedState.getPortletId(), state.getPortletId());
+ ValueMapAssert.assertEquals(expectedState.getProperties(), state.getProperties());
+ }
+}
More information about the jboss-svn-commits
mailing list