blob: f4ef903574eb2f4c55ceb090e1d812e8fde8fa06 [file] [log] [blame]
Felix Meschbergere3c0e4b2010-09-08 12:07:47 +00001/*
Felix Meschberger642b48e2007-04-11 18:14:28 +00002 * 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 Meschbergeradd2b4a2007-04-11 18:12:33 +00009 *
Felix Meschberger642b48e2007-04-11 18:14:28 +000010 * http://www.apache.org/licenses/LICENSE-2.0
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000011 *
Felix Meschberger642b48e2007-04-11 18:14:28 +000012 * 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 Meschbergeradd2b4a2007-04-11 18:12:33 +000018 */
19package org.apache.felix.cm.file;
20
21
22import java.io.File;
23import java.io.IOException;
24import java.lang.reflect.Array;
25import java.util.Arrays;
26import java.util.Dictionary;
27import java.util.Enumeration;
28import java.util.Hashtable;
29import java.util.Vector;
30
31import junit.framework.TestCase;
32
33
34/**
35 * The <code>FilePersistenceManagerTest</code> TODO
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000036 */
37public 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 Meschberger6b086e72007-07-03 09:49:39 +000065 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 Meschbergeradd2b4a2007-04-11 18:12:33 +000078 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 Meschbergere3c0e4b2010-09-08 12:07:47 +0000100 check( "QuotingSeparators", "\\()[]{}.,=\"\"''" );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000101 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 Meschbergeraeb8c5c2009-01-14 19:52:32 +0000121 { "one", "two", "three" } ) ) );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000122 check( "IntegerVector", new Vector( Arrays.asList( new Integer[]
Felix Meschbergeraeb8c5c2009-01-14 19:52:32 +0000123 { new Integer( 0 ), new Integer( 1 ), new Integer( 2 ) } ) ) );
124 }
Felix Meschbergere3c0e4b2010-09-08 12:07:47 +0000125
126
Felix Meschbergeraeb8c5c2009-01-14 19:52:32 +0000127 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 Meschbergeradd2b4a2007-04-11 18:12:33 +0000133 }
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 Meschbergere3c0e4b2010-09-08 12:07:47 +0000155 // 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 Meschbergeradd2b4a2007-04-11 18:12:33 +0000165 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 Meschbergere3c0e4b2010-09-08 12:07:47 +0000178 assertTrue( new File( file, FilePersistenceManager.encodePid( pid ) + ".config" ).exists() );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000179
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}