001/*
002 * Copyright 2002-2016 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.cache;
018
019import org.springframework.core.SpringProperties;
020import org.springframework.util.StringUtils;
021
022/**
023 * Collection of utilities for working with {@link ContextCache ContextCaches}.
024 *
025 * @author Sam Brannen
026 * @since 4.3
027 */
028public abstract class ContextCacheUtils {
029
030        /**
031         * Retrieve the maximum size of the {@link ContextCache}.
032         * <p>Uses {@link SpringProperties} to retrieve a system property or Spring
033         * property named {@code spring.test.context.cache.maxSize}.
034         * <p>Falls back to the value of the {@link ContextCache#DEFAULT_MAX_CONTEXT_CACHE_SIZE}
035         * if no such property has been set or if the property is not an integer.
036         * @return the maximum size of the context cache
037         * @see ContextCache#MAX_CONTEXT_CACHE_SIZE_PROPERTY_NAME
038         */
039        public static int retrieveMaxCacheSize() {
040                try {
041                        String maxSize = SpringProperties.getProperty(ContextCache.MAX_CONTEXT_CACHE_SIZE_PROPERTY_NAME);
042                        if (StringUtils.hasText(maxSize)) {
043                                return Integer.parseInt(maxSize.trim());
044                        }
045                }
046                catch (Exception ex) {
047                        // ignore
048                }
049
050                // Fallback
051                return ContextCache.DEFAULT_MAX_CONTEXT_CACHE_SIZE;
052        }
053
054}