001/*
002 * Copyright 2002-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.test.context.support;
018
019import org.springframework.core.style.ToStringCreator;
020import org.springframework.test.context.BootstrapContext;
021import org.springframework.test.context.CacheAwareContextLoaderDelegate;
022import org.springframework.util.Assert;
023
024/**
025 * Default implementation of the {@link BootstrapContext} interface.
026 *
027 * @author Sam Brannen
028 * @since 4.1
029 */
030public class DefaultBootstrapContext implements BootstrapContext {
031
032        private final Class<?> testClass;
033        private final CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate;
034
035
036        /**
037         * Construct a new {@code DefaultBootstrapContext} from the supplied arguments.
038         * @param testClass the test class for this bootstrap context; never {@code null}
039         * @param cacheAwareContextLoaderDelegate the context loader delegate to use for
040         * transparent interaction with the {@code ContextCache}; never {@code null}
041         */
042        public DefaultBootstrapContext(Class<?> testClass, CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate) {
043                Assert.notNull(testClass, "Test class must not be null");
044                Assert.notNull(cacheAwareContextLoaderDelegate, "CacheAwareContextLoaderDelegate must not be null");
045                this.testClass = testClass;
046                this.cacheAwareContextLoaderDelegate = cacheAwareContextLoaderDelegate;
047        }
048
049        /**
050         * {@inheritDoc}
051         */
052        @Override
053        public Class<?> getTestClass() {
054                return this.testClass;
055        }
056
057        /**
058         * {@inheritDoc}
059         */
060        @Override
061        public CacheAwareContextLoaderDelegate getCacheAwareContextLoaderDelegate() {
062                return this.cacheAwareContextLoaderDelegate;
063        }
064
065        /**
066         * Provide a String representation of this bootstrap context's state.
067         */
068        @Override
069        public String toString() {
070                return new ToStringCreator(this)//
071                .append("testClass", testClass.getName())//
072                .append("cacheAwareContextLoaderDelegate", cacheAwareContextLoaderDelegate.getClass().getName())//
073                .toString();
074        }
075
076}