/*---------------------------------------------------------------------------
* 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
* --------------------------------------------------------------------------*/
package speculoos.jndi;
import java.io.IOException;
import org.jmock.Mock;
import org.jmock.MockObjectTestCase;
import speculoos.config.xml.XMLConfigurator;
import speculoos.config.xml.XMLConfiguratorException;
import speculoos.manager.Configure;
import speculoos.manager.MapperConfigurationException;
public class XMLConfigTest extends MockObjectTestCase {
private Mock mockconfigure;
protected void setUp() throws Exception {
super.setUp();
mockconfigure = mock(Configure.class);
}
/**
* Simple test parsing XML configurator for JNDI.
* This parsing uses a validating parser.
*
* @throws IOException
* @throws MapperConfigurationException
* @throws XMLConfiguratorException
*/
public void test01Base() throws MapperConfigurationException,
XMLConfiguratorException {
XMLConfigurator xc = new XMLConfigurator();
xc.setValidating(true);
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jndi-sample.xml"));
// expects
mockconfigure.expects(once()).method("addParameter").with(
eq("ldifsource"), eq("canam.ldif"));
mockconfigure.expects(once()).method("addSource").with(
eq("ldifsource"), ANYTHING);
mockconfigure.expects(once()).method("addSource").with(
eq("pooledsource"), ANYTHING);
mockconfigure.expects(once()).method("addMapper").with(
eq("telAndMail"), ANYTHING);
mockconfigure.expects(once()).method("addMapper").with(eq("emps"),ANYTHING);
mockconfigure.expects(once()).method("link").with(eq("telAndMail"),
eq("ldifsource"));
mockconfigure.expects(once()).method("link").with(eq("emps"),
eq("ldifsource"));
// configure
xc.configure((Configure) mockconfigure.proxy());
}
/**
* Test resetting configurator.
*
* @throws IOException
* @throws MapperConfigurationException
*
*/
public void test02Reset() throws MapperConfigurationException,
XMLConfiguratorException {
XMLConfigurator xc = new XMLConfigurator();
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jndi-sample.xml"));
// expects
mockconfigure.expects(once()).method("addParameter").with(
eq("ldifsource"), eq("canam.ldif"));
mockconfigure.expects(once()).method("addSource").with(
eq("ldifsource"), ANYTHING);
mockconfigure.expects(once()).method("addSource").with(
eq("pooledsource"), ANYTHING);
mockconfigure.expects(once()).method("addMapper").with(
eq("telAndMail"), ANYTHING);
mockconfigure.expects(once()).method("addMapper").with(eq("emps"),
ANYTHING);
mockconfigure.expects(once()).method("link").with(eq("telAndMail"),
eq("ldifsource"));
mockconfigure.expects(once()).method("link").with(eq("emps"),
eq("ldifsource"));
// reset and reconfigure
xc.resetConfig();
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jndi-sample.xml"));
// configure
xc.configure((Configure) mockconfigure.proxy());
}
/**
* Test parsing of multiple files, one with errors.
*
*/
public void test03MultipleFilesWithError() {
}
public void test04ModifyOperations() throws MapperConfigurationException,
XMLConfiguratorException {
XMLConfigurator xc = new XMLConfigurator();
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jndi-sample-modify.xml"));
// expects
mockconfigure.expects(once()).method("addParameter").with(
eq("ldifsource"), eq("canam.ldif"));
mockconfigure.expects(once()).method("addSource").with(
eq("ldifsource"), ANYTHING);
mockconfigure.expects(once()).method("addMapper").with(
eq("addEmpToBoss"), ANYTHING);
mockconfigure.expects(once()).method("addMapper").with(
eq("delEmpFromBoss"), ANYTHING);
mockconfigure.expects(once()).method("addMapper").with(
eq("changeBossOfEmp"), ANYTHING);
mockconfigure.expects(once()).method("link").with(eq("addEmpToBoss"),
eq("ldifsource"));
mockconfigure.expects(once()).method("link").with(eq("delEmpFromBoss"),
eq("ldifsource"));
mockconfigure.expects(once()).method("link").with(
eq("changeBossOfEmp"), eq("ldifsource"));
// configure
xc.configure((Configure) mockconfigure.proxy());
}
public void test05RenameAndCompareOperations()
throws MapperConfigurationException, XMLConfiguratorException {
XMLConfigurator xc = new XMLConfigurator();
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jndi-sample-rename-compare.xml"));
// expects
mockconfigure.expects(once()).method("addParameter").with(
eq("ldifsource"), eq("canam.ldif"));
mockconfigure.expects(once()).method("addSource").with(
eq("ldifsource"), ANYTHING);
mockconfigure.expects(once()).method("addMapper").with(eq("moveEmp"),
ANYTHING);
mockconfigure.expects(once()).method("addMapper").with(
eq("compareEmp"), ANYTHING);
mockconfigure.expects(once()).method("link").with(eq("moveEmp"),
eq("ldifsource"));
mockconfigure.expects(once()).method("link").with(eq("compareEmp"),
eq("ldifsource"));
// configure
xc.configure((Configure) mockconfigure.proxy());
}
public void test06AddAndDeleteOperations()
throws MapperConfigurationException, XMLConfiguratorException {
XMLConfigurator xc = new XMLConfigurator();
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jndi-sample-add-delete.xml"));
// expects
mockconfigure.expects(once()).method("addParameter").with(
eq("ldifsource"), eq("canam.ldif"));
mockconfigure.expects(once()).method("addSource").with(
eq("ldifsource"), ANYTHING);
mockconfigure.expects(once()).method("addMapper").with(eq("addEmp"),
ANYTHING);
mockconfigure.expects(once()).method("addMapper").with(eq("delEmp"),
ANYTHING);
mockconfigure.expects(once()).method("link").with(eq("addEmp"),
eq("ldifsource"));
mockconfigure.expects(once()).method("link").with(eq("delEmp"),
eq("ldifsource"));
// configure
xc.configure((Configure) mockconfigure.proxy());
}
/**
* Test error in map inheritance : inherited map is unknown
*
* @throws MapperConfigurationException
*/
public void test07ErrorInMapInheritance()
throws MapperConfigurationException {
XMLConfigurator xc = new XMLConfigurator();
try {
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jndi-sample-error-1.xml"));
fail("Should have thrown exception");
} catch (XMLConfiguratorException e) {
}
}
/**
* Test error in map inheritance : inherited map is defined later
*
* @throws MapperConfigurationException
*/
public void test08ErrorInMapInheritanceForwardDeclaration()
throws MapperConfigurationException {
XMLConfigurator xc = new XMLConfigurator();
try {
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jndi-sample-error-2.xml"));
fail("Should have thrown exception");
} catch (XMLConfiguratorException e) {
}
}
/**
* Test error in map inheritance : inherited map is defined later
*
* @throws MapperConfigurationException
*/
public void test09ErroUnknownMapReference()
throws MapperConfigurationException {
XMLConfigurator xc = new XMLConfigurator();
try {
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jndi-sample-error-3.xml"));
fail("Should have thrown exception");
} catch (XMLConfiguratorException e) {
}
}
/**
* Uknown tag in JNDI configuration
*
* @throws MapperConfigurationException
*/
public void test10UndefinedHandler() throws MapperConfigurationException {
XMLConfigurator xc = new XMLConfigurator();
try {
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jndi-sample-error-4.xml"));
fail("Should have thrown exception");
} catch (XMLConfiguratorException e) {
}
}
/**
* Unknown op code in modify operation
*
* @throws MapperConfigurationException
*/
public void test11UnknownOpCode() throws MapperConfigurationException {
XMLConfigurator xc = new XMLConfigurator();
try {
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jndi-sample-error-5.xml"));
fail("Should have thrown exception");
} catch (XMLConfiguratorException e) {
}
}
/**
* Unknown property in search mapper
*
* @throws MapperConfigurationException
*/
public void test12UnknownSearchControlsProperty()
throws MapperConfigurationException {
XMLConfigurator xc = new XMLConfigurator();
try {
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jndi-sample-error-6.xml"));
fail("Should have thrown exception");
} catch (XMLConfiguratorException e) {
}
}
/**
* Forbid declaration of two maps with same name
*
*/
public void test13DoubleMapDeclaration() {
XMLConfigurator xc = new XMLConfigurator();
try {
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jndi-sample-error-7.xml"));
fail("duplicate map name : Should have thrown exception");
} catch (XMLConfiguratorException e) {
}
}
/**
* Forbid declaration of two sources with same name
*
*/
public void test14DoubleSourceDeclaration() {
XMLConfigurator xc = new XMLConfigurator();
try {
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jndi-sample-error-8.xml"));
fail("duplicate source name : Should have thrown exception");
} catch (XMLConfiguratorException e) {
}
}
/**
* Forbid declaration of two mappers with same name
*
*/
public void test15DoubleMapperDeclaration() {
XMLConfigurator xc = new XMLConfigurator();
try {
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jndi-sample-error-9.xml"));
fail("duplicate mapper name : Should have thrown exception");
} catch (XMLConfiguratorException e) {
}
}
public void test16Bean()
throws MapperConfigurationException, XMLConfiguratorException {
XMLConfigurator xc = new XMLConfigurator();
xc.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("jndi-sample-bean.xml"));
// expects
mockconfigure.expects(once()).method("addParameter").with(
eq("ldifsource"), eq("canam.ldif"));
mockconfigure.expects(once()).method("addSource").with(
eq("ldifsource"), ANYTHING);
mockconfigure.expects(once()).method("addMapper").with(eq("addEmp"),
ANYTHING);
mockconfigure.expects(once()).method("addMapper").with(eq("delEmp"),
ANYTHING);
mockconfigure.expects(once()).method("link").with(eq("addEmp"),
eq("ldifsource"));
mockconfigure.expects(once()).method("link").with(eq("delEmp"),
eq("ldifsource"));
// configure
xc.configure((Configure) mockconfigure.proxy());
}
}