View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.util;
29  
30  import java.util.Arrays;
31  import java.util.Collections;
32  import java.util.List;
33  
34  import org.junit.Assert;
35  import org.junit.Test;
36  
37  /**
38   * Unit tests for {@link Args}.
39   */
40  public class TestArgs {
41  
42      @Test
43      public void testArgCheckPass() {
44          Args.check(true, "All is well");
45      }
46  
47      @Test(expected=IllegalArgumentException.class)
48      public void testArgCheckFail() {
49          Args.check(false, "Oopsie");
50      }
51  
52      @Test
53      public void testArgNotNullPass() {
54          final String stuff = "stuff";
55          Assert.assertSame(stuff, Args.notNull(stuff, "Stuff"));
56      }
57  
58      @Test(expected=IllegalArgumentException.class)
59      public void testArgNotNullFail() {
60          Args.notNull(null, "Stuff");
61      }
62  
63      @Test
64      public void testArgNotEmptyPass() {
65          final String stuff = "stuff";
66          Assert.assertSame(stuff, Args.notEmpty(stuff, "Stuff"));
67      }
68  
69      @Test(expected=IllegalArgumentException.class)
70      public void testArgNotEmptyFail1() {
71          Args.notEmpty((String) null, "Stuff");
72      }
73  
74      @Test(expected=IllegalArgumentException.class)
75      public void testArgNotEmptyFail2() {
76          Args.notEmpty("", "Stuff");
77      }
78  
79      @Test(expected=IllegalArgumentException.class)
80      public void testArgNotBlankFail1() {
81          Args.notBlank((String) null, "Stuff");
82      }
83  
84      @Test(expected=IllegalArgumentException.class)
85      public void testArgNotBlankFail2() {
86          Args.notBlank("", "Stuff");
87      }
88  
89      @Test(expected=IllegalArgumentException.class)
90      public void testArgNotBlankFail3() {
91          Args.notBlank(" \t \n\r", "Stuff");
92      }
93  
94      @Test
95      public void testArgCollectionNotEmptyPass() {
96          final List<String> list = Arrays.asList("stuff");
97          Assert.assertSame(list, Args.notEmpty(list, "List"));
98      }
99  
100     @Test(expected=IllegalArgumentException.class)
101     public void testArgCollectionNotEmptyFail1() {
102         Args.notEmpty((List<?>) null, "List");
103     }
104 
105     @Test(expected=IllegalArgumentException.class)
106     public void testArgCollectionNotEmptyFail2() {
107         Args.notEmpty(Collections.emptyList(), "List");
108     }
109 
110     @Test
111     public void testPositiveIntPass() {
112         Assert.assertEquals(1, Args.positive(1, "Number"));
113     }
114 
115     @Test(expected=IllegalArgumentException.class)
116     public void testPositiveIntFail1() {
117         Args.positive(-1, "Number");
118     }
119 
120     @Test(expected=IllegalArgumentException.class)
121     public void testPositiveIntFail2() {
122         Args.positive(0, "Number");
123     }
124 
125     @Test
126     public void testPositiveLongPass() {
127         Assert.assertEquals(1L, Args.positive(1L, "Number"));
128     }
129 
130     @Test(expected=IllegalArgumentException.class)
131     public void testPositiveLongFail1() {
132         Args.positive(-1L, "Number");
133     }
134 
135     @Test(expected=IllegalArgumentException.class)
136     public void testPositiveLongFail2() {
137         Args.positive(0L, "Number");
138     }
139 
140     @Test
141     public void testNotNegativeIntPass1() {
142         Assert.assertEquals(1, Args.notNegative(1, "Number"));
143     }
144 
145     @Test
146     public void testNotNegativeIntPass2() {
147         Assert.assertEquals(0, Args.notNegative(0, "Number"));
148     }
149 
150     @Test(expected=IllegalArgumentException.class)
151     public void testNotNegativeIntFail1() {
152         Args.notNegative(-1, "Number");
153     }
154 
155     @Test
156     public void testNotNegativeLongPass1() {
157         Assert.assertEquals(1L, Args.notNegative(1L, "Number"));
158     }
159 
160     @Test
161     public void testNotNegativeLongPass2() {
162         Assert.assertEquals(0L, Args.notNegative(0L, "Number"));
163     }
164 
165     @Test(expected=IllegalArgumentException.class)
166     public void testNotNegativeLongFail1() {
167         Args.notNegative(-1L, "Number");
168     }
169 
170     //
171     public void testIntSmallestRangeOK() {
172         Args.checkRange(0, 0, 0, "Number");
173     }
174 
175     @Test(expected = IllegalArgumentException.class)
176     public void testIntSmallestRangeFailLow() {
177         Args.checkRange(-1, 0, 0, "Number");
178     }
179 
180     @Test(expected = IllegalArgumentException.class)
181     public void testIntRangeFailLow() {
182         Args.checkRange(-101, -100, 100, "Number");
183     }
184 
185     @Test(expected = IllegalArgumentException.class)
186     public void testIntRangeFailHigh() {
187         Args.checkRange(101, -100, 100, "Number");
188     }
189 
190     @Test(expected = IllegalArgumentException.class)
191     public void testIntSmallestRangeFailHigh() {
192         Args.checkRange(1, 0, 0, "Number");
193     }
194 
195     public void testIntFullRangeOK() {
196         Args.checkRange(0, Integer.MIN_VALUE, Integer.MAX_VALUE, "Number");
197     }
198 
199     //
200     public void testLongSmallestRangeOK() {
201         Args.checkRange(0L, 0L, 0L, "Number");
202     }
203 
204     @Test(expected = IllegalArgumentException.class)
205     public void testLongSmallestRangeFailLow() {
206         Args.checkRange(-1L, 0L, 0L, "Number");
207     }
208 
209     @Test(expected = IllegalArgumentException.class)
210     public void testLongRangeFailLow() {
211         Args.checkRange(-101L, -100L, 100L, "Number");
212     }
213 
214     @Test(expected = IllegalArgumentException.class)
215     public void testLongRangeFailHigh() {
216         Args.checkRange(101L, -100L, 100L, "Number");
217     }
218 
219     @Test(expected = IllegalArgumentException.class)
220     public void testLongSmallestRangeFailHigh() {
221         Args.checkRange(1L, 0L, 0L, "Number");
222     }
223 
224     public void testLongFullRangeOK() {
225         Args.checkRange(0L, Long.MIN_VALUE, Long.MAX_VALUE, "Number");
226     }
227 
228 }