Package org.ow2.easybeans.mavenplugin.examples.directory

Source Code of org.ow2.easybeans.mavenplugin.examples.directory.DirectoryTest

/**
* EasyBeans
* Copyright (C) 2007 Bull S.A.S.
* Contact: easybeans@objectweb.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
* USA
*
* --------------------------------------------------------------------------
* $Id: DirectoryTest.java 5369 2010-02-24 14:58:19Z benoitf $
* --------------------------------------------------------------------------
*/

package org.ow2.easybeans.mavenplugin.examples.directory;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNull;


/**
* Test implementation for Directory integration test.
* @author Julien Blais
*/
public class DirectoryTest {

    /**
    * JNDI name of the bean.
    */
    private static final String JNDI_NAME = "ejb/DirectoryBean";

    /**
     * EJB to test.
     */
    private static DirectoryRemote dir = getEJB();

    /**
     * Rigourous Test :-) for Directory.
     */
    @Test
    public void testDirectory() {
        try {
            System.out.println("[Start testing Directory Ejb's]");

            dir.create("totoUser", "mdptoto");

            System.out.println("There is " + dir.getList().size() + " persons in your directory.");

            /* Adding some persons in my directory */
            dir.add("Blais", "Julien", "Saint Eloi", "0673369413");
            dir.add("Michaud", "Vincent", "Poitiers", "0567349018");
            dir.add("Souadnia", "Bilel", "Poitiers", "0536451238");
            dir.add("Desbouchages", "Sebastien", "Beaulieu", "555-3459094");
            dir.add("Deneux", "Alexandre", "Jaunay-Clan", "0567345783");

            /* Gets a phone number from my directory*/
            String phone = dir.getPhoneNumber("Blais", "Julien");
            System.out.println("Phone number : " + phone);

            /* Check returned phone is good ...*/
            assertTrue(phone.equals("0673369413"));

            /* Print the list of persons in my directory*/
            ArrayList<Person> listPersons = dir.getList();
            System.out.println("List of persons in directory :");
            for (Person p : listPersons) {
                System.out.println("############################");
                System.out.println("Firstname : " + p.getFirstname());
                System.out.println("Lastname  : " + p.getLastname());
                System.out.println("Adress    : " + p.getAdress());
                System.out.println("Phone     : " + p.getPhone());
            }
            System.out.println("############################");

            /* remove a person from directory */
            System.out.println("Removing Vincent Michaud from directory");
            dir.remove("Vincent", "0567349018");

            /* Check removed */
            try {
                phone = null;
                System.out.println("Getting Vincent Michaud phone number ...");
                phone = dir.getPhoneNumber("Vincent", "Michaud");
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
            assertNull(phone);


            System.out.println("[End testing Directory Ejb's]");

        } catch (Exception ex) {
                ex.printStackTrace();
        }
    }

    /**
     * Get Initial Context.
     * @return smartFactory initialised context
     * @throws NamingException naming exception
     */
    private static Context getInitialContext() throws NamingException {

        Hashtable<String, Object> env = new Hashtable<String, Object>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "org.ow2.easybeans.component.smartclient.spi.SmartContextFactory");
        env.put(Context.PROVIDER_URL, "smart://localhost:22503");
        return new InitialContext(env);
    }

    /**
     * Get the DirectoryRemote EJB interface/.
     * @return Interface of EJB
     */
    private static DirectoryRemote getEJB() {
        DirectoryRemote directory = null;

        try {
            System.setProperty("java.rmi.server.useCodebaseOnly", "true");
            Context initialContext = getInitialContext();
            directory = (DirectoryRemote) initialContext.lookup(JNDI_NAME);

        } catch (NamingException ex) {
            Logger.getLogger(DirectoryRemote.class.getName()).log(Level.SEVERE, "Unable to found DirectoryRemote EJB.", ex);
        }
        return directory;
    }
}
TOP

Related Classes of org.ow2.easybeans.mavenplugin.examples.directory.DirectoryTest

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.