📄 componentfactorytest.java
字号:
package org.jbpm;
import java.util.*;
import junit.framework.*;
public class ComponentFactoryTest extends TestCase {
static { TestHelper.initLogging(); }
public static class FirstClass {
}
public static class SecondClass {
}
public ComponentFactoryTest(String name) {
super(name);
}
public void testStaticSingletonServiceLocator() throws Exception {
Properties configuration = new Properties();
JbpmConfiguration.configure( configuration );
assertNotNull( JbpmServiceLocator.getInstance().getDefinitionService() );
assertNotNull( JbpmServiceLocator.getInstance().getExecutionService() );
assertNotNull( JbpmServiceLocator.getInstance().getLogService() );
}
public void testMultipleServiceLocators() throws Exception {
Properties configurationOne = new Properties();
JbpmConfiguration jbpmConfigurationOne = new JbpmConfiguration( configurationOne );
JbpmServiceLocator serviceLocatorOne = new JbpmServiceLocator( jbpmConfigurationOne );
Properties configurationTwo = new Properties();
JbpmConfiguration jbpmConfigurationTwo = new JbpmConfiguration( configurationTwo );
JbpmServiceLocator serviceLocatorTwo = new JbpmServiceLocator( jbpmConfigurationTwo );
assertNotNull( serviceLocatorOne.getDefinitionService() );
assertNotNull( serviceLocatorOne.getExecutionService() );
assertNotNull( serviceLocatorOne.getLogService() );
assertSame( serviceLocatorOne.getDefinitionService(), serviceLocatorOne.getDefinitionService() );
assertSame( serviceLocatorOne.getExecutionService(), serviceLocatorOne.getExecutionService() );
assertSame( serviceLocatorOne.getLogService(), serviceLocatorOne.getLogService() );
assertTrue( serviceLocatorOne.getDefinitionService() != serviceLocatorTwo.getDefinitionService() );
assertTrue( serviceLocatorOne.getExecutionService() != serviceLocatorTwo.getExecutionService() );
assertTrue( serviceLocatorOne.getLogService() != serviceLocatorTwo.getLogService() );
}
public void testJbpmConfigurationObjectFactory() {
Properties configurationOne = new Properties();
configurationOne.setProperty( "same.class", "org.jbpm.ComponentFactoryTest$FirstClass" );
configurationOne.setProperty( "different.class", "org.jbpm.ComponentFactoryTest$FirstClass" );
JbpmConfiguration jbpmConfigurationOne = new JbpmConfiguration( new Properties(), configurationOne );
Properties configurationTwo = new Properties();
configurationTwo.setProperty( "same.class", "org.jbpm.ComponentFactoryTest$FirstClass" );
configurationTwo.setProperty( "different.class", "org.jbpm.ComponentFactoryTest$SecondClass" );
JbpmConfiguration jbpmConfigurationTwo = new JbpmConfiguration( new Properties(), configurationTwo );
Object commonObjectOfConfigOne = jbpmConfigurationOne.instantiate( "same.class", FirstClass.class );
Object firstObjectOfConfigOne = jbpmConfigurationOne.instantiate( "different.class", FirstClass.class );
Object commonObjectOfConfigTwo = jbpmConfigurationTwo.instantiate( "same.class", FirstClass.class );
Object firstObjectOfConfigTwo = jbpmConfigurationTwo.instantiate( "different.class", SecondClass.class );
assertTrue( commonObjectOfConfigOne != firstObjectOfConfigOne );
assertTrue( commonObjectOfConfigOne != commonObjectOfConfigTwo );
assertTrue( commonObjectOfConfigOne != firstObjectOfConfigTwo );
assertTrue( commonObjectOfConfigTwo != firstObjectOfConfigTwo );
assertSame( commonObjectOfConfigOne, jbpmConfigurationOne.instantiate( "same.class", FirstClass.class ) );
}
public void testJbpmConfigurationObjectFactoryCache() {
Properties configuration = new Properties();
configuration.setProperty( "firstclass", "org.jbpm.ComponentFactoryTest$FirstClass" );
configuration.setProperty( "secondclass", "org.jbpm.ComponentFactoryTest$SecondClass" );
configuration.setProperty( "unexistingclass", "UnexistingClass" );
JbpmConfiguration jbpmConfiguration = new JbpmConfiguration( new Properties(), configuration );
Object firstObject = jbpmConfiguration.instantiate( "firstclass", FirstClass.class );
assertEquals( FirstClass.class, firstObject.getClass() );
Object secondObject = jbpmConfiguration.instantiate( "secondclass", SecondClass.class );
assertEquals( SecondClass.class, secondObject.getClass() );
assertSame( firstObject, jbpmConfiguration.instantiate( "firstclass", FirstClass.class ) );
Properties newConfiguration = new Properties();
newConfiguration.setProperty( "firstclass", "java.lang.String" );
JbpmConfiguration newJbpmConfiguration = new JbpmConfiguration( new Properties(), newConfiguration );
Object stringObject = newJbpmConfiguration.instantiate( "firstclass", String.class );
assertEquals( String.class, stringObject.getClass() );
}
public void testObjectFactoryExceptionMessages() {
Properties configuration = new Properties();
configuration.setProperty( "firstclass", "org.jbpm.ComponentFactoryTest$FirstClass" );
configuration.setProperty( "secondclass", "org.jbpm.ComponentFactoryTest$SecondClass" );
configuration.setProperty( "unexistingclass", "UnexistingClass" );
JbpmConfiguration jbpmConfiguration = new JbpmConfiguration( new Properties(), configuration );
try {
jbpmConfiguration.instantiate( "unexistingkey", String.class );
fail();
} catch (ConfigurationException e) {
assertEquals( "no class-name configured for key 'unexistingkey'", e.getMessage() );
}
try {
jbpmConfiguration.instantiate( "unexistingclass", String.class );
fail();
} catch (ConfigurationException e) {
assertEquals( "couldn't find UnexistingClass (as configured in key unexistingclass)", e.getMessage() );
}
try {
jbpmConfiguration.instantiate( "firstclass", SecondClass.class );
fail();
} catch (ConfigurationException e) {
assertEquals( "class configured in key firstclass (org.jbpm.ComponentFactoryTest$FirstClass) is not castable to a org.jbpm.ComponentFactoryTest$SecondClass", e.getMessage() );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -