com.fstx.stdlib.author.old.AuthorizationManagerTest.java Source code

Java tutorial

Introduction

Here is the source code for com.fstx.stdlib.author.old.AuthorizationManagerTest.java

Source

/*
    
 Five Sticks
 6957 W. North Ave., #202
 Chicago, IL 60302
 USA
 http://www.fivesticks.com
 mailto:info@fivesticks.com
    
 Copyright (c) 2003-2004, Five Sticks Publications, Inc.
 All rights reserved.
    
 Redistribution and use in source and binary forms, 
 with or without modification, are permitted provided
 that the following conditions are met:
    
 * Redistributions of source code must retain 
 the above copyright notice, this list of conditions 
 and the following disclaimer.
 * Redistributions in binary form must reproduce 
 the above copyright notice, this list of conditions 
 and the following disclaimer in the documentation 
 and/or other materials provided with the distribution.
 * Neither the name of the Five Sticks Publications, Inc.,
 nor the names of its contributors may be used to 
 endorse or promote products derived from this software 
 without specific prior written permission.
    
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 SUCH DAMAGE.
    
 license: http://www.opensource.org/licenses/bsd-license.php
    
 This software uses a variety of Open Source software as
 a foundation.  See the file 
    
 [your install]/WEB-INF/component-acknowledgement.txt
     
 For more information.
 */
/*
 * Created on Aug 18, 2003
 *
 */
package com.fstx.stdlib.author.old;

import java.util.HashSet;

import junit.framework.TestCase;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fivesticks.time.config.DatabaseInitializer;
import com.fstx.stdlib.authen.users.GroupMembership;
import com.fstx.stdlib.authen.users.GroupMembershipDAO;
import com.fstx.stdlib.authen.users.GroupMembershipDAOImpl;
import com.fstx.stdlib.authen.users.GroupRights;
import com.fstx.stdlib.authen.users.GroupRightsDAO;
import com.fstx.stdlib.authen.users.GroupRightsDAOImpl;
import com.fstx.stdlib.authen.users.User;
import com.fstx.stdlib.authen.users.UserDAO;
import com.fstx.stdlib.authen.users.UserDAOImpl;
import com.fstx.stdlib.authen.users.UserGroup;
import com.fstx.stdlib.authen.users.UserGroupDAO;
import com.fstx.stdlib.authen.users.UserGroupDAOImpl;

/**
 * @author Nick
 *  
 */
public class AuthorizationManagerTest extends TestCase {
    private UserGroup g = null;

    private UserGroup g1 = null;

    private User u1 = null;

    private User u2 = null;

    private User u3 = null;

    private GroupMembership gm = null;

    private GroupMembership gm1 = null;

    private GroupMembership gm2 = null;

    private GroupRights gr = null;

    private GroupRights gr1 = null;

    private GroupRights gr2 = null;

    private GroupRights gr3 = null;

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

    public void testGetAuthorizationBean() throws AuthorizationException {
        //Puts the Bean into the hash.
        AuthorizationBean ab = AuthorizationManager.singleton.getAuthorizationBean("Nicholas");
        HashSet myRights = ab.getRights();
        assertEquals(myRights.size(), 4);
        assertTrue(ab.isAuthorized("right1"));
        assertTrue(ab.isAuthorized("right2"));
        assertTrue(ab.isAuthorized("right3"));
        assertTrue(ab.isAuthorized("Otherright2"));

        // Should be getting from the hash rather than the Builder
        AuthorizationBean ab2 = AuthorizationManager.singleton.getAuthorizationBean("Nicholas");
        HashSet myRights2 = ab2.getRights();
        assertEquals(myRights2.size(), 4);
        assertTrue(ab2.isAuthorized("right1"));
        assertTrue(ab2.isAuthorized("right2"));
        assertTrue(ab2.isAuthorized("right3"));
        assertTrue(ab2.isAuthorized("Otherright2"));

        AuthorizationBean ab3 = AuthorizationManager.singleton.getAuthorizationBean("Jose");
        HashSet myRights3 = ab3.getRights();
        assertEquals(myRights3.size(), 3);
        assertTrue(ab3.isAuthorized("right1"));
        assertTrue(ab3.isAuthorized("right2"));
        assertTrue(ab3.isAuthorized("right3"));

    }

