Felix Meschberger | e3c0e4b | 2010-09-08 12:07:47 +0000 | [diff] [blame^] | 1 | /* |
Felix Meschberger | 642b48e | 2007-04-11 18:14:28 +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 |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 9 | * |
Felix Meschberger | 642b48e | 2007-04-11 18:14:28 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 11 | * |
Felix Meschberger | 642b48e | 2007-04-11 18:14:28 +0000 | [diff] [blame] | 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. |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 18 | */ |
| 19 | package org.apache.felix.cm.file; |
| 20 | |
| 21 | |
| 22 | import java.io.File; |
| 23 | import java.io.IOException; |
| 24 | import java.lang.reflect.Array; |
| 25 | import java.util.Arrays; |
| 26 | import java.util.Dictionary; |
| 27 | import java.util.Enumeration; |
| 28 | import java.util.Hashtable; |
| 29 | import java.util.Vector; |
| 30 | |
| 31 | import junit.framework.TestCase; |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * The <code>FilePersistenceManagerTest</code> TODO |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 36 | */ |
| 37 | public class FilePersistenceManagerTest extends TestCase |
| 38 | { |
| 39 | private File file = new File( System.getProperty( "java.io.tmpdir" ), "config" ); |
| 40 | |
| 41 | private FilePersistenceManager fpm; |
| 42 | |
| 43 | |
| 44 | protected void setUp() throws Exception |
| 45 | { |
| 46 | super.setUp(); |
| 47 | |
| 48 | fpm = new FilePersistenceManager( file.getAbsolutePath() ); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | protected void tearDown() throws Exception |
| 53 | { |
| 54 | File[] children = file.listFiles(); |
| 55 | for ( int i = 0; children != null && i < children.length; i++ ) |
| 56 | { |
| 57 | children[i].delete(); |
| 58 | } |
| 59 | file.delete(); |
| 60 | |
| 61 | super.tearDown(); |
| 62 | } |
| 63 | |
| 64 | |
Felix Meschberger | 6b086e7 | 2007-07-03 09:49:39 +0000 | [diff] [blame] | 65 | public void testPidPlain() |
| 66 | { |
| 67 | assertEquals( "plain", FilePersistenceManager.encodePid( "plain" ) ); |
| 68 | assertEquals( "plain" + File.separatorChar + "path", FilePersistenceManager.encodePid( "plain.path" ) ); |
| 69 | assertEquals( "encod%00e8", FilePersistenceManager.encodePid( "encod\u00E8" ) ); |
| 70 | assertEquals( "encod%00e8" + File.separatorChar + "path", FilePersistenceManager.encodePid( "encod\u00E8/path" ) ); |
| 71 | assertEquals( "encode" + File.separatorChar + "%1234" + File.separatorChar + "path", FilePersistenceManager |
| 72 | .encodePid( "encode/\u1234/path" ) ); |
| 73 | assertEquals( "encode" + File.separatorChar + " %0025 " + File.separatorChar + "path", FilePersistenceManager |
| 74 | .encodePid( "encode/ % /path" ) ); |
| 75 | } |
| 76 | |
| 77 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 78 | public void testCreateDir() |
| 79 | { |
| 80 | assertTrue( file.isDirectory() ); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | public void testSimple() throws IOException |
| 85 | { |
| 86 | check( "String", "String Value" ); |
| 87 | check( "Integer", new Integer( 2 ) ); |
| 88 | check( "Long", new Long( 2 ) ); |
| 89 | check( "Float", new Float( 2 ) ); |
| 90 | check( "Double", new Double( 2 ) ); |
| 91 | check( "Byte", new Byte( ( byte ) 2 ) ); |
| 92 | check( "Short", new Short( ( short ) 2 ) ); |
| 93 | check( "Character", new Character( 'a' ) ); |
| 94 | check( "Boolean", Boolean.TRUE ); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | public void testQuoting() throws IOException |
| 99 | { |
Felix Meschberger | e3c0e4b | 2010-09-08 12:07:47 +0000 | [diff] [blame^] | 100 | check( "QuotingSeparators", "\\()[]{}.,=\"\"''" ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 101 | check( "QuotingWellKnown", "BSP:\b, TAB:\t, LF:\n, FF:\f, CR:\r" ); |
| 102 | check( "QuotingControl", new String( new char[] |
| 103 | { 5, 10, 32, 64 } ) ); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | public void testArray() throws IOException |
| 108 | { |
| 109 | check( "StringArray", new String[] |
| 110 | { "one", "two", "three" } ); |
| 111 | check( "IntArray", new int[] |
| 112 | { 0, 1, 2 } ); |
| 113 | check( "IntegerArray", new Integer[] |
| 114 | { new Integer( 0 ), new Integer( 1 ), new Integer( 2 ) } ); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | public void testVector() throws IOException |
| 119 | { |
| 120 | check( "StringVector", new Vector( Arrays.asList( new String[] |
Felix Meschberger | aeb8c5c | 2009-01-14 19:52:32 +0000 | [diff] [blame] | 121 | { "one", "two", "three" } ) ) ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 122 | check( "IntegerVector", new Vector( Arrays.asList( new Integer[] |
Felix Meschberger | aeb8c5c | 2009-01-14 19:52:32 +0000 | [diff] [blame] | 123 | { new Integer( 0 ), new Integer( 1 ), new Integer( 2 ) } ) ) ); |
| 124 | } |
Felix Meschberger | e3c0e4b | 2010-09-08 12:07:47 +0000 | [diff] [blame^] | 125 | |
| 126 | |
Felix Meschberger | aeb8c5c | 2009-01-14 19:52:32 +0000 | [diff] [blame] | 127 | public void testList() throws IOException |
| 128 | { |
| 129 | check( "StringList", Arrays.asList( new String[] |
| 130 | { "one", "two", "three" } ) ); |
| 131 | check( "IntegerList", Arrays.asList( new Integer[] |
| 132 | { new Integer( 0 ), new Integer( 1 ), new Integer( 2 ) } ) ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
| 136 | public void testMultiValue() throws IOException |
| 137 | { |
| 138 | Dictionary props = new Hashtable(); |
| 139 | props.put( "String", "String Value" ); |
| 140 | props.put( "Integer", new Integer( 2 ) ); |
| 141 | props.put( "Long", new Long( 2 ) ); |
| 142 | props.put( "Float", new Float( 2 ) ); |
| 143 | props.put( "Double", new Double( 2 ) ); |
| 144 | props.put( "Byte", new Byte( ( byte ) 2 ) ); |
| 145 | props.put( "Short", new Short( ( short ) 2 ) ); |
| 146 | props.put( "Character", new Character( 'a' ) ); |
| 147 | props.put( "Boolean", Boolean.TRUE ); |
| 148 | props.put( "Array", new boolean[] |
| 149 | { true, false } ); |
| 150 | |
| 151 | check( "MultiValue", props ); |
| 152 | } |
| 153 | |
| 154 | |
Felix Meschberger | e3c0e4b | 2010-09-08 12:07:47 +0000 | [diff] [blame^] | 155 | // test configuration keys not conforming to the recommended specification |
| 156 | // for configuration keys in OSGi CM 1.3, 104.4.2, Configuration Properties |
| 157 | public void testNonSpecKeys() throws IOException { |
| 158 | check( "with\ttab", "the value" ); |
| 159 | check( "with blank", "the value" ); |
| 160 | check( "\\()[]{}.,=\"\"''", "quoted key" ); |
| 161 | check( "\"with quotes\"", "key with quotes" ); |
| 162 | check( "=leading equals", "leading equals" ); |
| 163 | } |
| 164 | |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 165 | private void check( String name, Object value ) throws IOException |
| 166 | { |
| 167 | Dictionary props = new Hashtable(); |
| 168 | props.put( name, value ); |
| 169 | |
| 170 | check( name, props ); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | private void check( String pid, Dictionary props ) throws IOException |
| 175 | { |
| 176 | fpm.store( pid, props ); |
| 177 | |
Felix Meschberger | e3c0e4b | 2010-09-08 12:07:47 +0000 | [diff] [blame^] | 178 | assertTrue( new File( file, FilePersistenceManager.encodePid( pid ) + ".config" ).exists() ); |
Felix Meschberger | add2b4a | 2007-04-11 18:12:33 +0000 | [diff] [blame] | 179 | |
| 180 | Dictionary loaded = fpm.load( pid ); |
| 181 | assertNotNull( loaded ); |
| 182 | assertEquals( props.size(), loaded.size() ); |
| 183 | |
| 184 | for ( Enumeration pe = props.keys(); pe.hasMoreElements(); ) |
| 185 | { |
| 186 | String key = ( String ) pe.nextElement(); |
| 187 | checkValues( props.get( key ), loaded.get( key ) ); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | |
| 192 | private void checkValues( Object value1, Object value2 ) |
| 193 | { |
| 194 | assertNotNull( value2 ); |
| 195 | if ( value1.getClass().isArray() ) |
| 196 | { |
| 197 | assertTrue( value2.getClass().isArray() ); |
| 198 | assertEquals( value1.getClass().getComponentType(), value2.getClass().getComponentType() ); |
| 199 | assertEquals( Array.getLength( value1 ), Array.getLength( value2 ) ); |
| 200 | for ( int i = 0; i < Array.getLength( value1 ); i++ ) |
| 201 | { |
| 202 | assertEquals( Array.get( value1, i ), Array.get( value2, i ) ); |
| 203 | } |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | assertEquals( value1, value2 ); |
| 208 | } |
| 209 | } |
| 210 | } |