blob: b3a324425d4ba2405c71fe6327437214630a1697 [file] [log] [blame]
Felix Meschberger3f9e4da2009-08-17 07:52:39 +00001/*
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 */
19package org.apache.felix.cm.impl;
20
21
22import java.io.File;
23import java.io.IOException;
24import java.lang.reflect.Field;
25import java.util.Dictionary;
26
27import junit.framework.TestCase;
28
29import org.apache.felix.cm.MockBundle;
30import org.apache.felix.cm.MockBundleContext;
31import org.apache.felix.cm.file.FilePersistenceManager;
32import org.osgi.framework.Bundle;
33import org.osgi.framework.BundleContext;
34
35
36public class DynamicBindingsTest extends TestCase
37{
38
39 private File configLocation;
40
41 private File bindingsFile;
42
43 private FilePersistenceManager persistenceManager;
44
45 private static final String PID1 = "test.pid.1";
46
47 private static final String PID2 = "test.pid.2";
48
49 private static final String LOCATION1 = "test://location.1";
50
51 private static final String LOCATION2 = "test://location.2";
52
53
54 protected void setUp() throws Exception
55 {
56 super.setUp();
57
58 configLocation = new File( "target/config." + System.currentTimeMillis() );
59 persistenceManager = new FilePersistenceManager( configLocation.getAbsolutePath() );
60
61 bindingsFile = new File( configLocation, DynamicBindings.BINDINGS_FILE_NAME + ".config" );
62 }
63
64
65 protected void tearDown() throws Exception
66 {
67 bindingsFile.delete();
68 configLocation.delete();
69
70 super.tearDown();
71 }
72
73
74 public void test_no_bindings() throws IOException
75 {
76
77 // ensure there is no file
78 bindingsFile.delete();
79
80 final BundleContext ctx = new MockBundleContext();
81 final DynamicBindings dm = new DynamicBindings( ctx, persistenceManager );
82 final Dictionary bindings = getBindings( dm );
83
84 assertNotNull( bindings );
85 assertTrue( bindings.isEmpty() );
86 }
87
88
89 public void test_store_bindings() throws IOException
90 {
91 // ensure there is no file
92 bindingsFile.delete();
93
94 final BundleContext ctx = new MockBundleContext();
95 final DynamicBindings dm = new DynamicBindings( ctx, persistenceManager );
96
97 dm.putLocation( PID1, LOCATION1 );
98 assertEquals( LOCATION1, dm.getLocation( PID1 ) );
99
100 assertTrue( bindingsFile.exists() );
101
102 final Dictionary bindings = persistenceManager.load( DynamicBindings.BINDINGS_FILE_NAME );
103 assertNotNull( bindings );
104 assertEquals( 1, bindings.size() );
105 assertEquals( LOCATION1, bindings.get( PID1 ) );
106 }
107
108
109 public void test_store_and_load_bindings() throws IOException
110 {
111 // ensure there is no file
112 bindingsFile.delete();
113
114 // preset bindings
115 final DynamicBindings dm0 = new DynamicBindings( new MockBundleContext(), persistenceManager );
116 dm0.putLocation( PID1, LOCATION1 );
117
118 // check bindings
119 final BundleContext ctx = new DMTestMockBundleContext();
120 final DynamicBindings dm = new DynamicBindings( ctx, persistenceManager );
121
122 // API check
123 assertEquals( LOCATION1, dm.getLocation( PID1 ) );
124
125 // low level check
126 final Dictionary bindings = getBindings( dm );
127 assertNotNull( bindings );
128 assertEquals( 1, bindings.size() );
129 assertEquals( LOCATION1, bindings.get( PID1 ) );
130 }
131
132
133 public void test_store_and_load_bindings_with_cleanup() throws IOException
134 {
135 // ensure there is no file
136 bindingsFile.delete();
137
138 // preset bindings
139 final DynamicBindings dm0 = new DynamicBindings( new MockBundleContext(), persistenceManager );
140 dm0.putLocation( PID1, LOCATION1 );
141
142 // check bindings
143 final DynamicBindings dm = new DynamicBindings( new MockBundleContext(), persistenceManager );
144
145 // API check
146 assertNull( dm.getLocation( PID1 ) );
147
148 // low level check
149 final Dictionary bindings = getBindings( dm );
150 assertNotNull( bindings );
151 assertTrue( bindings.isEmpty() );
152 }
153
154
155 private static Dictionary getBindings( DynamicBindings dm )
156 {
157 try
158 {
159 final Field bindings = dm.getClass().getDeclaredField( "bindings" );
160 bindings.setAccessible( true );
161 return ( Dictionary ) bindings.get( dm );
162 }
163 catch ( Throwable t )
164 {
165 fail( "Cannot get bindings field: " + t );
166 return null;
167 }
168 }
169
170 private static class DMTestMockBundleContext extends MockBundleContext
171 {
172 public Bundle[] getBundles()
173 {
174 return new Bundle[]
175 { new MockBundle( this, LOCATION1 ) };
176 }
177 }
178}