You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@synapse.apache.org by hi...@apache.org on 2010/04/26 13:35:04 UTC

svn commit: r938007 - in /synapse/trunk/java/modules/core/src/test/java/org/apache/synapse: mediators/TestUtils.java registry/DynamicResourceTest.java registry/InMemoryRegistryEntry.java registry/SimpleInMemoryRegistry.java

Author: hiranya
Date: Mon Apr 26 11:35:04 2010
New Revision: 938007

URL: http://svn.apache.org/viewvc?rev=938007&view=rev
Log:
Test case for dynamic sequences


Added:
    synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/registry/DynamicResourceTest.java
    synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/registry/InMemoryRegistryEntry.java
    synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/registry/SimpleInMemoryRegistry.java
Modified:
    synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/TestUtils.java

Modified: synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/TestUtils.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/TestUtils.java?rev=938007&r1=938006&r2=938007&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/TestUtils.java (original)
+++ synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/TestUtils.java Mon Apr 26 11:35:04 2010
@@ -72,9 +72,15 @@ public class TestUtils {
 
     public static MessageContext createLightweightSynapseMessageContext(
             String payload) throws Exception {
+
+        return createLightweightSynapseMessageContext(payload, new SynapseConfiguration());        
+    }
+
+    public static MessageContext createLightweightSynapseMessageContext(
+            String payload, SynapseConfiguration config) throws Exception {
+
         org.apache.axis2.context.MessageContext mc =
                 new org.apache.axis2.context.MessageContext();
-        SynapseConfiguration config = new SynapseConfiguration();
         SynapseEnvironment env = new Axis2SynapseEnvironment(config);
         MessageContext synMc = new Axis2MessageContext(mc, config, env);
         SOAPEnvelope envelope =

Added: synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/registry/DynamicResourceTest.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/registry/DynamicResourceTest.java?rev=938007&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/registry/DynamicResourceTest.java (added)
+++ synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/registry/DynamicResourceTest.java Mon Apr 26 11:35:04 2010
@@ -0,0 +1,123 @@
+/*
+ *  Copyright (c) 2005-2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ *  WSO2 Inc. licenses this file to you 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.
+ *
+ */
+
+package org.apache.synapse.registry;
+
+import junit.framework.TestCase;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.config.SynapseConfiguration;
+import org.apache.synapse.mediators.TestUtils;
+import org.apache.synapse.mediators.base.SequenceMediator;
+import org.apache.axiom.om.OMNode;
+
+import java.util.Map;
+import java.util.HashMap;
+
+public class DynamicResourceTest extends TestCase {
+
+    private static final String DYNAMIC_ENDPOINT_1 =
+            "<endpoint xmlns=\"http://ws.apache.org/ns/synapse\">\n" +
+            "    <address uri=\"http://localhost:9000/services/SimpleStockQuoteService\"/>\n" +
+            "</endpoint>";
+
+    private static final String DYNAMIC_SEQUENCE_1 =
+            "<sequence xmlns=\"http://ws.apache.org/ns/synapse\" name=\"seq1\">\n" +
+            "    <property name=\"foo\" value=\"bar\" />" +
+            "</sequence>";
+
+    private static final String DYNAMIC_SEQUENCE_2 =
+            "<sequence xmlns=\"http://ws.apache.org/ns/synapse\" name=\"seq1\">\n" +
+            "    <property name=\"foo\" value=\"baz\" />" +
+            "</sequence>";
+
+    private static final String KEY_DYNAMIC_SEQUENCE_1 = "dynamic_sequence_1";
+    private static final String KEY_DYNAMIC_ENDPOINT_1 = "dynamic_endpoint_1";
+
+
+    private SimpleInMemoryRegistry registry;
+    private SynapseConfiguration config;
+
+    public void setUp() {
+        System.out.println("Initializing in-memory registry for dynamic resource tests...");
+
+        Map<String, OMNode> data = new HashMap<String, OMNode>();
+        data.put(KEY_DYNAMIC_ENDPOINT_1, TestUtils.createOMElement(DYNAMIC_ENDPOINT_1));
+        data.put(KEY_DYNAMIC_SEQUENCE_1, TestUtils.createOMElement(DYNAMIC_SEQUENCE_1));
+
+        registry = new SimpleInMemoryRegistry(data, 8000L);
+        config = new SynapseConfiguration();
+        config.setRegistry(registry);
+    }
+
+    public void testDynamicSequenceLookup() throws Exception {
+        System.out.println("Testing dynamic sequence lookup...");
+
+        // Phase 1
+        System.out.println("Testing basic registry lookup functionality...");
+        MessageContext synCtx = TestUtils.createLightweightSynapseMessageContext("<empty/>", config);
+        Mediator seq1 = synCtx.getSequence(KEY_DYNAMIC_SEQUENCE_1);
+        assertNotNull(seq1);
+        assertTrue(((SequenceMediator) seq1).isInitialized());
+        assertEquals(1, registry.getHitCount());
+        seq1.mediate(synCtx);
+        assertEquals("bar", synCtx.getProperty("foo"));
+
+        // Phase 2
+        System.out.println("Testing basic sequence caching...");
+        synCtx = TestUtils.createLightweightSynapseMessageContext("<empty/>", config);
+        Mediator seq2 = synCtx.getSequence(KEY_DYNAMIC_SEQUENCE_1);
+        assertNotNull(seq2);
+        assertTrue(((SequenceMediator) seq2).isInitialized());
+        assertEquals(1, registry.getHitCount());
+        seq2.mediate(synCtx);
+        assertEquals("bar", synCtx.getProperty("foo"));
+        assertTrue(seq1 == seq2);
+
+        // Phase 3
+        System.out.println("Testing advanced sequence caching...");
+        synCtx = TestUtils.createLightweightSynapseMessageContext("<empty/>", config);
+        System.out.println("Waiting for the cache to expire...");
+        Thread.sleep(8500L);
+        Mediator seq3 = synCtx.getSequence(KEY_DYNAMIC_SEQUENCE_1);
+        assertNotNull(seq3);
+        assertTrue(((SequenceMediator) seq3).isInitialized());
+        assertEquals(1, registry.getHitCount());
+        seq3.mediate(synCtx);
+        assertEquals("bar", synCtx.getProperty("foo"));
+        assertTrue(seq1 == seq3);
+
+        // Phase 4
+        System.out.println("Testing sequence reloading...");
+        registry.updateResource(KEY_DYNAMIC_SEQUENCE_1, TestUtils.createOMElement(DYNAMIC_SEQUENCE_2));
+        System.out.println("Waiting for the cache to expire...");
+        Thread.sleep(8500L);
+        synCtx = TestUtils.createLightweightSynapseMessageContext("<empty/>", config);
+        Mediator seq4 = synCtx.getSequence(KEY_DYNAMIC_SEQUENCE_1);
+        assertNotNull(seq4);
+        assertTrue(((SequenceMediator) seq4).isInitialized());
+        assertEquals(2, registry.getHitCount());
+        seq4.mediate(synCtx);
+        assertEquals("baz", synCtx.getProperty("foo"));
+        assertTrue(seq1 != seq4);
+        assertTrue(!((SequenceMediator) seq1).isInitialized());
+
+        System.out.println("Dynamic sequence lookup tests were successful...");
+    }
+}

Added: synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/registry/InMemoryRegistryEntry.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/registry/InMemoryRegistryEntry.java?rev=938007&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/registry/InMemoryRegistryEntry.java (added)
+++ synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/registry/InMemoryRegistryEntry.java Mon Apr 26 11:35:04 2010
@@ -0,0 +1,86 @@
+/*
+ *  Copyright (c) 2005-2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ *  WSO2 Inc. licenses this file to you 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.
+ *
+ */
+
+package org.apache.synapse.registry;
+
+import java.util.Date;
+
+public class InMemoryRegistryEntry implements RegistryEntry {
+
+    private long cachableDuration;
+    private long createdDate;
+    private long lastModifiedDate;
+    private String key;
+    private Object value;
+
+    public InMemoryRegistryEntry(String key) {
+        this.key = key;
+        this.createdDate = new Date().getTime();
+        this.lastModifiedDate = createdDate;
+    }
+
+    public void setCachableDuration(long cachableDuration) {
+        this.cachableDuration = cachableDuration;
+    }
+
+    public void setLastModifiedDate(long lastModifiedDate) {
+        this.lastModifiedDate = lastModifiedDate;
+    }
+
+    public long getCachableDuration() {
+        return cachableDuration;
+    }
+
+    public long getCreated() {
+        return createdDate;
+    }
+
+    public String getDescription() {
+        return "Resource at: " + key;
+    }
+
+    public String getKey() {
+        return key;
+    }
+
+    public long getLastModified() {
+        return lastModifiedDate;
+    }
+
+    public String getName() {
+        return key;
+    }
+
+    public String getType() {
+        return "text/xml";
+    }
+
+    public long getVersion() {
+        return lastModifiedDate;
+    }
+
+    public Object getValue() {
+        return value;
+    }
+
+    public void setValue(Object value) {
+        this.value = value;
+        this.lastModifiedDate = new Date().getTime();
+    }
+}

Added: synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/registry/SimpleInMemoryRegistry.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/registry/SimpleInMemoryRegistry.java?rev=938007&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/registry/SimpleInMemoryRegistry.java (added)
+++ synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/registry/SimpleInMemoryRegistry.java Mon Apr 26 11:35:04 2010
@@ -0,0 +1,88 @@
+/*
+ *  Copyright (c) 2005-2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ *  WSO2 Inc. licenses this file to you 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.
+ *
+ */
+
+package org.apache.synapse.registry;
+
+import org.apache.axiom.om.OMNode;
+
+import java.util.Map;
+import java.util.HashMap;
+
+public class SimpleInMemoryRegistry extends AbstractRegistry {    
+
+    private Map<String,InMemoryRegistryEntry> registry = new HashMap<String,InMemoryRegistryEntry>();
+    private long cacheDuration;
+    private int hitCount;
+
+    public SimpleInMemoryRegistry(Map<String,OMNode> data, long cacheDuration) {
+        this.cacheDuration = cacheDuration;
+        this.hitCount = 0;
+
+        for (String key : data.keySet()) {
+            InMemoryRegistryEntry entry = new InMemoryRegistryEntry(key);
+            entry.setValue(data.get(key));
+            entry.setCachableDuration(cacheDuration);
+            registry.put(key, entry);
+        }
+    }
+
+    public OMNode lookup(String key) {
+        hitCount++;
+        InMemoryRegistryEntry entry = registry.get(key);
+        if (entry != null) {
+            return (OMNode) entry.getValue();
+        }
+        return null;
+    }
+
+    public RegistryEntry getRegistryEntry(String key) {
+        return registry.get(key);
+    }
+
+    public RegistryEntry[] getChildren(RegistryEntry entry) {
+        return null;
+    }
+
+    public RegistryEntry[] getDescendants(RegistryEntry entry) {
+        return null;
+    }
+
+    public void delete(String key) {
+        registry.remove(key);
+    }
+
+    public void newResource(String key, boolean isDirectory) {
+        registry.put(key, new InMemoryRegistryEntry(key));
+    }
+
+    public void updateResource(String key, Object value) {
+        InMemoryRegistryEntry entry = registry.get(key);
+        if (entry != null) {
+            entry.setValue(value);
+        }
+    }
+
+    public void updateRegistryEntry(RegistryEntry entry) {
+
+    }
+
+    public int getHitCount() {
+        return hitCount;
+    }
+}