You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by cs...@apache.org on 2006/01/23 17:12:23 UTC

svn commit: r371565 - in /beehive/trunk/controls/test/src: junit-controls/org/apache/beehive/controls/test/controls/serialization/ junit-tests/org/apache/beehive/controls/test/junit/

Author: cschoett
Date: Mon Jan 23 08:12:17 2006
New Revision: 371565

URL: http://svn.apache.org/viewcvs?rev=371565&view=rev
Log:
Added a simple control's serialization test.  Uses the ControlTestCase container to serialize and then deserialize a custom control.

Added:
    beehive/trunk/controls/test/src/junit-controls/org/apache/beehive/controls/test/controls/serialization/
    beehive/trunk/controls/test/src/junit-controls/org/apache/beehive/controls/test/controls/serialization/ControlSerialization.java   (with props)
    beehive/trunk/controls/test/src/junit-controls/org/apache/beehive/controls/test/controls/serialization/ControlSerializationImpl.java   (with props)
    beehive/trunk/controls/test/src/junit-tests/org/apache/beehive/controls/test/junit/SimpleSerializationTest.java   (with props)

Added: beehive/trunk/controls/test/src/junit-controls/org/apache/beehive/controls/test/controls/serialization/ControlSerialization.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/controls/test/src/junit-controls/org/apache/beehive/controls/test/controls/serialization/ControlSerialization.java?rev=371565&view=auto
==============================================================================
--- beehive/trunk/controls/test/src/junit-controls/org/apache/beehive/controls/test/controls/serialization/ControlSerialization.java (added)
+++ beehive/trunk/controls/test/src/junit-controls/org/apache/beehive/controls/test/controls/serialization/ControlSerialization.java Mon Jan 23 08:12:17 2006
@@ -0,0 +1,36 @@
+/*
+   Copyright 2004 The Apache Software Foundation.
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+   
+       http://www.apache.org/licenses/LICENSE-2.0
+   
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+  
+   $Header:$
+*/
+package org.apache.beehive.controls.test.controls.serialization;
+
+import java.util.List;
+import org.apache.beehive.controls.api.bean.ControlInterface;
+
+/**
+ * Test control serialization and lifecycle events associated with serialization.
+ */
+@ControlInterface
+public interface ControlSerialization {
+
+    public List<String> getLifecycleEvents();
+
+    public void clearLifecycleEvents();
+
+    public int getControlState();
+
+    public void setControlState(int controlState);
+}

Propchange: beehive/trunk/controls/test/src/junit-controls/org/apache/beehive/controls/test/controls/serialization/ControlSerialization.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: beehive/trunk/controls/test/src/junit-controls/org/apache/beehive/controls/test/controls/serialization/ControlSerializationImpl.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/controls/test/src/junit-controls/org/apache/beehive/controls/test/controls/serialization/ControlSerializationImpl.java?rev=371565&view=auto
==============================================================================
--- beehive/trunk/controls/test/src/junit-controls/org/apache/beehive/controls/test/controls/serialization/ControlSerializationImpl.java (added)
+++ beehive/trunk/controls/test/src/junit-controls/org/apache/beehive/controls/test/controls/serialization/ControlSerializationImpl.java Mon Jan 23 08:12:17 2006
@@ -0,0 +1,97 @@
+/*
+   Copyright 2006 The Apache Software Foundation.
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+   
+       http://www.apache.org/licenses/LICENSE-2.0
+   
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+  
+   $Header:$
+*/
+package org.apache.beehive.controls.test.controls.serialization;
+
+import java.util.List;
+import java.util.LinkedList;
+import java.util.Collections;
+
+import org.apache.beehive.controls.api.context.Context;
+import org.apache.beehive.controls.api.context.ResourceContext;
+import org.apache.beehive.controls.api.context.ControlBeanContext;
+import org.apache.beehive.controls.api.context.ControlThreadContext;
+import org.apache.beehive.controls.api.context.ControlContainerContext;
+import org.apache.beehive.controls.api.events.EventHandler;
+import org.apache.beehive.controls.api.bean.ControlImplementation;
+
+/**
+ * Control implementation for testing control serialization and associated lifecycle events.
+ */
+@ControlImplementation
+public class ControlSerializationImpl
+    implements ControlSerialization, java.io.Serializable {
+
+    private LinkedList<String> _lifecycleEventStrings = new LinkedList<String>();
+    private boolean _onReleaseCalled = true;
+    private int _controlState = -1;
+
+    @Context
+    private ControlBeanContext _controlBeanContext;
+
+    @Context
+    private ResourceContext _resourceContext;
+
+    public List<String> getLifecycleEvents() {
+        return Collections.unmodifiableList(_lifecycleEventStrings);
+    }
+
+    public void clearLifecycleEvents() {
+        _lifecycleEventStrings.clear();
+    }
+
+    public int getControlState() {
+        return _controlState;
+    }
+
+    public void setControlState(int controlState) {
+        _controlState = controlState;
+    }
+
+    @EventHandler(field = "_controlBeanContext", eventSet = ControlBeanContext.LifeCycle.class, eventName = "onCreate")
+    public void onCreate() {
+        checkContainerContext();
+        _lifecycleEventStrings.add("onCreate");
+    }
+
+    @EventHandler(field = "_resourceContext", eventSet = ResourceContext.ResourceEvents.class, eventName = "onAcquire")
+    public void onAquire() {
+        System.out.println("bean context event -- onAcquire : state " + _controlState + " ctrl " + this);
+        checkContainerContext();
+
+        if(!_onReleaseCalled)
+            throw new IllegalStateException("onAcquire called without having called onRelease on the previous request! Ctrl state = " + _controlState);
+
+        _onReleaseCalled = false;
+        _lifecycleEventStrings.add("onAcquire");
+    }
+
+    @EventHandler(field = "_resourceContext", eventSet = ResourceContext.ResourceEvents.class, eventName = "onRelease")
+    public void onRelease() {
+        System.out.println("bean context event -- onRelease : state = " + _controlState + " ctrl " + this);
+        checkContainerContext();
+        _onReleaseCalled = true;
+
+        _lifecycleEventStrings.add("onRelease");
+    }
+
+    private void checkContainerContext() {
+        ControlContainerContext ccc = ControlThreadContext.getContext();
+        if(ccc == null)
+            throw new IllegalStateException("Control could not find a valid ControlContainerContext!");
+    }
+}

