You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by fc...@apache.org on 2011/06/22 17:33:06 UTC
svn commit: r1138506 - in /incubator/stanbol/trunk/commons/testing/http:
pom.xml
src/main/java/org/apache/stanbol/commons/testing/http/BundleContextMock.java
src/main/java/org/apache/stanbol/commons/testing/http/ServletContextMock.java
Author: fchrist
Date: Wed Jun 22 15:33:06 2011
New Revision: 1138506
URL: http://svn.apache.org/viewvc?rev=1138506&view=rev
Log:
Added two mocks for ServletContext and BundleContext.
Added:
incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/BundleContextMock.java
incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/ServletContextMock.java
Modified:
incubator/stanbol/trunk/commons/testing/http/pom.xml
Modified: incubator/stanbol/trunk/commons/testing/http/pom.xml
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/testing/http/pom.xml?rev=1138506&r1=1138505&r2=1138506&view=diff
==============================================================================
--- incubator/stanbol/trunk/commons/testing/http/pom.xml (original)
+++ incubator/stanbol/trunk/commons/testing/http/pom.xml Wed Jun 22 15:33:06 2011
@@ -43,6 +43,17 @@
<artifactId>commons-io</artifactId>
<version>2.0.1</version>
</dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <version>2.4</version>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <version>4.1.0</version>
+ </dependency>
+
</dependencies>
<build>
Added: incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/BundleContextMock.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/BundleContextMock.java?rev=1138506&view=auto
==============================================================================
--- incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/BundleContextMock.java (added)
+++ incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/BundleContextMock.java Wed Jun 22 15:33:06 2011
@@ -0,0 +1,185 @@
+package org.apache.stanbol.commons.testing.http;
+
+import java.io.File;
+import java.io.InputStream;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.BundleListener;
+import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkListener;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ * Simple Mock to mock a BundleContext in tests.
+ *
+ * <p>
+ * Use the {@link #putService(String clazz, Object instance)} method to put add a service instance to
+ * the BundleContextMock.
+ *
+ * @author Fabian Christ
+ */
+public class BundleContextMock implements BundleContext {
+
+ private Map<String,ServiceReference> serviceReferenceMap = new HashMap<String,ServiceReference>();
+ private Map<ServiceReference, Object> serviceMap = new HashMap<ServiceReference,Object>();
+
+ public void addBundleListener(BundleListener listener) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void addFrameworkListener(FrameworkListener listener) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void addServiceListener(ServiceListener listener) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void addServiceListener(ServiceListener listener, String filter) throws InvalidSyntaxException {
+ // TODO Auto-generated method stub
+
+ }
+
+ public Filter createFilter(String filter) throws InvalidSyntaxException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public ServiceReference[] getAllServiceReferences(String clazz, String filter) throws InvalidSyntaxException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Bundle getBundle() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Bundle getBundle(long id) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Bundle[] getBundles() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public File getDataFile(String filename) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getProperty(String key) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Object getService(ServiceReference reference) {
+ return this.serviceMap.get(reference);
+ }
+
+ /**
+ * Use this method to add a service instance to the mock.
+ *
+ * @param clazz FQCN of the class (or the interface)
+ * @param instance An instance of that class.
+ */
+ public void putService(String clazz, Object instance) {
+ ServiceReference dummy = new ServiceReference() {
+
+ public boolean isAssignableTo(Bundle bundle, String className) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public Bundle[] getUsingBundles() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String[] getPropertyKeys() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Object getProperty(String key) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Bundle getBundle() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int compareTo(Object reference) {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+ };
+ this.serviceReferenceMap.put(clazz, dummy);
+ this.serviceMap.put(dummy, instance);
+ }
+
+ public ServiceReference getServiceReference(String clazz) {
+ return this.serviceReferenceMap.get(clazz);
+ }
+
+ public ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Bundle installBundle(String location) throws BundleException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Bundle installBundle(String location, InputStream input) throws BundleException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public ServiceRegistration registerService(String[] clazzes, Object service, Dictionary properties) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public ServiceRegistration registerService(String clazz, Object service, Dictionary properties) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public void removeBundleListener(BundleListener listener) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void removeFrameworkListener(FrameworkListener listener) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void removeServiceListener(ServiceListener listener) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public boolean ungetService(ServiceReference reference) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+}
Added: incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/ServletContextMock.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/ServletContextMock.java?rev=1138506&view=auto
==============================================================================
--- incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/ServletContextMock.java (added)
+++ incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/ServletContextMock.java Wed Jun 22 15:33:06 2011
@@ -0,0 +1,158 @@
+package org.apache.stanbol.commons.testing.http;
+
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.Servlet;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+
+import org.osgi.framework.BundleContext;
+
+/**
+ * Simple Mock class that can be used to mock the ServletContext in tests.
+ *
+ * <p>
+ * By default the attribute map contains a {@link org.apache.stanbol.commons.testing.http.BundleContextMock}
+ * under the name "org.osgi.framework.BundleContext".
+ *
+ * @author Fabian Christ
+ */
+public class ServletContextMock implements ServletContext {
+
+ private Map<String,Object> attributeMap = new HashMap<String,Object>();
+
+ public ServletContextMock() {
+ this.attributeMap.put(BundleContext.class.getName(), new BundleContextMock());
+ }
+
+ public Object getAttribute(String name) {
+ return this.attributeMap.get(name);
+ }
+
+ public Enumeration getAttributeNames() {
+ return Collections.enumeration(this.attributeMap.keySet());
+ }
+
+ public void putAttribute(String name, Object value) {
+ this.attributeMap.put(name, value);
+ }
+
+ public ServletContext getContext(String uripath) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getInitParameter(String name) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Enumeration getInitParameterNames() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int getMajorVersion() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public String getMimeType(String file) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int getMinorVersion() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public RequestDispatcher getNamedDispatcher(String name) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getRealPath(String path) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public RequestDispatcher getRequestDispatcher(String path) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public URL getResource(String path) throws MalformedURLException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public InputStream getResourceAsStream(String path) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Set getResourcePaths(String path) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getServerInfo() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Servlet getServlet(String name) throws ServletException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getServletContextName() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Enumeration getServletNames() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Enumeration getServlets() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public void log(String msg) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void log(Exception exception, String msg) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void log(String message, Throwable throwable) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void removeAttribute(String name) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setAttribute(String name, Object object) {
+ // TODO Auto-generated method stub
+
+ }
+
+}