Skip to content
Permalink
Browse files
Upgrade tests to junit 4
  • Loading branch information
Amichai Rothman committed May 28, 2019
1 parent 47133d4 commit 344ab766905477c2b77f039fb1bff6c8da5932fe
Showing 6 changed files with 100 additions and 48 deletions.
@@ -20,6 +20,7 @@

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.junit.Assert.assertEquals;

import java.util.Dictionary;

@@ -33,7 +34,7 @@
import org.easymock.Capture;
import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.junit.Assert;
import org.junit.Test;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
import org.osgi.framework.ServiceEvent;
@@ -42,10 +43,9 @@
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;

import junit.framework.TestCase;

public class HttpServiceManagerTest extends TestCase {
public class HttpServiceManagerTest {

@Test
public void testGetAbsoluteAddress() {
HttpServiceManager manager = new HttpServiceManager();
manager.initFromConfig(null);
@@ -57,6 +57,7 @@ public void testGetAbsoluteAddress() {
assertEquals("http://localhost:8181/mycontext/myservice", address2);
}

@Test
public void testRegisterAndUnregisterServlet() throws Exception {
IMocksControl c = EasyMock.createControl();
BundleContext dswContext = c.createMock(BundleContext.class);
@@ -99,7 +100,7 @@ static class DummyHttpService implements HttpService {
@SuppressWarnings("rawtypes")
public void registerServlet(String alias, Servlet servlet, Dictionary initparams, HttpContext context)
throws ServletException {
Assert.assertEquals("/myService", alias);
assertEquals("/myService", alias);
servlet.init(config);
}

@@ -30,17 +30,20 @@
import javax.servlet.http.HttpServletResponse;

import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpContext;

import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

@SuppressWarnings({
"unchecked", "rawtypes"
})
public class SecurityDelegatingHttpContextTest extends TestCase {
public class SecurityDelegatingHttpContextTest {

protected HttpContext defaultHttpContext;
protected SecurityDelegatingHttpContext httpContext;
@@ -50,7 +53,7 @@ public class SecurityDelegatingHttpContextTest extends TestCase {
protected String mimeType;
protected URL url; // does not need to exist

@Override
@Before
public void setUp() throws Exception {
mimeType = "text/xml";
url = new URL("file:test.xml"); // does not need to exist
@@ -67,6 +70,7 @@ public void setUp() throws Exception {
EasyMock.replay(defaultHttpContext);
}

@Test
public void testFilterRequired() throws Exception {
// Mock up the service references
ServiceReference[] serviceReferences = {};
@@ -89,14 +93,15 @@ public void testFilterRequired() throws Exception {
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.replay(response);
boolean requestAllowed = httpContext.handleSecurity(request, response);
Assert.assertFalse(requestAllowed);
assertFalse(requestAllowed);

// Ensure that the httpContext returns true if there is no requirement for registered servlet filters
httpContext.requireFilter = false;
requestAllowed = httpContext.handleSecurity(request, response);
Assert.assertTrue(requestAllowed);
assertTrue(requestAllowed);
}

@Test
public void testSingleCommitFilter() throws Exception {
// Mock up the service references
ServiceReference filterReference = EasyMock.createNiceMock(ServiceReference.class);
@@ -123,14 +128,15 @@ public void testSingleCommitFilter() throws Exception {
// return value
EasyMock.expect(response.getWriter()).andReturn(new PrintWriter(System.out));
EasyMock.replay(response);
Assert.assertFalse(httpContext.handleSecurity(request, response));
assertFalse(httpContext.handleSecurity(request, response));

// Ensure that the appropriate filters were called
Assert.assertTrue(commitFilter.called);
Assert.assertFalse(doNothingFilter.called);
Assert.assertFalse(accessDeniedFilter.called);
assertTrue(commitFilter.called);
assertFalse(doNothingFilter.called);
assertFalse(accessDeniedFilter.called);
}

@Test
public void testFilterChain() throws Exception {
// Mock up the service references
ServiceReference filterReference = EasyMock.createNiceMock(ServiceReference.class);
@@ -158,14 +164,15 @@ public void testFilterChain() throws Exception {
EasyMock.expect(response.isCommitted()).andReturn(true); // the commit filter indicating that it committed the
// response
EasyMock.replay(response);
Assert.assertFalse(httpContext.handleSecurity(request, response));
assertFalse(httpContext.handleSecurity(request, response));

// Ensure that the appropriate filters were called
Assert.assertTrue(doNothingFilter.called);
Assert.assertTrue(commitFilter.called);
Assert.assertFalse(accessDeniedFilter.called);
assertTrue(doNothingFilter.called);
assertTrue(commitFilter.called);
assertFalse(accessDeniedFilter.called);
}

@Test
public void testAllowRequest() throws Exception {
// Mock up the service references
ServiceReference filterReference = EasyMock.createNiceMock(ServiceReference.class);
@@ -188,14 +195,15 @@ public void testAllowRequest() throws Exception {
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.expect(response.isCommitted()).andReturn(false);
EasyMock.replay(response);
Assert.assertTrue(httpContext.handleSecurity(request, response));
assertTrue(httpContext.handleSecurity(request, response));

// Ensure that the appropriate filters were called
Assert.assertTrue(doNothingFilter.called);
Assert.assertFalse(commitFilter.called);
Assert.assertFalse(accessDeniedFilter.called);
assertTrue(doNothingFilter.called);
assertFalse(commitFilter.called);
assertFalse(accessDeniedFilter.called);
}

@Test
public void testDelegation() {
BundleContext bundleContext = EasyMock.createNiceMock(BundleContext.class);
EasyMock.replay(bundleContext);
@@ -204,8 +212,8 @@ public void testDelegation() {
httpContext = new SecurityDelegatingHttpContext(bundleContext, defaultHttpContext);

// Ensure that it delegates non-security calls to the wrapped implementation (in this case, the mock)
Assert.assertEquals(mimeType, httpContext.getMimeType(""));
Assert.assertEquals(url, httpContext.getResource(""));
assertEquals(mimeType, httpContext.getMimeType(""));
assertEquals(url, httpContext.getResource(""));
}
}

@@ -25,23 +25,29 @@
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.osgi.service.remoteserviceadmin.EndpointDescription;
import org.osgi.service.remoteserviceadmin.RemoteConstants;

import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class PropertyHelperTest extends TestCase {
public class PropertyHelperTest {

@Test
public void testMultiValuePropertyAsString() {
assertEquals(Collections.singleton("hi"),
PropertyHelper.getMultiValueProperty("hi"));
PropertyHelper.getMultiValueProperty("hi"));
}

@Test
public void testMultiValuePropertyAsArray() {
assertEquals(Arrays.asList("a", "b"),
PropertyHelper.getMultiValueProperty(new String[] {"a", "b"}));
PropertyHelper.getMultiValueProperty(new String[]{"a", "b"}));
}

@Test
public void testMultiValuePropertyAsCollection() {
List<String> list = new ArrayList<>();
list.add("1");
@@ -50,10 +56,12 @@ public void testMultiValuePropertyAsCollection() {
assertEquals(list, PropertyHelper.getMultiValueProperty(list));
}

@Test
public void testMultiValuePropertyNull() {
assertTrue(PropertyHelper.getMultiValueProperty(null).isEmpty());
}

@Test
public void testGetProperty() {
Map<String, Object> p = new HashMap<>();
p.put(RemoteConstants.ENDPOINT_ID, "http://google.de");
@@ -64,8 +72,8 @@ public void testGetProperty() {
EndpointDescription endpoint = new EndpointDescription(p);

assertNull(PropertyHelper.getProperty(endpoint.getProperties(), "unknownProp"));
assertEquals(p.get(RemoteConstants.ENDPOINT_ID),
assertEquals(p.get(RemoteConstants.ENDPOINT_ID),
PropertyHelper.getProperty(endpoint.getProperties(), RemoteConstants.ENDPOINT_ID));
assertEquals(null, PropertyHelper.getProperty(endpoint.getProperties(), "notAString"));
assertNull(PropertyHelper.getProperty(endpoint.getProperties(), "notAString"));
}
}
@@ -21,28 +21,34 @@
import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;

import org.easymock.EasyMock;
import org.easymock.IAnswer;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

@SuppressWarnings("rawtypes")
public class InterfaceRuleTest extends TestCase {
public class InterfaceRuleTest {

@Test
public void testDUMMY() {
assertTrue(true);
}

@Test
public void testInterfaceRuleGetBundle() {
Bundle b = EasyMock.createMock(Bundle.class);
EasyMock.replay(b);
InterfaceRule ir = new InterfaceRule(b, "org.apache.Foo");
assertSame(b, ir.getBundle());
}

@Test
public void testInterfaceRule1() {
InterfaceRule ir = new InterfaceRule(null, "org.apache.Foo");
ir.addProperty("x", "y", String.class.getName());
@@ -60,6 +66,7 @@ public void testInterfaceRule1() {
assertEquals(expected, m);
}

@Test
public void testInterfaceRule2() {
InterfaceRule ir = new InterfaceRule(null, "org.apache.F(.*)");
ir.addPropMatch("boo", "baah");
@@ -79,6 +86,7 @@ public void testInterfaceRule2() {
assertEquals(expected, m);
}

@Test
public void testInterfaceRule3() {
InterfaceRule ir = new InterfaceRule(null, "org.apache.F(.*)");
ir.addProperty("x", "y", String.class.getName());
@@ -93,6 +101,7 @@ public void testInterfaceRule3() {
assertEquals(0, m.size());
}

@Test
public void testInterfaceRule4() {
InterfaceRule ir = new InterfaceRule(null, "org.apache.F(.*)");
ir.addPropMatch("boo", "baah");
@@ -107,6 +116,7 @@ public void testInterfaceRule4() {
assertEquals(0, m.size());
}

@Test
public void testInterfaceRule5() {
InterfaceRule ir = new InterfaceRule(null, "org.apache.Foo");
ir.addPropMatch("test.int", "42");
@@ -127,6 +137,7 @@ public void testInterfaceRule5() {
assertEquals(expected, m);
}

@Test
public void testInterfaceRule6() {
InterfaceRule ir = new InterfaceRule(null, "org.apache.Foo");
ir.addPropMatch("test.int", "42");

0 comments on commit 344ab76

Please sign in to comment.