uk.co.unclealex.process.spring.PackageCheckingPostProcessorTest.java Source code

Java tutorial

Introduction

Here is the source code for uk.co.unclealex.process.spring.PackageCheckingPostProcessorTest.java

Source

/**
 * Copyright 2012 Alex Jones
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 uk.co.unclealex.process.spring;

import java.util.SortedSet;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.support.BeanDefinitionValidationException;

import uk.co.unclealex.process.packages.PackageChecker;

import com.google.common.collect.Sets;

/**
 * The Class PackageCheckingPostProcessorTest.
 * 
 * @author alex
 */
public class PackageCheckingPostProcessorTest {

    /**
     * Test nomissing packages.
     */
    @Test
    public void testNomissingPackages() {
        test(new Object(), true);
    }

    /**
     * Test missing packages.
     */
    @Test
    public void testMissingPackages() {
        test(new MissingPackages(), false);
    }

    /**
     * Test.
     * 
     * @param obj
     *          the obj
     * @param expectSuccess
     *          the expect success
     */
    public void test(Object obj, boolean expectSuccess) {
        PackageCheckingPostProcessor packageCheckingPostProcessor = createPackageCheckingPostProcessor();
        Assert.assertEquals("The bean was changed during postProcessAfterInitialization.", obj,
                packageCheckingPostProcessor.postProcessAfterInitialization(obj, "hello"));
        try {
            Assert.assertEquals("The bean was changed during postProcessBeforeInitialization.", obj,
                    packageCheckingPostProcessor.postProcessBeforeInitialization(obj, "hello"));
            Assert.assertTrue("No exception was thrown for object " + obj + " when one was expected.",
                    expectSuccess);
        } catch (BeanDefinitionValidationException e) {
            Assert.assertFalse("An exception was thrown for object " + obj + " when one was not expected.",
                    expectSuccess);
        }
    }

    /**
     * Creates the package checking post processor.
     * 
     * @return the package checking post processor
     */
    protected PackageCheckingPostProcessor createPackageCheckingPostProcessor() {
        PackageChecker packageChecker = new PackageChecker() {

            @Override
            public SortedSet<String> listMissingPackages(Object obj) {
                SortedSet<String> missingPackages = Sets.newTreeSet();
                if (obj instanceof MissingPackages) {
                    missingPackages.add("missing");
                }
                return missingPackages;
            }

            @Override
            public SortedSet<String> listMissingPackages(Class<?> clazz) {
                return listMissingPackages((Object) clazz);
            }
        };
        return new PackageCheckingPostProcessor(packageChecker);
    }

    /**
     * The Class MissingPackages.
     */
    static class MissingPackages {
        // Empty class
    }
}