use of org.apache.struts2.config.entities.BeanConfig in project struts by apache.
the class LocationUtilsTest method testGetLocationStrutsJavaConfiguration.
public void testGetLocationStrutsJavaConfiguration() throws Exception {
StrutsJavaConfiguration conf = new StrutsJavaConfiguration() {
@Override
public List<String> unknownHandlerStack() {
return null;
}
@Override
public List<ConstantConfig> constants() {
return null;
}
@Override
public List<BeanConfig> beans() {
return null;
}
@Override
public Optional<BeanSelectionConfig> beanSelection() {
return Optional.empty();
}
};
Location loc = LocationUtils.getLocation(conf, null);
assertNotNull(loc);
assertEquals(conf.toString(), loc.getURI());
}
use of org.apache.struts2.config.entities.BeanConfig in project struts by apache.
the class StrutsJavaConfigurationProvider method register.
@Override
public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
Map<String, Object> loadedBeans = new HashMap<>();
// bean
List<BeanConfig> beanConfigs = javaConfig.beans();
if (beanConfigs != null) {
for (BeanConfig bc : beanConfigs) {
if (bc != null) {
registerBean(loadedBeans, builder, bc);
}
}
}
// constant
List<ConstantConfig> constantConfigList = javaConfig.constants();
if (constantConfigList != null) {
for (ConstantConfig constantConf : constantConfigList) {
if (constantConf != null) {
Map<String, String> constantMap = constantConf.getAllAsStringsMap();
for (Entry<String, String> entr : constantMap.entrySet()) {
if (entr.getKey() != null && entr.getValue() != null) {
registerConstant(props, entr.getKey(), entr.getValue());
}
}
}
}
}
// bean-selection
javaConfig.beanSelection().ifPresent(beanSelectionConfig -> {
try {
LOG.debug("Registering bean selection provider {} of type {}", beanSelectionConfig.getName(), beanSelectionConfig.getClazz().getName());
BeanSelectionProvider provider = beanSelectionConfig.getClazz().newInstance();
provider.register(builder, props);
} catch (IllegalAccessException | InstantiationException e) {
throw new ConfigurationException("Unable to load : name:" + beanSelectionConfig.getName() + " class:" + beanSelectionConfig.getClazz().getName());
}
});
// unknown-handler-stack
List<String> unknownHandlers = javaConfig.unknownHandlerStack();
if (unknownHandlers != null) {
List<UnknownHandlerConfig> unknownHandlerStack = new ArrayList<>();
for (String unknownHandler : unknownHandlers) {
Location location = LocationUtils.getLocation(unknownHandler);
unknownHandlerStack.add(new UnknownHandlerConfig(unknownHandler, location));
}
if (!unknownHandlerStack.isEmpty()) {
configuration.setUnknownHandlerStack(unknownHandlerStack);
}
}
}
use of org.apache.struts2.config.entities.BeanConfig in project struts by apache.
the class StrutsJavaConfigurationProviderTest method testRegister.
@Test
public void testRegister() throws Exception {
final ConstantConfig constantConfig = new ConstantConfig();
constantConfig.setDevMode(true);
final String expectedUnknownHandler = "expectedUnknownHandler";
StrutsJavaConfiguration javaConfig = new StrutsJavaConfiguration() {
@Override
public List<String> unknownHandlerStack() {
return Collections.singletonList(expectedUnknownHandler);
}
@Override
public List<ConstantConfig> constants() {
return Collections.singletonList(constantConfig);
}
@Override
public List<BeanConfig> beans() {
return Arrays.asList(new BeanConfig(TestBean.class, "struts"), new BeanConfig(TestBean.class, "struts.static", TestBean.class, Scope.PROTOTYPE, true, true), new BeanConfig(TestBean.class, "struts.test.bean", TestBean.class));
}
@Override
public Optional<BeanSelectionConfig> beanSelection() {
return Optional.of(new BeanSelectionConfig(TestBeanSelectionProvider.class, "testBeans"));
}
};
StrutsJavaConfigurationProvider provider = new StrutsJavaConfigurationProvider(javaConfig);
Configuration configuration = new MockConfiguration();
provider.init(configuration);
ContainerBuilder builder = new ContainerBuilder();
LocatableProperties props = new LocatableProperties();
provider.register(builder, props);
// constant
Assert.assertEquals(String.valueOf(constantConfig.getDevMode()), props.get(StrutsConstants.STRUTS_DEVMODE));
// unknown-handler-stack
Assert.assertNotNull(configuration.getUnknownHandlerStack());
Assert.assertEquals(1, configuration.getUnknownHandlerStack().size());
Assert.assertEquals(expectedUnknownHandler, configuration.getUnknownHandlerStack().get(0).getName());
// bean
Container container = builder.create(true);
TestBean testBean = container.getInstance(TestBean.class);
Assert.assertNotNull(testBean);
testBean = container.getInstance(TestBean.class, "struts");
Assert.assertNotNull(testBean);
// bean selection
Set<String> names = container.getInstanceNames(TestBean.class);
Assert.assertTrue(names.contains("struts"));
Assert.assertTrue(names.contains("struts.test.bean"));
}
Aggregations