Author: julien(a)jboss.com
Date: 2007-12-03 19:49:30 -0500 (Mon, 03 Dec 2007)
New Revision: 9260
Added:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModelTestCase.java
Modified:
branches/presentation/presentation/build.xml
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModel.java
Log:
test the mock model standalone also
Modified: branches/presentation/presentation/build.xml
===================================================================
--- branches/presentation/presentation/build.xml 2007-12-03 23:38:42 UTC (rev 9259)
+++ branches/presentation/presentation/build.xml 2007-12-04 00:49:30 UTC (rev 9260)
@@ -313,6 +313,7 @@
</x-sysproperty>
<x-test>
<test todir="${test.reports}"
name="org.jboss.portal.presentation.test.model.ModelTestCase"/>
+ <test todir="${test.reports}"
name="org.jboss.portal.presentation.test.model.state.structural.MockModelTestCase"/>
</x-test>
<x-classpath>
<path refid="jboss.portal/modules/common.classpath"/>
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
23:38:42 UTC (rev 9259)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModel.java 2007-12-04
00:49:30 UTC (rev 9260)
@@ -142,7 +142,9 @@
public StructuralObject loadParent(StructuralObject object)
{
- return getValidMockObject(object).getParent().takeSnapshot();
+ MockObject mockObject = getValidMockObject(object);
+ MockObject mockParent = mockObject.getParent();
+ return mockParent != null ? mockParent.takeSnapshot() : null;
}
private MockObject getValidMockObject(StructuralObject object)
Added:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModelTestCase.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModelTestCase.java
(rev 0)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModelTestCase.java 2007-12-04
00:49:30 UTC (rev 9260)
@@ -0,0 +1,277 @@
+/******************************************************************************
+ * 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 junit.framework.TestCase;
+import org.jboss.portal.presentation.model.UIContext;
+import org.jboss.portal.presentation.model.UIPortal;
+import org.jboss.portal.presentation.model.state.StaleStateException;
+import org.jboss.portal.presentation.model.state.structural.StructuralObject;
+import org.jboss.portal.presentation.model.state.structural.StructuralState;
+import
org.jboss.portal.presentation.model.state.structural.StructuralStateChangeListener;
+import org.jboss.portal.presentation.model.state.structural.StructuralStateContext;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * Test that the mock model we are using behaves in an expected manner
+ *
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+public class MockModelTestCase extends TestCase
+{
+
+ /** . */
+ private MockModel model;
+
+ /** . */
+ private StructuralStateContext ssc;
+
+ /** . */
+ private Map<String, StructuralObject> universe;
+
+ /** . */
+ private final StructuralStateChangeListener listener = new
StructuralStateChangeListener()
+ {
+ public void update(StructuralObject object)
+ {
+ universe.put(object.getId(), object);
+ }
+ };
+
+ protected void setUp() throws Exception
+ {
+ model = new MockModel();
+ ssc = model.getStructuralStateContext();
+ universe = new HashMap<String, StructuralObject>();
+ }
+
+ protected void tearDown() throws Exception
+ {
+ model = null;
+ ssc = null;
+ universe = null;
+ }
+
+ public void testRoot()
+ {
+ 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());
+
+ //
+ 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 void testSetProperty()
+ {
+ MockObject mockRoot = model.getRoot();
+ String rootId = mockRoot.getId();
+ int rootV0 = mockRoot.getVersion();
+
+ //
+ StructuralObject root0 = ssc.load(rootId);
+ StructuralState rootState0 = root0.getState();
+ assertEquals(Collections.EMPTY_MAP, rootState0.getProperties());
+
+ // Set
+ ssc.update(listener, root0, Collections.singletonMap("foo",
"bar"));
+
+ //
+ 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_0 = universe.get(rootId);
+ assertNotNull(root1_0);
+ assertNotStale(root1_0);
+ StructuralState rootState1_0 = root1_0.getState();
+ assertEquals(Collections.singletonMap("foo", "bar"),
rootState1_0.getProperties());
+ StructuralObject root1_1 = ssc.load(rootId);
+ assertNotNull(root1_1);
+ assertNotStale(root1_1);
+ StructuralState rootState1_1 = root1_1.getState();
+ assertEquals(Collections.singletonMap("foo", "bar"),
rootState1_1.getProperties());
+ }
+
+ public void testUpdateProperty()
+ {
+ MockObject mockRoot = model.getRoot();
+ String rootId = mockRoot.getId();
+ int rootV0 = mockRoot.getVersion();
+
+ //
+ StructuralObject root0 = ssc.load(rootId);
+ StructuralState rootState0 = root0.getState();
+ assertEquals(Collections.EMPTY_MAP, rootState0.getProperties());
+
+ // Set
+ mockRoot.setPropertyValue("foo", "bar");
+
+ //
+ 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());
+
+ // Update
+ ssc.update(listener, root1, Collections.singletonMap("foo",
"bar2"));
+
+ //
+ int rootV2 = mockRoot.getVersion();
+ assertTrue(rootV2 > rootV1);
+ assertEquals(Collections.singleton("foo"), mockRoot.getPropertyNames());
+ assertEquals("bar2", mockRoot.getPropertyValue("foo"));
+ assertEquals(Collections.EMPTY_MAP, rootState0.getProperties());
+ assertStale(root0);
+ assertStale(root1);
+
+ //
+ StructuralObject root2_0 = universe.get(rootId);
+ assertNotNull(root2_0);
+ assertNotStale(root2_0);
+ StructuralState rootState2_0 = root2_0.getState();
+ assertEquals(Collections.singletonMap("foo", "bar2"),
rootState2_0.getProperties());
+ StructuralObject root2_1 = ssc.load(rootId);
+ assertNotNull(root2_1);
+ assertNotStale(root2_1);
+ StructuralState rootState2_1 = root2_1.getState();
+ assertEquals(Collections.singletonMap("foo", "bar2"),
rootState2_1.getProperties());
+ }
+
+ private void assertStale(StructuralObject object)
+ {
+ try
+ {
+ ssc.loadParent(object);
+ fail();
+ }
+ catch (StaleStateException ignore)
+ {
+ }
+ try
+ {
+ ssc.loadChildren(object);
+ fail();
+ }
+ catch (StaleStateException ignore)
+ {
+ }
+ }
+
+ private void assertNotStale(StructuralObject object)
+ {
+ ssc.loadParent(object);
+ ssc.loadChildren(object);
+ }
+
+ public void testAddChild()
+ {
+ MockObject mockRoot = model.getRoot();
+ int rootV0 = mockRoot.getVersion();
+
+ //
+ String rootId = mockRoot.getId();
+ StructuralObject root = ssc.load(rootId);
+
+ //
+ MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PORTAL);
+ assertNotNull(mockFoo);
+ String fooId = mockFoo.getId();
+
+ //
+ assertNotNull(fooId);
+ assertEquals("foo", mockFoo.getName());
+ assertEquals(MockObject.Type.PORTAL, mockFoo.getType());
+ assertSame(mockRoot, mockFoo.getParent());
+ assertEquals(Collections.singletonList(mockFoo), mockRoot.getChildren());
+ assertEquals(Collections.EMPTY_SET, mockFoo.getPropertyNames());
+ assertTrue(mockRoot.getVersion() > rootV0);
+
+ //
+ try
+ {
+ ssc.loadParent(root);
+ fail();
+ }
+ catch (StaleStateException ignore)
+ {
+ }
+ try
+ {
+ ssc.loadChildren(root);
+ fail();
+ }
+ catch (StaleStateException ignore)
+ {
+ }
+
+ //
+ root = ssc.load(rootId);
+ assertNotNull(root);
+ assertEquals(rootId, root.getId());
+
+ //
+ List<StructuralObject> children = ssc.loadChildren(root);
+ assertNotNull(children);
+ assertEquals(1, children.size());
+ StructuralObject foo = children.get(0);
+ assertNotNull(foo);
+ assertEquals(fooId, foo.getId());
+ StructuralState fooState = foo.getState();
+ assertNotNull(fooState);
+ assertEquals("foo", fooState.getName());
+ assertEquals(Collections.EMPTY_MAP, fooState.getProperties());
+ assertEquals(UIPortal.class, fooState.getType());
+ root = ssc.loadParent(foo);
+ assertNotNull(root);
+ assertEquals(rootId, root.getId());
+ }
+
+}