[Registry-dev] svn commit r14811 - in trunk/registry/modules/core/src: main/java/org/wso2/registry/config main/java/org/wso2/registry/jdbc main/java/org/wso2/registry/jdbc/dao main/java/org/wso2/registry/lifecycle main/java/org/wso2/registry/secure main/java/org/wso2/registry/servlet test/java/org/wso2/registry/jdbc

svn at wso2.org svn at wso2.org
Fri Mar 14 03:44:54 PDT 2008


Author: deepal
Date: Fri Mar 14 03:44:36 2008
New Revision: 14811

Log:

adding a life cycle handling test case

Added:
   trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/LifeCycleTest.java
Modified:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/config/RegistryConfigurationProcessor.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/InMemoryJDBCRegistry.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/lifecycle/DefaultLifecycle.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/registry.xml

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/config/RegistryConfigurationProcessor.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/config/RegistryConfigurationProcessor.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/config/RegistryConfigurationProcessor.java	Fri Mar 14 03:44:36 2008
@@ -185,12 +185,11 @@
                     registryContext.addQueryProcessor(queryProcessorConfiguration);
                 }
                 Iterator lifecycleElement = configElement.
-                        getChildrenWithName(new QName("lifecycle "));
+                        getChildrenWithName(new QName("lifecycle"));
                 if (lifecycleElement != null) {
                     while (lifecycleElement.hasNext()) {
                         OMElement lifecycle = (OMElement) lifecycleElement.next();
                         String name = lifecycle.getAttributeValue(new QName("name"));
-                        String order = lifecycle.getAttributeValue(new QName("order"));
                         String clazz = lifecycle.getAttributeValue(new QName("class"));
                         try {
                             Class handlerClass = Class.forName(clazz);
@@ -200,7 +199,7 @@
                         } catch (Exception e) {
                             throw new RegistryException("Unable to load the class : " + clazz) ;
                         }
-                        if (name == null || order == null || clazz == null) {
+                        if (name == null || clazz == null) {
                             throw new RegistryException("Invalid lifecycle elemet , required " +
                                     "values are missing " + lifecycle.toString());
                         }

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/InMemoryJDBCRegistry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/InMemoryJDBCRegistry.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/InMemoryJDBCRegistry.java	Fri Mar 14 03:44:36 2008
@@ -17,6 +17,8 @@
 package org.wso2.registry.jdbc;
 
 import org.wso2.registry.RegistryException;
+import org.wso2.registry.RegistryConstants;
+import org.wso2.registry.config.RegistryContext;
 import org.wso2.registry.i18n.Messages;
 import org.wso2.registry.jdbc.hsql.DBUtils;
 import org.wso2.registry.jdbc.realm.RegistryRealm;
@@ -69,7 +71,11 @@
                                                                INMEMORY_DB_PASSWORD);
 
         startInProcessDatabase(dataSource);
-
+        RegistryContext registryContext = (RegistryContext)System.
+                getProperties().get(RegistryConstants.REGISTRY_CONTEXT);
+        if (registryContext !=null) {
+            registryContext.setDataSource(dataSource);
+        }
         super.configure(dataSource, registryRealm);
     }
 

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java	Fri Mar 14 03:44:36 2008
@@ -23,6 +23,7 @@
 import org.apache.commons.logging.LogFactory;
 import org.wso2.registry.Collection;
 import org.wso2.registry.*;
+import org.wso2.registry.config.RegistryContext;
 import org.wso2.registry.exceptions.ResourceNotFoundException;
 import org.wso2.registry.i18n.Messages;
 import org.wso2.registry.jdbc.dao.*;
@@ -1368,8 +1369,17 @@
 
     private Map<String, Lifecycle> lifecycles = new HashMap<String, Lifecycle>();
 
-    private Lifecycle getLifecycle(String name) {
-        return lifecycles.get(name);
+    private Lifecycle getLifecycle(String name) throws RegistryException {
+        Lifecycle lifecycle = lifecycles.get(name);
+        if (lifecycle == null) {
+            RegistryContext registryContext = (RegistryContext)System.
+                    getProperties().get(RegistryConstants.REGISTRY_CONTEXT);
+            lifecycle = registryContext.getLifecycle(name);
+        }
+        if (lifecycle == null) {
+            throw new RegistryException(" No lifecycle object found for the name :"  + name);
+        }
+        return lifecycle;
     }
 
     public void addLifecycle(String name, Lifecycle lifecycle) {

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java	Fri Mar 14 03:44:36 2008
@@ -979,7 +979,6 @@
      *
      * @param oldPath : Current path of the resource
      * @param newPath : Where to move the resource
-     * @param conn    : Connection to DB
      * @param userId  : current user
      * @param realm   : Realm
      * @throws org.wso2.registry.RegistryException
@@ -1007,25 +1006,28 @@
             if (resource.getAuthorUserName() == null) {
                 resource.setAuthorUserName(userId);
             }
+            InputStream in = resource.getContentStream();
             resource.setLastUpdaterUserName(userId);
             add(resource, conn);
             AuthorizationUtil.setDefaultAuthorizations(realm, newPath, userId);
             long newResourceID = getResourceID(newPath, conn);
             resource.setId(newResourceID);
+
+            resource.setContentStream(in);
             addResourceVersion(resource, conn);
             resource.setPath(resourcePath);
 
             long fromResourceId = getResourceID(oldPath, conn);
             //copying the comments
             CommentsDAO commentsDAO = new CommentsDAO();
-            commentsDAO.copyComments(oldPath, resourceID, conn);
+            commentsDAO.copyComments(oldPath, newResourceID, conn);
             //coping the tags
             TagsDAO tags = new TagsDAO(dataSource);
-            tags.copyTags(fromResourceId, resourceID, conn);
+            tags.copyTags(fromResourceId, newResourceID, conn);
 
             // copy ratings
             RatingsDAO ratingsDAO = new RatingsDAO();
-            ratingsDAO.copyRatings(fromResourceId, resourceID, conn);
+            ratingsDAO.copyRatings(fromResourceId, newResourceID, conn);
 
         }
     }
@@ -1142,7 +1144,9 @@
                     copyResource(childNode, currentPath, registry, false, conn);
                 }
             }
