/*
 * Copyright 2002-2009 the original author or authors.
 *
 * 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 anyframe.core.basis.service.impl;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import anyframe.core.basis.util.ConvertUtil;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.Mockery;
import org.junit.runner.RunWith;

import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * This class serves as the Base class for Manager test
 * cases.
 * @author <a href="mailto:matt@raibledesigns.com">Matt
 *         Raible</a>
 * @author modified by SooYeon Park
 */
@RunWith(JMock.class)
public abstract class BaseManagerMockTestCase {
    // ~ Static fields/initializers
    // =============================================
    final protected Log log = LogFactory.getLog(getClass());
    protected ResourceBundle rb;
    protected Mockery context = new JUnit4Mockery();

    // ~ Constructors
    // ===========================================================

    public BaseManagerMockTestCase() {
        // Since a ResourceBundle is not required for
        // each class, just
        // do a simple check to see if one exists
        String className = this.getClass().getName();

        try {
            rb = ResourceBundle.getBundle(className);
        } catch (MissingResourceException mre) {
            // log.warn("No resource bundle found for:
            // " + className);
        }
    }

    // ~ Methods
    // ================================================================

    /**
     * Utility method to populate a javabean-style
     * object with values from a Properties file
     * @param obj
     *        the model object to populate
     * @return Object populated object
     * @throws Exception
     *         if BeanUtils fails to copy properly
     */
    protected Object populate(Object obj) throws Exception {
        // loop through all the beans methods and set
        // its properties from
        // its .properties file
        Map map = ConvertUtil.convertBundleToMap(rb);

        BeanUtils.copyProperties(obj, map);

        return obj;
    }
}

