Package org.cafesip.jiplet.console.server.model.test

Source Code of org.cafesip.jiplet.console.server.model.test.ContainerMgmtTest

/*
* Created on Dec 16, 2004
*
* Copyright 2005 CafeSip.org
*
* 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.cafesip.jiplet.console.server.model.test;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

import junit.framework.TestCase;

import org.cafesip.jiplet.console.server.ContainerMgmt;
import org.cafesip.jiplet.console.server.Model;
import org.cafesip.jiplet.jmxbeans.ContextElement;
import org.cafesip.jiplet.jmxbeans.JipletElement;
import org.cafesip.jiplet.utils.FileUtils;

/**
* @author amit
*
*/
public class ContainerMgmtTest extends TestCase
{
    private String exampleAppDirectory;

    private String exampleAppSpr;

    private String testDataDirectory = "test/testdata";

    private ContainerMgmt container;

    /**
     * Constructor for ContainerMgmtTest.
     *
     * @param arg0
     */
    public ContainerMgmtTest(String arg0)
    {
        super(arg0);

        String e = System.getProperty("org.cafesip.jiplet.test.data.dir");
        if (e != null)
        {
            testDataDirectory = e;
        }

        File f = new File(testDataDirectory, "my-context.spr");
        exampleAppSpr = f.getAbsolutePath();

        f = new File(testDataDirectory, "deploy.jar");
        FileUtils ant = new FileUtils();
        assertEquals("deploy.jar could not be extracted", true, ant.extract(f
                .getAbsolutePath(), (new File(testDataDirectory, "tmp"))
                .getAbsolutePath()));

        f = new File(testDataDirectory, "tmp/deploy");
        exampleAppDirectory = f.getAbsolutePath();
    }

    /*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception
    {
        Model.init();

        container = new ContainerMgmt();
        cleanup();
    }

    /*
     * @see TestCase#tearDown()
     */
    protected void tearDown() throws Exception
    {
        cleanup();
    }

    public void testContextOperations() throws Exception
    {
        // Test: Get the deploy directory name
        String deploy_dir = null;
        try
        {
            deploy_dir = container.getDeploymentDir();
        }
        catch (Exception e)
        {
            fail("Error getting deploy dir");
        }

        // Test: Get a list of deployed contexts. Check that the list is empty.
        String[] contexts = container.listContexts();
        assertNotNull("listContexts() returned a null object", contexts);
        assertEquals("listContexts() returned a non-empty string", 0,
                contexts.length);

        // Test: Start the exploded example application. Check that it is
        // started.
        try
        {
            container.createContext(exampleAppDirectory, null, null);
        }
        catch (Exception e)
        {
            fail("The context could not be added");
        }

        // Test: Start another context specifying an invalid path. Check that
        // the start operation fails.
        try
        {
            container.createContext("bogus", null, null);
            fail("The bogus context was added successully");
        }
        catch (Exception e)
        {
        }

        // Test: Get a list of deployed context. Check that the list contains
        // one element
        contexts = container.listContexts();
        assertNotNull("listContexts() returned a null object", contexts);
        assertEquals("listContexts() returned a empty string", 1,
                contexts.length);
        assertEquals("Context name does not match", "deploy", contexts[0]);

        // Test: Get the property of the deployed context. Check that the
        // properties match.
        ContextElement element = null;
        try
        {
            element = container.getContextProperty("deploy");
        }
        catch (Exception e)
        {
            fail("Error getting context property");
        }
        assertNotNull(element);
        assertEquals("getContextProperty returned invalid name", "deploy",
                element.getName());
        File f = new File(deploy_dir, "deploy");
        assertEquals("getContextProperty returned invalid path", f.toURL()
                .toString(), (new File(element.getPath())).toURL().toString());
        assertEquals("getDisplayName returned unexpected name",
                "Example TEST Context", element.getDisplayName());
        assertEquals("getTypeDescription returned invalid deployment type",
                "Deployed", element.getTypeDescription());
        ArrayList criteria = element.getSelectionCriteria();
        assertNotNull("The selection criteria for the context does not exist",
                criteria);
        assertEquals("The number of selection criteria do not match", 1,
                criteria.size());
        String criterion = (String) criteria.get(0);
        assertEquals("The criterion does not match",
                "sip-connector:(request.uri subdomain-of \"quik-j.com\")",
                criterion);

        // Test: Get the property of a context that is not deployed.
        try
        {
            element = container.getContextProperty("test-context-1");
            fail("Got properties of nonexistent context");
        }
        catch (Exception e)
        {
        }

        // Test: Delete the context created above. Check that the delete
        // operation is successful.
        try
        {
            container.deleteContext("deploy");
        }
        catch (Exception e)
        {
            fail("The context could not be deleted: " + e.getMessage());
        }

        // Test: Delete a context that has not been created. Check that the
        // delete operation fails.
        try
        {
            container.deleteContext("test-context-2");
            fail("The bogus context was deleted successfully");
        }
        catch (Exception e)
        {
        }

        // Test: Start the example spr application . Check that it is started.
        try
        {
            container.createContext(exampleAppSpr, "my-renamed-context", null);
        }
        catch (Exception e)
        {
            fail("The context could not be added");
        }

        // Test: Get a list of deployed context. Check that the list contains
        // one element
        contexts = container.listContexts();
        assertNotNull("listContexts() returned a null object", contexts);
        assertEquals("listContexts() returned a empty string", 1,
                contexts.length);
        assertEquals("Context name does not match", "my-renamed-context",
                contexts[0]);

        // Test: Get the property of the deployed context. Check that the
        // properties match.
        try
        {
            element = container.getContextProperty("my-renamed-context");
        }
        catch (Exception e)
        {
            fail("Error getting context property");
        }
        assertNotNull(element);
        assertEquals("getContextProperty returned invalid name",
                "my-renamed-context", element.getName());
        f = new File(deploy_dir, "my-renamed-context");
        assertEquals("getContextProperty returned invalid path", f.toURL()
                .toString(), (new File(element.getPath())).toURL().toString());

        // Test: Delete the context created above. Check that the delete
        // operation is successful.
        try
        {
            container.deleteContext("my-renamed-context");
        }
        catch (Exception e)
        {
            fail("The context could not be deleted");
        }

        // Test: Get a list of deployed contexts. Check that the list is empty.
        contexts = container.listContexts();
        assertNotNull("listContexts() returned a null object", contexts);
        assertEquals("listContexts() returned a non-empty string", 0,
                contexts.length);

    }

