/*--
Copyright (C) 2002-2005 Adrian Price.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.
3. The names "OBE" and "Open Business Engine" must not be used to
endorse or promote products derived from this software without prior
written permission. For written permission, please contact
adrianprice@sourceforge.net.
4. Products derived from this software may not be called "OBE" or
"Open Business Engine", nor may "OBE" or "Open Business Engine"
appear in their name, without prior written permission from
Adrian Price (adrianprice@users.sourceforge.net).
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
For more information on OBE, please see
<http://obe.sourceforge.net/>.
*/
package org.obe.test;
import org.obe.client.api.repository.RepositoryException;
import org.obe.spi.service.InstanceRepository;
import org.obe.spi.service.ProcessRepository;
import org.obe.spi.service.ServiceManager;
import org.obe.xpdl.model.misc.AbstractWFElement;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
/**
* An abstract base class for repository tests.
*
* @author Adrian Price
*/
public abstract class AbstractRepositoryTest extends OBETestCase {
private static final int MILLIS_PER_SEC = 1000;
protected static ServiceManager _svcMgr;
protected static ProcessRepository _processRepository;
protected static InstanceRepository _instanceRepository;
private Properties _svcMgrProps;
protected AbstractRepositoryTest(String name, Properties svcMgrProps) {
super(name);
_svcMgrProps = svcMgrProps;
}
protected void checkCount(String hint, int expected, Object[] elements) {
assertEquals(hint + " count is wrong;", expected, elements.length);
for (int i = 0; i < elements.length; i++) {
assertNull(hint + " count returned an entity;", elements[i]);
}
}
protected void checkIds(String hint, String[] expected,
AbstractWFElement[] elements) {
String[] actualIds = new String[elements.length];
for (int i = 0; i < elements.length; i++)
actualIds[i] = elements[i].getId();
checkIds(hint, expected, actualIds);
}
protected void checkIds(String hint, String[] expected, String[] actual) {
assertEquals(hint + " count is wrong;", expected.length, actual.length);
boolean[] found = new boolean[expected.length];
for (int i = 0; i < actual.length; i++) {
String id = actual[i];
for (int j = 0; j < expected.length; j++) {
if (id.equals(expected[j])) {
found[j] = true;
break;
}
}
}
for (int i = 0; i < found.length; i++) {
if (!found[i])
fail(hint + " query failed to find ID = " + expected[i]);
}
}
// This method returns a date from which the milliseconds component has
// been removed. This is because some databases only support a date/time
// resolution of one second.
protected static Date ignoreMillis(Date date) {
if (date != null) {
long millis = date.getTime();
if (millis % MILLIS_PER_SEC != 0)
date = new Date(millis - millis % MILLIS_PER_SEC);
}
return date;
}
protected void initializeRepositories()
throws IOException, RepositoryException {
// If the subclass constructor passed service manager properties, use
// them; otherwise, use the system properties and configuration file.
_svcMgr = _svcMgrProps == null
? new ServiceManager() : new ServiceManager(_svcMgrProps);
_svcMgr.init();
_processRepository = _svcMgr.getProcessRepository();
_instanceRepository = _svcMgr.getInstanceRepository();
_instanceRepository.purge();
_processRepository.purge();
getLogger().info("Repositories initialized");
}
protected void uninitializeRepositories()
throws IOException, RepositoryException {
_svcMgr.exit();
_svcMgr = null;
_processRepository = null;
_instanceRepository = null;
getLogger().info("Repositories uninitialized");
}
}
|