Felix Meschberger | 40d4e1b | 2008-03-26 10:41:00 +0000 | [diff] [blame] | 1 | /* |
| 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.cm.impl; |
| 20 | |
Felix Meschberger | 88d6d10 | 2009-01-19 09:48:53 +0000 | [diff] [blame] | 21 | |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.Collection; |
| 24 | import java.util.HashSet; |
| 25 | import java.util.List; |
| 26 | import java.util.Vector; |
| 27 | |
Felix Meschberger | 40d4e1b | 2008-03-26 10:41:00 +0000 | [diff] [blame] | 28 | import junit.framework.TestCase; |
| 29 | |
Felix Meschberger | 88d6d10 | 2009-01-19 09:48:53 +0000 | [diff] [blame] | 30 | |
Felix Meschberger | 40d4e1b | 2008-03-26 10:41:00 +0000 | [diff] [blame] | 31 | public class CaseInsensitiveDictionaryTest extends TestCase |
| 32 | { |
| 33 | |
Felix Meschberger | 88d6d10 | 2009-01-19 09:48:53 +0000 | [diff] [blame] | 34 | public void testCheckValueNull() |
| 35 | { |
| 36 | // null which must throw IllegalArgumentException |
| 37 | try |
| 38 | { |
| 39 | CaseInsensitiveDictionary.checkValue( null ); |
| 40 | fail( "Expected IllegalArgumentException for null value" ); |
| 41 | } |
| 42 | catch ( IllegalArgumentException iae ) |
| 43 | { |
| 44 | |
| 45 | } |
| 46 | |
| 47 | } |
| 48 | |
| 49 | |
| 50 | public void testCheckValueSimple() |
| 51 | { |
| 52 | internalTestCheckValue( "String" ); |
| 53 | internalTestCheckValue( new Integer( 1 ) ); |
| 54 | internalTestCheckValue( new Long( 1 ) ); |
| 55 | internalTestCheckValue( new Float( 1 ) ); |
| 56 | internalTestCheckValue( new Double( 1 ) ); |
| 57 | internalTestCheckValue( new Byte( ( byte ) 1 ) ); |
| 58 | internalTestCheckValue( new Short( ( short ) 1 ) ); |
| 59 | internalTestCheckValue( new Character( 'a' ) ); |
| 60 | internalTestCheckValue( Boolean.TRUE ); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | public void testCheckValueSimpleArray() |
| 65 | { |
| 66 | internalTestCheckValue( new String[] |
| 67 | { "String" } ); |
| 68 | internalTestCheckValue( new Integer[] |
| 69 | { new Integer( 1 ) } ); |
| 70 | internalTestCheckValue( new Long[] |
| 71 | { new Long( 1 ) } ); |
| 72 | internalTestCheckValue( new Float[] |
| 73 | { new Float( 1 ) } ); |
| 74 | internalTestCheckValue( new Double[] |
| 75 | { new Double( 1 ) } ); |
| 76 | internalTestCheckValue( new Byte[] |
| 77 | { new Byte( ( byte ) 1 ) } ); |
| 78 | internalTestCheckValue( new Short[] |
| 79 | { new Short( ( short ) 1 ) } ); |
| 80 | internalTestCheckValue( new Character[] |
| 81 | { new Character( 'a' ) } ); |
| 82 | internalTestCheckValue( new Boolean[] |
| 83 | { Boolean.TRUE } ); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | public void testCheckValuePrimitiveArray() |
| 88 | { |
| 89 | internalTestCheckValue( new long[] |
| 90 | { 1 } ); |
| 91 | internalTestCheckValue( new int[] |
| 92 | { 1 } ); |
| 93 | internalTestCheckValue( new short[] |
| 94 | { 1 } ); |
| 95 | internalTestCheckValue( new char[] |
| 96 | { 1 } ); |
| 97 | internalTestCheckValue( new byte[] |
| 98 | { 1 } ); |
| 99 | internalTestCheckValue( new double[] |
| 100 | { 1 } ); |
| 101 | internalTestCheckValue( new float[] |
| 102 | { 1 } ); |
| 103 | internalTestCheckValue( new boolean[] |
| 104 | { true } ); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | public void testCheckValueSimpleVector() |
| 109 | { |
| 110 | internalTestCheckValue( "String", Vector.class ); |
| 111 | internalTestCheckValue( new Integer( 1 ), Vector.class ); |
| 112 | internalTestCheckValue( new Long( 1 ), Vector.class ); |
| 113 | internalTestCheckValue( new Float( 1 ), Vector.class ); |
| 114 | internalTestCheckValue( new Double( 1 ), Vector.class ); |
| 115 | internalTestCheckValue( new Byte( ( byte ) 1 ), Vector.class ); |
| 116 | internalTestCheckValue( new Short( ( short ) 1 ), Vector.class ); |
| 117 | internalTestCheckValue( new Character( 'a' ), Vector.class ); |
| 118 | internalTestCheckValue( Boolean.TRUE, Vector.class ); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | public void testCheckValueSimpleSet() |
| 123 | { |
| 124 | internalTestCheckValue( "String", HashSet.class ); |
| 125 | internalTestCheckValue( new Integer( 1 ), HashSet.class ); |
| 126 | internalTestCheckValue( new Long( 1 ), HashSet.class ); |
| 127 | internalTestCheckValue( new Float( 1 ), HashSet.class ); |
| 128 | internalTestCheckValue( new Double( 1 ), HashSet.class ); |
| 129 | internalTestCheckValue( new Byte( ( byte ) 1 ), HashSet.class ); |
| 130 | internalTestCheckValue( new Short( ( short ) 1 ), HashSet.class ); |
| 131 | internalTestCheckValue( new Character( 'a' ), HashSet.class ); |
| 132 | internalTestCheckValue( Boolean.TRUE, HashSet.class ); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | public void testCheckValueSimpleArrayList() |
| 137 | { |
| 138 | internalTestCheckValue( "String", ArrayList.class ); |
| 139 | internalTestCheckValue( new Integer( 1 ), ArrayList.class ); |
| 140 | internalTestCheckValue( new Long( 1 ), ArrayList.class ); |
| 141 | internalTestCheckValue( new Float( 1 ), ArrayList.class ); |
| 142 | internalTestCheckValue( new Double( 1 ), ArrayList.class ); |
| 143 | internalTestCheckValue( new Byte( ( byte ) 1 ), ArrayList.class ); |
| 144 | internalTestCheckValue( new Short( ( short ) 1 ), ArrayList.class ); |
| 145 | internalTestCheckValue( new Character( 'a' ), ArrayList.class ); |
| 146 | internalTestCheckValue( Boolean.TRUE, ArrayList.class ); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | private void internalTestCheckValue( Object value, Class collectionType ) |
| 151 | { |
| 152 | Collection coll; |
| 153 | try |
| 154 | { |
| 155 | coll = ( Collection ) collectionType.newInstance(); |
| 156 | } |
| 157 | catch ( Throwable t ) |
| 158 | { |
| 159 | throw new IllegalArgumentException( collectionType + " cannot be instantiated as a Collection" ); |
| 160 | } |
| 161 | |
| 162 | coll.add( value ); |
| 163 | internalTestCheckValue( coll ); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | private void internalTestCheckValue( Object value ) |
| 168 | { |
| 169 | assertEqualValue( value, CaseInsensitiveDictionary.checkValue( value ) ); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | private void assertEqualValue( Object expected, Object actual ) |
| 174 | { |
| 175 | if ( ( expected instanceof Collection ) && ( actual instanceof Collection ) ) |
| 176 | { |
| 177 | Collection eColl = ( Collection ) expected; |
| 178 | Collection aColl = ( Collection ) actual; |
| 179 | if ( eColl.size() != aColl.size() ) |
| 180 | { |
| 181 | fail( "Unexpected size. expected:" + eColl.size() + ", actual: " + aColl.size() ); |
| 182 | } |
| 183 | |
| 184 | // create a list from the expected collection and remove |
| 185 | // all values from the actual collection, this should get |
| 186 | // an empty collection |
| 187 | List eList = new ArrayList( eColl ); |
| 188 | eList.removeAll( aColl ); |
| 189 | assertTrue( "Collections do not match. expected:" + eColl + ", actual: " + aColl, eList.isEmpty() ); |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | assertEquals( expected, actual ); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | |
| 198 | public void testValidKeys() |
| 199 | { |
Felix Meschberger | 40d4e1b | 2008-03-26 10:41:00 +0000 | [diff] [blame] | 200 | CaseInsensitiveDictionary.checkKey( "a" ); |
| 201 | CaseInsensitiveDictionary.checkKey( "1" ); |
| 202 | CaseInsensitiveDictionary.checkKey( "-" ); |
| 203 | CaseInsensitiveDictionary.checkKey( "_" ); |
| 204 | CaseInsensitiveDictionary.checkKey( "A" ); |
| 205 | CaseInsensitiveDictionary.checkKey( "a.b.c" ); |
| 206 | CaseInsensitiveDictionary.checkKey( "a.1.c" ); |
| 207 | CaseInsensitiveDictionary.checkKey( "a-sample.dotted_key.end" ); |
| 208 | } |
Felix Meschberger | 88d6d10 | 2009-01-19 09:48:53 +0000 | [diff] [blame] | 209 | |
| 210 | |
| 211 | public void testKeyDots() |
| 212 | { |
Felix Meschberger | e3c0e4b | 2010-09-08 12:07:47 +0000 | [diff] [blame^] | 213 | // FELIX-2184 these keys are all valid (but not recommended) |
| 214 | CaseInsensitiveDictionary.checkKey( "." ); |
| 215 | CaseInsensitiveDictionary.checkKey( "a.b.c." ); |
| 216 | CaseInsensitiveDictionary.checkKey( ".a.b.c." ); |
| 217 | CaseInsensitiveDictionary.checkKey( "a..b" ); |
| 218 | |
| 219 | // valid key as of OSGi Compendium R4.2 (CM 1.3) |
Felix Meschberger | 5e18fac | 2009-10-09 10:28:53 +0000 | [diff] [blame] | 220 | CaseInsensitiveDictionary.checkKey( ".a.b.c" ); |
Felix Meschberger | 40d4e1b | 2008-03-26 10:41:00 +0000 | [diff] [blame] | 221 | } |
Felix Meschberger | 88d6d10 | 2009-01-19 09:48:53 +0000 | [diff] [blame] | 222 | |
| 223 | |
| 224 | public void testKeyIllegalCharacters() |
| 225 | { |
Felix Meschberger | 5e18fac | 2009-10-09 10:28:53 +0000 | [diff] [blame] | 226 | testFailingKey( null ); |
| 227 | testFailingKey( "" ); |
Felix Meschberger | e3c0e4b | 2010-09-08 12:07:47 +0000 | [diff] [blame^] | 228 | |
| 229 | // FELIX-2184 these keys are all valid (but not recommended) |
| 230 | CaseInsensitiveDictionary.checkKey( " " ); |
| 231 | CaseInsensitiveDictionary.checkKey( "ยง" ); |
| 232 | CaseInsensitiveDictionary.checkKey( "${yikes}" ); |
| 233 | CaseInsensitiveDictionary.checkKey( "a key with spaces" ); |
| 234 | CaseInsensitiveDictionary.checkKey( "fail:key" ); |
Felix Meschberger | 40d4e1b | 2008-03-26 10:41:00 +0000 | [diff] [blame] | 235 | } |
Felix Meschberger | 88d6d10 | 2009-01-19 09:48:53 +0000 | [diff] [blame] | 236 | |
| 237 | |
| 238 | private void testFailingKey( String key ) |
| 239 | { |
| 240 | try |
| 241 | { |
Felix Meschberger | 40d4e1b | 2008-03-26 10:41:00 +0000 | [diff] [blame] | 242 | CaseInsensitiveDictionary.checkKey( key ); |
Felix Meschberger | 88d6d10 | 2009-01-19 09:48:53 +0000 | [diff] [blame] | 243 | fail( "Expected IllegalArgumentException for key [" + key + "]" ); |
| 244 | } |
| 245 | catch ( IllegalArgumentException iae ) |
| 246 | { |
Felix Meschberger | 40d4e1b | 2008-03-26 10:41:00 +0000 | [diff] [blame] | 247 | // expected |
| 248 | } |
| 249 | } |
| 250 | } |