+            InputStream in = resource.getContentStream();
             resource.setPath(currentPath);
+            resource.setContentStream(in);
             registry.put(currentPath, resource);
             try {
                 long resourceId = getResourceID(currentPath, conn);

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/lifecycle/DefaultLifecycle.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/lifecycle/DefaultLifecycle.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/lifecycle/DefaultLifecycle.java	Fri Mar 14 03:44:36 2008
@@ -34,6 +34,7 @@
     public static final String PHASE_DEVELOPED = "developed";
     public static final String PHASE_TESTED = "tested";
     public static final String PHASE_DEPLOYED = "deployed";
+    private static final String ORIGINAL_PATH = "originalPath";
 
     public static final String[] phases = new String[]{
             PHASE_CREATED, PHASE_DEVELOPED, PHASE_TESTED, PHASE_DEPLOYED
@@ -65,6 +66,12 @@
                                         "'.  Only valid action is '" + ACTION + "'");
         }
         Resource resource = context.getResource();
+        String originalPath = resource.getProperty(ORIGINAL_PATH);
+        if (originalPath == null ) {
+            resource.setProperty(ORIGINAL_PATH , resource.getPath());
+            originalPath = resource.getPath();
+        }
+
         String currentPhase = resource.getProperty(PHASE_PROPERTY);
         if (currentPhase == null)
             throw new RegistryException("DefaultLifecycle: Resource '" + resource.getPath() +
@@ -90,7 +97,9 @@
         }
 
         String nextPhase = phases[i + 1];
-        String newResourcePath = "/" + nextPhase + "/" + resource.getPath();
+        resource.setProperty(PHASE_PROPERTY ,nextPhase);
+        String newResourcePath = "/" + nextPhase + originalPath;
+
 
         // There has got to be a better way of doing this.  Do we really need to be messing
         // around with VersionedResourceDAO and realms in here?
@@ -108,6 +117,7 @@
                                                      realm,
                                                      context.getRegistry());
         } catch (Exception e) {
+            e.printStackTrace();
             throw new RegistryException("Error while coping the resource");
         }
 

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java	Fri Mar 14 03:44:36 2008
@@ -756,11 +756,11 @@
     }
 
      public void associateLifeCycle(String resourcePath, String lifecycle) throws RegistryException {
-        throw new UnsupportedOperationException();
+        registry.associateLifeCycle(resourcePath , lifecycle);
     }
 
     public void lifeCycleTransition(String resourcePath, String lifecycleName, String action) throws RegistryException {
-        throw new UnsupportedOperationException();
+        registry.lifeCycleTransition(resourcePath ,lifecycleName , action );
     }
 
     public String[] getLifecycleActions(String resourcePath, String lifecycleName)

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java	Fri Mar 14 03:44:36 2008
@@ -77,7 +77,6 @@
                         }
                     });
             System.getProperties().put(RegistryConstants.REGISTRY_CONTEXT, registryContext);
