Felix Meschberger | fc30620 | 2010-02-18 10:05:13 +0000 | [diff] [blame] | 1 | /* |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.metatype; |
| 20 | |
Carsten Ziegeler | 50cf6c2 | 2013-09-03 18:03:27 +0000 | [diff] [blame] | 21 | import java.util.HashMap; |
| 22 | import java.util.Map; |
| 23 | |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 24 | import junit.framework.TestCase; |
| 25 | |
| 26 | import org.osgi.service.metatype.AttributeDefinition; |
| 27 | |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 28 | /** |
| 29 | * The <code>ADTest</code> class tests the static helper methods of the |
| 30 | * {@link AD} class. |
Felix Meschberger | fc30620 | 2010-02-18 10:05:13 +0000 | [diff] [blame] | 31 | * |
Richard S. Hall | 3bc8afd | 2009-08-12 19:03:19 +0000 | [diff] [blame] | 32 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 33 | */ |
| 34 | public class ADTest extends TestCase |
| 35 | { |
| 36 | |
| 37 | private static final String BLANK = " \r\n \t"; |
| 38 | |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 39 | public void testNull() |
| 40 | { |
| 41 | String listString = null; |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 42 | String[] list = AD.splitList(listString); |
| 43 | assertNull(list); |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 46 | public void testEmpty() |
| 47 | { |
| 48 | String listString = ""; |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 49 | String[] list = AD.splitList(listString); |
| 50 | assertNotNull(list); |
| 51 | assertEquals(1, list.length); |
| 52 | assertEquals(listString, list[0]); |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 55 | public void testSingle() |
| 56 | { |
| 57 | String value0 = "value"; |
| 58 | String listString = value0; |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 59 | String[] list = AD.splitList(listString); |
| 60 | assertNotNull(list); |
| 61 | assertEquals(1, list.length); |
| 62 | assertEquals(value0, list[0]); |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 65 | public void testTwo() |
| 66 | { |
| 67 | String value0 = "value0"; |
| 68 | String value1 = "value1"; |
| 69 | String listString = value0 + "," + value1; |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 70 | String[] list = AD.splitList(listString); |
| 71 | assertNotNull(list); |
| 72 | assertEquals(2, list.length); |
| 73 | assertEquals(value0, list[0]); |
| 74 | assertEquals(value1, list[1]); |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Jan Willem Janssen | 4c8292d | 2012-11-15 16:48:35 +0000 | [diff] [blame] | 77 | public void testEmptySecond() |
| 78 | { |
| 79 | String value0 = "value0"; |
| 80 | String value1 = ""; |
| 81 | String listString = value0 + ","; |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 82 | String[] list = AD.splitList(listString); |
| 83 | assertNotNull(list); |
| 84 | assertEquals(2, list.length); |
| 85 | assertEquals(value0, list[0]); |
| 86 | assertEquals(value1, list[1]); |
Jan Willem Janssen | 4c8292d | 2012-11-15 16:48:35 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | public void testSpacedSecond() |
| 90 | { |
| 91 | String value0 = "value0"; |
| 92 | String value1 = ""; |
| 93 | String listString = value0 + ", "; |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 94 | String[] list = AD.splitList(listString); |
| 95 | assertNotNull(list); |
| 96 | assertEquals(2, list.length); |
| 97 | assertEquals(value0, list[0]); |
| 98 | assertEquals(value1, list[1]); |
Jan Willem Janssen | 4c8292d | 2012-11-15 16:48:35 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 101 | public void testSingleBlanks() |
| 102 | { |
| 103 | String value0 = "value"; |
| 104 | String listString = BLANK + value0 + BLANK; |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 105 | String[] list = AD.splitList(listString); |
| 106 | assertNotNull(list); |
| 107 | assertEquals(1, list.length); |
| 108 | assertEquals(value0, list[0]); |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 111 | public void testTwoBlanks() |
| 112 | { |
| 113 | String value0 = "value0"; |
| 114 | String value1 = "value1"; |
| 115 | String listString = BLANK + value0 + BLANK + "," + BLANK + value1 + BLANK; |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 116 | String[] list = AD.splitList(listString); |
| 117 | assertNotNull(list); |
| 118 | assertEquals(2, list.length); |
| 119 | assertEquals(value0, list[0]); |
| 120 | assertEquals(value1, list[1]); |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Carsten Ziegeler | 8807b7b | 2013-10-24 12:34:04 +0000 | [diff] [blame] | 123 | public void testSpaces() |
| 124 | { |
| 125 | String value = "Hello World"; |
Jan Willem Janssen | 01b31f3 | 2015-01-21 16:25:18 +0000 | [diff] [blame] | 126 | String listString = BLANK + value + BLANK + "," + BLANK + value + BLANK + "," + value; |
Carsten Ziegeler | 8807b7b | 2013-10-24 12:34:04 +0000 | [diff] [blame] | 127 | String[] list = AD.splitList(listString); |
| 128 | assertNotNull(list); |
| 129 | assertEquals(3, list.length); |
| 130 | assertEquals(value, list[0]); |
| 131 | assertEquals(value, list[1]); |
| 132 | assertEquals(value, list[2]); |
| 133 | } |
| 134 | |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 135 | public void testStandardSample() |
| 136 | { |
| 137 | String value0 = "a,b"; |
| 138 | String value1 = "b,c"; |
Jan Willem Janssen | 4c8292d | 2012-11-15 16:48:35 +0000 | [diff] [blame] | 139 | String value2 = " c\\"; |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 140 | String value3 = "d"; |
Jan Willem Janssen | 4c8292d | 2012-11-15 16:48:35 +0000 | [diff] [blame] | 141 | String listString = "a\\,b,b\\,c,\\ c\\\\,d"; |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 142 | String[] list = AD.splitList(listString); |
| 143 | assertNotNull(list); |
| 144 | assertEquals(4, list.length); |
| 145 | assertEquals(value0, list[0]); |
| 146 | assertEquals(value1, list[1]); |
| 147 | assertEquals(value2, list[2]); |
| 148 | assertEquals(value3, list[3]); |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 151 | public void testToTypeString() |
| 152 | { |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 153 | assertEquals(AttributeDefinition.STRING, AD.toType("String")); |
| 154 | assertEquals(AttributeDefinition.LONG, AD.toType("Long")); |
| 155 | assertEquals(AttributeDefinition.DOUBLE, AD.toType("Double")); |
| 156 | assertEquals(AttributeDefinition.FLOAT, AD.toType("Float")); |
| 157 | assertEquals(AttributeDefinition.INTEGER, AD.toType("Integer")); |
| 158 | assertEquals(AttributeDefinition.BYTE, AD.toType("Byte")); |
| 159 | assertEquals(AttributeDefinition.CHARACTER, AD.toType("Char")); |
Jan Willem Janssen | 3c9335d | 2015-01-21 09:35:35 +0000 | [diff] [blame] | 160 | assertEquals(AttributeDefinition.CHARACTER, AD.toType("Character")); |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 161 | assertEquals(AttributeDefinition.BOOLEAN, AD.toType("Boolean")); |
| 162 | assertEquals(AttributeDefinition.SHORT, AD.toType("Short")); |
| 163 | assertEquals(AttributeDefinition.PASSWORD, AD.toType("Password")); |
| 164 | assertEquals(AttributeDefinition.STRING, AD.toType("JohnDoe")); |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 165 | } |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 166 | |
Jan Willem Janssen | 4c8292d | 2012-11-15 16:48:35 +0000 | [diff] [blame] | 167 | /** |
Carsten Ziegeler | 50cf6c2 | 2013-09-03 18:03:27 +0000 | [diff] [blame] | 168 | * FELIX-3757: if an AD has only its 'required' property set, but no |
| 169 | * min/max or option values defined, the validation still should detect |
| 170 | * empty values. |
Jan Willem Janssen | 4c8292d | 2012-11-15 16:48:35 +0000 | [diff] [blame] | 171 | */ |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 172 | public void testValidateRequiredValueWithMinimalOptions() |
| 173 | { |
| 174 | AD ad = new AD(); |
| 175 | ad.setType("Integer"); |
| 176 | ad.setRequired(true); |
Jan Willem Janssen | 4c8292d | 2012-11-15 16:48:35 +0000 | [diff] [blame] | 177 | |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 178 | assertEquals(AD.VALIDATE_MISSING, ad.validate(null)); |
Jan Willem Janssen | 4c8292d | 2012-11-15 16:48:35 +0000 | [diff] [blame] | 179 | } |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 180 | |
Jan Willem Janssen | 4c8292d | 2012-11-15 16:48:35 +0000 | [diff] [blame] | 181 | /** |
| 182 | * FELIX-3756: if an AD is optional, but its validate method is called |
| 183 | * with invalid data, the value is regarded missing. |
| 184 | */ |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 185 | public void testValidateOptionalValueWithInvalidData() |
| 186 | { |
| 187 | AD ad = new AD(); |
| 188 | ad.setType("Integer"); |
| 189 | ad.setRequired(false); |
| 190 | |
| 191 | assertEquals(AD.VALIDATE_INVALID_VALUE, ad.validate("abc")); |
Jan Willem Janssen | 4c8292d | 2012-11-15 16:48:35 +0000 | [diff] [blame] | 192 | } |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 193 | |
Jan Willem Janssen | 4c8292d | 2012-11-15 16:48:35 +0000 | [diff] [blame] | 194 | /** |
| 195 | * FELIX-3758: if an AD has a cardinality != 0, the validation method |
| 196 | * cannot handle a comma-separated input. |
| 197 | */ |
Jan Willem Janssen | 46bff2b | 2012-11-16 10:15:04 +0000 | [diff] [blame] | 198 | public void testValidateValueWithMultiValueCardinality() |
| 199 | { |
| 200 | AD ad = new AD(); |
| 201 | ad.setType("Integer"); |
| 202 | ad.setCardinality(2); |
| 203 | ad.setRequired(true); |
| 204 | |
| 205 | assertEquals("", ad.validate("1,2")); |
Jan Willem Janssen | 4c8292d | 2012-11-15 16:48:35 +0000 | [diff] [blame] | 206 | } |
Carsten Ziegeler | 50cf6c2 | 2013-09-03 18:03:27 +0000 | [diff] [blame] | 207 | |
| 208 | /** |
Jan Willem Janssen | 01b31f3 | 2015-01-21 16:25:18 +0000 | [diff] [blame] | 209 | * FELIX-3884 : if an AD has options, default values must be in the option values. |
Carsten Ziegeler | 50cf6c2 | 2013-09-03 18:03:27 +0000 | [diff] [blame] | 210 | */ |
| 211 | public void testOptionsAndDefaultValues() |
| 212 | { |
Jan Willem Janssen | 01b31f3 | 2015-01-21 16:25:18 +0000 | [diff] [blame] | 213 | AD ad = new AD(); |
| 214 | ad.setCardinality(2); |
Carsten Ziegeler | 50cf6c2 | 2013-09-03 18:03:27 +0000 | [diff] [blame] | 215 | ad.setType("String"); |
Jan Willem Janssen | 01b31f3 | 2015-01-21 16:25:18 +0000 | [diff] [blame] | 216 | ad.setRequired(false); |
| 217 | |
| 218 | Map options = new HashMap(); |
Carsten Ziegeler | 50cf6c2 | 2013-09-03 18:03:27 +0000 | [diff] [blame] | 219 | options.put("A", "L-A"); |
| 220 | options.put("B", "L-B"); |
| 221 | ad.setOptions(options); |
| 222 | |
Jan Willem Janssen | 01b31f3 | 2015-01-21 16:25:18 +0000 | [diff] [blame] | 223 | ad.setDefaultValue("A,B"); |
| 224 | assertArrayEquals(new String[] { "A", "B" }, ad.getDefaultValue()); |
Carsten Ziegeler | 50cf6c2 | 2013-09-03 18:03:27 +0000 | [diff] [blame] | 225 | |
Jan Willem Janssen | 01b31f3 | 2015-01-21 16:25:18 +0000 | [diff] [blame] | 226 | ad.setDefaultValue("A,B,C"); |
| 227 | assertArrayEquals(new String[] { "A", "B" }, ad.getDefaultValue()); |
Carsten Ziegeler | 50cf6c2 | 2013-09-03 18:03:27 +0000 | [diff] [blame] | 228 | |
Jan Willem Janssen | 01b31f3 | 2015-01-21 16:25:18 +0000 | [diff] [blame] | 229 | ad.setDefaultValue("X,Y,B"); |
| 230 | assertArrayEquals(new String[] { "B" }, ad.getDefaultValue()); |
Carsten Ziegeler | 50cf6c2 | 2013-09-03 18:03:27 +0000 | [diff] [blame] | 231 | |
Jan Willem Janssen | 01b31f3 | 2015-01-21 16:25:18 +0000 | [diff] [blame] | 232 | ad.setDefaultValue("X,Y,Z"); |
| 233 | assertArrayEquals(new String[0], ad.getDefaultValue()); |
| 234 | |
| 235 | ad.setDefaultValue(null); |
Carsten Ziegeler | 50cf6c2 | 2013-09-03 18:03:27 +0000 | [diff] [blame] | 236 | assertNull(ad.getDefaultValue()); |
| 237 | } |
| 238 | |
Jan Willem Janssen | 01b31f3 | 2015-01-21 16:25:18 +0000 | [diff] [blame] | 239 | /** |
| 240 | * FELIX-3884/FELIX-4665 - Default values. |
| 241 | */ |
| 242 | public void testDefaultValuesForSingleValuedAttributes() |
| 243 | { |
| 244 | AD ad = new AD(); |
| 245 | ad.setCardinality(0); |
| 246 | ad.setType("String"); |
| 247 | ad.setRequired(false); |
| 248 | |
| 249 | ad.setDefaultValue(null); |
| 250 | assertNull(ad.getDefaultValue()); |
| 251 | |
| 252 | ad.setDefaultValue("A,B"); |
| 253 | assertArrayEquals(new String[] { "A" }, ad.getDefaultValue()); |
| 254 | |
| 255 | ad.setDefaultValue(""); |
| 256 | assertArrayEquals(new String[] { "" }, ad.getDefaultValue()); |
| 257 | |
| 258 | // corner case: in case of required values, an empty default makes no sense |
| 259 | // for single values, hence that the empty default is coerced into null... |
| 260 | ad.setRequired(true); |
| 261 | ad.setDefaultValue(""); |
| 262 | assertNull(ad.getDefaultValue()); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * FELIX-3884/FELIX-4665 - Default values. |
| 267 | */ |
| 268 | public void testDefaultValuesForMultiValuedAttributes() |
| 269 | { |
| 270 | AD ad = new AD(); |
| 271 | ad.setCardinality(-2); // sign doesn't matter in this case |
| 272 | ad.setType("String"); |
| 273 | ad.setRequired(false); |
| 274 | |
| 275 | ad.setDefaultValue(null); |
| 276 | assertNull(ad.getDefaultValue()); |
| 277 | |
| 278 | ad.setDefaultValue("A,B"); |
| 279 | assertArrayEquals(new String[] { "A", "B" }, ad.getDefaultValue()); |
| 280 | |
| 281 | ad.setDefaultValue(",,"); |
| 282 | assertArrayEquals(new String[] { "", "" }, ad.getDefaultValue()); |
| 283 | |
| 284 | ad.setDefaultValue(""); |
| 285 | assertArrayEquals(new String[] { "" }, ad.getDefaultValue()); |
| 286 | |
| 287 | // corner case: in case of required values, an empty default is coerced |
| 288 | // into a empty array... |
| 289 | ad.setRequired(true); |
| 290 | ad.setDefaultValue(""); |
| 291 | assertArrayEquals(new String[0], ad.getDefaultValue()); |
| 292 | } |
| 293 | |
| 294 | private static void assertArrayEquals(String[] a, String[] b) |
Carsten Ziegeler | 50cf6c2 | 2013-09-03 18:03:27 +0000 | [diff] [blame] | 295 | { |
| 296 | assertEquals(a.length, b.length); |
Jan Willem Janssen | 01b31f3 | 2015-01-21 16:25:18 +0000 | [diff] [blame] | 297 | for (int i = 0; i < a.length; i++) |
Carsten Ziegeler | 50cf6c2 | 2013-09-03 18:03:27 +0000 | [diff] [blame] | 298 | { |
| 299 | assertEquals(a[i], b[i]); |
| 300 | } |
| 301 | } |
Felix Meschberger | cb7c1e7 | 2007-04-11 18:27:12 +0000 | [diff] [blame] | 302 | } |