[Registry-dev] svn commit r6790 - in trunk/registry/java/modules/core/src: main/java/org/wso2/registry/inmemory test/java/org/wso2/registry/engine test/java/org/wso2/registry/inmemory

svn at wso2.org svn at wso2.org
Fri Aug 31 02:56:02 PDT 2007


Author: chathura
Date: Fri Aug 31 02:55:34 2007
New Revision: 6790

Added:
   trunk/registry/java/modules/core/src/main/java/org/wso2/registry/inmemory/ResourceMap.java
   trunk/registry/java/modules/core/src/test/java/org/wso2/registry/engine/RegistryTest.java
      - copied, changed from r6784, trunk/registry/java/modules/core/src/test/java/org/wso2/registry/engine/RegistryEngineTest.java
   trunk/registry/java/modules/core/src/test/java/org/wso2/registry/inmemory/InMemoryDataStoreTest.java
Removed:
   trunk/registry/java/modules/core/src/test/java/org/wso2/registry/engine/RegistryEngineTest.java
   trunk/registry/java/modules/core/src/test/java/org/wso2/registry/inmemory/InMemoryRegistryProviderTest.java
Modified:
   trunk/registry/java/modules/core/src/main/java/org/wso2/registry/inmemory/InMemoryDataStore.java
Log:

Cleaned up the InMemoryData store.
Modified the test cases according to the new code.



Modified: trunk/registry/java/modules/core/src/main/java/org/wso2/registry/inmemory/InMemoryDataStore.java
==============================================================================
--- trunk/registry/java/modules/core/src/main/java/org/wso2/registry/inmemory/InMemoryDataStore.java	(original)
+++ trunk/registry/java/modules/core/src/main/java/org/wso2/registry/inmemory/InMemoryDataStore.java	Fri Aug 31 02:55:34 2007
@@ -95,7 +95,9 @@
             // exists create directories for them. If there is a non-directory resource
             // corresponding to any of those parts, we can't add the resource.
             String currentPath = RegistryConstants.ROOT_PATH;
