LDAP-257: More JUnit style cleanup.

This commit is contained in:
Mattias Hellborg Arthursson
2013-08-27 13:35:44 +02:00
parent 469549d04e
commit 36cbb8894e
6 changed files with 61 additions and 182 deletions

View File

@@ -15,24 +15,20 @@
*/
package org.springframework.ldap.core.simple;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import org.junit.Before;
import org.junit.Test;
import org.springframework.ldap.core.ObjectRetrievalException;
import javax.naming.Binding;
import javax.naming.NamingException;
import javax.naming.ldap.Control;
import javax.naming.ldap.HasControls;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.runner.RunWith;
import org.springframework.ldap.core.ObjectRetrievalException;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
/**
* Unit tests for the {@link ContextMapperCallbackHandlerWithControlsTest}
@@ -40,8 +36,7 @@ import org.springframework.ldap.core.ObjectRetrievalException;
*
* @author Ulrik Sandberg
*/
@RunWith(JUnit4ClassRunner.class)
public class ContextMapperCallbackHandlerWithControlsTest extends TestCase {
public class ContextMapperCallbackHandlerWithControlsTest {
private ParameterizedContextMapperWithControls<Object> mapperMock;
@@ -62,20 +57,10 @@ public class ContextMapperCallbackHandlerWithControlsTest extends TestCase {
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
super.setUp();
mapperMock = createMock(ParameterizedContextMapperWithControls.class);
tested = new ContextMapperCallbackHandlerWithControls(mapperMock);
}
@After
public void tearDown() throws Exception {
super.tearDown();
mapperMock = null;
tested = null;
}
@Test(expected = IllegalArgumentException.class)
public void testConstructorWithEmptyArgument() {
new ContextMapperCallbackHandlerWithControls(null);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2010 the original author or authors.
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,76 +15,70 @@
*/
package org.springframework.ldap.util;
import org.junit.Before;
import org.junit.Test;
import org.springframework.ldap.support.ListComparator;
import java.util.Arrays;
import java.util.List;
import org.springframework.ldap.support.ListComparator;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Tests for ListComparator.
*
*
* @author Mattias Hellborg Arthursson
*/
public class ListComparatorTest extends TestCase {
public class ListComparatorTest {
private ListComparator tested;
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
tested = new ListComparator();
}
protected void tearDown() throws Exception {
super.tearDown();
}
@Test
public void testCompare_Equals() {
List list1 = Arrays.asList(new Object[] { new Integer(0),
new Integer(0) });
List list2 = Arrays.asList(new Object[] { new Integer(0),
new Integer(0) });
List<Integer> list1 = Arrays.asList(0, 0);
List<Integer> list2 = Arrays.asList(0, 0);
int result = tested.compare(list1, list2);
assertEquals(0, result);
}
@Test
public void testCompare_Less() {
List list1 = Arrays.asList(new Object[] { new Integer(0),
new Integer(0) });
List list2 = Arrays.asList(new Object[] { new Integer(0),
new Integer(1) });
List<Integer> list1 = Arrays.asList(0, 0);
List<Integer> list2 = Arrays.asList(0, 1);
int result = tested.compare(list1, list2);
assertTrue(result < 0);
}
@Test
public void testCompare_Greater() {
List list1 = Arrays.asList(new Object[] { new Integer(0),
new Integer(1) });
List list2 = Arrays.asList(new Object[] { new Integer(0),
new Integer(0) });
List<Integer> list1 = Arrays.asList(0, 1);
List<Integer> list2 = Arrays.asList(0, 0);
int result = tested.compare(list1, list2);
assertTrue(result > 0);
}
@Test
public void testCompare_Longer() {
List list1 = Arrays.asList(new Object[] { new Integer(0),
new Integer(0), new Integer(0) });
List list2 = Arrays.asList(new Object[] { new Integer(0),
new Integer(0) });
List<Integer> list1 = Arrays.asList(0, 0, 0);
List<Integer> list2 = Arrays.asList(0, 0);
int result = tested.compare(list1, list2);
assertTrue(result > 0);
}
@Test
public void testCompare_Shorter() {
List list1 = Arrays.asList(new Object[] { new Integer(0),
new Integer(0) });
List list2 = Arrays.asList(new Object[] { new Integer(0),
new Integer(0), new Integer(0) });
List<Integer> list1 = Arrays.asList(0, 0);
List<Integer> list2 = Arrays.asList(0, 0, 0);
int result = tested.compare(list1, list2);
assertTrue(result < 0);

View File

@@ -10,7 +10,6 @@ ext.junitVersion = '4.10'
ext.commonsPoolVersion = '1.5.4'
ext.commonsLangVersion = '2.4'
ext.commonsLoggingVersion = '1.0.4'
ext.easyMockVersion = '1.2_Java1.3'
ext.gsbaseVersion = '2.0.1'
ext.log4jVersion = '1.2.15'
ext.mockitoVersion = '1.9.5'

View File

@@ -6,6 +6,6 @@ dependencies {
provided "com.sun:ldapbp:1.0"
testCompile "junit:junit:$junitVersion",
"easymock:easymock:$easyMockVersion",
"org.mockito:mockito-core:$mockitoVersion",
"gsbase:gsbase:$gsbaseVersion"
}

View File

@@ -1,58 +0,0 @@
/*
* Copyright 2005-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.control;
import java.util.Arrays;
import javax.naming.ldap.Control;
import org.easymock.AbstractMatcher;
public class ControlArrayMatcher extends AbstractMatcher {
protected boolean argumentMatches(Object expected, Object actual) {
Control[] expectedControls = (Control[]) expected;
Control[] actualControls = (Control[]) actual;
if (expectedControls.length != actualControls.length) {
return false;
}
for (int i = 0; i < actualControls.length; i++) {
Control actualControl = actualControls[i];
Control expectedControl = expectedControls[i];
if (actualControl == null && expectedControl != null) {
return false;
}
if (actualControl != null && expectedControl == null) {
return false;
}
if (actualControl == null && expectedControl == null) {
continue;
}
if (!actualControl.getClass().equals(expectedControl.getClass())) {
return false;
}
}
return true;
}
protected String argumentToString(Object argument) {
if (argument instanceof Control[]) {
Control[] control = (Control[]) argument;
return Arrays.toString(control);
}
return super.argumentToString(argument);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2005-2010 the original author or authors.
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,87 +15,46 @@
*/
package org.springframework.ldap.control;
import java.io.IOException;
import javax.naming.ldap.Control;
import javax.naming.ldap.LdapContext;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.ldap.OperationNotSupportedException;
import com.sun.jndi.ldap.Ber;
import com.sun.jndi.ldap.BerDecoder;
import com.sun.jndi.ldap.BerEncoder;
import com.sun.jndi.ldap.ctl.SortControl;
import com.sun.jndi.ldap.ctl.VirtualListViewControl;
import com.sun.jndi.ldap.ctl.VirtualListViewResponseControl;
import junit.framework.AssertionFailedError;
import org.junit.Before;
import org.junit.Test;
import org.springframework.ldap.OperationNotSupportedException;
import javax.naming.ldap.Control;
import javax.naming.ldap.LdapContext;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Unit tests for the VirtualListViewControlDirContextProcessor class.
*
* @author Ulrik Sandberg
*/
public class VirtualListViewControlDirContextProcessorTest extends TestCase {
public class VirtualListViewControlDirContextProcessorTest {
private static final String OID_REQUEST = "2.16.840.1.113730.3.4.9";
private static final String OID_RESPONSE = "2.16.840.1.113730.3.4.10";
private MockControl ldapContextControl;
private LdapContext ldapContextMock;
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
// Create ldapContext mock
ldapContextControl = MockControl.createControl(LdapContext.class);
ldapContextMock = (LdapContext) ldapContextControl.getMock();
ldapContextMock = mock(LdapContext.class);
}
protected void tearDown() throws Exception {
super.tearDown();
ldapContextControl = null;
ldapContextMock = null;
}
protected void replay() {
ldapContextControl.replay();
}
protected void verify() {
ldapContextControl.verify();
}
/* public void testPreProcess() throws Exception {
int pageSize = 5;
VirtualListViewControlAggregateDirContextProcessor tested = new VirtualListViewControlAggregateDirContextProcessor(
"cn",
pageSize);
ldapContextControl.expectAndReturn(
ldapContextMock.getRequestControls(), new Control[0]);
SortControl sortControl = new SortControl(new String[] { "cn" }, true);
VirtualListViewControl vlvControl = new VirtualListViewControl(0, 0, 0,
0, true);
Control[] controls = new Control[] { sortControl, vlvControl };
ldapContextMock.setRequestControls(controls);
// just check that class names match
ldapContextControl.setMatcher(new ControlArrayMatcher());
replay();
tested.preProcess(ldapContextMock);
verify();
}
*/
@Test
public void testCreateRequestControlWithTargetAsOffset() throws Exception {
int pageSize = 5;
int targetOffset = 25;
@@ -118,6 +77,7 @@ public class VirtualListViewControlDirContextProcessorTest extends TestCase {
new byte[0]);
}
@Test
public void testCreateRequestControlWithTargetAsPercentage()
throws Exception {
int pageSize = 5;
@@ -143,6 +103,7 @@ public class VirtualListViewControlDirContextProcessorTest extends TestCase {
new byte[0]);
}
@Test
public void testPostProcess() throws Exception {
int pageSize = 5;
int targetOffset = 25;
@@ -156,10 +117,7 @@ public class VirtualListViewControlDirContextProcessorTest extends TestCase {
virtualListViewResult);
VirtualListViewResponseControl control = new VirtualListViewResponseControl(
OID_RESPONSE, false, encoded);
ldapContextControl.expectAndDefaultReturn(ldapContextMock
.getResponseControls(), new Control[] { control });
replay();
when(ldapContextMock.getResponseControls()).thenReturn(new Control[]{control});
try {
tested.postProcess(ldapContextMock);
@@ -172,11 +130,12 @@ public class VirtualListViewControlDirContextProcessorTest extends TestCase {
assertEquals("[LDAP: error code 53 - Unwilling To Perform]", cause
.getMessage());
}
verify();
assertNotNull(tested.getCookie());
assertEquals(0, tested.getCookie().getCookie().length);
}
@Test
public void testBerDecoding() throws Exception {
int virtualListViewResult = 53; // unwilling to perform
byte[] encoded = encodeResponseValue(10, 1000, virtualListViewResult);