    public void testJipletOperations() throws Exception
    {
        // Setup: Deploy and start the example test context with the expected
        // jiplets
        try
        {
            container.createContext(exampleAppDirectory, null, null);
        }
        catch (Exception e)
        {
            fail("The context could not be added");
        }
        // get the context name
        String[] contexts = container.listContexts();
        String context = contexts[0];

        // Test: Verify the list of jiplets in the context
        String[] jiplets = container.listJiplets(context);
        assertEquals("Unexpected number of jiplets in the test context", 2,
                jiplets.length);
        assertEquals("Unexpected name for first jiplet",
                "Example TEST SIP Registrar Jiplet", jiplets[0]);
        String full_jiplet = jiplets[0];
        assertEquals("Unexpected name for second jiplet",
                "Minimal TEST Jiplet", jiplets[1]);
        String min_jiplet = jiplets[1];

        // Test: List jiplets for a nonexistent context
        try
        {
            String[] bad = container.listJiplets(context + "bogus");
            // assertNull(
            // "Nonexistent test context unexpectedly exists during test",
            // bad);
            fail("Nonexistent test context unexpectedly exists during test");
        }
        catch (Exception e)
        {
        }

        // Test: Get jiplet properties from a nonexistent context
        try
        {
            JipletElement ele = container.getJipletProperty(context + "bogus",
                    full_jiplet);
            fail("Jiplet property element returned for nonexistent context");
        }
        catch (Exception e)
        {
        }

        // Test: Get jiplet properties for a nonexistent jiplet
        try
        {
            JipletElement ele = container.getJipletProperty(context,
                    full_jiplet + "bogus");
            fail("Jiplet property element is not null for nonexistent jiplet");
        }
        catch (Exception e)
        {
        }

        // Test: Verify getting the full set of jiplet properties
        JipletElement ele = null;
        try
        {
            ele = container.getJipletProperty(context, full_jiplet);
        }
        catch (Exception e)
        {
            fail("Error getting Jiplet property element for full jiplet");
        }

        assertEquals("Jiplet property returned unexpected name",
                "Example TEST SIP Registrar Jiplet", ele.getName());
        assertEquals(
                "Jiplet property returned unexpected description",
                "This jiplet will receive REGISTER message in order to register user location",
                removeWhitespace(ele.getDescription()));
        assertEquals("Jiplet property returned unexpected connector name",
                "sip-connector", ele.getConnectorName());
        assertEquals("Jiplet property returned unexpected parent context",
                context, ele.getParentContext());

        // check init params
        HashMap params = ele.getInitParams();
        assertNotNull("Jiplet property returned null init params", params);
        assertEquals("Incorrect number of init params", 2, params.size());
        // param 1
        assertEquals("sample-param init param is missing", true, params
                .containsKey("sample-param"));
        String value = (String) params.get("sample-param");
        assertEquals("sample-param init param has the wrong value",
                "some value", value);
        // param 2
        assertEquals("another-param init param is missing", true, params
                .containsKey("another-param"));
        value = (String) params.get("another-param");
        assertEquals("another-param init param has the wrong value",
                "another value", value);

        // Test: Get properties for a minimally defined jiplet
        try
        {
            ele = container.getJipletProperty(context, min_jiplet);
        }
        catch (Exception e)
        {
            fail("Error getting Jiplet property element for minimal jiplet");
        }

        assertEquals("Jiplet property returned unexpected name",
                "Minimal TEST Jiplet", ele.getName());
        assertEquals("Jiplet property returned unexpected description",
                "no description available", ele.getDescription());
        assertEquals("Jiplet property returned unexpected connector name",
                container.getDefaultConnectorName(), ele.getConnectorName());
        assertEquals("Jiplet property returned unexpected parent context",
                context, ele.getParentContext());
        // check init params
        params = ele.getInitParams();
        assertNull("Jiplet property returned non-null init params", params);

        ArrayList list = ele.getSelectionCriteria();
        assertNotNull("The selection criteria for the jiplet returned null",
                list);
        assertEquals(
                "The selection criteria returned invalid number of selection criteria",
                1, list.size());
        assertEquals("The jiplet selection criteria does not match",
                "(request.uri matches \"*\")", (String) list.get(0));
    }

    public static String removeWhitespace(String x)
    {
        // removes all whitespace, including inner (replaced with blank)

        if ((x == null) || (x.length() == 0))
        {
            return x;
        }

        StringBuffer buf = new StringBuffer();

        StringTokenizer tok = new StringTokenizer(x);
        while (tok.hasMoreTokens())
        {
            buf.append(tok.nextToken());
            buf.append(' ');
        }

        buf.setLength(buf.length() - 1);

        return buf.toString();
    }

    private void cleanup()
    {
        try
        {
            String[] contexts = container.listContexts();
            for (int i = 0; i < contexts.length; i++)
            {
                try
                {
                    container.deleteContext(contexts[i]);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
TOP

Related Classes of org.cafesip.jiplet.console.server.model.test.ContainerMgmtTest

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.