Author: julien(a)jboss.com
Date: 2008-04-03 11:04:02 -0400 (Thu, 03 Apr 2008)
New Revision: 10491
Added:
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/ContextNode.java
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/PageNode.java
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/StructuralNode.java
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/StructuralObjectImpl.java
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/StructuralStateContextImpl.java
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/StructuralStateImpl.java
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/WindowNode.java
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/state/
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/state/structural/
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/state/structural/StructuralStateContextTestCase.java
Modified:
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/model/AbstractModelTestCase.java
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/model/MockModelTestCase.java
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/model/state/structural/MockObject.java
Log:
implementation of structural state context that is not based on versioning (that uses
state comparison during refresh)
Added:
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/ContextNode.java
===================================================================
---
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/ContextNode.java
(rev 0)
+++
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/ContextNode.java 2008-04-03
15:04:02 UTC (rev 10491)
@@ -0,0 +1,43 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, 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.impl.state.structural;
+
+import org.jboss.portal.presentation.model2.ui.UIObject;
+import org.jboss.portal.presentation.model2.ui.UIContext;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public class ContextNode extends StructuralNode
+{
+ public ContextNode(String name, StructuralStateContextImpl structuralStateContext)
+ {
+ super(name, structuralStateContext);
+ }
+
+ public Class<? extends UIObject> getType()
+ {
+ return UIContext.class;
+ }
+}
Added:
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/PageNode.java
===================================================================
---
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/PageNode.java
(rev 0)
+++
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/PageNode.java 2008-04-03
15:04:02 UTC (rev 10491)
@@ -0,0 +1,43 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, 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.impl.state.structural;
+
+import org.jboss.portal.presentation.model2.ui.UIObject;
+import org.jboss.portal.presentation.model2.ui.UIPage;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public class PageNode extends StructuralNode
+{
+ public PageNode(String name, StructuralStateContextImpl structuralStateContext)
+ {
+ super(name, structuralStateContext);
+ }
+
+ public Class<? extends UIObject> getType()
+ {
+ return UIPage.class;
+ }
+}
Added:
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/StructuralNode.java
===================================================================
---
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/StructuralNode.java
(rev 0)
+++
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/StructuralNode.java 2008-04-03
15:04:02 UTC (rev 10491)
@@ -0,0 +1,352 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, 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.impl.state.structural;
+
+import org.jboss.portal.presentation.state.structural.StructuralObject;
+import org.jboss.portal.presentation.state.StaleStateException;
+import org.jboss.portal.presentation.model2.ui.UIObject;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Collections;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Collection;
+import java.util.ArrayList;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public abstract class StructuralNode
+{
+
+ /** . */
+ private final Set<String> EMPTY_STRING_SET = Collections.emptySet();
+
+ /** . */
+ private StructuralNode parent;
+
+ /** . */
+ private final Map<String, StructuralNode> children = new HashMap<String,
StructuralNode>();
+
+ /** . */
+ private final StructuralStateContextImpl structuralStateContext;
+
+ /** . */
+ private final String id;
+
+ /** . */
+ private final String name;
+
+ /** . */
+ private boolean valid;
+
+ /** . */
+ private final Map<String, String> properties;
+
+ protected StructuralNode(String name, StructuralStateContextImpl
structuralStateContext)
+ {
+ this.name = name;
+ this.id = structuralStateContext.nextId();
+ this.structuralStateContext = structuralStateContext;
+ this.properties = new HashMap<String, String>();
+ this.valid = true;
+ }
+
+ public abstract Class<? extends UIObject> getType();
+
+ Collection<StructuralObject> takeChildrenSnapshot(StructuralObject
structuralObject)
+ {
+ synchronized (this)
+ {
+ if (!takeSnapshot().equals(structuralObject))
+ {
+ throw new StaleStateException();
+ }
+
+ //
+ Set<StructuralObject> structuralChildren = new
HashSet<StructuralObject>();
+ for (StructuralNode child : children.values())
+ {
+ structuralChildren.add(child.takeSnapshot());
+ }
+
+ //
+ return structuralChildren;
+ }
+ }
+
+ StructuralObject takeParentSnapshot(StructuralObject structuralObject)
+ {
+ synchronized (this)
+ {
+ if (!takeSnapshot().equals(structuralObject))
+ {
+ throw new StaleStateException();
+ }
+
+ //
+ return parent != null ? parent.takeSnapshot() : null;
+ }
+ }
+
+ StructuralObject takeSnapshot()
+ {
+ synchronized (this)
+ {
+ StructuralStateImpl state = new StructuralStateImpl(
+ getType(),
+ name,
+ Collections.unmodifiableMap(new HashMap<String, String>(properties)));
+
+ //
+ Set<String> childrenIds = new HashSet<String>();
+ for (StructuralNode child : children.values())
+ {
+ childrenIds.add(child.id);
+ }
+
+ //
+ String parentId = parent != null ? parent.id : null;
+
+ //
+ return new StructuralObjectImpl(id, parentId,
Collections.unmodifiableSet(childrenIds), state);
+ }
+ }
+
+ StructuralObject.Refresh refresh(StructuralObjectImpl structuralObject)
+ {
+ synchronized (this)
+ {
+ Map<String, StructuralNode> childrenMap = new HashMap<String,
StructuralNode>();
+ for (StructuralNode child : children.values())
+ {
+ childrenMap.put(child.getId(), child);
+ }
+
+ //
+ Map<String, StructuralNode> addedChildrenMap = new HashMap<String,
StructuralNode>(childrenMap);
+ addedChildrenMap.keySet().removeAll(structuralObject.getChildrenIds());
+
+ //
+ Map<String, StructuralNode> staleChildren = new HashMap<String,
StructuralNode>(childrenMap);
+ staleChildren.keySet().retainAll(structuralObject.getChildrenIds());
+
+ //
+ Set<String> removedChildrenIds = new
HashSet<String>(structuralObject.getChildrenIds());
+ removedChildrenIds.removeAll(staleChildren.keySet());
+
+ //
+ Set<StructuralObject> addedStructuralChildren = new
HashSet<StructuralObject>();
+ for (StructuralNode addedChild : addedChildrenMap.values())
+ {
+ addedStructuralChildren.add(addedChild.takeSnapshot());
+ }
+
+ //
+ Map<String, StructuralObject> staleStructuralChildren = new
HashMap<String, StructuralObject>();
+ for (StructuralNode stateChild : staleChildren.values())
+ {
+ staleStructuralChildren.put(stateChild.getId(), stateChild.takeSnapshot());
+ }
+
+ //
+ return new StructuralObject.Refresh(
+ parent != null ? parent.takeSnapshot() : null,
+ takeSnapshot(),
+ addedStructuralChildren,
+ removedChildrenIds,
+ staleStructuralChildren,
+ EMPTY_STRING_SET);
+ }
+ }
+
+ public boolean isValid()
+ {
+ return valid;
+ }
+
+ public String getId()
+ {
+ return id;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public StructuralNode getParent()
+ {
+ return parent;
+ }
+
+ public Set<String> getPropertyNames()
+ {
+ return Collections.unmodifiableSet(properties.keySet());
+ }
+
+ public String getProperty(String propertyName)
+ {
+ if (propertyName == null)
+ {
+ throw new IllegalArgumentException("No null property name");
+ }
+
+ //
+ synchronized (this)
+ {
+ return properties.get(propertyName);
+ }
+ }
+
+ public void setProperty(String propertyName, String propertyValue)
+ {
+ if (propertyName == null)
+ {
+ throw new IllegalArgumentException("No null property name");
+ }
+
+ //
+ synchronized (this)
+ {
+ if (propertyValue != null)
+ {
+ properties.put(propertyName, propertyValue);
+ }
+ else
+ {
+ properties.remove(propertyName);
+ }
+ }
+ }
+
+ public Collection<? extends StructuralNode> getChildren()
+ {
+ return Collections.unmodifiableCollection(children.values());
+ }
+
+ public <T extends StructuralNode> T addChild(String name, Class<T> type)
+ {
+ if (name == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (type == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ T child;
+ try
+ {
+ Constructor<T> ctor = type.getConstructor(String.class,
StructuralStateContextImpl.class);
+ child = ctor.newInstance(name, structuralStateContext);
+ }
+ catch (InstantiationException e)
+ {
+ throw new AssertionError(e);
+ }
+ catch (IllegalAccessException e)
+ {
+ throw new AssertionError(e);
+ }
+ catch (NoSuchMethodException e)
+ {
+ throw new AssertionError(e);
+ }
+ catch (InvocationTargetException e)
+ {
+ throw new AssertionError(e);
+ }
+
+ //
+ synchronized (this)
+ {
+ if (children.containsKey(name))
+ {
+ throw new IllegalArgumentException("Child already exist");
+ }
+ children.put(name, child);
+ child.parent = this;
+ }
+
+ //
+ structuralStateContext.nodes.put(child.id, child);
+
+ //
+ return child;
+ }
+
+ public void destroyChild(String name)
+ {
+ if (name == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ StructuralNode child;
+ synchronized (this)
+ {
+ if (!children.containsKey(name))
+ {
+ throw new IllegalArgumentException("No such child " + name);
+ }
+
+ child = children.get(name);
+
+ //
+ for (String blah : new ArrayList<String>(child.children.keySet()))
+ {
+ child.destroyChild(blah);
+ }
+
+ //
+ children.remove(name);
+ structuralStateContext.nodes.remove(child.id);
+
+ //
+ child.valid = false;
+ child.parent = null;
+ }
+ }
+
+ public StructuralNode getChild(String name)
+ {
+ if (name == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ synchronized (this)
+ {
+ return children.get(name);
+ }
+ }
+}
Added:
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/StructuralObjectImpl.java
===================================================================
---
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/StructuralObjectImpl.java
(rev 0)
+++
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/StructuralObjectImpl.java 2008-04-03
15:04:02 UTC (rev 10491)
@@ -0,0 +1,121 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, 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.impl.state.structural;
+
+import org.jboss.portal.presentation.state.structural.StructuralObject;
+import org.jboss.portal.presentation.state.structural.StructuralState;
+
+import java.util.Set;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public class StructuralObjectImpl implements StructuralObject
+{
+
+ /** . */
+ private final String id;
+
+ /** . */
+ private final StructuralStateImpl state;
+
+ /** . */
+ private final String parentId;
+
+ /** . */
+ private final Set<String> childrenIds;
+
+ public StructuralObjectImpl(
+ String id,
+ String parentId,
+ Set<String> childrenIds,
+ StructuralStateImpl state)
+ {
+ this.id = id;
+ this.parentId = parentId;
+ this.childrenIds = childrenIds;
+ this.state = state;
+ }
+
+ String getParentId()
+ {
+ return parentId;
+ }
+
+ Set<String> getChildrenIds()
+ {
+ return childrenIds;
+ }
+
+ public String getId()
+ {
+ return id;
+ }
+
+ public StructuralState getState()
+ {
+ return state;
+ }
+
+ public boolean equals(Object obj)
+ {
+ if (obj == this)
+ {
+ return true;
+ }
+ if (obj instanceof StructuralObjectImpl)
+ {
+ StructuralObjectImpl that = (StructuralObjectImpl)obj;
+
+ //
+ if (!that.id.equals(this.id))
+ {
+ return false;
+ }
+
+ //
+ if (!that.state.equals(this.state))
+ {
+ return false;
+ }
+
+ //
+ if (that.parentId == null)
+ {
+ if (this.parentId != null)
+ {
+ return false;
+ }
+ }
+ else if (!that.parentId.equals(this.parentId))
+ {
+ return false;
+ }
+
+ //
+ return that.childrenIds.equals(this.childrenIds);
+ }
+ return false;
+ }
+}
Added:
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/StructuralStateContextImpl.java
===================================================================
---
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/StructuralStateContextImpl.java
(rev 0)
+++
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/StructuralStateContextImpl.java 2008-04-03
15:04:02 UTC (rev 10491)
@@ -0,0 +1,193 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, 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.impl.state.structural;
+
+import org.jboss.portal.presentation.state.structural.StructuralObject;
+import org.jboss.portal.presentation.state.structural.StructuralStateContext;
+import org.jboss.portal.presentation.state.StateException;
+import org.jboss.portal.presentation.state.NoSuchStateException;
+import org.jboss.portal.presentation.state.StaleStateException;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public class StructuralStateContextImpl implements StructuralStateContext
+{
+
+ /** . */
+ private final AtomicLong sequence = new AtomicLong();
+
+ /** . */
+ final ConcurrentMap<String, StructuralNode> nodes = new
ConcurrentHashMap<String, StructuralNode>();
+
+ /** . */
+ private final ContextNode root = new ContextNode("", this);
+
+ public StructuralStateContextImpl()
+ {
+ nodes.put(root.getId(), root);
+ }
+
+ String nextId()
+ {
+ return Long.toString(sequence.getAndIncrement());
+ }
+
+ public ContextNode getRoot()
+ {
+ return root;
+ }
+
+ public void destroy(String objectId)
+ {
+ if (objectId == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ StructuralNode node = nodes.get(objectId);
+
+ //
+ StructuralNode parent = node.getParent();
+
+ //
+ if (parent == null)
+ {
+ throw new IllegalArgumentException("Cannot destroy root");
+ }
+
+ //
+ parent.destroyChild(node.getName());
+ }
+
+ public StructuralObject load(String objectId) throws IllegalArgumentException
+ {
+ if (objectId == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ StructuralNode node = nodes.get(objectId);
+
+ //
+ return node != null ? node.takeSnapshot() : null;
+ }
+
+ public Collection<StructuralObject> loadChildren(StructuralObject
structuralParent) throws IllegalArgumentException, StateException
+ {
+ if (structuralParent == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ StructuralNode parentNode = nodes.get(structuralParent.getId());
+
+ //
+ if (parentNode == null)
+ {
+ throw new NoSuchStateException();
+ }
+
+ //
+ return parentNode.takeChildrenSnapshot(structuralParent);
+ }
+
+ public StructuralObject loadParent(StructuralObject structuralChild) throws
IllegalArgumentException, StateException
+ {
+ if (structuralChild == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ StructuralNode childNode = nodes.get(structuralChild.getId());
+
+ //
+ if (childNode == null)
+ {
+ throw new NoSuchStateException();
+ }
+
+ //
+ return childNode.takeParentSnapshot(structuralChild);
+ }
+
+ public String getRootId()
+ {
+ return root.getId();
+ }
+
+ public void validate(StructuralObject structuralObject) throws
IllegalArgumentException, StateException
+ {
+ if (structuralObject == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ StructuralNode structuralNode = nodes.get(structuralObject.getId());
+
+ //
+ if (structuralNode == null)
+ {
+ throw new NoSuchStateException();
+ }
+
+ //
+ if (!structuralNode.takeSnapshot().equals(structuralObject))
+ {
+ throw new StaleStateException();
+ }
+ }
+
+ public StructuralObject.Refresh refresh(StructuralObject structuralObject) throws
IllegalArgumentException, StateException
+ {
+ if (structuralObject == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ StructuralNode structuralNode = nodes.get(structuralObject.getId());
+
+ //
+ if (structuralNode == null)
+ {
+ throw new NoSuchStateException();
+ }
+
+ //
+ return structuralNode.refresh((StructuralObjectImpl)structuralObject);
+ }
+}
Added:
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/StructuralStateImpl.java
===================================================================
---
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/StructuralStateImpl.java
(rev 0)
+++
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/StructuralStateImpl.java 2008-04-03
15:04:02 UTC (rev 10491)
@@ -0,0 +1,98 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, 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.impl.state.structural;
+
+import org.jboss.portal.presentation.state.structural.StructuralState;
+import org.jboss.portal.presentation.model2.ui.UIObject;
+
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public 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;
+ }
+
+ public boolean equals(Object obj)
+ {
+ if (obj == this)
+ {
+ return true;
+ }
+ if (obj instanceof StructuralStateImpl)
+ {
+ StructuralStateImpl that = (StructuralStateImpl)obj;
+
+ //
+ if (!that.type.equals(this.type))
+ {
+ return false;
+ }
+
+ //
+ if (!that.name.equals(this.name))
+ {
+ return false;
+ }
+
+ //
+ return that.properties.equals(this.properties);
+ }
+ return false;
+ }
+}
Added:
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/WindowNode.java
===================================================================
---
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/WindowNode.java
(rev 0)
+++
modules/presentation/trunk/presentation/src/main/java/org/jboss/portal/presentation/impl/state/structural/WindowNode.java 2008-04-03
15:04:02 UTC (rev 10491)
@@ -0,0 +1,43 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, 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.impl.state.structural;
+
+import org.jboss.portal.presentation.model2.ui.UIObject;
+import org.jboss.portal.presentation.model2.ui.UIWindow;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public class WindowNode extends StructuralNode
+{
+ public WindowNode(String name, StructuralStateContextImpl structuralStateContext)
+ {
+ super(name, structuralStateContext);
+ }
+
+ public Class<? extends UIObject> getType()
+ {
+ return UIWindow.class;
+ }
+}
Modified:
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/model/AbstractModelTestCase.java
===================================================================
---
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/model/AbstractModelTestCase.java 2008-04-03
09:16:19 UTC (rev 10490)
+++
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/model/AbstractModelTestCase.java 2008-04-03
15:04:02 UTC (rev 10491)
@@ -22,20 +22,31 @@
******************************************************************************/
package org.jboss.portal.presentation.test.model;
-import org.jboss.portal.presentation.test.model.state.structural.MockModel;
-import org.jboss.portal.presentation.test.model.state.structural.MockModelImpl;
import junit.framework.TestCase;
+import org.jboss.portal.presentation.model2.ui.UIObject;
+import org.jboss.portal.presentation.model2.ui.UIContext;
+import org.jboss.portal.presentation.model2.ui.UIPage;
+import org.jboss.portal.presentation.state.structural.StructuralObject;
+import org.jboss.portal.presentation.state.structural.StructuralState;
+import org.jboss.portal.presentation.state.structural.StructuralStateContext;
+import org.jboss.portal.presentation.state.StateException;
+import org.jboss.portal.presentation.state.StaleStateException;
+import org.jboss.portal.presentation.state.NoSuchStateException;
+import org.jboss.portal.presentation.test.model.state.structural.MockException;
+import java.util.Set;
+import java.util.List;
+import java.util.Collections;
+import java.util.Collection;
+import java.util.Map;
+
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 630 $
*/
-public class AbstractModelTestCase extends TestCase
+public abstract class AbstractModelTestCase<MockObject> extends TestCase
{
- /** . */
- protected MockModel model;
-
public AbstractModelTestCase()
{
}
@@ -45,18 +56,391 @@
super(testName);
}
- protected void setUp() throws Exception
+ protected abstract StructuralStateContext getStructuralStateContext();
+
+ protected abstract MockObject getRoot();
+
+ protected abstract Set<String> getPropertyNames(MockObject object);
+
+ protected abstract MockObject getParent(MockObject object);
+
+ protected abstract List<? extends MockObject> getChildren(MockObject object);
+
+ protected abstract String getName(MockObject object);
+
+ protected abstract boolean isValid(MockObject object);
+
+ protected abstract Class<? extends UIObject> getType(MockObject object);
+
+ protected abstract String getId(MockObject object);
+
+ protected abstract MockObject addChild(MockObject object, String name, Class<?
extends UIObject> modelType);
+
+ protected abstract void destroy(MockObject object);
+
+ protected abstract void setProperty(MockObject object, String propertyName, String
propertyValue);
+
+ protected abstract String getProperty(MockObject object, String name);
+
+ protected final StructuralObject getStructuralObject(MockObject object)
{
- this.model = new MockModelImpl();
+ return getStructuralStateContext().load(getId(object));
}
- protected void tearDown() throws Exception
+ public void testRoot()
{
- this.model = null;
+ MockObject mockRoot = getRoot();
+ assertNotNull(mockRoot);
+ assertEquals(Collections.EMPTY_SET, getPropertyNames(mockRoot));
+ assertNull(getParent(mockRoot));
+ assertEquals(Collections.EMPTY_LIST, getChildren(mockRoot));
+ assertEquals(UIContext.class, getType(mockRoot));
+ assertEquals("", getName(mockRoot));
+ assertEquals(true, isValid(mockRoot));
+
+ //
+ String rootId = getStructuralStateContext().getRootId();
+ assertNotNull(rootId);
+ StructuralObject root = getStructuralStateContext().load(rootId);
+ assertNotNull(root);
+ assertEquals(rootId, root.getId());
+ StructuralState state = root.getState();
+ assertEquals(UIContext.class, state.getType());
+ assertEquals("", state.getName());
+ assertEquals(Collections.EMPTY_MAP, state.getProperties());
}
- protected final void resetModel()
+ public void testAddChild()
{
- this.model = new MockModelImpl();
+ MockObject mockRoot = getRoot();
+ // int rootV0 = mockRoot.getVersion();
+
+ //
+ String rootId = getId(mockRoot);
+ StructuralObject root = getStructuralStateContext().load(rootId);
+
+ //
+ MockObject mockFoo = addChild(mockRoot, "foo", UIPage.class);
+ assertNotNull(mockFoo);
+ String fooId = getId(mockFoo);
+
+ //
+ assertNotNull(fooId);
+ assertEquals("foo", getName(mockFoo));
+ assertEquals(UIPage.class, getType(mockFoo));
+ assertSame(mockRoot, getParent(mockFoo));
+ assertEquals(Collections.singletonList(mockFoo), getChildren(mockRoot));
+ assertEquals(Collections.EMPTY_SET, getPropertyNames(mockFoo));
+ // assertTrue(mockRoot.getVersion() > rootV0);
+
+ //
+ try
+ {
+ getStructuralStateContext().loadParent(root);
+ fail();
+ }
+ catch (StaleStateException ignore)
+ {
+ }
+ try
+ {
+ getStructuralStateContext().loadChildren(root);
+ fail();
+ }
+ catch (StaleStateException ignore)
+ {
+ }
+
+ //
+ root = getStructuralStateContext().load(rootId);
+ assertNotNull(root);
+ assertEquals(rootId, root.getId());
+
+ //
+ Collection<StructuralObject> children =
getStructuralStateContext().loadChildren(root);
+ assertNotNull(children);
+ assertEquals(1, children.size());
+ StructuralObject foo = children.iterator().next();
+ assertNotNull(foo);
+ assertEquals(fooId, foo.getId());
+ StructuralState fooState = foo.getState();
+ assertNotNull(fooState);
+ assertEquals("foo", fooState.getName());
+ assertEquals(Collections.EMPTY_MAP, fooState.getProperties());
+ assertEquals(UIPage.class, fooState.getType());
+ root = getStructuralStateContext().loadParent(foo);
+ assertNotNull(root);
+ assertEquals(rootId, root.getId());
}
+
+ public void testDestroy() throws MockException
+ {
+ MockObject mockRoot = getRoot();
+ MockObject mockFoo = addChild(mockRoot, "foo", UIPage.class);
+ MockObject mockBar = addChild(mockFoo, "bar", UIPage.class);
+ MockObject mockJuu = addChild(mockFoo, "juu", UIPage.class);
+// int rootV0 = mockRoot.getVersion();
+// int fooV0 = mockFoo.getVersion();
+// int barV0 = mockBar.getVersion();
+ String fooId = getId(mockFoo);
+ String barId = getId(mockBar);
+ String juuId = getId(mockJuu);
+ StructuralObject foo0 = getStructuralStateContext().load(fooId);
+ StructuralObject bar0 = getStructuralStateContext().load(barId);
+ StructuralObject juu0 = getStructuralStateContext().load(juuId);
+
+ //
+ destroy(mockJuu);
+
+ //
+ StructuralObject foo1 = getStructuralStateContext().load(fooId);
+ StructuralObject bar1 = getStructuralStateContext().load(barId);
+ StructuralObject juu1 = getStructuralStateContext().load(juuId);
+// int rootV1 = mockRoot.getVersion();
+// int fooV1 = mockFoo.getVersion();
+// int barV1 = mockBar.getVersion();
+
+ //
+ assertStale(juu0);
+ assertStale(foo0);
+ assertNotStale(bar0);
+ assertNull(juu1);
+ assertNotStale(foo1);
+ assertNotStale(bar1);
+ assertEquals(Collections.singletonList(mockFoo), getChildren(mockRoot));
+ assertEquals(Collections.singletonList(mockBar), getChildren(mockFoo));
+ assertTrue(isValid(mockFoo));
+ assertTrue(isValid(mockBar));
+ assertFalse(isValid(mockJuu));
+// assertTrue(rootV1 == rootV0);
+// assertTrue(fooV1 > fooV0);
+// assertTrue(barV1 == barV0);
+
+ //
+ destroy(mockFoo);
+
+// int rootV2 = mockRoot.getVersion();
+ StructuralObject foo2 = getStructuralStateContext().load(fooId);
+ StructuralObject bar2 = getStructuralStateContext().load(barId);
+ StructuralObject juu2 = getStructuralStateContext().load(juuId);
+
+ //
+ assertStale(juu0);
+ assertStale(foo0);
+ assertStale(bar0);
+ assertNull(juu1);
+ assertStale(foo1);
+ assertStale(bar1);
+ assertNull(foo2);
+ assertNull(bar2);
+ assertNull(juu2);
+ assertEquals(Collections.emptyList(), getChildren(mockRoot));
+ assertFalse(isValid(mockFoo));
+ assertFalse(isValid(mockBar));
+ assertFalse(isValid(mockJuu));
+// assertTrue(rootV2 > rootV1);
+ }
+
+ public void testUpdateProperty() throws MockException
+ {
+ MockObject mockRoot = getRoot();
+ String rootId = getId(mockRoot);
+// int rootV0 = mockRoot.getVersion();
+
+ //
+ StructuralObject root0 = getStructuralStateContext().load(rootId);
+ StructuralState rootState0 = root0.getState();
+ assertEquals(Collections.EMPTY_MAP, rootState0.getProperties());
+
+ // Set
+ setProperty(mockRoot, "foo", "bar");
+
+ //
+// int rootV1 = mockRoot.getVersion();
+// assertTrue(rootV1 > rootV0);
+ assertEquals(Collections.singleton("foo"), getPropertyNames(mockRoot));
+ assertEquals("bar", getProperty(mockRoot, "foo"));
+ assertEquals(Collections.EMPTY_MAP, rootState0.getProperties());
+ assertStale(root0);
+ StructuralObject root1 = getStructuralStateContext().load(rootId);
+ assertNotNull(root1);
+ assertNotStale(root1);
+ StructuralState rootState1 = root1.getState();
+ assertEquals(Collections.singletonMap("foo", "bar"),
rootState1.getProperties());
+ }
+
+ public void testInvalidatedByDestruction() throws MockException
+ {
+ MockObject mockRoot = getRoot();
+
+ //
+ MockObject mockFoo = addChild(mockRoot, "foo", UIPage.class);
+ MockObject mockBar = addChild(mockRoot, "bar", UIPage.class);
+
+ //
+ StructuralObject foo = getStructuralObject(mockFoo);
+
+ //
+ destroy(mockFoo);
+
+ //
+ assertValidity(foo, NoSuchStateException.class);
+ }
+
+ public void testInvalidatedByPropertyUpdate() throws MockException
+ {
+ MockObject mockRoot = getRoot();
+
+ //
+ MockObject mockFoo = addChild(mockRoot, "foo", UIPage.class);
+
+ //
+ StructuralObject foo = getStructuralObject(mockFoo);
+
+ //
+ setProperty(mockFoo, "a", "b");
+
+ //
+ assertValidity(foo, StaleStateException.class);
+ }
+
+ public void testInvalidatedByChildCreation() throws MockException
+ {
+ MockObject mockRoot = getRoot();
+
+ //
+ MockObject mockFoo = addChild(mockRoot, "foo", UIPage.class);
+
+ //
+ StructuralObject foo = getStructuralObject(mockFoo);
+
+ //
+ addChild(mockFoo, "juu", UIPage.class);
+
+ //
+ assertValidity(foo, StaleStateException.class);
+ }
+
+ public void testInvalidatedByChildDestruction() throws MockException
+ {
+ MockObject mockRoot = getRoot();
+
+ //
+ MockObject mockFoo = addChild(mockRoot, "foo", UIPage.class);
+ MockObject mockJuu = addChild(mockFoo, "juu", UIPage.class);
+
+ //
+ StructuralObject foo = getStructuralObject(mockFoo);
+
+ //
+ destroy(mockJuu);
+
+ //
+ assertValidity(foo, StaleStateException.class);
+ }
+
+
+ public void testRefresh() throws MockException
+ {
+ MockObject mockRoot = getRoot();
+ String rootId = getId(mockRoot);
+ MockObject mockFoo = addChild(mockRoot, "foo", UIPage.class);
+ String fooId = getId(mockFoo);
+ MockObject mockJuu = addChild(mockRoot, "juu", UIPage.class);
+ String juuId = getId(mockJuu);
+ MockObject mockFaa = addChild(mockRoot, "faa", UIPage.class);
+ String faaId = getId(mockFaa);
+ StructuralObject rootV0 = getStructuralStateContext().load(rootId);
+
+ //
+ MockObject mockBar = addChild(mockRoot, "bar", UIPage.class);
+ String barId = getId(mockBar);
+ destroy(mockFoo);
+ setProperty(mockJuu, "blah", "blah");
+
+ //
+ StructuralObject.Refresh comparison = getStructuralStateContext().refresh(rootV0);
+ assertNotNull(comparison);
+ Set<String> removed = comparison.getRemovedChildren();
+ assertNotNull(removed);
+ assertEquals(1, removed.size());
+ assertEquals(fooId, removed.iterator().next());
+
+ //
+ Set<StructuralObject> added = comparison.getAddedChildren();
+ assertNotNull(added);
+ assertEquals(1, added.size());
+ assertEquals(barId, added.iterator().next().getId());
+
+ //
+ Map<String, StructuralObject> stale = comparison.getStaleChildren();
+ assertNotNull(stale);
+ assertTrue(stale.size() >= 1);
+ StructuralObject juu = stale.get(getId(mockJuu));
+ assertNotNull(juu);
+ assertEquals(juuId, juu.getId());
+ assertEquals("blah",
juu.getState().getProperties().get("blah"));
+
+ //
+ Set<String> valid = comparison.getValidChildren();
+ assertNotNull(valid);
+ assertEquals(2 - stale.size(), valid.size());
+ if (valid.size() >= 1)
+ {
+ assertTrue(valid.contains(faaId));
+ }
+ }
+
+ protected final void assertValidity(StructuralObject object, Class<? extends
StateException> expected) throws MockException
+ {
+ try
+ {
+ getStructuralStateContext().validate(object);
+ fail();
+ }
+ catch (StateException e)
+ {
+ assertTrue(expected.isInstance(e));
+ }
+
+ //
+ try
+ {
+ getStructuralStateContext().loadChildren(object);
+ fail();
+ }
+ catch (StateException e)
+ {
+ assertTrue(expected.isInstance(e));
+ }
+
+ //
+ try
+ {
+ getStructuralStateContext().loadParent(object);
+ fail();
+ }
+ catch (StateException e)
+ {
+ assertTrue(expected.isInstance(e));
+ }
+
+ }
+
+ protected final void assertStale(StructuralObject object)
+ {
+ try
+ {
+ getStructuralStateContext().validate(object);
+ fail();
+ }
+ catch (StateException ignore)
+ {
+ }
+ }
+
+ protected final void assertNotStale(StructuralObject object)
+ {
+ getStructuralStateContext().validate(object);
+ }
}
Modified:
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/model/MockModelTestCase.java
===================================================================
---
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/model/MockModelTestCase.java 2008-04-03
09:16:19 UTC (rev 10490)
+++
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/model/MockModelTestCase.java 2008-04-03
15:04:02 UTC (rev 10491)
@@ -22,11 +22,11 @@
******************************************************************************/
package org.jboss.portal.presentation.test.model;
-import junit.framework.TestCase;
import org.jboss.portal.presentation.model2.ui.UIContext;
import org.jboss.portal.presentation.model2.ui.UIPage;
+import org.jboss.portal.presentation.model2.ui.UIObject;
+import org.jboss.portal.presentation.model2.ui.UIWindow;
import org.jboss.portal.presentation.state.StaleStateException;
-import org.jboss.portal.presentation.state.StateException;
import org.jboss.portal.presentation.state.NoSuchStateException;
import org.jboss.portal.presentation.state.structural.StructuralObject;
import org.jboss.portal.presentation.state.structural.StructuralState;
@@ -39,7 +39,7 @@
import java.util.Collections;
import java.util.Set;
import java.util.Map;
-import java.util.Collection;
+import java.util.List;
/**
* Test that the mock model we are using behaves in an expected manner
@@ -47,222 +47,125 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 630 $
*/
-public class MockModelTestCase extends TestCase
+public class MockModelTestCase extends AbstractModelTestCase<MockObject>
{
/** . */
private MockModel model;
/** . */
- private StructuralStateContext ssc;
+ private StructuralStateContext structuralStateContext;
protected void setUp() throws Exception
{
model = new MockModelImpl();
- ssc = model.getStructuralStateContext();
+ structuralStateContext = model.getStructuralStateContext();
}
protected void tearDown() throws Exception
{
model = null;
- ssc = null;
+ structuralStateContext = null;
}
- public void testRoot()
+ protected MockObject getRoot()
{
- MockObject mockRoot = model.getRoot();
- assertNotNull(mockRoot);
- assertEquals(Collections.EMPTY_SET, mockRoot.getPropertyNames());
- assertNull(mockRoot.getParent());
- assertEquals(Collections.EMPTY_LIST, mockRoot.getChildren());
- assertEquals(MockObject.Type.CONTEXT, mockRoot.getType());
- assertEquals("", mockRoot.getName());
- assertEquals(true, mockRoot.isValid());
+ return model.getRoot();
+ }
- //
- String rootId = ssc.getRootId();
- assertNotNull(rootId);
- StructuralObject root = ssc.load(rootId);
- assertNotNull(root);
- assertEquals(rootId, root.getId());
- StructuralState state = root.getState();
- assertEquals(UIContext.class, state.getType());
- assertEquals("", state.getName());
- assertEquals(Collections.EMPTY_MAP, state.getProperties());
+ public StructuralStateContext getStructuralStateContext()
+ {
+ return structuralStateContext;
}
- public void testUpdateProperty() throws MockException
+ protected Set<String> getPropertyNames(MockObject object)
{
- MockObject mockRoot = model.getRoot();
- String rootId = mockRoot.getId();
- int rootV0 = mockRoot.getVersion();
+ return object.getPropertyNames();
+ }
- //
- StructuralObject root0 = ssc.load(rootId);
- StructuralState rootState0 = root0.getState();
- assertEquals(Collections.EMPTY_MAP, rootState0.getProperties());
+ protected MockObject getParent(MockObject object)
+ {
+ return object.getParent();
+ }
- // Set
- mockRoot.setPropertyValue("foo", "bar");
+ protected List<? extends MockObject> getChildren(MockObject object)
+ {
+ return object.getChildren();
+ }
- //
- int rootV1 = mockRoot.getVersion();
- assertTrue(rootV1 > rootV0);
- assertEquals(Collections.singleton("foo"), mockRoot.getPropertyNames());
- assertEquals("bar", mockRoot.getPropertyValue("foo"));
- assertEquals(Collections.EMPTY_MAP, rootState0.getProperties());
- assertStale(root0);
- StructuralObject root1 = ssc.load(rootId);
- assertNotNull(root1);
- assertNotStale(root1);
- StructuralState rootState1 = root1.getState();
- assertEquals(Collections.singletonMap("foo", "bar"),
rootState1.getProperties());
+ protected String getName(MockObject object)
+ {
+ return object.getName();
}
- public void testAddChild() throws MockException
+ protected boolean isValid(MockObject object)
{
- MockObject mockRoot = model.getRoot();
- int rootV0 = mockRoot.getVersion();
+ return object.isValid();
+ }
- //
- String rootId = mockRoot.getId();
- StructuralObject root = ssc.load(rootId);
+ protected Class<? extends UIObject> getType(MockObject object)
+ {
+ return object.getType().getModelClass();
+ }
- //
- MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PAGE);
- assertNotNull(mockFoo);
- String fooId = mockFoo.getId();
+ protected String getId(MockObject object)
+ {
+ return object.getId();
+ }
- //
- assertNotNull(fooId);
- assertEquals("foo", mockFoo.getName());
- assertEquals(MockObject.Type.PAGE, mockFoo.getType());
- assertSame(mockRoot, mockFoo.getParent());
- assertEquals(Collections.singletonList(mockFoo), mockRoot.getChildren());
- assertEquals(Collections.EMPTY_SET, mockFoo.getPropertyNames());
- assertTrue(mockRoot.getVersion() > rootV0);
+ protected void setProperty(MockObject object, String propertyName, String
propertyValue)
+ {
+ object.setPropertyValue(propertyName, propertyValue);
+ }
- //
- try
+ protected String getProperty(MockObject mockRoot, String name)
+ {
+ return mockRoot.getPropertyValue(name);
+ }
+
+
+ protected MockObject addChild(MockObject object, String name, Class<? extends
UIObject> modelType)
+ {
+ MockObject.Type type;
+ if (UIPage.class.equals(modelType))
{
- ssc.loadParent(root);
- fail();
+ type = MockObject.Type.PAGE;
}
- catch (StaleStateException ignore)
+ else if (UIContext.class.equals(modelType))
{
+ type = MockObject.Type.CONTEXT;
}
- try
+ else if (UIWindow.class.equals(modelType))
{
- ssc.loadChildren(root);
- fail();
+ type = MockObject.Type.WINDOW;
}
- catch (StaleStateException ignore)
+ else
{
+ throw new AssertionError();
}
//
- root = ssc.load(rootId);
- assertNotNull(root);
- assertEquals(rootId, root.getId());
-
- //
- Collection<StructuralObject> children = ssc.loadChildren(root);
- assertNotNull(children);
- assertEquals(1, children.size());
- StructuralObject foo = children.iterator().next();
- assertNotNull(foo);
- assertEquals(fooId, foo.getId());
- StructuralState fooState = foo.getState();
- assertNotNull(fooState);
- assertEquals("foo", fooState.getName());
- assertEquals(Collections.EMPTY_MAP, fooState.getProperties());
- assertEquals(UIPage.class, fooState.getType());
- root = ssc.loadParent(foo);
- assertNotNull(root);
- assertEquals(rootId, root.getId());
+ return object.addChild(name, type);
}
- public void testDestroy() throws MockException
+ protected void destroy(MockObject object)
{
- MockObject mockRoot = model.getRoot();
- MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PAGE);
- MockObject mockBar = mockFoo.addChild("bar", MockObject.Type.PAGE);
- MockObject mockJuu = mockFoo.addChild("juu", MockObject.Type.PAGE);
- int rootV0 = mockRoot.getVersion();
- int fooV0 = mockFoo.getVersion();
- int barV0 = mockBar.getVersion();
- String fooId = mockFoo.getId();
- String barId = mockBar.getId();
- String juuId = mockJuu.getId();
- StructuralObject foo0 = ssc.load(fooId);
- StructuralObject bar0 = ssc.load(barId);
- StructuralObject juu0 = ssc.load(juuId);
-
- //
- model.destroy(mockJuu);
-
- //
- StructuralObject foo1 = ssc.load(fooId);
- StructuralObject bar1 = ssc.load(barId);
- StructuralObject juu1 = ssc.load(juuId);
- int rootV1 = mockRoot.getVersion();
- int fooV1 = mockFoo.getVersion();
- int barV1 = mockBar.getVersion();
-
- //
- assertStale(juu0);
- assertStale(foo0);
- assertNotStale(bar0);
- assertNull(juu1);
- assertNotStale(foo1);
- assertNotStale(bar1);
- assertEquals(Collections.singletonList(mockFoo), mockRoot.getChildren());
- assertEquals(Collections.singletonList(mockBar), mockFoo.getChildren());
- assertTrue(mockFoo.isValid());
- assertTrue(mockBar.isValid());
- assertFalse(mockJuu.isValid());
- assertTrue(rootV1 == rootV0);
- assertTrue(fooV1 > fooV0);
- assertTrue(barV1 == barV0);
-
- //
- model.destroy(mockFoo);
-
- int rootV2 = mockRoot.getVersion();
- StructuralObject foo2 = ssc.load(fooId);
- StructuralObject bar2 = ssc.load(barId);
- StructuralObject juu2 = ssc.load(juuId);
-
- //
- assertStale(juu0);
- assertStale(foo0);
- assertStale(bar0);
- assertNull(juu1);
- assertStale(foo1);
- assertStale(bar1);
- assertNull(foo2);
- assertNull(bar2);
- assertNull(juu2);
- assertEquals(Collections.emptyList(), mockRoot.getChildren());
- assertFalse(mockFoo.isValid());
- assertFalse(mockBar.isValid());
- assertFalse(mockJuu.isValid());
- assertTrue(rootV2 > rootV1);
+ model.destroy(object);
}
public void testDestroyInvalidates() throws MockException
{
- MockObject mockRoot = model.getRoot();
- MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PAGE);
+ MockObject mockRoot = getRoot();
+ MockObject mockFoo = addChild(mockRoot, "foo", UIPage.class);
//
- model.destroy(mockFoo);
+ destroy(mockFoo);
//
- assertEquals(false, mockFoo.isValid());
+ assertEquals(false, isValid(mockFoo));
try
{
- mockFoo.addChild("juu", MockObject.Type.PAGE);
+ addChild(mockFoo, "juu", UIPage.class);
}
catch (MockException e)
{
@@ -270,7 +173,7 @@
}
try
{
- model.destroy(mockFoo);
+ destroy(mockFoo);
}
catch (MockException e)
{
@@ -278,7 +181,7 @@
}
try
{
- mockFoo.setPropertyValue("blah", "bluh");
+ setProperty(mockFoo, "blah", "bluh");
}
catch (MockException e)
{
@@ -288,25 +191,25 @@
public void testMove() throws MockException
{
- MockObject mockRoot = model.getRoot();
+ MockObject mockRoot = getRoot();
- MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PAGE);
- MockObject mockBar = mockRoot.addChild("bar", MockObject.Type.PAGE);
+ MockObject mockFoo = addChild(mockRoot, "foo", UIPage.class);
+ MockObject mockBar = addChild(mockRoot, "bar", UIPage.class);
mockFoo.move(mockBar);
- assertSame(mockBar, mockFoo.getParent());
- assertEquals(Collections.singletonList(mockFoo), mockBar.getChildren());
- assertEquals(Collections.singletonList(mockBar), mockRoot.getChildren());
+ assertSame(mockBar, getParent(mockFoo));
+ assertEquals(Collections.singletonList(mockFoo), getChildren(mockBar));
+ assertEquals(Collections.singletonList(mockBar), getChildren(mockRoot));
}
public void testMoveViolatesConstraint() throws MockException
{
- MockObject mockRoot = model.getRoot();
+ MockObject mockRoot = getRoot();
- MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PAGE);
- MockObject mockBar = mockRoot.addChild("bar", MockObject.Type.PAGE);
- MockObject mockJuu = mockBar.addChild("juu", MockObject.Type.PAGE);
+ MockObject mockFoo = addChild(mockRoot, "foo", UIPage.class);
+ MockObject mockBar = addChild(mockRoot, "bar", UIPage.class);
+ MockObject mockJuu = addChild(mockBar, "juu", UIPage.class);
//
try
@@ -349,84 +252,14 @@
}
}
- public void testInvalidatedByDestruction() throws MockException
- {
- MockObject mockRoot = model.getRoot();
-
- //
- MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PAGE);
- MockObject mockBar = mockRoot.addChild("bar", MockObject.Type.PAGE);
-
- //
- StructuralObject foo = mockFoo.takeSnapshot();
-
- //
- model.destroy(mockFoo);
-
- //
- assertValidity(foo, NoSuchStateException.class);
- }
-
- public void testInvalidatedByPropertyUpdate() throws MockException
- {
- MockObject mockRoot = model.getRoot();
-
- //
- MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PAGE);
-
- //
- StructuralObject foo = mockFoo.takeSnapshot();
-
- //
- mockFoo.setPropertyValue("a", "b");
-
- //
- assertValidity(foo, StaleStateException.class);
- }
-
- public void testInvalidatedByChildCreation() throws MockException
- {
- MockObject mockRoot = model.getRoot();
-
- //
- MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PAGE);
-
- //
- StructuralObject foo = mockFoo.takeSnapshot();
-
- //
- mockFoo.addChild("juu", MockObject.Type.PAGE);
-
- //
- assertValidity(foo, StaleStateException.class);
- }
-
- public void testInvalidatedByChildDestruction() throws MockException
- {
- MockObject mockRoot = model.getRoot();
-
- //
- MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PAGE);
- MockObject mockJuu = mockFoo.addChild("juu", MockObject.Type.PAGE);
-
- //
- StructuralObject foo = mockFoo.takeSnapshot();
-
- //
- model.destroy(mockJuu);
-
- //
- assertValidity(foo, StaleStateException.class);
- }
-
public void testInvalidatedByMove() throws MockException
{
- MockObject mockRoot = model.getRoot();
+ MockObject mockRoot = getRoot();
//
- MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PAGE);
- MockObject mockBar = mockFoo.addChild("bar", MockObject.Type.PAGE);
- MockObject mockJuu = mockFoo.addChild("juu", MockObject.Type.PAGE);
+ MockObject mockFoo = addChild(mockRoot, "foo", UIPage.class);
+ MockObject mockBar = addChild(mockFoo, "bar", UIPage.class);
+ MockObject mockJuu = addChild(mockFoo, "juu", UIPage.class);
//
StructuralObject foo = mockFoo.takeSnapshot();
@@ -441,106 +274,4 @@
assertValidity(bar, StaleStateException.class);
assertValidity(juu, StaleStateException.class);
}
-
- public void testRefresh() throws MockException
- {
- MockObject mockRoot = model.getRoot();
- String rootId = mockRoot.getId();
- MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PAGE);
- String fooId = mockFoo.getId();
- MockObject mockJuu = mockRoot.addChild("juu", MockObject.Type.PAGE);
- String juuId = mockJuu.getId();
- MockObject mockFaa = mockRoot.addChild("faa", MockObject.Type.PAGE);
- String faaId = mockFaa.getId();
- StructuralObject rootV0 = ssc.load(rootId);
-
- //
- MockObject mockBar = mockRoot.addChild("bar", MockObject.Type.PAGE);
- String barId = mockBar.getId();
- model.destroy(mockFoo);
- mockJuu.setPropertyValue("blah", "blah");
-
- //
- StructuralObject.Refresh comparison = ssc.refresh(rootV0);
- assertNotNull(comparison);
- Set<String> removed = comparison.getRemovedChildren();
- assertNotNull(removed);
- assertEquals(1, removed.size());
- assertEquals(fooId, removed.iterator().next());
-
- //
- Set<StructuralObject> added = comparison.getAddedChildren();
- assertNotNull(added);
- assertEquals(1, added.size());
- assertEquals(barId, added.iterator().next().getId());
-
- //
- Map<String, StructuralObject> stale = comparison.getStaleChildren();
- assertNotNull(stale);
- assertEquals(1, stale.size());
- StructuralObject juu = stale.values().iterator().next();
- assertNotNull(juu);
- assertEquals(juuId, juu.getId());
- assertEquals("blah",
juu.getState().getProperties().get("blah"));
-
- //
- Set<String> valid = comparison.getValidChildren();
- assertNotNull(valid);
- assertEquals(1, valid.size());
- assertEquals(faaId, valid.iterator().next());
- }
-
- private void assertValidity(StructuralObject object, Class<? extends
StateException> expected) throws MockException
- {
- //
- try
- {
- ssc.validate(object);
- fail();
- }
- catch (StateException e)
- {
- assertTrue(expected.isInstance(e));
- }
-
- //
- try
- {
- ssc.loadChildren(object);
- fail();
- }
- catch (StateException e)
- {
- assertTrue(expected.isInstance(e));
- }
-
- //
- try
- {
- ssc.loadParent(object);
- fail();
- }
- catch (StateException e)
- {
- assertTrue(expected.isInstance(e));
- }
-
- }
-
- private void assertStale(StructuralObject object)
- {
- try
- {
- ssc.validate(object);
- fail();
- }
- catch (StateException ignore)
- {
- }
- }
-
- private void assertNotStale(StructuralObject object)
- {
- ssc.validate(object);
- }
}
Modified:
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/model/state/structural/MockObject.java
===================================================================
---
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/model/state/structural/MockObject.java 2008-04-03
09:16:19 UTC (rev 10490)
+++
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/model/state/structural/MockObject.java 2008-04-03
15:04:02 UTC (rev 10491)
@@ -54,6 +54,11 @@
{
this.clazz = clazz;
}
+
+ public Class<? extends UIObject> getModelClass()
+ {
+ return clazz;
+ }
}
public abstract static class UpdateBehavior
Added:
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/state/structural/StructuralStateContextTestCase.java
===================================================================
---
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/state/structural/StructuralStateContextTestCase.java
(rev 0)
+++
modules/presentation/trunk/presentation/src/test/java/org/jboss/portal/presentation/test/state/structural/StructuralStateContextTestCase.java 2008-04-03
15:04:02 UTC (rev 10491)
@@ -0,0 +1,141 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, 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.state.structural;
+
+import org.jboss.portal.presentation.impl.state.structural.StructuralStateContextImpl;
+import org.jboss.portal.presentation.impl.state.structural.ContextNode;
+import org.jboss.portal.presentation.impl.state.structural.StructuralNode;
+import org.jboss.portal.presentation.impl.state.structural.PageNode;
+import org.jboss.portal.presentation.impl.state.structural.WindowNode;
+import org.jboss.portal.presentation.state.structural.StructuralStateContext;
+import org.jboss.portal.presentation.model2.ui.UIContext;
+import org.jboss.portal.presentation.model2.ui.UIPage;
+import org.jboss.portal.presentation.model2.ui.UIObject;
+import org.jboss.portal.presentation.model2.ui.UIWindow;
+import org.jboss.portal.presentation.test.model.AbstractModelTestCase;
+
+import java.util.ArrayList;
+import java.util.Set;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public class StructuralStateContextTestCase extends
AbstractModelTestCase<StructuralNode>
+{
+
+ /** . */
+ private StructuralStateContextImpl structuralStateContext;
+
+ protected StructuralStateContext getStructuralStateContext()
+ {
+ return structuralStateContext;
+ }
+
+ protected StructuralNode getRoot()
+ {
+ return structuralStateContext.getRoot();
+ }
+
+ protected Set<String> getPropertyNames(StructuralNode structuralNode)
+ {
+ return structuralNode.getPropertyNames();
+ }
+
+ protected StructuralNode getParent(StructuralNode structuralNode)
+ {
+ return structuralNode.getParent();
+ }
+
+ protected List<? extends StructuralNode> getChildren(StructuralNode
structuralNode)
+ {
+ return new ArrayList<StructuralNode>(structuralNode.getChildren());
+ }
+
+ protected String getName(StructuralNode structuralNode)
+ {
+ return structuralNode.getName();
+ }
+
+ protected boolean isValid(StructuralNode structuralNode)
+ {
+ return structuralNode.isValid();
+ }
+
+ protected Class<? extends UIObject> getType(StructuralNode structuralNode)
+ {
+ return structuralNode.getType();
+ }
+
+ protected String getId(StructuralNode structuralNode)
+ {
+ return structuralNode.getId();
+ }
+
+ protected StructuralNode addChild(StructuralNode structuralNode, String name,
Class<? extends UIObject> modelType)
+ {
+ Class<? extends StructuralNode> structuralNodeType;
+
+ //
+ if (modelType == UIContext.class)
+ {
+ structuralNodeType = ContextNode.class;
+ }
+ else if (modelType == UIPage.class)
+ {
+ structuralNodeType = PageNode.class;
+ }
+ else if (modelType == UIWindow.class)
+ {
+ structuralNodeType = WindowNode.class;
+ }
+ else
+ {
+ throw new AssertionError();
+ }
+
+ //
+ return structuralNode.addChild(name, structuralNodeType);
+ }
+
+ protected void setProperty(StructuralNode structuralNode, String propertyName, String
propertyValue)
+ {
+ structuralNode.setProperty(propertyName, propertyValue);
+ }
+
+ protected String getProperty(StructuralNode structuralNode, String name)
+ {
+ return structuralNode.getProperty(name);
+ }
+
+ protected void destroy(StructuralNode structuralNode)
+ {
+ structuralStateContext.destroy(structuralNode.getId());
+ }
+
+ protected void setUp() throws Exception
+ {
+ structuralStateContext = new StructuralStateContextImpl();
+ }
+}