UniversalManagerTest.java :  » Media » m2engine » com » integrationpath » mengine » service » impl » Java Open Source

Java Open Source » Media » m2engine 
m2engine » com » integrationpath » mengine » service » impl » UniversalManagerTest.java
/**
 * Copyright (C) 2008-2009 Cristian Olaru <colaru@gmail.com>
 * 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 com.integrationpath.mengine.service.impl;

import org.acegisecurity.providers.dao.DaoAuthenticationProvider;
import com.integrationpath.mengine.dao.UniversalDao;
import com.integrationpath.mengine.model.User;
import org.jmock.Mock;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.test.AssertThrows;

/**
 * This class tests the generic UniversalManager and UniversalManagerImpl implementation.
 */
public class UniversalManagerTest extends BaseManagerMockTestCase {
    protected UniversalManagerImpl manager = new UniversalManagerImpl();
    protected Mock dao;
    
    protected void setUp() throws Exception {
        super.setUp();
        dao = new Mock(UniversalDao.class);
        manager.setDao((UniversalDao) dao.proxy());
    }
    
    protected void tearDown() throws Exception {
        manager = null;
        dao = null;
    }

    /**
     * Simple test to verify BaseDao works.
     */
    public void testCreate() {
        User user = createUser();
        dao.expects(once()).method("save").will(returnValue(user));
        user = (User) manager.save(user);
    }
    
    public void testRetrieve() {
        User user = createUser();
        dao.expects(once()).method("get").will(returnValue(user));
        user = (User) manager.get(User.class, user.getUsername());
    }
    
    public void testUpdate() {
        User user = createUser();
        dao.expects(once()).method("save").isVoid();
        user.getAddress().setCountry("USA");
        user = (User) manager.save(user);
    }
    
    public void testDelete() {
        Exception ex = new ObjectRetrievalFailureException(User.class, "foo");
        dao.expects(once()).method("remove").isVoid();            
        dao.expects(once()).method("get").will(throwException(ex));
        manager.remove(User.class, "foo");
        new AssertThrows(ObjectRetrievalFailureException.class) {
            public void test() {
                manager.get(User.class, "foo");
            }
        }.runTest();
    }
    
    private User createUser() {
        User user = new User();
        // set required fields
        user.setUsername("foo");
        return user;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.