Clover coverage report -
Coverage timestamp: Do Okt 21 2004 12:21:23 CEST
file stats: LOC: 177   Methods: 13
NCLOC: 136   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JDBCHandlerTest.java 0% 92% 92,3% 90,2%
coverage coverage
 1    package org.jconfig.handler;
 2   
 3    import junit.framework.TestCase;
 4   
 5    import org.jconfig.*;
 6    import org.jconfig.utils.*;
 7    import java.io.*;
 8    /**
 9    * @author Terry Dye <terry dot dye at xcom dot de>
 10    */
 11    public class JDBCHandlerTest extends TestCase {
 12   
 13    /**
 14    * Constructor for JDBCHandlerTest.
 15    * @param arg0
 16    */
 17  7 public JDBCHandlerTest(String arg0) {
 18  7 super(arg0);
 19    }
 20   
 21  0 public static void main(String[] args) {
 22  0 junit.textui.TestRunner.run(JDBCHandlerTest.class);
 23    }
 24   
 25    /*
 26    * @see TestCase#setUp()
 27    */
 28  7 protected void setUp() throws Exception {
 29  7 prepareDB();
 30   
 31    }
 32   
 33    /*
 34    * @see TestCase#tearDown()
 35    */
 36  7 protected void tearDown() throws Exception {
 37  7 super.tearDown();
 38    }
 39   
 40  1 public void testLoad() throws ConfigurationManagerException {
 41  1 ConfigurationHandler handler = new MockJDBCHandler();
 42    // first we try to load a <null> JDBCHandler
 43  1 try {
 44  1 handler.load(null);
 45  0 fail("Should have thrown an exception");
 46    } catch (ConfigurationManagerException e) {
 47  1 assertEquals("Configuration name cannot be <null>", e.getMessage());
 48    // this was expected
 49    }
 50    // next we try an empty String
 51  1 try {
 52  1 handler.load("");
 53  0 fail("Should have thrown an exception");
 54    } catch (ConfigurationManagerException e) {
 55  1 assertEquals("Configuration name not valid. Empty String not allowed", e.getMessage());
 56    // this was expected
 57    }
 58    // next we check for valid jdbc settings
 59  1 Configuration configuration = handler.load("test_ls");
 60  1 assertNotNull("JDBC Configuration could not be loaded.", configuration);
 61  1 Category news = configuration.getCategory("news");
 62  1 assertNotNull("Category was <null>. This cannot be true.", news);
 63  1 assertEquals("Expected value was wrong.", "10", news.getProperty("count"));
 64  1 Category general = configuration.getCategory("general");
 65  1 assertNotNull("Category was <null>. This cannot be true", general);
 66  1 assertEquals("Expected value was wrong.", "JDBC is cool", general.getProperty("description"));
 67    }
 68   
 69  1 public void testLoadAndSave() {
 70  1 Configuration cfg = getConfig();
 71  1 assertNotNull(cfg);
 72  1 Category news = cfg.getCategory("news");
 73  1 assertNotNull("Category was <null>. This cannot be true.", news);
 74  1 assertEquals("Expected value was wrong.", "10", news.getProperty("count"));
 75    }
 76   
 77    /**
 78    * make sure we have loaded all variables
 79    */
 80  1 public void testVariables() {
 81  1 Configuration cfg = getConfig();
 82  1 String var = VariableManager.getInstance().getVariable("test_ls","user.name");
 83  1 assertEquals("developer",var);
 84  1 var = VariableManager.getInstance().getVariable("test_ls","my.offset");
 85  1 assertEquals("100",var);
 86  1 var = VariableManager.getInstance().getVariable("test_ls","app.name");
 87  1 assertEquals("MyStuff",var);
 88  1 var = VariableManager.getInstance().getVariable("test_ls","my.path");
 89  1 assertEquals("/usr/development/mystuff/etc",var);
 90    }
 91   
 92  1 public void testReplaceVariable() {
 93  1 Configuration cfg = getConfig();
 94  1 String var = cfg.getProperty("workflow_file");
 95  1 assertNotNull(var);
 96  1 assertEquals(var,"/usr/development/mystuff/etc/workflow.xml");
 97    }
 98   
 99  1 public void testRemoveAndReplaceVariable() {
 100  1 Configuration cfg = getConfig();
 101  1 String var = cfg.getProperty("workflow_file");
 102  1 assertNotNull(var);
 103  1 assertEquals(var,"/usr/development/mystuff/etc/workflow.xml");
 104  1 VariableManager.getInstance().removeVariable("my.path","test_ls");
 105  1 try {
 106  1 ConfigurationHandler handler = new MockJDBCHandler();
 107  1 handler.store(cfg);
 108    } catch (ConfigurationManagerException e) {
 109    }
 110  1 cfg = getConfig();
 111  1 var = cfg.getProperty("workflow_file");
 112  1 assertNotNull(var);
 113  1 assertEquals(var,"${my.path}/workflow.xml");
 114    }
 115   
 116  1 public void testRemoveProperty() {
 117  1 Configuration cfg = getConfig();
 118  1 cfg.removeProperty("delete");
 119  1 try {
 120  1 ConfigurationHandler handler = new MockJDBCHandler();
 121  1 handler.store(cfg);
 122    } catch (ConfigurationManagerException e) {
 123    }
 124   
 125  1 cfg = getConfig();
 126  1 assertNull(cfg.getProperty("delete"));
 127    }
 128   
 129  1 public void testAddProperty() {
 130  1 Configuration cfg = getConfig();
 131  1 cfg.setProperty("hello","world","new.one");
 132  1 try {
 133  1 ConfigurationHandler handler = new MockJDBCHandler();
 134  1 handler.store(cfg);
 135    } catch (ConfigurationManagerException e) {
 136    }
 137   
 138  1 cfg = getConfig();
 139  1 assertNotNull(cfg.getProperty("hello",null,"new.one"));
 140    }
 141   
 142  9 private Configuration getConfig() {
 143  9 ConfigurationManager.getInstance().removeConfiguration("test_ls");
 144  9 ConfigurationHandler handler = new MockJDBCHandler();
 145  9 Configuration cfg = null;
 146  9 try {
 147  9 cfg = handler.load("test_ls");
 148    } catch (ConfigurationManagerException e) {
 149  0 e.printStackTrace();
 150  0 fail("unexpected exception");
 151    }
 152  9 assertNotNull(cfg);
 153  9 return cfg;
 154    }
 155   
 156  7 private void prepareDB() {
 157  7 try {
 158  7 File src = ConfigurationUtils.getFileFromInputStream("configDB.script.org");
 159  7 File dst = ConfigurationUtils.getFileFromInputStream("configDB.script");
 160  7 InputStream in = new FileInputStream(src);
 161  7 OutputStream out = new FileOutputStream(dst);
 162   
 163    // Transfer bytes from in to out
 164  7 byte[] buf = new byte[1024];
 165  7 int len;
 166  ? while ((len = in.read(buf)) > 0) {
 167  42 out.write(buf, 0, len);
 168    }
 169  7 in.close();
 170  7 out.close();
 171    }
 172    catch (Exception e) {
 173  0 e.printStackTrace();
 174  0 fail("unable to copy original database");
 175    }
 176    }
 177    }