    public void testRemoveAuthorizationBean() throws AuthorizationException {
        AuthorizationBean ab = AuthorizationManager.singleton.getAuthorizationBean("Nicholas");
        HashSet myRights = ab.getRights();
        assertEquals(myRights.size(), 4);
        assertTrue(myRights.contains("right1"));
        assertTrue(myRights.contains("right2"));
        assertTrue(myRights.contains("right3"));
        assertTrue(myRights.contains("Otherright2"));

        assertTrue(AuthorizationManager.singleton.removeAuthorizationBean("Nicholas"));

        // Should be getting from the builder again.
        AuthorizationBean ab2 = AuthorizationManager.singleton.getAuthorizationBean("Nicholas");
        HashSet myRights2 = ab2.getRights();
        assertEquals(myRights2.size(), 4);
        assertTrue(myRights2.contains("right1"));
        assertTrue(myRights2.contains("right2"));
        assertTrue(myRights2.contains("right3"));
        assertTrue(myRights2.contains("Otherright2"));
    }

    static Log log = LogFactory.getLog(AuthorizationBeanBuilderTest.class.getName());

    /*
     * (non-Javadoc)
     * 
     * @see junit.framework.TestCase#setUp()
     */
    protected void setUp() throws Exception {
        new DatabaseInitializer().initializeTables();
        log.info("\n\nSetting UP");
        UserGroupDAO dao = new UserGroupDAOImpl();
        GroupMembershipDAO gmdao = new GroupMembershipDAOImpl();
        UserDAO udao = new UserDAOImpl();
        GroupRightsDAO grdao = new GroupRightsDAOImpl();
        //Create groups
        g = new UserGroup("myEMSGroupsfsdfs");
        g1 = new UserGroup("myOtherGroup");

        //Create Users
        u1 = new User("Nicholas", "pass", "email@yahoo.com");
        u2 = new User("Jim", "pass", "email2@yahoo.com");
        u3 = new User("Jose", "pass", "email3@yahoo.com");

        // Save groups
        g = dao.save(g);
        g1 = dao.save(g1);

        // Save users
        u1 = udao.save(u1);
        u2 = udao.save(u2);
        u3 = udao.save(u3);

        // Create Group Memberships
        gm = new GroupMembership(g.getId(), u1.getId());
        gm1 = new GroupMembership(g1.getId(), u1.getId());
        gm2 = new GroupMembership(g.getId(), u3.getId());

        // Create Group Rights
        gr = new GroupRights(g.getId(), "right1");
        gr1 = new GroupRights(g.getId(), "right2");
        gr2 = new GroupRights(g.getId(), "right3");
        gr3 = new GroupRights(g1.getId(), "Otherright2");

        // Save Group Rights
        gr = grdao.save(gr);
        gr1 = grdao.save(gr1);
        gr2 = grdao.save(gr2);
        gr3 = grdao.save(gr3);

        // Save GroupMemberships
        gm = gmdao.save(gm);
        gm1 = gmdao.save(gm1);
        gm2 = gmdao.save(gm2);
        // TODO Auto-generated method stub
        super.setUp();
    }

    /*
     * (non-Javadoc)
     * 
     * @see junit.framework.TestCase#tearDown()
     */
    protected void tearDown() throws Exception {
        log.info("\n\nTearing Down");
        new UserGroupDAOImpl().delete(g);
        new UserGroupDAOImpl().delete(g1);
        new UserDAOImpl().delete(u1);
        new UserDAOImpl().delete(u2);
        new UserDAOImpl().delete(u3);

        new GroupMembershipDAOImpl().delete(gm);
        new GroupMembershipDAOImpl().delete(gm1);
        new GroupMembershipDAOImpl().delete(gm2);

        new GroupRightsDAOImpl().delete(gr);
        new GroupRightsDAOImpl().delete(gr1);
        new GroupRightsDAOImpl().delete(gr2);
        new GroupRightsDAOImpl().delete(gr3);

        super.tearDown();
    }

}