-
             if (configPath != null) {
                 RegistryConfigurationProcessor.populateRegistryConfig(configPath, registryContext);
             } else {
@@ -121,6 +120,7 @@
                 registryRealm = new RegistryRealm(dataSource);
                 coreRegistry = new JDBCRegistry(dataSource, registryRealm);
 
+
                 // add configured handers to the jdbc registry
                 Iterator<HandlerConfiguration> handlers =
                         registryContext.getHandlerConfigurations().iterator();

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/registry.xml
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/registry.xml	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/registry.xml	Fri Mar 14 03:44:36 2008
@@ -55,10 +55,10 @@
         </filter>
     </handler>
 
-    <lifecycle name="Created" order="1" class="org.wso2.registry.lifecycle.DefaultLifecycle"/>
-    <lifecycle name="Developed" order="2" class="org.wso2.registry.lifecycle.DefaultLifecycle"/>
-    <lifecycle name="Tested" order="3" class="org.wso2.registry.lifecycle.DefaultLifecycle"/>
-    <lifecycle name="Deployed" order="4" class="org.wso2.registry.lifecycle.DefaultLifecycle"/>
+    <lifecycle name="created" class="org.wso2.registry.lifecycle.DefaultLifecycle"/>
+    <lifecycle name="developed" class="org.wso2.registry.lifecycle.DefaultLifecycle"/>
+    <lifecycle name="tested" class="org.wso2.registry.lifecycle.DefaultLifecycle"/>
+    <lifecycle name="deployed" class="org.wso2.registry.lifecycle.DefaultLifecycle"/>
 
     <!--<mediaTypeHandler>-->
         <!--<mediaType>application/vnd.apache.synapse</mediaType>-->

Added: trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/LifeCycleTest.java
==============================================================================
--- (empty file)
+++ trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/LifeCycleTest.java	Fri Mar 14 03:44:36 2008
@@ -0,0 +1,128 @@
+/*
+ * Copyright (c) 2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * 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.
+ */
+
+package org.wso2.registry.jdbc;
+
+import junit.framework.TestCase;
+import org.wso2.registry.*;
+import org.wso2.registry.app.RegistryServer;
+import org.wso2.registry.lifecycle.DefaultLifecycle;
+import org.wso2.registry.jdbc.realm.InMemoryRegistryRealm;
+import org.wso2.registry.jdbc.realm.RegistryRealm;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class LifeCycleTest extends TestCase {
+    /**
+     * Registry instance for use in tests. Note that there should be only one Registry instance in a
+     * JVM.
+     */
+    protected static Registry registry = null;
+    RegistryServer server = new RegistryServer();
+
+    public void setUp() {
+        try {
+            if (registry == null) {
+                server.start();
+                 System.setProperty("in-memory" ,"false");
+                registry = (Registry)System.getProperties().get(RegistryConstants.REGISTRY);
+            }
+        } catch (Exception e) {
+            fail("Failed to initialize the registry.");
+        }
+    }
+
+    public void testLifecycle() throws RegistryException {
+
+        Resource r1 = registry.newResource();
+        byte[] r1content = "R1 content".getBytes();
+        r1.setContent(r1content);
+        registry.put("/d12/r1", r1);
+
+        String comment1 = "this can be used as a test resource.";
+        String comment2 = "I like this";
+        registry.addComment("/d12/r1", new Comment(comment1));
+        registry.addComment("/d12/r1", new Comment(comment2));
+
+        Comment[] comments = registry.getComments("/d12/r1");
+        assertNotNull(registry.get("/d12/r1").getContent());
+
+        boolean commentFound = false;
+        for (Comment comment : comments) {
+            if (comment.getText().equals(comment1)) {
+                commentFound = true;
+                break;
+            }
+        }
+        assertTrue("comment '" + comment1 +
+                "' is not associated with the artifact /d12/r1", commentFound);
+
+        Resource commentsResource = registry.get("/d12/r1;comments");
+        assertTrue("Comment collection resource should be a directory.",
+                commentsResource instanceof Collection);
+        comments = (Comment[]) commentsResource.getContent();
+
+        List commentTexts = new ArrayList();
+        for (Comment comment : comments) {
+            Resource commentResource = registry.get(comment.getCommentPath());
+            commentTexts.add(commentResource.getContent());
+        }
+
+        assertTrue(comment1 + " is not associated for resource /d12/r1.",
+                commentTexts.contains(comment1));
+        assertTrue(comment2 + " is not associated for resource /d12/r1.",
+                commentTexts.contains(comment2));
+
+        registry.associateLifeCycle("/d12/r1" , DefaultLifecycle.PHASE_CREATED);
+        registry.associateLifeCycle("/d12/r1" , DefaultLifecycle.PHASE_TESTED);
+        registry.associateLifeCycle("/d12/r1" , DefaultLifecycle.PHASE_DEVELOPED);
+
+        registry.lifeCycleTransition("/d12/r1" ,"created" ,"promote");
+        Resource resource = registry.get("/developed/d12/r1");
+        assertNotNull(resource);
+        assertNotNull(resource.getContent());
+
+        comments = registry.getComments("/developed/d12/r1");
+
+        commentFound = false;
+        for (Comment comment : comments) {
+            if (comment.getText().equals(comment1)) {
+                commentFound = true;
+                break;
+            }
+        }
+        assertTrue("comment '" + comment1 +
+                "' is not associated with the artifact /developed/d12/r1", commentFound);
+
+        commentsResource = registry.get("/developed/d12/r1;comments");
+        assertTrue("Comment collection resource should be a directory.",
+                commentsResource instanceof Collection);
+        comments = (Comment[]) commentsResource.getContent();
+
+        commentTexts = new ArrayList();
+        for (Comment comment : comments) {
+            Resource commentResource = registry.get(comment.getCommentPath());
+            commentTexts.add(commentResource.getContent());
+        }
+
+        assertTrue(comment1 + " is not associated for resource /developed/d12/r1.",
+                commentTexts.contains(comment1));
+        assertTrue(comment2 + " is not associated for resource /developed/d12/r1.",
+                commentTexts.contains(comment2));
+
+    }
+}



More information about the Registry-dev mailing list