Author: julien(a)jboss.com
Date: 2007-12-03 16:56:04 -0500 (Mon, 03 Dec 2007)
New Revision: 9255
Added:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/
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
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/StructuralObjectImpl.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/StructuralStateImpl.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/UIModelTester.java
Log:
clean up mock model implementation
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
21:36:09 UTC (rev 9254)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java 2007-12-03
21:56:04 UTC (rev 9255)
@@ -35,6 +35,8 @@
import org.jboss.portal.presentation.model.state.NoSuchStateException;
import org.jboss.portal.presentation.model.state.StateChangeVetoException;
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;
import java.util.Collections;
import java.util.List;
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/UIModelTester.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/UIModelTester.java 2007-12-03
21:36:09 UTC (rev 9254)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/UIModelTester.java 2007-12-03
21:56:04 UTC (rev 9255)
@@ -27,6 +27,7 @@
import org.jboss.portal.presentation.impl.model.UIContextImpl;
import
org.jboss.portal.presentation.impl.model.state.navigational.NavigationalStateContextImpl;
import org.jboss.portal.presentation.model.UIContext;
+import org.jboss.portal.presentation.test.model.state.structural.MockModel;
/**
* @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
Copied:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModel.java
(from rev 9252,
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/MockModel.java)
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModel.java
(rev 0)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModel.java 2007-12-03
21:56:04 UTC (rev 9255)
@@ -0,0 +1,166 @@
+/******************************************************************************
+ * 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.test.model.state.structural;
+
+import org.jboss.portal.presentation.model.state.NoSuchStateException;
+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;
+import org.jboss.portal.presentation.model.state.structural.StructuralObject;
+import org.jboss.portal.presentation.model.state.structural.StructuralStateContext;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class MockModel
+{
+
+ /** . */
+ private int generator = 0;
+
+ /** . */
+ private final Map<String, MockObjectImpl> universe = new HashMap<String,
MockObjectImpl>();
+
+ /** . */
+ private final MockObjectImpl root = createObject(MockObject.Type.CONTEXT,
"");
+
+ public StructuralStateContext getStructuralStateContext()
+ {
+ return structuralStateContext;
+ }
+
+ public MockObject getRoot()
+ {
+ return root;
+ }
+
+ public void destroy(String objectId)
+ {
+ MockObjectImpl object = universe.remove(objectId);
+
+ // Remove children recursively
+ for (String childId : new ArrayList<String>(object.children.keySet()))
+ {
+ destroy(childId);
+ }
+
+ // Detach from parent
+ if (object.parent != null)
+ {
+ object.parent.children.remove(object.id);
+ object.parent = null;
+ }
+
+ // We cannot use it anymore
+ object.valid = false;
+ }
+
+ MockObjectImpl createObject(MockObject.Type type, String name)
+ {
+ MockObjectImpl mockObject = new MockObjectImpl(this, name, type, "" +
generator++);
+ universe.put(mockObject.getId(), mockObject);
+ return mockObject;
+ }
+
+ private final StructuralStateContext structuralStateContext = new
AbstractStructuralStateContext()
+ {
+ public StructuralObject load(String objectId) throws IllegalArgumentException
+ {
+ if (objectId == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ MockObjectImpl object = universe.get(objectId);
+ return object != null ? object.takeSnapshot() : null;
+ }
+
+ public String getRootId()
+ {
+ return root.getId();
+ }
+
+ public void update(StructuralObject structuralObject, Map<String, String>
changes) throws StateChangeVetoException
+ {
+ MockObjectImpl object = universe.get(structuralObject.getId());
+ if (object == null)
+ {
+ throw new NoSuchStateException();
+ }
+
+ //
+ for (Map.Entry<String, String> entry : changes.entrySet())
+ {
+ String propertyName = entry.getKey();
+ MockObject.UpdateBehavior behavior =
object.propertyBehaviors.get(propertyName);
+ if (behavior instanceof MockObject.Veto)
+ {
+ throw new StateChangeVetoException("Cannot modify non behavior
property");
+ }
+ else if (behavior instanceof MockObject.Failure)
+ {
+ MockObject.Failure failure = (MockObject.Failure)behavior;
+ failure.throwAs(IllegalArgumentException.class).
+ throwAs(StateChangeVetoException.class).
+ throwAs(StateException.class);
+ }
+ else
+ {
+ String propertyValue = entry.getValue();
+ if (propertyValue != null)
+ {
+ object.propertyValues.put(propertyName, propertyValue);
+ }
+ else
+ {
+ object.propertyValues.remove(propertyName);
+ }
+ }
+ }
+ }
+
+ public List<StructuralObject> loadChildren(StructuralObject object)
+ {
+ StructuralObjectImpl mockStructuralObject = (StructuralObjectImpl)object;
+ MockObjectImpl mockObject = universe.get(mockStructuralObject.getId());
+ List<StructuralObject> tmp = new ArrayList<StructuralObject>();
+ for (MockObjectImpl mockChild : mockObject.children.values())
+ {
+ tmp.add(mockChild.takeSnapshot());
+ }
+ return tmp;
+ }
+
+ public StructuralObject loadParent(StructuralObject object)
+ {
+ StructuralObjectImpl mockStructuralObject = (StructuralObjectImpl)object;
+ MockObjectImpl mockObject = universe.get(mockStructuralObject.getId());
+ return mockObject.parent.takeSnapshot();
+ }
+ };
+}
Copied:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObject.java
(from rev 9252,
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/MockObject.java)
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObject.java
(rev 0)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObject.java 2007-12-03
21:56:04 UTC (rev 9255)
@@ -0,0 +1,114 @@
+/******************************************************************************
+ * 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.test.model.state.structural;
+
+import org.jboss.portal.presentation.model.UIContext;
+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.UIWindow;
+import org.jboss.portal.presentation.model.state.structural.StructuralObject;
+
+import java.util.Set;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public interface MockObject
+{
+
+ /**
+ *
+ */
+ public enum Type
+ {
+
+ PORTAL(UIPortal.class), PAGE(UIPage.class), WINDOW(UIWindow.class),
CONTEXT(UIContext.class);
+
+ /** . */
+ final Class<? extends UIObject> clazz;
+
+ Type(Class<? extends UIObject> clazz)
+ {
+ this.clazz = clazz;
+ }
+ }
+
+ public abstract static class UpdateBehavior
+ {
+ public static UpdateBehavior veto()
+ {
+ return new Veto();
+ }
+ public static UpdateBehavior failure(Throwable throwable)
+ {
+ return new Failure(throwable);
+ }
+ }
+
+ static class Veto extends UpdateBehavior
+ {
+ }
+
+ static class Failure extends UpdateBehavior
+ {
+
+ /** . */
+ final Throwable throwable;
+
+ public Failure(Throwable throwable)
+ {
+ this.throwable = throwable;
+ }
+
+ public <T extends Throwable> Failure throwAs(Class<T> type) throws T
+ {
+ if (type.isInstance(throwable))
+ {
+ throw type.cast(throwable);
+ }
+ return this;
+ }
+ }
+
+ Type getType();
+
+ String getName();
+
+ String getId();
+
+ String getPropertyValue(String propertyName);
+
+ void setPropertyBehavior(String propertyName, UpdateBehavior propertyBehavior);
+
+ UpdateBehavior getPropertyBehavior(String propertyName);
+
+ void setPropertyValue(String propertyName, String propertyValue);
+
+ Set<String> getPropertyNames();
+
+ MockObject addChild(String name, Type type);
+
+ boolean isValid();
+}
Added:
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
(rev 0)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObjectImpl.java 2007-12-03
21:56:04 UTC (rev 9255)
@@ -0,0 +1,173 @@
+/******************************************************************************
+ * 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.test.model.state.structural;
+
+import org.jboss.portal.presentation.model.state.structural.StructuralObject;
+
+import java.util.Map;
+import java.util.LinkedHashMap;
+import java.util.HashMap;
+import java.util.Set;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+class MockObjectImpl implements MockObject
+{
+
+ /** . */
+ final MockObject.Type type;
+
+ /** . */
+ final String name;
+
+ /** . */
+ final String id;
+
+ /** . */
+ final Map<String, String> propertyValues;
+
+ /** . */
+ final Map<String, UpdateBehavior> propertyBehaviors;
+
+ /** . */
+ final Map<String, MockObjectImpl> children;
+
+ /** . */
+ final MockModel model;
+
+ /** . */
+ MockObjectImpl parent;
+
+ /** . */
+ boolean valid;
+
+ MockObjectImpl(MockModel model, String name, MockObject.Type type, String id)
+ {
+ this.model = model;
+ this.name = name;
+ this.type = type;
+ this.children = new LinkedHashMap<String, MockObjectImpl>();
+ this.propertyValues = new HashMap<String, String>();
+ this.propertyBehaviors = new HashMap<String, UpdateBehavior>();
+ this.id = id;
+ this.valid = true;
+ }
+
+ public MockObject.Type getType()
+ {
+ return type;
+ }
+
+ public boolean isValid()
+ {
+ return valid;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public String getId()
+ {
+ return id;
+ }
+
+ public String getPropertyValue(String propertyName)
+ {
+ return propertyValues.get(propertyName);
+ }
+
+ public MockObject addChild(String name, MockObject.Type type)
+ {
+ if (name == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ if (children.containsKey(name))
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ MockObjectImpl child = model.createObject(type, name);
+
+ //
+ child.parent = this;
+ children.put(child.id, child);
+
+ //
+ return child;
+ }
+
+ public Set<String> getPropertyNames()
+ {
+ return propertyValues.keySet();
+ }
+
+ public void setPropertyBehavior(String propertyName, UpdateBehavior propertyBehavior)
+ {
+ if (propertyName == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (propertyBehavior != null)
+ {
+ propertyBehaviors.put(name, propertyBehavior);
+ }
+ else
+ {
+ propertyBehaviors.remove(name);
+ }
+ }
+
+ public UpdateBehavior getPropertyBehavior(String propertyName)
+ {
+ return propertyBehaviors.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);
+ }
+ }
+
+ StructuralObject takeSnapshot()
+ {
+ return new StructuralObjectImpl(id, new StructuralStateImpl(type.clazz, name, new
HashMap<String, String>(propertyValues)));
+ }
+}
Added:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/StructuralObjectImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/StructuralObjectImpl.java
(rev 0)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/StructuralObjectImpl.java 2007-12-03
21:56:04 UTC (rev 9255)
@@ -0,0 +1,56 @@
+/******************************************************************************
+ * 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.test.model.state.structural;
+
+import org.jboss.portal.presentation.model.state.structural.StructuralObject;
+import org.jboss.portal.presentation.model.state.structural.StructuralState;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+class StructuralObjectImpl implements StructuralObject
+{
+
+ /** . */
+ private final String id;
+
+ /** . */
+ private final StructuralStateImpl state;
+
+ public StructuralObjectImpl(String id, StructuralStateImpl state)
+ {
+ this.id = id;
+ this.state = state;
+ }
+
+ public String getId()
+ {
+ return id;
+ }
+
+ public StructuralState getState()
+ {
+ return state;
+ }
+}
Added:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/StructuralStateImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/StructuralStateImpl.java
(rev 0)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/StructuralStateImpl.java 2007-12-03
21:56:04 UTC (rev 9255)
@@ -0,0 +1,67 @@
+/******************************************************************************
+ * 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.test.model.state.structural;
+
+import org.jboss.portal.presentation.model.state.structural.StructuralState;
+import org.jboss.portal.presentation.model.UIObject;
+
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+class StructuralStateImpl implements StructuralState
+{
+
+ /** . */
+ private final Class<? extends UIObject> type;
+
+ /** . */
+ private final String name;
+
+ /** . */
+ private final Map<String, String> properties;
+
+ public StructuralStateImpl(Class<? extends UIObject> type, String name,
Map<String, String> properties)
+ {
+ this.type = type;
+ this.name = name;
+ this.properties = properties;
+ }
+
+ public Class<? extends UIObject> getType()
+ {
+ return type;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public Map<String, String> getProperties()
+ {
+ return properties;
+ }
+}