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