com.talis.inject.guice.PropertiesConfiguredModuleFactoryTest.java Source code

Java tutorial

Introduction

Here is the source code for com.talis.inject.guice.PropertiesConfiguredModuleFactoryTest.java

Source

/*
 * Copyright 2010 Talis Information Ltd
 * 
 *    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.talis.inject.guice;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.junit.Test;

import com.google.inject.AbstractModule;
import com.google.inject.ConfigurationException;
import com.google.inject.Module;

public class PropertiesConfiguredModuleFactoryTest {

    @Test
    public void readPropertiesFromClasspath() throws Exception {
        Module[] modules = new PropertiesConfiguredModuleFactory().getModules();
        assertEquals(2, modules.length);
        assertTrue(TestModuleOne.class.isInstance(modules[0]));
        assertTrue(TestModuleTwo.class.isInstance(modules[1]));
    }

    @Test
    public void readPropertiesFromFileSpecifiedBySystemProperty() throws Exception {

        File tmpFile = File.createTempFile("injector-module", ".properties");
        tmpFile.deleteOnExit();
        FileUtils.writeStringToFile(tmpFile, String.format("%s=%s",
                PropertiesConfiguredModuleFactory.MODULE_PROPERTY, TestModuleThree.class.getName()));
        System.clearProperty(PropertiesConfiguredModuleFactory.PROPERTIES_LOCATION_PROP);

        try {
            System.setProperty(PropertiesConfiguredModuleFactory.PROPERTIES_LOCATION_PROP,
                    tmpFile.getAbsolutePath());
            Module[] modules = new PropertiesConfiguredModuleFactory().getModules();
            assertEquals(1, modules.length);
            assertTrue(TestModuleThree.class.isInstance(modules[0]));
        } finally {
            System.clearProperty(PropertiesConfiguredModuleFactory.PROPERTIES_LOCATION_PROP);
        }
    }

    @Test(expected = ConfigurationException.class)
    public void propertiesFileNotFound() throws Exception {

        File noFile = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
        assertFalse(noFile.exists());
        System.clearProperty(PropertiesConfiguredModuleFactory.PROPERTIES_LOCATION_PROP);
        try {
            System.setProperty(PropertiesConfiguredModuleFactory.PROPERTIES_LOCATION_PROP,
                    noFile.getAbsolutePath());
            new PropertiesConfiguredModuleFactory().getModules();
        } finally {
            System.clearProperty(PropertiesConfiguredModuleFactory.PROPERTIES_LOCATION_PROP);
        }
    }

    @Test(expected = ConfigurationException.class)
    public void invalidClassSpecifiedInFile() throws Exception {
        File tmpFile = File.createTempFile("injector-module", ".properties");
        tmpFile.deleteOnExit();
        FileUtils.writeStringToFile(tmpFile,
                String.format("%s=%s", PropertiesConfiguredModuleFactory.MODULE_PROPERTY, String.class.getName()));
        System.clearProperty(PropertiesConfiguredModuleFactory.PROPERTIES_LOCATION_PROP);

        try {
            System.setProperty(PropertiesConfiguredModuleFactory.PROPERTIES_LOCATION_PROP,
                    tmpFile.getAbsolutePath());
            new PropertiesConfiguredModuleFactory().getModules();
        } finally {
            System.clearProperty(PropertiesConfiguredModuleFactory.PROPERTIES_LOCATION_PROP);
        }
    }

    @Test
    public void returnEmptyListIfModulesSpecifiedInFile() throws Exception {
        File tmpFile = File.createTempFile("injector-module", ".properties");
        tmpFile.deleteOnExit();
        FileUtils.writeStringToFile(tmpFile, "some.other.property=SomeRandomValue");
        System.clearProperty(PropertiesConfiguredModuleFactory.PROPERTIES_LOCATION_PROP);

        try {
            System.setProperty(PropertiesConfiguredModuleFactory.PROPERTIES_LOCATION_PROP,
                    tmpFile.getAbsolutePath());
            Module[] modules = new PropertiesConfiguredModuleFactory().getModules();
            assertEquals(0, modules.length);
        } finally {
            System.clearProperty(PropertiesConfiguredModuleFactory.PROPERTIES_LOCATION_PROP);
        }
    }

    public static class TestModuleOne extends AbstractModule {
        @Override
        protected void configure() {
        }
    }

    public static class TestModuleTwo extends AbstractModule {
        @Override
        protected void configure() {
        }
    }

    public static class TestModuleThree extends AbstractModule {
        @Override
        protected void configure() {
        }
    }
}