|
| 1 | +/* |
| 2 | + * Copyright 2011 LMAX Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.lmax.simpledsl.internal; |
| 17 | + |
| 18 | +import com.lmax.simpledsl.api.DslArg; |
| 19 | +import com.lmax.simpledsl.api.OptionalArg; |
| 20 | +import com.lmax.simpledsl.api.RequiredArg; |
| 21 | +import com.lmax.simpledsl.api.SimpleDslArg; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import static java.util.Arrays.asList; |
| 30 | +import static java.util.Collections.emptyList; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 35 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 36 | + |
| 37 | +public class RepeatingParamValuesTest |
| 38 | +{ |
| 39 | + |
| 40 | + @Test |
| 41 | + public void shouldReturnValue() |
| 42 | + { |
| 43 | + final RequiredArg requiredArg = new RequiredArg("foo"); |
| 44 | + final OptionalArg otherArg = new OptionalArg("bar"); |
| 45 | + final Map<String, List<String>> values = new HashMap<>(); |
| 46 | + values.put("foo", Collections.singletonList("abc")); |
| 47 | + values.put("bar", Collections.singletonList("123")); |
| 48 | + final RepeatingParamValues params = new RepeatingParamValues(asList(requiredArg, otherArg).toArray(new DslArg[0]), values); |
| 49 | + |
| 50 | + assertEquals("abc", params.value("foo")); |
| 51 | + assertEquals("123", params.value("bar")); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void shouldReturnMultipleValues() |
| 56 | + { |
| 57 | + final RequiredArg requiredArg = new RequiredArg("foo"); |
| 58 | + final OptionalArg otherArg = new OptionalArg("bar"); |
| 59 | + final Map<String, List<String>> values = new HashMap<>(); |
| 60 | + values.put("foo", Collections.singletonList("abc")); |
| 61 | + values.put("bar", asList("123", "456")); |
| 62 | + final RepeatingParamValues params = new RepeatingParamValues(asList(requiredArg, otherArg).toArray(new DslArg[0]), values); |
| 63 | + |
| 64 | + assertArrayEquals(new String[]{"123", "456"}, params.values("bar")); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void shouldReportOptionalValueAsPresentWhenValueProvided() |
| 69 | + { |
| 70 | + final RequiredArg requiredArg = new RequiredArg("foo"); |
| 71 | + final OptionalArg otherArg = new OptionalArg("bar"); |
| 72 | + final Map<String, List<String>> values = new HashMap<>(); |
| 73 | + values.put("foo", Collections.singletonList("abc")); |
| 74 | + values.put("bar", Collections.singletonList("123")); |
| 75 | + final RepeatingParamValues params = new RepeatingParamValues(asList(requiredArg, otherArg).toArray(new DslArg[0]), values); |
| 76 | + |
| 77 | + assertTrue(params.hasValue("bar")); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void shouldNotReportOptionalValueAsPresentWhenNoValueProvided() |
| 82 | + { |
| 83 | + final RequiredArg requiredArg = new RequiredArg("foo"); |
| 84 | + final OptionalArg otherArg = new OptionalArg("bar"); |
| 85 | + final Map<String, List<String>> values = new HashMap<>(); |
| 86 | + values.put("foo", Collections.singletonList("abc")); |
| 87 | + values.put("bar", emptyList()); |
| 88 | + final RepeatingParamValues params = new RepeatingParamValues(asList(requiredArg, otherArg).toArray(new DslArg[0]), values); |
| 89 | + |
| 90 | + assertFalse(params.hasValue("bar")); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void shouldReportRequiredValueAsPresentWhenEmptyValueProvided() |
| 95 | + { |
| 96 | + final RequiredArg requiredArg = new RequiredArg("foo"); |
| 97 | + final OptionalArg otherArg = new OptionalArg("bar"); |
| 98 | + final Map<String, List<String>> values = new HashMap<>(); |
| 99 | + values.put("foo", Collections.singletonList("")); |
| 100 | + values.put("bar", Collections.singletonList("123")); |
| 101 | + final RepeatingParamValues params = new RepeatingParamValues(asList(requiredArg, otherArg).toArray(new DslArg[0]), values); |
| 102 | + |
| 103 | + assertTrue(params.hasValue("foo")); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void shouldReportOptionalValueAsPresentWhenEmptyValueProvided() |
| 108 | + { |
| 109 | + final RequiredArg requiredArg = new RequiredArg("foo"); |
| 110 | + final OptionalArg otherArg = new OptionalArg("bar"); |
| 111 | + final Map<String, List<String>> values = new HashMap<>(); |
| 112 | + values.put("foo", Collections.singletonList("abc")); |
| 113 | + values.put("bar", Collections.singletonList("")); |
| 114 | + final RepeatingParamValues params = new RepeatingParamValues(asList(requiredArg, otherArg).toArray(new DslArg[0]), values); |
| 115 | + |
| 116 | + assertTrue(params.hasValue("bar")); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void shouldThrowWhenParameterDoesNotExist() |
| 121 | + { |
| 122 | + final RequiredArg requiredArg = new RequiredArg("foo"); |
| 123 | + final OptionalArg otherArg = new OptionalArg("bar"); |
| 124 | + final Map<String, List<String>> values = new HashMap<>(); |
| 125 | + values.put("foo", Collections.singletonList("")); |
| 126 | + values.put("bar", Collections.singletonList("123")); |
| 127 | + final RepeatingParamValues params = new RepeatingParamValues(asList(requiredArg, otherArg).toArray(new DslArg[0]), values); |
| 128 | + |
| 129 | + assertThrows(IllegalArgumentException.class, |
| 130 | + () -> params.value("fake")); |
| 131 | + assertThrows(IllegalArgumentException.class, |
| 132 | + () -> params.values("fake")); |
| 133 | + assertThrows(IllegalArgumentException.class, |
| 134 | + () -> params.hasValue("fake")); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void shouldThrowExceptionWhenAttemptToGetValueButContainsMultiple() |
| 139 | + { |
| 140 | + final RequiredArg requiredArg = new RequiredArg("foo"); |
| 141 | + final SimpleDslArg otherArg = new OptionalArg("bar").setAllowMultipleValues(); |
| 142 | + final Map<String, List<String>> values = new HashMap<>(); |
| 143 | + values.put("foo", Collections.singletonList("abc")); |
| 144 | + values.put("bar", asList("123", "456")); |
| 145 | + final RepeatingParamValues params = new RepeatingParamValues(asList(requiredArg, otherArg).toArray(new DslArg[0]), values); |
| 146 | + |
| 147 | + final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, |
| 148 | + () -> params.value("bar") |
| 149 | + ); |
| 150 | + |
| 151 | + assertEquals("values() should be used when multiple values are allowed", exception.getMessage()); |
| 152 | + } |
| 153 | +} |
0 commit comments