-            CollectionContent currentCollectionContent = (CollectionContent) resourceMap.get(RegistryConstants.ROOT_PATH);
+            Resource currentResource =
+                    (Resource) resourceMap.get(RegistryConstants.ROOT_PATH);
+
             for (int i = 1; i < parts.length - 1; i++) {
 
                 // check if a Directory with current Path is already in the resourceMap
@@ -112,13 +114,17 @@
                     directoryResource.setContent(new CollectionContent());
 
                     resourceMap.put(currentPath, directoryResource);
-                    if (currentCollectionContent != null) {
-                        currentCollectionContent.addChildName(parts[i]);
+                    if (currentResource != null) {
+                        CollectionContent content =
+                                (CollectionContent) currentResource.getContent();
+                        content.addResourcePath(currentPath);
                     }
-                    currentCollectionContent = dr;
+                    currentResource = directoryResource;
+
                 } else {
-                    if (r instanceof CollectionContent) {
-                        currentCollectionContent = (CollectionContent) r;
+                    Object o = r.getContent();
+                    if (o instanceof CollectionContent) {
+                        currentResource = r;
                     } else {
                         String message =
                                 Messages.getMessage("resource.delete.error", currentPath, path);
@@ -130,7 +136,9 @@
             // now we have the parent directory for the new resource. add the new resource to
             // the resourceMap and make it a child of the parent directory
             resourceMap.put(path, resource);
-            currentCollectionContent.addChildName(parts[parts.length - 1]);
+            CollectionContent currentColeCollectionContent =
+                    (CollectionContent) currentResource.getContent();
+            currentColeCollectionContent.addResourcePath(path);
         } else {
             // new resource is a top level resource. just add it to the resourceMap
             resourceMap.put(path, resource);
@@ -158,14 +166,15 @@
             throw new RegistryException(message);
         }
 
-        if (resource instanceof CollectionContent) {
+        Object o = resource.getContent();
+        if (o instanceof CollectionContent) {
 
             // remove all descendent resources
-            CollectionContent collectionContent = (CollectionContent) resource;
-            removeDescendents(collectionContent);
+            CollectionContent collectionContent = (CollectionContent) o;
+            removeDescendents(resource);
 
             // remove the directory
-            removeResource(collectionContent.getPath());
+            removeResource(resource.getPath());
 
         } else {
             removeResource(resource.getPath());
@@ -213,25 +222,26 @@
     /**
      * Removes descendents (child directories and resources) from the given directory.
      *
-     * @param collectionContent Directory from which the descendents should be removed.
+     * @param resource Resource object with a CollectionContent
      */
-    private void removeDescendents(CollectionContent collectionContent) {
+    private void removeDescendents(Resource resource) {
 
-        String[] childNames = collectionContent.getResourcePaths();
+        CollectionContent content = (CollectionContent) resource.getContent();
+        String[] childPaths = content.getResourcePaths();
 
-        for (int i = 0; i < childNames.length; i++) {
-            String childPath = collectionContent.getPath() + childNames[i];
+        for (int i = 0; i < childPaths.length; i++) {
+            String childPath = childPaths[i];
 
-            Resource resource = (Resource) resourceMap.get(childPath);
+            Resource childResource = (Resource) resourceMap.get(childPath);
 
-            if (resource instanceof CollectionContent) {
+            Object o = childResource.getContent();
+            if (o instanceof CollectionContent) {
 
-                CollectionContent d = (CollectionContent) resource;
-                removeDescendents(d);
-                removeResource(d.getPath());
+                removeDescendents(childResource);
+                removeResource(childResource.getPath());
 
             } else {
-                removeResource(resource.getPath());
+                removeResource(childResource.getPath());
             }
         }
     }

Added: trunk/registry/java/modules/core/src/main/java/org/wso2/registry/inmemory/ResourceMap.java
==============================================================================
--- (empty file)
+++ trunk/registry/java/modules/core/src/main/java/org/wso2/registry/inmemory/ResourceMap.java	Fri Aug 31 02:55:34 2007
@@ -0,0 +1,42 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF 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.wso2.registry.inmemory;
+
+import org.wso2.registry.RegistryConstants;
+
+import java.util.HashMap;
+
+public class ResourceMap extends HashMap {
+
+    public Object put(Object key, Object value) {
+
+        preparePath((String)key);
+
+        return super.put(key, value);
+    }
+
+    private void preparePath(String path) {
+
+        // make sure that the path does not end with a "/"
+        if (path.endsWith(RegistryConstants.PATH_SEPARATOR)) {
+            path = path.substring(0, path.length() - RegistryConstants.PATH_SEPARATOR.length());
+        }
+    }
+}

Copied: trunk/registry/java/modules/core/src/test/java/org/wso2/registry/engine/RegistryTest.java (from r6784, trunk/registry/java/modules/core/src/test/java/org/wso2/registry/engine/RegistryEngineTest.java)
==============================================================================
--- trunk/registry/java/modules/core/src/test/java/org/wso2/registry/engine/RegistryEngineTest.java	(original)
+++ trunk/registry/java/modules/core/src/test/java/org/wso2/registry/engine/RegistryTest.java	Fri Aug 31 02:55:34 2007
@@ -20,25 +20,29 @@
 package org.wso2.registry.engine;
 
 import junit.framework.TestCase;
+import org.wso2.registry.DataStore;
+import org.wso2.registry.Registry;
+import org.wso2.registry.RegistryException;
+import org.wso2.registry.resources.Resource;
+import org.wso2.registry.inmemory.InMemoryDataStore;
 
-public class RegistryEngineTest extends TestCase {
+public class RegistryTest extends TestCase {
 
-    public void testRegistryEngineWithInMemoryRegistry() {
+    public void testRegistryWithInMemoryDataStore() {
 
-        //RegistryProvider dataStore = new InMemoryRegistryProvider();
-        //RegistryEngine2 registryEngine2 = new RegistryEngine2(dataStore);
-        //RegistrySessionContext registrySessionContext = new RegistrySessionContext();
-        //
-        //// add a resource
-        //
-        //Resource r1 = new Resource();
-        //r1.setAuthor("Author R1");
-        //r1.setContent("R1 content");
-        //
-        //try {
-        //    registryEngine2.put("/d1/r1", r1, registrySessionContext);
-        //} catch (RegistryException e) {
-        //    fail("Valid pur resource scenario failed.");
-        //}
+        DataStore dataStore = new InMemoryDataStore();
+        Registry registry = new Registry(dataStore);
+
+        // add a resource
+
+        Resource r1 = new Resource();
+        r1.setAuthor("Author R1");
+        r1.setContent("R1 content");
+        
+        try {
+            registry.put("/d1/r1", r1);
+        } catch (RegistryException e) {
+            fail("Valid pur resource scenario failed.");
+        }
     }
 }

Added: trunk/registry/java/modules/core/src/test/java/org/wso2/registry/inmemory/InMemoryDataStoreTest.java
==============================================================================
--- (empty file)
+++ trunk/registry/java/modules/core/src/test/java/org/wso2/registry/inmemory/InMemoryDataStoreTest.java	Fri Aug 31 02:55:34 2007
@@ -0,0 +1,234 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF 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.wso2.registry.inmemory;
+
+import junit.framework.TestCase;
+import org.wso2.registry.RegistryException;
+import org.wso2.registry.ResourceNotFoundException;
+import org.wso2.registry.DataStore;
+import org.wso2.registry.comments.Comment;
+import org.wso2.registry.resources.Resource;
+import org.wso2.registry.resources.CollectionContent;
+
+public class InMemoryDataStoreTest extends TestCase {
+
+    public void testRegistry() throws RegistryException {
+
+        // create an in memory registry
+        DataStore registry = new InMemoryDataStore();
+
+        // create a content resource
+        Resource resource1 = new Resource();
+        resource1.setAuthor("Chathura");
+        String content1 = "This is the content2.";
+        resource1.setContent(content1);
+
+        // store it in a non existence path
+        String path1 = "/d1/d2/myresource";
+        registry.put(path1, resource1);
+
+        // store another content resource in the same path
+        Resource resource2 = new Resource();
+        resource2.setAuthor("Asankha");
+        String content2 = "This is content2";
+        resource2.setContent(content2);
+
+        String path2 = "/d1/d2/secondresource";
+        registry.put(path2, resource2);
+
+        // store the third resource in a path starting from previous path
+        Resource resource3 = new Resource();
+        resource3.setAuthor("Ruwan");
+        String content3 = "This is content3";
+        resource3.setContent(content3);
+
+        String path3 = "/d1/d2/d3/thirdresource";
+        registry.put(path3, resource3);
+
+        // now we should have a directory /d1
+        Resource d1 = registry.get("/d1");
+        assertNotNull("/d1 does not exists", d1);
+        assertTrue("/d1 is not a directory", d1.getContent() instanceof CollectionContent);
+
+        // there should be a directory /d1/d2
+        Resource d2 = registry.get("/d1/d2");
+        assertNotNull("/d1/d2 does not exists", d2);
+        assertTrue("/d1/d2 is not a directory", d2.getContent() instanceof CollectionContent);
+
+        // there should be a directory /d1/d2/d3
+        Resource d3 = registry.get("/d1/d2/d3");
+        assertNotNull("/d1/d2/d3 does not exists", d3);
+        assertTrue("/d1/d2/d3 is not a directory", d3.getContent() instanceof CollectionContent);
+
+        // there should be a content resource /d1/d2/myresource
+        Resource r1 = registry.get("/d1/d2/myresource");
+        assertNotNull("/d1/d2/myresource does not exists", r1);
+        assertTrue("/d1/d2/myresource is not a Resource", r1 instanceof Resource);
+
+        // there should be a content resource /d1/d2/secondresource
+        Resource r2 = registry.get("/d1/d2/secondresource");
+        assertNotNull("/d1/d2/secondresource does not exists", r2);
+        assertTrue("/d1/d2/secondresource is not a Resource", r2 instanceof Resource);
+
+        // there should be a content resource /d1/d2/d3/thirdresource
+        Resource r3 = registry.get("/d1/d2/d3/thirdresource");
+        assertNotNull("/d1/d2/d3/thirdresource does not exists", r3);
+        assertTrue(
+                "/d1/d2/d3/thirdresource is not a Resource", r3 instanceof Resource);
+        
+
+        // let's delete "/d1/d2"
+        registry.delete("/d1/d2");
+
+        boolean gotError = false;
+        try {
+            Resource dd1 = registry.get("/d1/d2");
+        } catch (ResourceNotFoundException e) {
+            gotError = true;
+        }
+
+        if (!gotError) {
+            fail("Deleted resource /d1/d2 still exists.");
+        }
+
+    }
+
+    public void testComments() {
+
+        DataStore dataStore = new InMemoryDataStore();
+
+        String resource1Path = "/d1/testresource";
+        Resource resource = new Resource();
+        resource.setAuthor("Chathura");
+        resource.setDescription("This is a test resource.");
+
+        try {
+            dataStore.put(resource1Path, resource);
+        } catch (RegistryException e) {
+            fail("Valid put resource scenario failed.");
+        }
+
+        Comment comment1 = new Comment();
+        comment1.setCommentText("This resource looks like a test resource.");
+
+        try {
+            dataStore.addComment(resource1Path, comment1);
+        } catch (RegistryException e) {
+            e.printStackTrace();
+            fail("Exception occured while adding a valid comment.");
+        }
+
+        Comment comment2 = new Comment();
+        comment2.setCommentText("I don't think this is a production resource.");
+
+        try {
+            dataStore.addComment(resource1Path, comment2);
+        } catch (RegistryException e) {
+            e.printStackTrace();
+            fail("Exception occured while adding a valid comment.");
+        }
+
+        Comment[] comments1 = dataStore.getComments(resource1Path);
+        if (!(comments1[0].getCommentText().equals("This resource looks like a test resource.") ||
+                comments1[0].getCommentText().equals("I don't think this is a production resource."))) {
+            fail("Comments are not added properly.");
+        }
+
+        if (!(comments1[1].getCommentText().equals("This resource looks like a test resource.") ||
+                comments1[1].getCommentText().equals("I don't think this is a production resource."))) {
+            fail("Comments are not added properly.");
+        }
+    }
+
+    public void testTags() {
+
+        DataStore dataStore = new InMemoryDataStore();
+
+        String resource1Path = "/d1/testresource";
+        Resource resource1 = new Resource();
+        resource1.setAuthor("Chathura");
+        resource1.setDescription("This is a test resource.");
+
+        try {
+            dataStore.put(resource1Path, resource1);
+        } catch (RegistryException e) {
+            fail("Valid put resource scenario failed.");
+        }
+
+        String resource2Path = "/d2/ferarri";
+        Resource resource2 = new Resource();
+        resource2.setAuthor("Chathura");
+        resource2.setDescription("Ferarri Enzo, best in Ferarri family.");
+
+        try {
+            dataStore.put(resource2Path, resource2);
+        } catch (RegistryException e) {
+            fail("Valid put resource scenario failed.");
+        }
+
+        try {
+            dataStore.applyTag(resource1Path, "project1");
+            dataStore.applyTag(resource1Path, "requirements");
+            dataStore.applyTag(resource1Path, "favarite");
+            dataStore.applyTag(resource2Path, "super car");
+            dataStore.applyTag(resource2Path, "red car");
+            dataStore.applyTag(resource2Path, "favarite");
+        } catch (RegistryException e) {
+            e.printStackTrace();
+            fail("Valid add tag scenario failed. " + e.getMessage());
+        }
+
+        String[] resources1 = dataStore.getResourcePathsWithTag("project1");
+        assertEquals("tags not added properly", resources1[0], resource1Path);
+
+        String[] tags1 = new String[0];
+        try {
+            tags1 = dataStore.getTags(resource2Path);
+        } catch (RegistryException e) {
+            fail("Valid get tags scenario failed. " + e.getMessage());
+        }
+        assertEquals("tags not added properly", tags1.length, 3);
+    }
+
+    public void testRatings() {
+
+        DataStore dataStore = new InMemoryDataStore();
+
+        String resource1Path = "/d1/testresource";
+        Resource resource1 = new Resource();
+        resource1.setAuthor("Chathura");
+        resource1.setDescription("This is a test resource.");
+
+        try {
+            dataStore.put(resource1Path, resource1);
+        } catch (RegistryException e) {
+            fail("Valid put resource scenario failed.");
+        }
+
+        try {
+            dataStore.rateResource(resource1Path, 2);
+            dataStore.rateResource(resource1Path, 4);
+        } catch (RegistryException e) {
+            fail("Valid rating scenario failed." + e.getMessage());
+        }
+
+        assertEquals("Average rating should be 3", dataStore.getAverageRating(resource1Path), 3);
+    }
+}



More information about the Registry-dev mailing list