001: package org.vraptor.config;
002:
003: import java.io.BufferedOutputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.PrintStream;
006: import java.io.PrintWriter;
007: import java.io.StringReader;
008: import java.util.Map;
009:
010: import org.jmock.Mock;
011: import org.vraptor.AbstractTest;
012: import org.vraptor.VRaptorException;
013: import org.vraptor.plugin.DefaultPluginManager;
014: import org.vraptor.plugin.VRaptorPlugin;
015: import org.vraptor.webapp.WebApplication;
016:
017: public class XMLConfigurationLoaderTest extends AbstractTest {
018:
019: public void testLoadsAutoDiscover() {
020: String xml = "<vraptor><auto-discover>false</auto-discover></vraptor>";
021: VRaptorConfiguration config = getConfig(xml);
022: assertFalse(config.isAutoDiscover());
023: }
024:
025: private VRaptorConfiguration getConfig(String xml) {
026: XMLConfigurationLoader loader = new XMLConfigurationLoader();
027: VRaptorConfiguration config = loader
028: .read(new StringReader(xml));
029: return config;
030: }
031:
032: public void testDefaultsAutoDiscoverToTrue() {
033: String xml = "<vraptor></vraptor>";
034: VRaptorConfiguration config = getConfig(xml);
035: assertTrue(config.isAutoDiscover());
036: }
037:
038: public void testLoadsComponents() {
039: String xml = "<vraptor><component>"
040: + MyComponent.class.getName()
041: + "</component></vraptor>";
042: VRaptorConfiguration config = getConfig(xml);
043: assertEquals(1, config.getItems().size());
044: assertTrue(config.getItems().get(0).getClass().equals(
045: ComponentConfig.class));
046: }
047:
048: public static class MyComponent {
049: }
050:
051: public void testPluginUsingSimpleApproach() {
052: String xml = "<vraptor><plugin>org.vraptor.config.XMLConfigurationLoaderTest$MyPlugin</plugin></vraptor>";
053: VRaptorConfiguration config = getConfig(xml);
054: assertEquals(1, config.getItems().size());
055: assertTrue(config.getItems().get(0).getClass().equals(
056: PluginConfig.class));
057: }
058:
059: public void testPluginUsingProperties() throws ConfigException {
060: String xml = "<vraptor>"
061: + "<plugin type=\"org.vraptor.config.XMLConfigurationLoaderTest$MyPlugin\">"
062: + "<property name=\"property1\">value1</property>"
063: + "</plugin>" + "</vraptor>";
064: VRaptorConfiguration config = getConfig(xml);
065: assertEquals(1, config.getItems().size());
066: assertTrue(config.getItems().get(0).getClass().equals(
067: PluginConfig.class));
068:
069: PluginConfig pluginConfig = (PluginConfig) config.getItems()
070: .get(0);
071: Mock webApp = mock(WebApplication.class);
072: DefaultPluginManager pluginManager = new DefaultPluginManager();
073: webApp.expects(once()).method("getPluginManager")
074: .withNoArguments().will(returnValue(pluginManager));
075: pluginConfig.register((WebApplication) webApp.proxy());
076: MyPlugin instance = (MyPlugin) pluginManager.getPlugins()
077: .get(0);
078: assertEquals("value1", instance.properties.get("property1"));
079: }
080:
081: public void testWriteConfigItens() throws Exception {
082: String xml = "<vraptor>\n"
083: + " <plugin>org.vraptor.config.XMLConfigurationLoaderTest$MyPlugin</plugin>\n"
084: + "</vraptor>";
085: VRaptorConfiguration config = getConfig(xml);
086: XMLConfigurationLoader loader = new XMLConfigurationLoader();
087: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
088: loader.write(outputStream, config.getItems());
089: assertEquals(xml, outputStream.toString());
090: }
091:
092: public static class MyPlugin implements VRaptorPlugin {
093: private final Map<String, String> properties;
094:
095: public MyPlugin(Map<String, String> properties) {
096: this .properties = properties;
097:
098: }
099:
100: public void init(WebApplication application)
101: throws VRaptorException {
102: }
103: }
104: }
|