Package org.wso2.carbon.registry.extensions.test.jdbc

Source Code of org.wso2.carbon.registry.extensions.test.jdbc.DefaultLifecycleTest

/*
* 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.carbon.registry.extensions.test.jdbc;

import junit.framework.Assert;
import org.wso2.carbon.registry.core.internal.RegistryCoreServiceComponent;
import org.wso2.carbon.registry.core.jdbc.EmbeddedRegistryService;
import org.wso2.carbon.registry.extensions.aspects.DefaultLifecycle;
import org.wso2.carbon.registry.extensions.test.utils.BaseTestCase;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.*;

import java.util.ArrayList;
import java.util.List;

public class DefaultLifecycleTest extends BaseTestCase {

    // todo: aspect
    /**
     * Registry instance for use in tests. Note that there should be only one Registry instance in a
     * JVM.
     */
    protected static Registry registry = null;
    public final String LIFECYCLE_NAME = "DefaultLifecycle";

    public void setUp() throws RegistryException {
        super.setUp();
        ctx.addAspect(LIFECYCLE_NAME, new DefaultLifecycle(), 0);
        EmbeddedRegistryService embeddedRegistry = ctx.getEmbeddedRegistryService();
        new RegistryCoreServiceComponent().registerBuiltInHandlers(embeddedRegistry);
        registry = embeddedRegistry.getRegistry("admin", "admin");
    }

    public void testLifecycle() throws RegistryException {

        Resource r1 = registry.newResource();
        byte[] r1content = "R1 content".getBytes();
        r1.setContent(r1content);
        registry.put("/d12/r1", r1);

        String text1 = "this can be used as a test resource.";
        String text2 = "I like this";
        final Comment comment1 = new Comment(text1);
        comment1.setUser("someone");
        registry.addComment("/d12/r1", comment1);
        final Comment comment2 = new Comment(text2);
        comment2.setUser("someone");
        registry.addComment("/d12/r1", comment2);

        Comment[] comments = registry.getComments("/d12/r1");
        Assert.assertNotNull(registry.get("/d12/r1").getContent());

        boolean commentFound = false;
        for (Comment comment : comments) {
            if (comment.getText().equals(text1)) {
                commentFound = true;
                break;
            }
        }
        Assert.assertTrue("comment '" + text1 +
                "' is not associated with the artifact /d12/r1", commentFound);

        Resource commentsResource = registry.get("/d12/r1;comments");
        Assert.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.getPath());
            commentTexts.add(commentResource.getContent());
        }

        Assert.assertTrue(text1 + " is not associated for resource /d12/r1.",
                commentTexts.contains(text1));
        Assert.assertTrue(text2 + " is not associated for resource /d12/r1.",
                commentTexts.contains(text2));

        registry.associateAspect("/d12/r1", LIFECYCLE_NAME);

        registry.invokeAspect("/d12/r1", LIFECYCLE_NAME, "promote");

        Resource resource = registry.get("/developed/d12/r1");
        Assert.assertNotNull(resource);
        Assert.assertNotNull(resource.getContent());

        comments = registry.getComments("/developed/d12/r1");

        commentFound = false;
        for (Comment comment : comments) {
            if (comment.getText().equals(text1)) {
                commentFound = true;
                break;
            }
        }
        Assert.assertTrue("comment '" + text1 +
                "' is not associated with the artifact /developed/d12/r1", commentFound);

        commentsResource = registry.get("/developed/d12/r1;comments");
        Assert.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.getPath());
            commentTexts.add(commentResource.getContent());
        }

        Assert.assertTrue(text1 + " is not associated for resource /developed/d12/r1.",
                commentTexts.contains(text1));
        Assert.assertTrue(text2 + " is not associated for resource /developed/d12/r1.",
                commentTexts.contains(text2));

    }
}
TOP

Related Classes of org.wso2.carbon.registry.extensions.test.jdbc.DefaultLifecycleTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.