From 65f9d050b0dd51660c92573220be4d3a06ff9d85 Mon Sep 17 00:00:00 2001 From: Ulrik Sandberg Date: Thu, 30 Jul 2009 15:07:24 +0000 Subject: [PATCH] Added unit test structure to core-tiger, and added a test for the new ContextMapperCallbackHandlerWithControls. --- core-tiger/pom.xml | 7 + ...MapperCallbackHandlerWithControlsTest.java | 122 ++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 core-tiger/src/test/java/org/springframework/ldap/core/simple/ContextMapperCallbackHandlerWithControlsTest.java diff --git a/core-tiger/pom.xml b/core-tiger/pom.xml index 4ba353b0..1685f9e5 100644 --- a/core-tiger/pom.xml +++ b/core-tiger/pom.xml @@ -40,6 +40,13 @@ junit junit + 4.4 + test + + + org.easymock + easymock + 2.5.1 test diff --git a/core-tiger/src/test/java/org/springframework/ldap/core/simple/ContextMapperCallbackHandlerWithControlsTest.java b/core-tiger/src/test/java/org/springframework/ldap/core/simple/ContextMapperCallbackHandlerWithControlsTest.java new file mode 100644 index 00000000..4e443be1 --- /dev/null +++ b/core-tiger/src/test/java/org/springframework/ldap/core/simple/ContextMapperCallbackHandlerWithControlsTest.java @@ -0,0 +1,122 @@ +/* + * Copyright 2005-2008 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.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 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; + +/** + * Unit tests for the {@link ContextMapperCallbackHandlerWithControlsTest} + * class. + * + * @author Ulrik Sandberg + */ +@RunWith(JUnit4ClassRunner.class) +public class ContextMapperCallbackHandlerWithControlsTest extends TestCase { + + private ParameterizedContextMapperWithControls mapperMock; + + private ContextMapperCallbackHandlerWithControls tested; + + private static class MyBindingThatHasControls extends Binding implements HasControls { + private static final long serialVersionUID = 1L; + + public MyBindingThatHasControls(String name, Object obj) { + super(name, obj); + } + + public Control[] getControls() throws NamingException { + return null; + } + } + + @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); + } + + @Test + public void testGetObjectFromNameClassPair() { + Object expectedObject = "object"; + Object expectedResult = "result"; + Binding expectedBinding = new Binding("some name", expectedObject); + + expect(mapperMock.mapFromContext(expectedObject)).andReturn(expectedResult); + + replay(mapperMock); + Object actualResult = tested.getObjectFromNameClassPair(expectedBinding); + verify(mapperMock); + + assertEquals(expectedResult, actualResult); + } + + @Test + public void testGetObjectFromNameClassPairImplementingHasControls() { + Object expectedObject = "object"; + Object expectedResult = "result"; + MyBindingThatHasControls expectedBinding = new MyBindingThatHasControls("some name", expectedObject); + + expect(mapperMock.mapFromContextWithControls(expectedObject, expectedBinding)).andReturn(expectedResult); + + replay(mapperMock); + Object actualResult = tested.getObjectFromNameClassPair(expectedBinding); + verify(mapperMock); + + assertEquals(expectedResult, actualResult); + } + + @Test(expected = ObjectRetrievalException.class) + public void testGetObjectFromNameClassPairObjectRetrievalException() { + Binding expectedBinding = new Binding("some name", null); + + replay(mapperMock); + tested.getObjectFromNameClassPair(expectedBinding); + verify(mapperMock); + } +}