Package mfinder.spring

Source Code of mfinder.spring.SpringObjectFactoryTest

/*
* Copyright (C) 2010-2011 sunjumper@163.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 mfinder.spring;

import mfinder.ObjectFactory;
import mfinder.SimpleAction;
import mfinder.URLTestAction2;
import mfinder.impl.DefaultActionFactory;
import mfinder.interceptor.DemoInterceptor;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* SpringObjectFactoryTest。
*/
public class SpringObjectFactoryTest {

    //springframework ApplicationContext
    public final ApplicationContext context = new ClassPathXmlApplicationContext("mfinder-spring_test.xml");

    //DefaultActionFactory
    private DefaultActionFactory factory;

    //ObjectFactory
    private ObjectFactory objectFactory;

    @Before
    public void setUp() {
        //create ActionFactory not by springframework
        factory = new DefaultActionFactory();

        //SpringObjectFactory
        objectFactory = new SpringObjectFactory(context);
        factory.setObjectFactory(objectFactory);

        factory.addInterceptors(DemoInterceptor.class);
        factory.addInterceptorStacks(DemoInterceptor.class);

        factory.addActions(SimpleAction.class);

    }

    @After
    public void tearDown() {
        factory.clear();
    }

    /**
     * Test of newInstance method, of class SpringObjectFactory.
     */
    @Test
    public void testNewInstance() {
        SimpleAction sa1 = objectFactory.newInstance(SimpleAction.class);
        sa1.springInject();
        SimpleAction sa2 = objectFactory.newInstance(SimpleAction.class);
        sa2.springInject();
        assertNotSame(sa1, sa2);
        //注入的为单例bean对象
        assertSame(sa1.springInject(), sa2.springInject());
    }

    /**
     * 测试 springframework 注入。
     *
     * @see SimpleAction#springInject()
     */
    @Test
    public void testSpringInject() {
        String url = "/test/springInject";

        //check interceptors
        assertEquals(1, factory.getActions().get(url).getInterceptors().size());
        assertEquals(DemoInterceptor.SPRING_DEMO, factory.getActions().get(url).getInterceptors().get(0).getName());

        assertTrue(factory.invokeAction(url) instanceof URLTestAction2);
        assertSame(factory.invokeAction(url), factory.invokeAction(url));
    }
}
TOP

Related Classes of mfinder.spring.SpringObjectFactoryTest

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.