Coverage Report - org.mule.providers.jdbc.util.MuleDerbyUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleDerbyUtils
81%
29/36
50%
4/8
1.875
 
 1  
 /*
 2  
  * $Id: MuleDerbyUtils.java 9989 2007-12-04 23:55:05Z aperepel $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.providers.jdbc.util;
 12  
 
 13  
 import org.mule.util.ClassUtils;
 14  
 import org.mule.util.FileUtils;
 15  
 
 16  
 import java.io.File;
 17  
 import java.io.IOException;
 18  
 import java.lang.reflect.Method;
 19  
 import java.net.URL;
 20  
 import java.sql.Driver;
 21  
 import java.sql.DriverManager;
 22  
 import java.sql.SQLException;
 23  
 import java.util.Properties;
 24  
 
 25  
 /**
 26  
  * NOTE: Don't forget to duplicate all the changes in {@link org.mule.examples.loanbroker.bpm.DbUtils}
 27  
  */
 28  
 public class MuleDerbyUtils
 29  
 {
 30  
     private static final String DERBY_DRIVER_CLASS = "org.apache.derby.jdbc.EmbeddedDriver";
 31  
     
 32  
     //class cannot be instantiated
 33  
     private MuleDerbyUtils()
 34  
     {
 35  0
         super();
 36  0
     }
 37  
     
 38  
     //by default, set the derby home to the target directory
 39  
     public static String setDerbyHome()
 40  
     {
 41  2
         return setDerbyHome("target");
 42  
     }
 43  
     
 44  
     public static String setDerbyHome(String path)
 45  
     {
 46  2
         File derbySystemHome = new File(System.getProperty("user.dir"), path);
 47  2
         System.setProperty("derby.system.home",  derbySystemHome.getAbsolutePath());
 48  2
         return derbySystemHome.getAbsolutePath();
 49  
     }
 50  
     
 51  
     /**
 52  
      * Properly shutdown an embedded Derby database
 53  
      * 
 54  
      * @throws SQLException
 55  
      * @see <h href="http://db.apache.org/derby/docs/10.3/devguide/tdevdvlp20349.html">Derby docs</a>
 56  
      */
 57  
     public static void stopDatabase() throws SQLException
 58  
     {
 59  
         try
 60  
         {
 61  
             // force loading the driver so it's available even if no prior connection to the
 62  
             // database was made
 63  2
             ClassUtils.instanciateClass(DERBY_DRIVER_CLASS, new Object[0]);
 64  
 
 65  2
             DriverManager.getConnection("jdbc:derby:;shutdown=true");
 66  
         }
 67  2
         catch (SQLException sqlex)
 68  
         {
 69  
             // this exception is documented to be thrown upon shutdown
 70  2
             if (!"XJ015".equals(sqlex.getSQLState()))
 71  
             {
 72  0
                 throw sqlex;
 73  
             }
 74  
         }
 75  0
         catch (Exception ex)
 76  
         {
 77  
             // this can only happen when the driver class is not in classpath. In this case, just
 78  
             // throw up
 79  0
             throw new RuntimeException(ex);
 80  2
         }
 81  2
     }
 82  
     
 83  
     public static void cleanupDerbyDb(String derbySystemHome, String databaseName) throws IOException, SQLException
 84  
     {
 85  2
         stopDatabase();
 86  2
         FileUtils.deleteTree(new File(derbySystemHome + File.separator + databaseName));
 87  2
     }
 88  
     
 89  
     public static void createDataBase(String databaseName) throws SQLException
 90  
     {
 91  
         // Do not use the EmbeddedDriver class here directly to avoid compile time references
 92  
         // on derby.jar
 93  
         try
 94  
         {
 95  2
             Driver derbyDriver = (Driver) ClassUtils.instanciateClass(DERBY_DRIVER_CLASS, new Object[0]);
 96  
             
 97  2
             Method connectMethod = derbyDriver.getClass().getMethod("connect", 
 98  6
                 new Class[] { String.class, Properties.class });
 99  
             
 100  2
             String connectionName = "jdbc:derby:" + databaseName + ";create=true";
 101  2
             connectMethod.invoke(derbyDriver, new Object[] { connectionName, null });
 102  
         }
 103  0
         catch (Exception ex)
 104  
         {
 105  0
             throw new RuntimeException("Error creating the database " + databaseName, ex);
 106  2
         }
 107  2
     }
 108  
     
 109  
     public static String loadDatabaseName(String propertiesLocation, String propertyName) throws IOException
 110  
     {
 111  2
         Properties derbyProperties = new Properties();
 112  2
         URL resource = ClassUtils.getResource(propertiesLocation, MuleDerbyUtils.class);
 113  2
         derbyProperties.load(resource.openStream());
 114  2
         return derbyProperties.getProperty(propertyName);
 115  
     }
 116  
 
 117  
     public static void defaultDerbyCleanAndInit(String propertiesLocation, String propertyName) throws IOException, SQLException
 118  
     {
 119  2
         String derbyHome = setDerbyHome();
 120  2
         String dbName = loadDatabaseName(propertiesLocation, propertyName);
 121  2
         cleanupDerbyDb(derbyHome, dbName);
 122  2
         createDataBase(dbName);
 123  2
     }
 124  
 }
 125  
 
 126