empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From franci...@apache.org
Subject svn commit: r881610 - in /incubator/empire-db/trunk: ./ empire-db-codegen/src/main/java/org/apache/empire/db/codegen/ empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/ empire-db-codegen/src/test/java/org/ empire-db-codegen/src/test/jav...
Date Tue, 17 Nov 2009 23:06:03 GMT
Author: francisdb
Date: Tue Nov 17 23:06:02 2009
New Revision: 881610

URL: http://svn.apache.org/viewvc?rev=881610&view=rev
Log:
EMPIREDB-52 - Cleaning up and added 2 unit tests

Added:
    incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/
    incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/
    incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/
    incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/
    incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/
    incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/
    incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/DBUtilTest.java
    incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/FileUtilsTest.java
Modified:
    incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGen.java
    incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenParser.java
    incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/DBUtil.java
    incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/FileUtils.java
    incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/ParserUtil.java
    incubator/empire-db/trunk/pom.xml

Modified: incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGen.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGen.java?rev=881610&r1=881609&r2=881610&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGen.java
(original)
+++ incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGen.java
Tue Nov 17 23:06:02 2009
@@ -317,19 +317,21 @@
 
 	private static void writeFile(File file, String templatePath,
 			VelocityContext context) {
+		Writer writer = null;
 		try {
 			Template template = Velocity.getTemplate(templatePath);
-			Writer writer = new FileWriter(file);
+			writer = new FileWriter(file);
 			template.merge(context, writer);
-			writer.close();
 		} catch (IOException e) {
-			e.printStackTrace();
+			log.error(e.getMessage(), e);
 		} catch (ResourceNotFoundException e) {
-			e.printStackTrace();
+			log.error(e.getMessage(), e);
 		} catch (ParseErrorException e) {
-			e.printStackTrace();
+			log.error(e.getMessage(), e);
 		} catch (Exception e) {
-			e.printStackTrace();
+			log.error(e.getMessage(), e);
+		} finally {
+			FileUtils.close(writer);
 		}
 
 	}

Modified: incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenParser.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenParser.java?rev=881610&r1=881609&r2=881610&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenParser.java
(original)
+++ incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenParser.java
Tue Nov 17 23:06:02 2009
@@ -103,7 +103,7 @@
 		} catch (SQLException e) {
 			throw new RuntimeException(e);
 		} finally {
-			DBUtil.closeResultSet(tables, log);
+			DBUtil.close(tables, log);
 		}
 	}
 
@@ -137,7 +137,7 @@
 		} catch (SQLException e) {
 			throw new RuntimeException(e);
 		} finally {
-			DBUtil.closeResultSet(rs, log);
+			DBUtil.close(rs, log);
 		}
 	}
 
@@ -158,7 +158,7 @@
 		} catch (SQLException e) {
 			throw new RuntimeException(e);
 		} finally {
-			DBUtil.closeResultSet(rs, log);
+			DBUtil.close(rs, log);
 		}
 		return cols;
 	}

Modified: incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/DBUtil.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/DBUtil.java?rev=881610&r1=881609&r2=881610&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/DBUtil.java
(original)
+++ incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/DBUtil.java
Tue Nov 17 23:06:02 2009
@@ -23,8 +23,22 @@
 
 import org.apache.commons.logging.Log;
 
