001/*
002 * Copyright 2002-2017 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.web.client;
018
019import org.springframework.util.Assert;
020
021/**
022 * A simple type representing a range for an expected count.
023 *
024 * <p>Examples:
025 * <pre>
026 * import static org.springframework.test.web.client.ExpectedCount.*
027 *
028 * once()
029 * manyTimes()
030 * times(5)
031 * min(2)
032 * max(4)
033 * between(2, 4)
034 * never()
035 * </pre>
036 *
037 * @author Rossen Stoyanchev
038 * @since 4.3
039 */
040public class ExpectedCount {
041
042        private final int minCount;
043
044        private final int maxCount;
045
046
047        /**
048         * Private constructor.
049         * See static factory methods in this class.
050         */
051        private ExpectedCount(int minCount, int maxCount) {
052                Assert.isTrue(minCount >= 0, "minCount >= 0 is required");
053                Assert.isTrue(maxCount >= minCount, "maxCount >= minCount is required");
054                this.minCount = minCount;
055                this.maxCount = maxCount;
056        }
057
058
059        /**
060         * Return the {@code min} boundary of the expected count range.
061         */
062        public int getMinCount() {
063                return this.minCount;
064        }
065
066        /**
067         * Return the {@code max} boundary of the expected count range.
068         */
069        public int getMaxCount() {
070                return this.maxCount;
071        }
072
073
074        /**
075         * Exactly once.
076         */
077        public static ExpectedCount once() {
078                return new ExpectedCount(1, 1);
079        }
080
081        /**
082         * Many times (range of 1..Integer.MAX_VALUE).
083         */
084        public static ExpectedCount manyTimes() {
085                return new ExpectedCount(1, Integer.MAX_VALUE);
086        }
087
088        /**
089         * Exactly N times.
090         */
091        public static ExpectedCount times(int count) {
092                Assert.isTrue(count >= 1, "'count' must be >= 1");
093                return new ExpectedCount(count, count);
094        }
095
096        /**
097         * At least {@code min} number of times.
098         */
099        public static ExpectedCount min(int min) {
100                Assert.isTrue(min >= 1, "'min' must be >= 1");
101                return new ExpectedCount(min, Integer.MAX_VALUE);
102        }
103
104        /**
105         * At most {@code max} number of times.
106         */
107        public static ExpectedCount max(int max) {
108                Assert.isTrue(max >= 1, "'max' must be >= 1");
109                return new ExpectedCount(1, max);
110        }
111
112        /**
113         * No calls expected at all, i.e. min=0 and max=0.
114         * @since 4.3.6
115         */
116        public static ExpectedCount never() {
117                return new ExpectedCount(0, 0);
118        }
119
120        /**
121         * Between {@code min} and {@code max} number of times.
122         */
123        public static ExpectedCount between(int min, int max) {
124                return new ExpectedCount(min, max);
125        }
126
127}