View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.rcp.unit;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.net.URL;
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.Dictionary;
24  import java.util.Enumeration;
25  import java.util.Properties;
26  
27  import org.osgi.framework.Bundle;
28  import org.osgi.framework.BundleContext;
29  import org.osgi.framework.BundleException;
30  import org.osgi.framework.ServiceReference;
31  
32  /**
33   * テスト用の {@link Bundle} オブジェクトです。<br />
34   * 
35   * @author y-komori
36   */
37  public class MockBundle implements Bundle {
38      static final String SYMBOLIC_NAME = "org.seasar.uruma.rcp.core";
39  
40      protected MockBundleContext context;
41  
42      public Enumeration<?> findEntries(final String path,
43              final String filePattern, final boolean recurse) {
44          URL url = getClass().getClassLoader().getResource(
45                  getClass().getName() + ".class");
46          if (url != null) {
47              ArrayList<URL> entries = new ArrayList<URL>();
48              entries.add(url);
49              return Collections.enumeration(entries);
50          } else {
51              return getEmptyEnumeration();
52          }
53      }
54  
55      public BundleContext getBundleContext() {
56          if (context == null) {
57              context = new MockBundleContext(this);
58          }
59          return context;
60      }
61  
62      public long getBundleId() {
63          return 0;
64      }
65  
66      public URL getEntry(final String path) {
67          return null;
68      }
69  
70      public Enumeration<?> getEntryPaths(final String path) {
71          return getEmptyEnumeration();
72      }
73  
74      public Dictionary<?, ?> getHeaders() {
75          return new Properties();
76      }
77  
78      public Dictionary<?, ?> getHeaders(final String locale) {
79          return new Properties();
80      }
81  
82      public long getLastModified() {
83          return 0;
84      }
85  
86      public String getLocation() {
87          return null;
88      }
89  
90      public ServiceReference[] getRegisteredServices() {
91          return null;
92      }
93  
94      public URL getResource(final String name) {
95          return null;
96      }
97  
98      public Enumeration<?> getResources(final String name) throws IOException {
99          return getEmptyEnumeration();
100     }
101 
102     public ServiceReference[] getServicesInUse() {
103         return null;
104     }
105 
106     public int getState() {
107         return 0;
108     }
109 
110     public String getSymbolicName() {
111         return SYMBOLIC_NAME;
112     }
113 
114     public boolean hasPermission(final Object permission) {
115         return true;
116     }
117 
118     public Class<?> loadClass(final String name) throws ClassNotFoundException {
119         return Class.forName(name);
120     }
121 
122     public void start() throws BundleException {
123         // Do nothing.
124     }
125 
126     public void start(final int options) throws BundleException {
127         // Do nothing.
128     }
129 
130     public void stop() throws BundleException {
131         // Do nothing.
132     }
133 
134     public void stop(final int options) throws BundleException {
135         // Do nothing.
136     }
137 
138     public void uninstall() throws BundleException {
139         // Do nothing.
140     }
141 
142     public void update() throws BundleException {
143         // Do nothing.
144     }
145 
146     public void update(final InputStream in) throws BundleException {
147         // Do nothing.
148     }
149 
150     @SuppressWarnings("unchecked")
151     protected Enumeration<?> getEmptyEnumeration() {
152         return Collections.enumeration(Collections.EMPTY_LIST);
153     }
154 }