-public class DBUtil {
-	public static boolean closeResultSet(ResultSet rs, Log log) {
+
+public final class DBUtil {
+	
+	private DBUtil()
+	{
+		// Utility class
+	}
+	
+	/**
+	 * Closes a sql resultset and logs exceptions
+	 * 
+	 * @param rs the resultset to close
+	 * @param log the logger instance to use for logging
+	 * @return true on succes
+	 */
+	public static boolean close(ResultSet rs, Log log) {
 		boolean b = false;
 		try {
 			if(rs!=null)

Modified: incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/FileUtils.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/FileUtils.java?rev=881610&r1=881609&r2=881610&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/FileUtils.java
(original)
+++ incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/FileUtils.java
Tue Nov 17 23:06:02 2009
@@ -20,6 +20,7 @@
 
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
+import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -30,7 +31,23 @@
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * File utilities
+ * TODO would be better to let these methods throw IOExceptions!
+ *
+ */
 public class FileUtils {
+	
+	private static final Log log =  LogFactory.getLog(FileUtils.class);
+	
+	private FileUtils()
+	{
+		// Utility class
+	}
+	
 	/**
 	 * Recursively cleans (removes) all files under the given 
 	 * directory.  Note that this removes all sub-directories
@@ -38,29 +55,50 @@
 	 * @param directory
 	 */
 	public static void cleanDirectory(File directory) {
-		if (directory.isDirectory()) {
+		boolean success;
+		if (directory.isDirectory()) 
+		{
             String[] children = directory.list();
-            for (int i=0; i<children.length; i++) {
+            for (int i=0; i<children.length; i++) 
+            {
             	File child = new File(directory, children[i]);
-            	if (child.isDirectory()) deleteDirectory(child);
-            	else child.delete();
+            	if (child.isDirectory())
+            	{
+            		success = deleteDirectory(child);
+            	}
+            	else 
+            	{
+            		success = child.delete();
+            	}
+            	if(!success){
+            		// TODO throw IO Exception or return false?
+            	}
             }
         }
 	}
 	
 	/**
 	 * Recursively deletes a directory and everything under it.
+	 * 
 	 * @param directory
+	 * @return true on success
 	 */
-	public static void deleteDirectory(File directory) {
-        if (directory.isDirectory()) {
+	public static boolean deleteDirectory(File directory) {
+		boolean globalSuccess = true;
+		boolean success;
+        if (directory.isDirectory()) 
+        {
             String[] children = directory.list();
-            for (int i=0; i<children.length; i++) {
-            	deleteDirectory(new File(directory, children[i]));
+            for (int i=0; i<children.length; i++) 
+            {
+            	success = deleteDirectory(new File(directory, children[i]));
+            	globalSuccess = success && globalSuccess;
             }
         }   
         // The directory is now empty so delete it
-        directory.delete();
+        success = directory.delete();
+        globalSuccess = success && globalSuccess;
+        return globalSuccess;
 	}
 	
 	/**
@@ -68,6 +106,7 @@
 	 * Files in sub-directories not deleted.
 	 * @param directory
 	 */
+	// TODO remove as not in use?
 	public static void deleteFiles(File directory) {
 		if (directory.isDirectory()) {
             String[] children = directory.list();
@@ -76,86 +115,139 @@
             }
         }
 	}
+	
+	// TODO remove as not in use?
 	public static Object readObject(String fileName) {
 		Object o = null;
+		ObjectInputStream ois = null;
 		try {
-			ObjectInputStream ois = new ObjectInputStream(
+			ois = new ObjectInputStream(
 				new FileInputStream(fileName));
 			o = ois.readObject();
-			ois.close();
 		} catch (FileNotFoundException e) {
 			e.printStackTrace();
 		} catch (IOException e) {
 			e.printStackTrace();
 		} catch (ClassNotFoundException e) {
 			e.printStackTrace();
+		} finally {
+			close(ois);
 		}
 		return o;
 	}
 	
+	// TODO remove as not in use?	
 	public static void writeObject(String fileName, Object o) {
+		ObjectOutputStream oos = null;
 		try {
-			ObjectOutputStream oos = new ObjectOutputStream(
+			oos = new ObjectOutputStream(
 				new FileOutputStream(fileName));
 			oos.writeObject(o);
-			oos.close();
 		} catch (FileNotFoundException e) {
 			e.printStackTrace();
 		} catch (IOException e) {
 			e.printStackTrace();
+		} finally {
+			close(oos);
 		}
 	}
+	
+	/**
+	 * Reads a file to String
+	 * 
+	 * @param file the file to read
+	 * @return the file contents as String
+	 */
+	// TODO remove as not in use?
 	public static String getFileAsString(File file) {
 		StringBuilder sb = new StringBuilder();
+		BufferedReader br = null;
 		try {
-			BufferedReader br = new BufferedReader(
-					new FileReader(file));
+			br = new BufferedReader( new FileReader(file));
 			String line;
-			while ( (line = br.readLine()) != null) {
+			while ( (line = br.readLine()) != null) 
+			{
 				sb.append(line).append("\n");
 			}
-			br.close();
-		} catch (FileNotFoundException e) {
-			e.printStackTrace();
-		} catch (IOException e) {
-			e.printStackTrace();
+		} 
+		catch (IOException e) 
+		{
+			log.error(e.getMessage(), e);
+		}
+		finally
+		{
+			close(br);
 		}
 		return sb.toString();
 	}
 
+	// TODO remove as not in use?
 	public static void writeStringToFile(File file, String contents) {
+		BufferedWriter bw = null;
 		try {
-			if (!file.exists()) {
+			if (!file.exists()) 
+			{
 				file.createNewFile();
 			}
-			BufferedWriter bw;
+
 			bw = new BufferedWriter(new FileWriter(file));
 			bw.write(contents);
-			bw.close();
-		} catch (IOException e) {
-			e.printStackTrace();
+		} 
+		catch (IOException e) 
+		{
+			log.error(e.getMessage(), e);
+		} 
+		finally
+		{
+			close(bw);
 		}
 
 	}
 
-	public static void replaceAll(File file, String input, String output) {
+	// TODO remove as not in use?
+	public static void replaceAll(File file, String input, String output)
+	{
 		String fileText = getFileAsString(file);
 		StringBuilder sb = new StringBuilder(fileText);
 		int index = -1;
 		int length = input.length();
-		while ( (index = sb.indexOf(input)) != -1 ) {
+		while ( (index = sb.indexOf(input)) != -1 )
+		{
 			sb.replace(index, index + length, output);
 		}
-		BufferedWriter bw;
+		BufferedWriter bw = null;
 		try {
 			bw = new BufferedWriter(
 					new FileWriter(file));
 			bw.write(sb.toString());
-			bw.close();
-		} catch (IOException e) {
-			e.printStackTrace();
+		} 
+		catch (IOException e) 
+		{
+			log.error(e.getMessage(), e);
+		}
+		finally
+		{
+			close(bw);
+		}
+	}
+	
+	/**
+	 * Closes a closeable and logs exceptions
+	 * @param closeable
+	 */
+	public static void close(Closeable closeable)
+	{
+		if(closeable != null)
+		{
+			try 
+			{
+				closeable.close();
+			} 
+			catch (IOException e) 
+			{
+				log.warn(e.getMessage());
+			}
 		}
-
 	}
 
 }

Modified: incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/ParserUtil.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/ParserUtil.java?rev=881610&r1=881609&r2=881610&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/ParserUtil.java
(original)
+++ incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/ParserUtil.java
Tue Nov 17 23:06:02 2009
@@ -30,7 +30,6 @@
 /**
  * This class is used by the velocity templates.
  */
-
 public class ParserUtil {
 
 	private CodeGenConfig config;
@@ -78,10 +77,11 @@
 	 * uses this information to generate the neccessary import expression.
 	 */
 	public boolean hasBigDecimalField(DBTable t) {
-		for (DBColumn c : t.getColumns())
+		
+		for (DBColumn c : t.getColumns()){
 			if (getJavaType(c).equalsIgnoreCase("BigDecimal"))
 				return true;
-
+		}
 		return false;
 	}
 
@@ -92,9 +92,10 @@
 	public boolean hasDateField(DBTable t) {
 
 		for (DBColumn c : t.getColumns())
+		{
 			if (getJavaType(c).equalsIgnoreCase("Date"))
 				return true;
-
+		}
 		return false;
 	}
 
@@ -169,7 +170,8 @@
 		StringBuilder sb = new StringBuilder();
 		sb.append(Character.toUpperCase(name.charAt(0)));
 		boolean upperCase = false;
-		for (int i = 1; i < name.length(); i++) {
+		for (int i = 1; i < name.length(); i++) 
+		{
 			char c = name.charAt(i);
 			if (c == '_') {
 				upperCase = true;

Added: incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/DBUtilTest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/DBUtilTest.java?rev=881610&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/DBUtilTest.java
(added)
+++ incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/DBUtilTest.java
Tue Nov 17 23:06:02 2009
@@ -0,0 +1,36 @@
+package org.apache.empire.db.codegen.util;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.apache.commons.logging.Log;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class DBUtilTest {
+
+	@Test
+	public void testCloseResultSet() throws SQLException {
+		// null
+		Log log = Mockito.mock(Log.class);
+		boolean succes = DBUtil.close(null, log);
+		assertTrue(succes);
+		
+		// normal
+		ResultSet rs = Mockito.mock(ResultSet.class);
+		boolean succes2 = DBUtil.close(rs, log);
+		assertTrue(succes2);
+		
+		// exception
+		ResultSet rsFail = Mockito.mock(ResultSet.class);
+		Exception exception = new SQLException("test");
+		Mockito.doThrow(exception).when(rsFail).close();
+		boolean succes3 = DBUtil.close(rsFail, log);
+		assertFalse(succes3);
+		Mockito.verify(log).error("The resultset could not bel closed!", exception);
+	}
+
+}

Added: incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/FileUtilsTest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/FileUtilsTest.java?rev=881610&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/FileUtilsTest.java
(added)
+++ incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/FileUtilsTest.java
Tue Nov 17 23:06:02 2009
@@ -0,0 +1,45 @@
+package org.apache.empire.db.codegen.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class FileUtilsTest {
+	
+	private File testParent;
+
+	@Before
+	public void createTestFolder(){
+		testParent = new File("target/testparent");
+		cleanup();
+		boolean success = testParent.mkdirs();
+		assertTrue("could not create test file",success);
+	}
+	
+	@After
+	public void cleanup(){
+		if(testParent.exists()){
+			boolean success = testParent.delete();
+			assertTrue(success);
+		}
+	}
+	
+	@Test
+	public void testCleanDirectory() {
+		File testChildren = new File(testParent, "this/is/a/test");
+		boolean success = testChildren.mkdirs();
+		assertTrue("Could not create test folder", success);
+		FileUtils.cleanDirectory(testParent);
+		assertTrue("Parent was deleted", testParent.exists());
+		assertEquals(0, testParent.list().length);
+		
+		// TODO test with files
+		// TODO test fail if file in use
+	}
+
+}

Modified: incubator/empire-db/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/pom.xml?rev=881610&r1=881609&r2=881610&view=diff
==============================================================================
--- incubator/empire-db/trunk/pom.xml (original)
+++ incubator/empire-db/trunk/pom.xml Tue Nov 17 23:06:02 2009
@@ -263,6 +263,11 @@
 				<artifactId>junit</artifactId>
 				<version>4.6</version>
 			</dependency> 
+			<dependency>
+				<groupId>org.mockito</groupId>
+				<artifactId>mockito-core</artifactId>
+				<version>1.8.0</version>
+			</dependency> 
 		</dependencies>
 	</dependencyManagement>
 	<dependencies>
@@ -282,6 +287,11 @@
 			<artifactId>junit</artifactId>
 			<scope>test</scope>
 		</dependency>
+		<dependency>
+			<groupId>org.mockito</groupId>
+			<artifactId>mockito-core</artifactId>
+			<scope>test</scope>
+		</dependency>
 	</dependencies>
 
 	<build>



Mime
View raw message