coreexample.AppConfigTest.java Source code

Java tutorial

Introduction

Here is the source code for coreexample.AppConfigTest.java

Source

/*
 * Copyright 2012 Eng Kam Hon (kamhon@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 coreexample;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;

import java.util.Locale;

import net.kamhon.ieagle.dao.DynamicDataSource;
import net.kamhon.ieagle.dao.GenericDao;

import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * This test cases are testing on inheritance behaviour on AppConfig
 */
public class AppConfigTest {
    @Test
    @Ignore
    public void testBaseAppConfig() {
        doTesting(BaseAppConfig.class);
    }

    @Test
    @Ignore
    public void testExampleAppConfig() {
        ApplicationContext context = doTesting(ExampleAppConfig.class);

        Boolean isProductionMode = context.getBean("productionMode", Boolean.class);
        assertFalse("should be false for productMode", isProductionMode);

        DynamicDataSource ds = context.getBean("dataSource", DynamicDataSource.class);
        assertEquals("org.hsqldb.jdbcDriver", ds.getDriverClassName());
    }

    private ApplicationContext doTesting(Class<?> appConfigClass) {
        Locale expected = new Locale("en", "US");
        ApplicationContext context = new AnnotationConfigApplicationContext(appConfigClass);

        Locale systemLocale = context.getBean("systemLocale", Locale.class);

        assertNotNull(systemLocale);
        assertNotSame(expected, context);

        return context;
    }

    @Test
    @Ignore
    public void testGenericDao() {
        ApplicationContext context = new AnnotationConfigApplicationContext(BaseAppConfig.class);
        @SuppressWarnings("unchecked")
        GenericDao<Object> genericDao = context.getBean("genericDao", GenericDao.class);

        assertNotNull(genericDao);
        assertTrue(genericDao instanceof GenericDao);
    }

    @Test
    @Ignore
    public void testExampleDao() {
        ApplicationContext context = new AnnotationConfigApplicationContext(BaseAppConfig.class);
        ExampleDao exampleDao = context.getBean(ExampleDao.BEAN_NAME, ExampleDao.class);

        assertNotNull(exampleDao);
    }

    @Test
    @Ignore
    public void testExample2Dao() {
        ApplicationContext context = new AnnotationConfigApplicationContext(ExampleAppConfig.class);
        Example2Dao example2Dao = context.getBean(Example2Dao.BEAN_NAME, Example2Dao.class);

        assertNotNull(example2Dao);
    }

    @Test
    @Ignore
    public void testGetSystemLocaleFromExampleDao() {
        ApplicationContext context = new AnnotationConfigApplicationContext(ExampleAppConfig.class);
        Locale expected = context.getBean("systemLocale", Locale.class);

        ExampleDao exampleDao = context.getBean(ExampleDao.BEAN_NAME, ExampleDao.class);
        Locale systemLocale = exampleDao.getSystemLocale();

        assertNotNull(systemLocale);
        assertEquals(expected, systemLocale);
    }

    @Test
    @Ignore
    public void testGetSystemLocaleFromExample2Dao() {
        ApplicationContext context = new AnnotationConfigApplicationContext(ExampleAppConfig.class);
        Locale expected = context.getBean("systemLocale", Locale.class);

        Example2Dao example2Dao = context.getBean(Example2Dao.BEAN_NAME, Example2Dao.class);
        Locale systemLocale = example2Dao.getSystemLocale();

        assertNotNull(systemLocale);
        assertEquals(expected, systemLocale);
    }
}