/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This 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 software 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 software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jbpm.svc;
import java.security.AccessControlException;
import java.security.Permission;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
import org.jbpm.logging.LoggingService;
import org.jbpm.logging.log.ProcessLog;
import org.jbpm.msg.Message;
import org.jbpm.msg.MessageService;
import org.jbpm.persistence.db.DbPersistenceService;
import org.jbpm.persistence.db.DbPersistenceServiceFactory;
import org.jbpm.scheduler.SchedulerService;
import org.jbpm.scheduler.exe.Timer;
import org.jbpm.security.AuthenticationService;
import org.jbpm.security.AuthorizationService;
import org.jbpm.security.authentication.DefaultAuthenticationService;
public class ServicesTest extends TestCase {
public void testUnavailableServiceFactory() {
Map serviceFactories = new HashMap();
Services services = new Services(serviceFactories);
assertNull(services.getService("unexisting-service"));
}
public static class TestServiceFactory implements ServiceFactory {
private static final long serialVersionUID = 1L;
public Service openService() {
return new TestService();
}
}
public static class TestService implements Service {
private static final long serialVersionUID = 1L;
public void close() {
}
}
public void testGetService() {
Map serviceFactories = new HashMap();
serviceFactories.put("testservice", new TestServiceFactory());
Services services = new Services(serviceFactories);
TestService testService = (TestService) services.getService("testservice");
assertNotNull(testService);
assertSame(testService, services.getService("testservice"));
}
public static class TestGivenServiceFactory implements ServiceFactory {
private static final long serialVersionUID = 1L;
Service service = null;
public TestGivenServiceFactory(Service service) {
this.service = service;
}
public Service openService() {
return service;
}
}
public void testAuthenticationService() {
Map serviceFactories = new HashMap();
AuthenticationService authenticationService = new DefaultAuthenticationService();
serviceFactories.put("authentication", new TestGivenServiceFactory(
authenticationService
));
Services services = new Services(serviceFactories);
assertSame(authenticationService, services.getAuthenticationService());
}
public void testAuthorizationService() {
Map serviceFactories = new HashMap();
AuthorizationService authorizationService = new AuthorizationService() {
private static final long serialVersionUID = 1L;
public void checkPermission(Permission permission) throws AccessControlException {
}
public void close() {
}
};
serviceFactories.put("authorization", new TestGivenServiceFactory(
authorizationService
));
Services services = new Services(serviceFactories);
assertSame(authorizationService, services.getAuthorizationService());
}
public void testLoggingService() {
Map serviceFactories = new HashMap();
LoggingService loggingService = new LoggingService(){
private static final long serialVersionUID = 1L;
public void log(ProcessLog processLog) {}
public void close() {}
};
serviceFactories.put("logging", new TestGivenServiceFactory(
loggingService
));
Services services = new Services(serviceFactories);
assertSame(loggingService, services.getLoggingService());
}
public void testMessageService() {
Map serviceFactories = new HashMap();
MessageService messageService = new MessageService(){
private static final long serialVersionUID = 1L;
public void send(Message message) {}
public void suspendMessages(Token token) {}
public void resumeMessages(Token token) {}
public void close() {}
};
serviceFactories.put(Services.SERVICENAME_MESSAGE, new TestGivenServiceFactory(
messageService
));
Services services = new Services(serviceFactories);
assertSame(messageService, services.getMessageService());
}
public void testPersistenceService() {
Map serviceFactories = new HashMap();
DbPersistenceService persistenceService = new DbPersistenceService(new DbPersistenceServiceFactory());
serviceFactories.put("persistence", new TestGivenServiceFactory(
persistenceService
));
Services services = new Services(serviceFactories);
assertSame(persistenceService, services.getPersistenceService());
}
public void testSchedulerService() {
Map serviceFactories = new HashMap();
SchedulerService schedulerService = new SchedulerService() {
private static final long serialVersionUID = 1L;
public void createTimer(Timer timer) {}
public void cancelTimersByName(String timerName, Token token) {}
public void cancelTimersByProcessInstance(ProcessInstance processInstance) {}
public void suspendTimers(Token token) {}
public void resumeTimers(Token token) {}
public void close() {}
};
serviceFactories.put("scheduler", new TestGivenServiceFactory(
schedulerService
));
Services services = new Services(serviceFactories);
assertSame(schedulerService, services.getSchedulerService());
}
}