View Javadoc

1   /*
2    * Copyright 2006 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.springframework.osgi.mock;
17  
18  import java.net.URL;
19  import java.util.Dictionary;
20  import java.util.Enumeration;
21  
22  import org.osgi.framework.BundleContext;
23  
24  /**
25   * Mock Bundle that allows the entry to return on future calls.
26   * 
27   * @author Adrian Colyer
28   * 
29   */
30  public class EntryLookupControllingMockBundle extends MockBundle {
31  
32  	protected Enumeration nextFindResult = null;
33  
34  	protected URL nextEntryResult = null;
35  
36  	public EntryLookupControllingMockBundle(Dictionary headers) {
37  		super(headers);
38  	}
39  
40  	public void setResultsToReturnOnNextCallToFindEntries(String[] findResult) {
41  		if (findResult == null) {
42  			findResult = new String[0];
43  		}
44  		this.nextFindResult = createEnumerationOver(findResult);
45  	}
46  
47  	public Enumeration findEntries(String path, String filePattern, boolean recurse) {
48  		if (this.nextFindResult == null) {
49  			return super.findEntries(path, filePattern, recurse);
50  		}
51  		else {
52  			Enumeration ret = this.nextFindResult;
53  			this.nextFindResult = null;
54  			return ret;
55  		}
56  	}
57  
58  	public void setEntryReturnOnNextCallToGetEntry(URL entry) {
59  		this.nextEntryResult = entry;
60  	}
61  
62  	public URL getEntry(String name) {
63  		if (this.nextEntryResult != null) {
64  			URL result = this.nextEntryResult;
65  			this.nextEntryResult = null;
66  			return result;
67  		}
68  		else {
69  			return super.getEntry(name);
70  		}
71  	}
72  
73  	public URL getResource(String name) {
74  		return getEntry(name);
75  	}
76  
77  	// for OsgiResourceUtils
78  	public BundleContext getContext() {
79  		return super.getContext();
80  	}
81  
82  	protected Enumeration createEnumerationOver(String[] entries) {
83  		return new ArrayEnumerator(entries);
84  	}
85  }