/*---------------------------------------------------------------------------
* Speculoos, LDAP to objet mapping.
* Copyright (C) 2006 Norsys and oQube
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Created on 27 sept. 2005
* Author: Arnaud Bailly
* --------------------------------------------------------------------------*/
package speculoos.manager;
import java.util.HashMap;
import java.util.Map;
import org.jmock.Mock;
import org.jmock.MockObjectTestCase;
import speculoos.core.Mapper;
import speculoos.core.MapperException;
import speculoos.spi.Source;
/**
* This class tests the life-cycle aspects of the mapper without any reference
* to specific mapper issues.
*
* @author nono
* @version $Id: MapperManageTest.java 259 2006-05-23 10:34:50Z /C=FR/ST=Nord/L=Lille/O=Norsys SA/OU=UE/CN=Arnaud Bailly/emailAddress=abailly@norsys.fr $
*/
public class MapperManageTest extends MockObjectTestCase {
/* states */
int state = 0;
private Mock mockmapper;
private Mock mocksrc;
private MapperManager manager;
private Mapper mp1;
private Source src1;
private HashMap env;
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
/* create stub and driver */
mockmapper = mock(Mapper.class);
mockmapper.expects(atLeastOnce()).method("getName");
mocksrc = mock(Source.class);
/* create manager */
manager = new MapperManager();
mp1 = (Mapper) mockmapper.proxy();
src1 = (Source) mocksrc.proxy();
manager.addMapper("map", mp1);
manager.addSource("source", src1);
manager.link("map", "source");
this.env = new HashMap();
env.put("toto", "tutu");
manager.addParameter("toto", "tutu");
manager.setConfigured();
/* manager is configured */
}
/**
* Basic mapper lifecycle testing. Ensures source gets a chance to prepare
* mapper for operation.
*
* @throws MapperException
*/
public void test01BasicManage() throws MapperException {
/* configure */
/* expect */
mocksrc.expects(once()).method("start").with(ANYTHING);
mocksrc.expects(once()).method("create").with(eq("map"), ANYTHING);
mocksrc.expects(once()).method("release").with(same(mp1));
mocksrc.expects(once()).method("stop");
/* get/release mapper */
manager.start();
Mapper mp = manager.getMapper("map");
/* we know m == mp */
manager.release(mp1);
manager.stop();
}
/**
* Start and stop are idempotent
*
* @throws MapperException
*
*/
public void test02DoubleStartStop() throws MapperException {
/* expect */
mocksrc.expects(once()).method("start").with(ANYTHING);
mocksrc.expects(once()).method("stop");
/* get/release mapper */
manager.start();
manager.start();
manager.stop();
manager.stop();
}
/**
* Test parameter passing to source objects.
*
* @throws MapperException
*
*/
public void test03ParameterPassingToSource() throws MapperException {
/* expect */
mocksrc.expects(once()).method("start").with(eq(env));
manager.start();
}
// //////////////////////////////////////////////////////
// ERROR CASES
// //////////////////////////////////////////////////////
public void test04ReleaseNullMapper() throws MapperException {
mocksrc.expects(once()).method("start").with(ANYTHING);
manager.start();
try {
manager.release(null);
} catch (IllegalArgumentException e) {
// OK
} catch (MapperException e) {
fail(e.getLocalizedMessage());
}
}
public void test05ResetAfterStart() throws MapperException {
mocksrc.expects(once()).method("start").with(ANYTHING);
manager.start();
try {
manager.reset();
fail("Should have thrown exception");
} catch (MapperConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void test06ReleaseUnknownMapperSource() throws MapperException {
Mapper m = new Mapper() {
public Object map(Object input, Map param) throws MapperException {
// TODO Auto-generated method stub
return null;
}
public String getName() {
// TODO Auto-generated method stub
return null;
}
};
mocksrc.expects(once()).method("start").with(ANYTHING);
mocksrc.expects(once()).method("release").with(same(m)).will(throwException(new MapperException("")));
manager.start();
try {
manager.release(m);
fail("Should have thrown exceptin");
} catch (MapperException e) {
// OK
}
}
/**
* Manager is not configured properly
*/
public void test07GetMapperErrorNotconfigured() throws MapperException {
try {
manager.getMapper("toto");
fail("Should have thrown mapper config exception");
} catch (MapperConfigurationException e) {
}
}
/**
* Manager is not ready
*
* @throws MapperException
*/
public void test08GetMapperErrorNotReady() throws MapperException {
try {
Mapper m = manager.getMapper("map");
fail("Should have thrown mapper config exception");
} catch (MapperConfigurationException e) {
}
}
/**
* Mapper is not registered
*
* @throws MapperException
*/
public void test09GetMapperErrorNotRegistered() throws MapperException {
try {
mocksrc.expects(once()).method("start").with(eq(env));
manager.start();
Mapper m = manager.getMapper("mip");
fail("Should have thrown mapper config exception");
} catch (MapperConfigurationException e) {
}
}
/**
* Mapper has no source
*
* @throws MapperException
*/
public void test10GetMapperErrorNoSource() throws MapperException {
try {
manager.reset();
manager.addMapper("map", mp1);
manager.setConfigured();
manager.start();
Mapper m = manager.getMapper("map");
fail("Should have thrown mapper config exception");
} catch (MapperConfigurationException e) {
}
}
/**
* Release unregistered mapper.
*
* @throws MapperException
*/
public void test11ReleaseUnknownMapper() throws MapperException {
Mapper m = new Mapper() {
public Object map(Object input, Map param) throws MapperException {
// TODO Auto-generated method stub
return null;
}
public String getName() {
return "toto";
}
};
mocksrc.expects(once()).method("start").with(ANYTHING);
manager.start();
try {
manager.release(m);
fail("Should have thrown exceptin");
} catch (MapperException e) {
// OK
}
}
public void test12ConfigureStartedMapper() throws MapperException {
mocksrc.expects(once()).method("start").with(ANYTHING);
manager.start();
try {
manager.setConfigured();
fail("Should have thrown exception");
} catch (MapperConfigurationException e) {
// OK
}
}
}