Propchange: beehive/trunk/controls/test/src/junit-controls/org/apache/beehive/controls/test/controls/serialization/ControlSerializationImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: beehive/trunk/controls/test/src/junit-tests/org/apache/beehive/controls/test/junit/SimpleSerializationTest.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/controls/test/src/junit-tests/org/apache/beehive/controls/test/junit/SimpleSerializationTest.java?rev=371565&view=auto
==============================================================================
--- beehive/trunk/controls/test/src/junit-tests/org/apache/beehive/controls/test/junit/SimpleSerializationTest.java (added)
+++ beehive/trunk/controls/test/src/junit-tests/org/apache/beehive/controls/test/junit/SimpleSerializationTest.java Mon Jan 23 08:12:17 2006
@@ -0,0 +1,125 @@
+/*
+   Copyright 2004 The Apache Software Foundation.
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+   
+       http://www.apache.org/licenses/LICENSE-2.0
+   
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+  
+   $Header:$
+*/
+package org.apache.beehive.controls.test.junit;
+
+import java.util.List;
+import java.io.FileOutputStream;
+import java.io.ObjectOutputStream;
+import java.io.FileInputStream;
+import java.io.ObjectInputStream;
+import java.io.File;
+
+import org.apache.beehive.controls.api.bean.Control;
+import org.apache.beehive.controls.api.context.ControlContainerContext;
+import org.apache.beehive.controls.test.controls.serialization.ControlSerialization;
+import org.apache.beehive.controls.test.controls.serialization.ControlSerializationBean;
+import org.apache.beehive.controls.test.container.ControlTestContainerContext;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * Test serialization of a simple control.
+ */
+public class SimpleSerializationTest
+    extends ControlTestCase {
+
+    @Control
+    private ControlSerialization _serializationControl;
+
+    public void setUp() {
+        /* intentionally, this does not call super */
+    }
+
+    public void tearDown() {
+        /* intentionally, this does not call super */
+    }
+
+    public void testSimpleSerialization() throws Exception {
+
+        //
+        // start the context
+        //
+        getControlContainerContextManager().beginContext();
+
+        initializeControls();
+        assertNotNull(_serializationControl);
+
+        _serializationControl.setControlState(6);
+        _serializationControl.clearLifecycleEvents();
+
+        ControlContainerContext ccc = getControlContainerContext();
+
+        //
+        // end the context
+        //
+        getControlContainerContextManager().endContext();
+
+        //
+        // serialize the object
+        //
+        File serFile = File.createTempFile("controls", "ser");
+        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(serFile));
+        ControlTestContainerContext ctcc = (ControlTestContainerContext)ccc;
+        ctcc.writeChildren(oos);
+        oos.close();
+
+        //
+        // deserialize the ctcc contents
+        //
+        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(serFile));
+        ctcc.readChildren(ois);
+        ois.close();
+        serFile.delete();
+
+        //
+        // find the deserialized instance in the ctcc
+        //
+        Object[] ctrls = ctcc.toArray();
+        ControlSerialization deserializedControl = null;
+        for (Object c : ctrls) {
+            if (c instanceof ControlSerializationBean && !c.equals(_serializationControl)) {
+                deserializedControl = (ControlSerialization)c;
+                break;
+            }
+        }
+
+        //
+        // start the context
+        //
+        getControlContainerContextManager().beginContext();
+
+        assertNotNull(deserializedControl);
+        assertFalse(deserializedControl == _serializationControl);
+        assertEquals(6, deserializedControl.getControlState());
+        List<String> events = deserializedControl.getLifecycleEvents();
+
+        // onRelease should have been invoked before the context was first ended
+        assertEquals("onRelease", events.get(0));
+        assertEquals("onAcquire", events.get(1));
+
+        getControlContainerContextManager().endContext();
+    }
+
+    public static Test suite() {
+        return new TestSuite(SimpleSerializationTest.class);
+    }
+
+    public static void main(String[] args) {
+        junit.textui.TestRunner.run(suite());
+    }
+}

Propchange: beehive/trunk/controls/test/src/junit-tests/org/apache/beehive/controls/test/junit/SimpleSerializationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native