Author: julien(a)jboss.com
Date: 2007-11-27 15:52:01 -0500 (Tue, 27 Nov 2007)
New Revision: 9137
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIObjectImpl.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/UIObject.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/StateChangeVetoException.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/MockModel.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/MockObject.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java
Log:
implement property update of the model + test cases
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIObjectImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIObjectImpl.java 2007-11-27
20:17:12 UTC (rev 9136)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/UIObjectImpl.java 2007-11-27
20:52:01 UTC (rev 9137)
@@ -22,10 +22,10 @@
******************************************************************************/
package org.jboss.portal.presentation.impl.model;
-import org.jboss.portal.common.NotYetImplemented;
import org.jboss.portal.presentation.model.StateScopeType;
import org.jboss.portal.presentation.model.UIObject;
import org.jboss.portal.presentation.model.state.structural.ObjectState;
+import org.jboss.portal.presentation.model.state.StateChangeVetoException;
import java.io.Serializable;
import java.util.HashMap;
@@ -57,7 +57,7 @@
/**
* This is used to assist with data needed during lazy loading, other state related
data etc
*/
- private final ObjectState state;
+ private ObjectState state;
/**
*
@@ -152,22 +152,38 @@
return getProperty(scopeType, propertyName, Object.class);
}
- public <T> void setProperty(StateScopeType scopeType, String propertyName, T
propertyValue)
+ public <T> void setProperty(StateScopeType scopeType, String propertyName, T
propertyValue) throws StateChangeVetoException
{
- Map map;
switch (scopeType)
{
case CONTENT:
- map = transientState;
+ updateProperty(transientState, propertyName, propertyValue);
break;
case NAVIGATIONAL:
- map = navigationalState;
+ updateProperty(navigationalState, propertyName, propertyValue);
break;
case STRUCTURAL:
- throw new NotYetImplemented();
+ if (propertyValue instanceof String)
+ {
+ Map<String, String> changes = new HashMap<String, String>();
+ changes.put(propertyName, (String)propertyValue);
+ context.getModelLoader().update(id, changes);
+ Map<String, String> updatedProperties = new HashMap<String,
String>(state.getProperties());
+ updateProperty(updatedProperties, propertyName, (String)propertyValue);
+ state = new ObjectState(state.getType(), state.getName(),
updatedProperties, state.getParentId(), state.getChildrenIds());
+ }
+ else
+ {
+ throw new StateChangeVetoException("Structural property value must be
string value");
+ }
+ break;
default:
throw new AssertionError();
}
+ }
+
+ private <T> void updateProperty(Map<String, T> map, String propertyName, T
propertyValue)
+ {
if (propertyValue == null)
{
map.remove(propertyName);
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/UIObject.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/UIObject.java 2007-11-27
20:17:12 UTC (rev 9136)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/UIObject.java 2007-11-27
20:52:01 UTC (rev 9137)
@@ -22,6 +22,8 @@
******************************************************************************/
package org.jboss.portal.presentation.model;
+import org.jboss.portal.presentation.model.state.StateChangeVetoException;
+
import java.util.List;
/**
@@ -53,7 +55,7 @@
<T> T getProperty(StateScopeType scopeType, String propertyName, Class<T>
propertyType);
/**
- * Equivalent to call
<code>getProperty(StateScopeType,String,Class)</code> with the
<code>Object.class</code>
+ * Equivalent to call
<code>getPropertyValue(StateScopeType,String,Class)</code> with the
<code>Object.class</code>
* literal.
*
* @param scopeType
@@ -62,7 +64,7 @@
*/
Object getProperty(StateScopeType scopeType, String propertyName);
- <T> void setProperty(StateScopeType scopeType, String propertyName, T
propertyValue);
+ <T> void setProperty(StateScopeType scopeType, String propertyName, T
propertyValue) throws StateChangeVetoException;
/**
* Create a child with a specified type.
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/StateChangeVetoException.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/StateChangeVetoException.java 2007-11-27
20:17:12 UTC (rev 9136)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/StateChangeVetoException.java 2007-11-27
20:52:01 UTC (rev 9137)
@@ -26,7 +26,7 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 630 $
*/
-public class StateChangeVetoException extends Exception
+public class StateChangeVetoException extends RuntimeException
{
public StateChangeVetoException()
{
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/MockModel.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/MockModel.java 2007-11-27
20:17:12 UTC (rev 9136)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/MockModel.java 2007-11-27
20:52:01 UTC (rev 9137)
@@ -31,6 +31,7 @@
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
+import java.util.Set;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
@@ -81,9 +82,12 @@
private final String id = "" + generator++;
/** . */
- private final Map<String, String> properties;
+ private final Map<String, String> propertyValues;
/** . */
+ private final Map<String, Boolean> propertyMutables;
+
+ /** . */
private final Map<String, MockObjectImpl> children;
/** . */
@@ -95,7 +99,8 @@
this.name = "";
this.type = MockObject.Type.CONTEXT;
this.children = new LinkedHashMap<String, MockObjectImpl>();
- this.properties = new HashMap<String, String>();
+ this.propertyValues = new HashMap<String, String>();
+ this.propertyMutables = new HashMap<String, Boolean>();
//
universe.put(id, this);
@@ -112,7 +117,8 @@
this.name = name;
this.type = type;
this.children = new LinkedHashMap<String, MockObjectImpl>();
- this.properties = new HashMap<String, String>();
+ this.propertyValues = new HashMap<String, String>();
+ this.propertyMutables = new HashMap<String, Boolean>();
//
this.parent = parent;
@@ -144,12 +150,12 @@
{
childrenIds.add(child.id);
}
- return new ObjectState(type.clazz, name, new HashMap<String,
String>(properties), parent != null ? parent.id : null, childrenIds);
+ return new ObjectState(type.clazz, name, new HashMap<String,
String>(propertyValues), parent != null ? parent.id : null, childrenIds);
}
- public String getProperty(String name)
+ public String getPropertyValue(String propertyName)
{
- return properties.get(name);
+ return propertyValues.get(propertyName);
}
public MockObject addChild(String name, MockObject.Type type)
@@ -163,21 +169,47 @@
return new MockObjectImpl(this, name, type);
}
- public void setProperty(String name, String value)
+ public Set<String> getPropertyNames()
{
- if (name == null)
+ return propertyValues.keySet();
+ }
+
+ public void setPropertyMutable(String propertyName, Boolean propertyMutable)
+ {
+ if (propertyName == null)
{
throw new IllegalArgumentException();
}
- if (value != null)
+ if (propertyMutable != null)
{
- properties.put(name, value);
+ propertyMutables.put(name, propertyMutable);
}
else
{
- properties.remove(name);
+ propertyMutables.remove(name);
}
}
+
+ public Boolean isPropertyMutable(String propertyName)
+ {
+ return propertyMutables.get(propertyName);
+ }
+
+ public void setPropertyValue(String propertyName, String propertyValue)
+ {
+ if (propertyName == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (propertyValue != null)
+ {
+ propertyValues.put(propertyName, propertyValue);
+ }
+ else
+ {
+ propertyValues.remove(propertyName);
+ }
+ }
}
public ObjectState create(String parentId, Class<? extends UIObject> type,
String name, Map<String, String> properties) throws StateChangeVetoException
@@ -195,8 +227,42 @@
throw new StateChangeVetoException();
}
- public void update(String objectId, Map<String, String> properties) throws
StateChangeVetoException
+ public void update(String objectId, Map<String, String> changes) throws
StateChangeVetoException
{
- throw new StateChangeVetoException();
+ if (objectId == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (changes == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ MockObjectImpl object = universe.get(objectId);
+ if (object == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ for (Map.Entry<String, String> entry : changes.entrySet())
+ {
+ String propertyName = entry.getKey();
+ Boolean mutable = object.propertyMutables.get(propertyName);
+ if (Boolean.FALSE.equals(mutable))
+ {
+ throw new StateChangeVetoException("Cannot modify non mutable
property");
+ }
+ String propertyValue = entry.getValue();
+ if (propertyValue != null)
+ {
+ object.propertyValues.put(propertyName, propertyValue);
+ }
+ else
+ {
+ object.propertyValues.remove(propertyName);
+ }
+ }
}
}
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/MockObject.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/MockObject.java 2007-11-27
20:17:12 UTC (rev 9136)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/MockObject.java 2007-11-27
20:52:01 UTC (rev 9137)
@@ -29,6 +29,8 @@
import org.jboss.portal.presentation.model.UIWindow;
import org.jboss.portal.presentation.model.state.structural.ObjectState;
+import java.util.Set;
+
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 630 $
@@ -59,10 +61,16 @@
String getId();
- String getProperty(String name);
+ String getPropertyValue(String propertyName);
- void setProperty(String name, String value);
+ void setPropertyMutable(String propertyName, Boolean propertyMutable);
+ Boolean isPropertyMutable(String propertyName);
+
+ void setPropertyValue(String propertyName, String propertyValue);
+
+ Set<String> getPropertyNames();
+
MockObject addChild(String name, Type type);
/**
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java 2007-11-27
20:17:12 UTC (rev 9136)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java 2007-11-27
20:52:01 UTC (rev 9137)
@@ -22,6 +22,7 @@
******************************************************************************/
package org.jboss.portal.presentation.test.model;
+import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.log4j.Logger;
import org.jboss.portal.presentation.impl.model.UIContextImpl;
@@ -30,6 +31,7 @@
import org.jboss.portal.presentation.model.UIObject;
import org.jboss.portal.presentation.model.UIPage;
import org.jboss.portal.presentation.model.UIPortal;
+import org.jboss.portal.presentation.model.state.StateChangeVetoException;
import java.util.Collections;
import java.util.List;
@@ -186,54 +188,202 @@
assertEquals(mockDefaultPortal.getId(), defaultPortal.getId());
}
- public void testProperty()
+ private static class PropertyAssert
{
+ /** . */
+ private final UIObject object;
+
+ private PropertyAssert(UIObject object)
+ {
+ this.object = object;
+ }
+
+ private static final class Blah
+ {
+ }
+
+ void assertStructuralEquals(String propertyName, String propertyValue)
+ {
+ assertEquals(StateScopeType.STRUCTURAL, propertyName, propertyValue,
String.class);
+ }
+
+ <T> void assertNavigationalEquals(String propertyName, T propertyValue,
Class<T> propertyClass)
+ {
+ assertEquals(StateScopeType.NAVIGATIONAL, propertyName, propertyValue,
propertyClass);
+ }
+
+ <T> void assertContentEquals(String propertyName, T propertyValue,
Class<T> propertyClass)
+ {
+ assertEquals(StateScopeType.CONTENT, propertyName, propertyValue,
propertyClass);
+ }
+
+ <T> void assertEquals(StateScopeType scopeType, String propertyName, T
propertyValue, Class<T> propertyClass)
+ {
+ Assert.assertEquals(propertyValue, object.getProperty(scopeType, propertyName,
propertyClass));
+ Assert.assertEquals(propertyValue, object.getProperty(scopeType, propertyName,
Object.class));
+ Assert.assertEquals(propertyValue, object.getProperty(scopeType,
propertyName));
+ Assert.assertEquals(null, object.getProperty(scopeType, propertyName,
Blah.class)); }
+ }
+
+ public void testExistingMutableStructuralProperty()
+ {
MockObject mockFoo = model.getContext().addChild("foo",
MockObject.Type.PORTAL);
- mockFoo.setProperty("foo_name", "foo_value");
+ mockFoo.setPropertyValue("foo", "foo_value");
+ mockFoo.setPropertyMutable("foo", true);
UIContext context = new UIContextImpl(model);
- //
+ // Check initial state
UIPortal foo = (UIPortal)context.getObject(mockFoo.getId());
- assertEquals("foo_value", foo.getProperty(StateScopeType.STRUCTURAL,
"foo_name", String.class));
- assertEquals("foo_value", foo.getProperty(StateScopeType.STRUCTURAL,
"foo_name", Object.class));
- assertEquals("foo_value", foo.getProperty(StateScopeType.STRUCTURAL,
"foo_name"));
- assertEquals(null, foo.getProperty(StateScopeType.STRUCTURAL, "foo_name",
Float.class));
- assertEquals(null, foo.getProperty(StateScopeType.NAVIGATIONAL,
"foo_name", Object.class));
- assertEquals(null, foo.getProperty(StateScopeType.NAVIGATIONAL,
"foo_name"));
- assertEquals(null, foo.getProperty(StateScopeType.NAVIGATIONAL,
"foo_name", Float.class));
- assertEquals(null, foo.getProperty(StateScopeType.CONTENT, "foo_name",
Object.class));
- assertEquals(null, foo.getProperty(StateScopeType.CONTENT, "foo_name"));
- assertEquals(null, foo.getProperty(StateScopeType.CONTENT, "foo_name",
Float.class));
+ PropertyAssert fooAssert = new PropertyAssert(foo);
+ fooAssert.assertStructuralEquals("foo", "foo_value");
- //
- foo.setProperty(StateScopeType.NAVIGATIONAL, "foo_name", 0);
- assertEquals("foo_value", foo.getProperty(StateScopeType.STRUCTURAL,
"foo_name", String.class));
- assertEquals("foo_value", foo.getProperty(StateScopeType.STRUCTURAL,
"foo_name", Object.class));
- assertEquals("foo_value", foo.getProperty(StateScopeType.STRUCTURAL,
"foo_name"));
- assertEquals(null, foo.getProperty(StateScopeType.STRUCTURAL, "foo_name",
Float.class));
- assertEquals(0, foo.getProperty(StateScopeType.NAVIGATIONAL, "foo_name",
Object.class));
- assertEquals(new Integer(0), foo.getProperty(StateScopeType.NAVIGATIONAL,
"foo_name", Integer.class));
- assertEquals(0, foo.getProperty(StateScopeType.NAVIGATIONAL,
"foo_name"));
- assertEquals(null, foo.getProperty(StateScopeType.NAVIGATIONAL,
"foo_name", Float.class));
- assertEquals(null, foo.getProperty(StateScopeType.CONTENT, "foo_name",
Object.class));
- assertEquals(null, foo.getProperty(StateScopeType.CONTENT, "foo_name"));
- assertEquals(null, foo.getProperty(StateScopeType.CONTENT, "foo_name",
Float.class));
+ // Update structural property
+ foo.setProperty(StateScopeType.STRUCTURAL, "foo",
"foo_new_value");
+ fooAssert.assertStructuralEquals("foo", "foo_new_value");
- //
- foo.setProperty(StateScopeType.CONTENT, "foo_name", true);
- assertEquals("foo_value", foo.getProperty(StateScopeType.STRUCTURAL,
"foo_name", String.class));
- assertEquals("foo_value", foo.getProperty(StateScopeType.STRUCTURAL,
"foo_name", Object.class));
- assertEquals("foo_value", foo.getProperty(StateScopeType.STRUCTURAL,
"foo_name"));
- assertEquals(null, foo.getProperty(StateScopeType.STRUCTURAL, "foo_name",
Float.class));
- assertEquals(0, foo.getProperty(StateScopeType.NAVIGATIONAL, "foo_name",
Object.class));
- assertEquals(new Integer(0), foo.getProperty(StateScopeType.NAVIGATIONAL,
"foo_name", Integer.class));
- assertEquals(0, foo.getProperty(StateScopeType.NAVIGATIONAL,
"foo_name"));
- assertEquals(null, foo.getProperty(StateScopeType.NAVIGATIONAL,
"foo_name", Float.class));
- assertEquals(true, foo.getProperty(StateScopeType.CONTENT, "foo_name",
Object.class));
- assertEquals(true, foo.getProperty(StateScopeType.CONTENT, "foo_name"));
- assertEquals(Boolean.TRUE, foo.getProperty(StateScopeType.CONTENT,
"foo_name", Boolean.class));
- assertEquals(null, foo.getProperty(StateScopeType.CONTENT, "foo_name",
Float.class));
+ // Try a non string type
+ try
+ {
+ foo.setProperty(StateScopeType.STRUCTURAL, "foo", 2);
+ fail("Property update should have been vetoed");
+ }
+ catch (StateChangeVetoException ignore)
+ {
+ }
+ fooAssert.assertStructuralEquals("foo", "foo_new_value");
+ }
- //
+ public void testExistingNonMutableStructuralProperty()
+ {
+ MockObject mockFoo = model.getContext().addChild("foo",
MockObject.Type.PORTAL);
+ mockFoo.setPropertyValue("foo", "foo_value");
+ mockFoo.setPropertyMutable("foo", false);
+ UIContext context = new UIContextImpl(model);
+
+ // Check initial state
+ UIPortal foo = (UIPortal)context.getObject(mockFoo.getId());
+ PropertyAssert fooAssert = new PropertyAssert(foo);
+ fooAssert.assertStructuralEquals("foo", "foo_value");
+
+ // Try an update
+ try
+ {
+ foo.setProperty(StateScopeType.STRUCTURAL, "foo",
"foo_new_value");
+ fail("Property update should have been vetoed");
+ }
+ catch (StateChangeVetoException ignore)
+ {
+ }
+ fooAssert.assertStructuralEquals("foo", "foo_value");
+
+ // Try a non string type
+ try
+ {
+ foo.setProperty(StateScopeType.STRUCTURAL, "foo", 2);
+ fail("Property update should have been vetoed");
+ }
+ catch (StateChangeVetoException ignore)
+ {
+ }
+ fooAssert.assertStructuralEquals("foo", "foo_value");
}
+
+ public void testNonExistingMutableStructuralProperty()
+ {
+ MockObject mockFoo = model.getContext().addChild("foo",
MockObject.Type.PORTAL);
+ mockFoo.setPropertyMutable("foo", true);
+ UIContext context = new UIContextImpl(model);
+
+ // Check initial state
+ UIPortal foo = (UIPortal)context.getObject(mockFoo.getId());
+ PropertyAssert fooAssert = new PropertyAssert(foo);
+ fooAssert.assertStructuralEquals("foo", null);
+
+ // Update structural property
+ foo.setProperty(StateScopeType.STRUCTURAL, "foo",
"foo_new_value");
+ fooAssert.assertStructuralEquals("foo", "foo_new_value");
+
+ // Try a non string type
+ try
+ {
+ foo.setProperty(StateScopeType.STRUCTURAL, "foo", 2);
+ fail("Property update should have been vetoed");
+ }
+ catch (StateChangeVetoException ignore)
+ {
+ }
+ fooAssert.assertStructuralEquals("foo", "foo_new_value");
+ }
+
+ public void testNonExistingNonMutableStructuralProperty()
+ {
+ MockObject mockFoo = model.getContext().addChild("foo",
MockObject.Type.PORTAL);
+ mockFoo.setPropertyMutable("foo", false);
+ UIContext context = new UIContextImpl(model);
+
+ // Check initial state
+ UIPortal foo = (UIPortal)context.getObject(mockFoo.getId());
+ PropertyAssert fooAssert = new PropertyAssert(foo);
+ fooAssert.assertStructuralEquals("foo", null);
+
+ // Try an update
+ try
+ {
+ foo.setProperty(StateScopeType.STRUCTURAL, "foo",
"foo_new_value");
+ fail("Property update should have been vetoed");
+ }
+ catch (StateChangeVetoException ignore)
+ {
+ }
+ fooAssert.assertStructuralEquals("foo", null);
+
+ // Try a non string type
+ try
+ {
+ foo.setProperty(StateScopeType.STRUCTURAL, "foo", 2);
+ fail("Property update should have been vetoed");
+ }
+ catch (StateChangeVetoException ignore)
+ {
+ }
+ fooAssert.assertStructuralEquals("foo", null);
+ }
+
+ public void testContentProperty()
+ {
+ MockObject mockFoo = model.getContext().addChild("foo",
MockObject.Type.PORTAL);
+ UIContext context = new UIContextImpl(model);
+
+ // Check initial state
+ UIPortal foo = (UIPortal)context.getObject(mockFoo.getId());
+ PropertyAssert fooAssert = new PropertyAssert(foo);
+ fooAssert.assertContentEquals("foo", null, String.class);
+
+ // Update content state
+ foo.setProperty(StateScopeType.CONTENT, "foo",
"foo_new_value");
+ fooAssert.assertContentEquals("foo", "foo_new_value",
String.class);
+
+ // Update content state
+ foo.setProperty(StateScopeType.CONTENT, "foo", 2);
+ fooAssert.assertContentEquals("foo", 2, Integer.class);
+ }
+
+ public void testNavigationalProperty()
+ {
+ MockObject mockFoo = model.getContext().addChild("foo",
MockObject.Type.PORTAL);
+ UIContext context = new UIContextImpl(model);
+
+ // Check initial state
+ UIPortal foo = (UIPortal)context.getObject(mockFoo.getId());
+ PropertyAssert fooAssert = new PropertyAssert(foo);
+ fooAssert.assertNavigationalEquals("foo", null, String.class);
+
+ // Update navigational state
+ foo.setProperty(StateScopeType.NAVIGATIONAL, "foo",
"foo_new_value");
+ fooAssert.assertNavigationalEquals("foo", "foo_new_value",
String.class);
+
+ // Update navigational state
+ foo.setProperty(StateScopeType.NAVIGATIONAL, "foo", 2);
+ fooAssert.assertNavigationalEquals("foo", 2, Integer.class);
+ }
}