Author: julien(a)jboss.com
Date: 2007-12-03 17:37:26 -0500 (Mon, 03 Dec 2007)
New Revision: 9257
Added:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/StaleStateException.java
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/state/structural/MockModel.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObject.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObjectImpl.java
Log:
- implement a basic state versionning in the mock structural state context impl
- test case the model for stale state detection on property update for now
Added:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/StaleStateException.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/StaleStateException.java
(rev 0)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/StaleStateException.java 2007-12-03
22:37:26 UTC (rev 9257)
@@ -0,0 +1,49 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, 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.presentation.model.state;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class StaleStateException extends StateException
+{
+ public StaleStateException()
+ {
+ }
+
+ public StaleStateException(String s)
+ {
+ super(s);
+ }
+
+ public StaleStateException(String s, Throwable throwable)
+ {
+ super(s, throwable);
+ }
+
+ public StaleStateException(Throwable throwable)
+ {
+ super(throwable);
+ }
+}
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-12-03
22:09:21 UTC (rev 9256)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java 2007-12-03
22:37:26 UTC (rev 9257)
@@ -33,7 +33,9 @@
import org.jboss.portal.presentation.model.UIPage;
import org.jboss.portal.presentation.model.UIPortal;
import org.jboss.portal.presentation.model.state.NoSuchStateException;
+import org.jboss.portal.presentation.model.state.StaleStateException;
import org.jboss.portal.presentation.model.state.StateChangeVetoException;
+import org.jboss.portal.presentation.model.state.StateException;
import org.jboss.portal.presentation.model.state.structural.StructuralStateModification;
import org.jboss.portal.presentation.test.model.state.structural.MockModel;
import org.jboss.portal.presentation.test.model.state.structural.MockObject;
@@ -464,4 +466,34 @@
foo.setProperty(StateScopeType.NAVIGATIONAL, "foo", 2);
fooAssert.assertNavigationalEquals("foo", 2, Integer.class);
}
+
+ public void testStructuralPropertyUpdateKeepModelStateConsistent()
+ {
+ UIContext context = createContext();
+
+ //
+ context.setProperty(StateScopeType.STRUCTURAL, "foo", "bar");
+ context.setProperty(StateScopeType.STRUCTURAL, "foo", "bar2");
+ }
+
+ public void testConcurrentStructuralPropertyDetection()
+ {
+ UIContext context = createContext();
+
+ //
+ context.setProperty(StateScopeType.STRUCTURAL, "foo", "bar");
+
+ // The concurrent update
+ model.getRoot().setPropertyValue("foo", "bar2");
+
+ //
+ try
+ {
+ context.setProperty(StateScopeType.STRUCTURAL, "foo",
"bar3");
+ fail("Was expecting an stale state exception");
+ }
+ catch (StaleStateException ignore)
+ {
+ }
+ }
}
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModel.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModel.java 2007-12-03
22:09:21 UTC (rev 9256)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModel.java 2007-12-03
22:37:26 UTC (rev 9257)
@@ -23,6 +23,7 @@
package org.jboss.portal.presentation.test.model.state.structural;
import org.jboss.portal.presentation.model.state.NoSuchStateException;
+import org.jboss.portal.presentation.model.state.StaleStateException;
import org.jboss.portal.presentation.model.state.StateChangeVetoException;
import org.jboss.portal.presentation.model.state.StateException;
import
org.jboss.portal.presentation.model.state.structural.AbstractStructuralStateContext;
@@ -92,19 +93,15 @@
return root.getId();
}
- public void update(StructuralObject structuralObject, Map<String, String>
changes) throws StateChangeVetoException
+ public void update(StructuralObject object, Map<String, String> changes)
throws StateChangeVetoException
{
- MockObjectImpl object = universe.get(structuralObject.getId());
- if (object == null)
- {
- throw new NoSuchStateException();
- }
+ MockObject mockObject = getValidMockObject(object);
//
for (Map.Entry<String, String> entry : changes.entrySet())
{
String propertyName = entry.getKey();
- MockObject.UpdateBehavior behavior =
object.getPropertyBehavior(propertyName);
+ MockObject.UpdateBehavior behavior =
mockObject.getPropertyBehavior(propertyName);
if (behavior instanceof MockObject.Veto)
{
throw new StateChangeVetoException("Cannot modify non behavior
property");
@@ -118,15 +115,16 @@
}
else
{
- object.setPropertyValue(propertyName, entry.getValue());
+ mockObject.setPropertyValue(propertyName, entry.getValue());
}
}
}
public List<StructuralObject> loadChildren(StructuralObject object)
{
- StructuralObjectImpl mockStructuralObject = (StructuralObjectImpl)object;
- MockObjectImpl mockObject = universe.get(mockStructuralObject.getId());
+ MockObject mockObject = getValidMockObject(object);
+
+ //
List<StructuralObject> tmp = new ArrayList<StructuralObject>();
for (MockObject mockChild : mockObject.getChildren())
{
@@ -137,9 +135,43 @@
public StructuralObject loadParent(StructuralObject object)
{
- StructuralObjectImpl mockStructuralObject = (StructuralObjectImpl)object;
- MockObjectImpl mockObject = universe.get(mockStructuralObject.getId());
- return mockObject.getParent().takeSnapshot();
+ return getValidMockObject(object).getParent().takeSnapshot();
}
+
+ private MockObject getValidMockObject(StructuralObject object)
+ {
+ if (object == null)
+ {
+ throw new IllegalArgumentException("No null object accepted");
+ }
+
+ //
+ StructuralObjectImpl structuralObject = (StructuralObjectImpl)object;
+
+ //
+ MockObjectImpl mockObject = universe.get(structuralObject.getId());
+
+ //
+ if (mockObject == null)
+ {
+ throw new NoSuchStateException();
+ }
+
+ //
+ if (!mockObject.isValid())
+ {
+ throw new StaleStateException();
+ }
+
+ //
+ if (mockObject.getVersion() != structuralObject.version)
+ {
+ throw new StaleStateException("Version has changed obtained=" +
mockObject.getVersion() + " wanted=" + structuralObject.version);
+ }
+
+ //
+ return mockObject;
+ }
+
};
}
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObject.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObject.java 2007-12-03
22:09:21 UTC (rev 9256)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObject.java 2007-12-03
22:37:26 UTC (rev 9257)
@@ -99,6 +99,8 @@
String getId();
+ int getVersion();
+
String getPropertyValue(String propertyName);
void setPropertyBehavior(String propertyName, UpdateBehavior propertyBehavior);
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObjectImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObjectImpl.java 2007-12-03
22:09:21 UTC (rev 9256)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObjectImpl.java 2007-12-03
22:37:26 UTC (rev 9257)
@@ -136,6 +136,7 @@
//
child.parent = this;
children.put(child.id, child);
+ version++;
//
return child;
@@ -186,6 +187,11 @@
this.version++;
}
+ public int getVersion()
+ {
+ return version;
+ }
+
public StructuralObject takeSnapshot()
{
return new StructuralObjectImpl(id, version, new StructuralStateImpl(type.clazz,
name, new HashMap<String, String>(propertyValues)));
@@ -203,6 +209,7 @@
if (parent != null)
{
parent.children.remove(id);
+ parent.version++;
parent = null;
}