Java tutorial
/* * Created on Dec 9, 2004 by Justin Sher * * ginp - Java Web Application for Viewing Photo Collections * * This library 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 any later version. * * This library 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 library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Note: Commons Logging is rather difficult to manipulate in unit * testing. We cannot force a new instance of the LogFactory and * cannot control the logging level of an instantiated log. The * Systemm properties are read once, during instantiation of the * first Log. However, the Log properties are also read and over-ride * the System values. All Log levels not explicitly defined are then * set to the default value. * * This separate test class is specifically to process invalid XML * files, which normally produce large numbers of alarming and * confusing stack trace messages when the test works as intended. */ package net.sf.ginp; import junit.framework.TestCase; import net.sf.ginp.config.Configuration; import net.sf.ginp.setup.SetupException; import net.sf.ginp.setup.SetupManager; import net.sf.ginp.setup.data.DirectoryPref; import net.sf.ginp.setup.data.SetupVisit; import net.sf.ginp.util.GinpUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hivemind.Registry; import org.apache.hivemind.impl.RegistryBuilder; import org.dom4j.Document; import org.dom4j.DocumentException; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.List; import java.util.Properties; import javax.xml.transform.TransformerException; /** * Tests all the ginp setup related code * @author Justin Sher */ public class TestSetupInvalid extends TestCase { Registry registry; SetupManager service; static String CONFIG_FILE_LOCATION = System.getProperty("java.io.tmpdir") + File.separator + "TestSetupInvalid" + File.separator + "ginp.xml"; // used to prevent intentional XML errors being logged // by the testing Log implementation - SimpleLog private static final Properties systemProperties = System.getProperties(); private static String INHIBIT_ERRORS = "fatal"; private static String MY_LOG_PROPERTY = "org.apache.commons.logging.simplelog.log.net.sf.ginp.TestSetupInvalid"; private static String XML_LOG_PROPERTY = "org.apache.commons.logging.simplelog.log.net.sf.ginp.setup.SetupManagerImpl"; /** * Sets up test by creating registry * * The logging environment is special... we want to suppress the * expected high number of stack traces from XML errors because * they make the test appear to be failing even though it is working * properly. If we suppress all logging lower than "fatal", then * RegistryBuilder croaks. Therefore, we have to set a high * logging threshold for just the two specific loggers. * @see junit.framework.TestCase#setUp() */ public void setUp() { // want loggers that won't show error messages systemProperties.put(MY_LOG_PROPERTY, INHIBIT_ERRORS); systemProperties.put(XML_LOG_PROPERTY, INHIBIT_ERRORS); // grab the factory and try to get rid of any residual loggers LogFactory logFactory = LogFactory.getFactory(); logFactory.release(); // with luck, the System props will be merged into the // simple-log.properties values and set the desired level net.sf.ginp.config.Configuration.setConfigfilelocation(CONFIG_FILE_LOCATION); registry = RegistryBuilder.constructDefaultRegistry(); service = (SetupManager) registry.getService(SetupManager.class); } /** * Free the Registry * @see junit.framework.TestCase#tearDown() */ public void tearDown() { registry.shutdown(); // always remove my logging props over-rides systemProperties.remove(MY_LOG_PROPERTY); systemProperties.remove(XML_LOG_PROPERTY); } /** * Test that Hivemind initialized ok */ public void testHivemind() { assertTrue(service != null); } /** * Test that validation of a bad config against a ginp dtd fails * @throws SetupException * @throws IOException * @see net.sf.ginp.setup.SetupManager#testValidConfig */ public void testInvalidConfig() { final String CONFIG_FILE = "invalidConfig.xml"; try { service.testValidConfig(this.getClass().getResourceAsStream(File.separator + CONFIG_FILE)); fail("Exception expected!"); } catch (Exception e1) { // unroll the nested Exception and verify diagnostics assertTrue("SetupException expected: " + e1, e1 instanceof SetupException); String reason = e1.getMessage(); assertTrue(reason.indexOf("Trouble parsing document") > -1); Throwable e2 = e1.getCause(); assertNotNull(e2); assertTrue(e2 instanceof DocumentException); String reason2 = e2.getMessage(); assertTrue(reason2.indexOf("Element type \"foo\" must be declared") > -1); // Sadly, SetupManagerImpl has no idea where we got the bad // file from, so the Exception misleadingly reports the path // to the DTD, which is probably fine. //assertTrue(reason2.indexOf(CONFIG_FILE)>-1); } } }