empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From franci...@apache.org
Subject svn commit: r907503 - in /incubator/empire-db/trunk/empire-db-codegen/src: main/java/org/apache/empire/db/codegen/ main/java/org/apache/empire/db/codegen/util/ main/resources/templates/ test/java/org/apache/empire/db/codegen/
Date Sun, 07 Feb 2010 22:20:39 GMT
Author: francisdb
Date: Sun Feb  7 22:20:38 2010
New Revision: 907503

URL: http://svn.apache.org/viewvc?rev=907503&view=rev
Log:
EMPIREDB-64
Cleaning up the code
Add initial unit tests for the WriterService

Added:
    incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/WriterService.java
      - copied, changed from r907497, incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/ParserUtil.java
    incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/WriterServiceTest.java
Removed:
    incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/ParserUtil.java
Modified:
    incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenWriter.java
    incubator/empire-db/trunk/empire-db-codegen/src/main/resources/templates/Table.vm

Modified: incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenWriter.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenWriter.java?rev=907503&r1=907502&r2=907503&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenWriter.java
(original)
+++ incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenWriter.java
Sun Feb  7 22:20:38 2010
@@ -31,7 +31,6 @@
 import org.apache.empire.db.DBTable;
 import org.apache.empire.db.DBView;
 import org.apache.empire.db.codegen.util.FileUtils;
-import org.apache.empire.db.codegen.util.ParserUtil;
 import org.apache.velocity.Template;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.Velocity;
@@ -78,7 +77,7 @@
 	public static final String RECORD_TEMPLATE = "Record.vm";
 
 	// Services
-	private final ParserUtil parserUtil;
+	private final WriterService writerService;
 
 	// Properties
 	private final CodeGenConfig config;
@@ -91,7 +90,7 @@
 	 * Constructor
 	 */
 	public CodeGenWriter(CodeGenConfig config) {
-		this.parserUtil = new ParserUtil(config);
+		this.writerService = new WriterService(config);
 		this.config = config;
 		// we have to keep this in sync with our logging system
 		// http://velocity.apache.org/engine/releases/velocity-1.5/developer-guide.html#simpleexampleofacustomlogger
@@ -180,7 +179,7 @@
 		File file = new File(baseDir, config.getDbClassName() + ".java");
 		VelocityContext context = new VelocityContext();
 		// TODO fall back to getPackageName() is the other names are not set
-		context.put("parser", parserUtil);
+		context.put("parser", writerService);
 		context.put("tableClassSuffix", config.getTableClassSuffix());
 		context.put("basePackageName", config.getPackageName());
 		context.put("dbClassName", config.getDbClassName());
@@ -207,10 +206,10 @@
 	}
 
 	private File createTableClass(DBDatabase db, DBTable table) {
-		File file = new File(tableDir, parserUtil.getTableClassName(table.getName())
+		File file = new File(tableDir, writerService.getTableClassName(table.getName())
 				+ ".java");
 		VelocityContext context = new VelocityContext();
-		context.put("parser", parserUtil);
+		context.put("parser", writerService);
 		context.put("basePackageName", config.getPackageName());
 		context.put("tablePackageName", config.getTablePackageName());
 		context.put("baseTableClassName", config.getTableBaseName());
@@ -231,10 +230,10 @@
 	}
 
 	private File createViewClass(DBDatabase db, DBView view) {
-		File file = new File(viewDir, parserUtil.getViewClassName(view.getName())
+		File file = new File(viewDir, writerService.getViewClassName(view.getName())
 				+ ".java");
 		VelocityContext context = new VelocityContext();
-		context.put("parser", parserUtil);
+		context.put("parser", writerService);
 		context.put("basePackageName", config.getPackageName());
 		context.put("viewPackageName", config.getViewPackageName());
 		context.put("baseViewClassName", config.getViewBaseName());
@@ -258,9 +257,9 @@
 	}
 
 	private File createRecordClass(DBDatabase db, DBTable table) {
-		File file = new File(recordDir, parserUtil.getRecordClassName(table.getName()) + ".java");
+		File file = new File(recordDir, writerService.getRecordClassName(table.getName()) + ".java");
 		VelocityContext context = new VelocityContext();
-		context.put("parser", parserUtil);
+		context.put("parser", writerService);
 		context.put("basePackageName", config.getPackageName());
 		// If the tables shall be nested within the database classe, their include path for the
records needs to be changed
 		if (config.isNestTables())

Copied: incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/WriterService.java
(from r907497, 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/WriterService.java?p2=incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/WriterService.java&p1=incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/ParserUtil.java&r1=907497&r2=907503&rev=907503&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/WriterService.java
Sun Feb  7 22:20:38 2010
@@ -16,10 +16,13 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.empire.db.codegen.util;
+package org.apache.empire.db.codegen;
 
 import java.lang.reflect.Method;
+import java.math.BigDecimal;
+import java.util.Date;
 import java.util.HashSet;
+import java.util.Set;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -28,95 +31,108 @@
 import org.apache.empire.db.DBRecord;
 import org.apache.empire.db.DBTable;
 import org.apache.empire.db.DBTableColumn;
-import org.apache.empire.db.codegen.CodeGenConfig;
 
 /**
  * This class is used by the velocity templates.
  */
-public class ParserUtil {
+public class WriterService {
 
-	private static final Log log = LogFactory.getLog(ParserUtil.class);
+	private static final Log log = LogFactory.getLog(WriterService.class);
 
-	private CodeGenConfig config;
+	private final CodeGenConfig config;
+	private final Set<String> dbrecMethodNames;
 	
-	private static HashSet<String> dbrecMethodNames;
-	
-	public ParserUtil(CodeGenConfig config) {
-
+	public WriterService(CodeGenConfig config)
+	{
+		this.dbrecMethodNames = loadDBRecordMethodNames();
 		this.config = config;
 	}
 	
-	public static HashSet<String> getDBRecordMethodNames(){
-		// some g/setters like getState() can conflict with DBRecord
-		// methods. To check for conflicts, we'll need to know
-		// the method names.
-		if(dbrecMethodNames == null){
-			Method[] dbrecMethods = DBRecord.class.getMethods();
-			dbrecMethodNames = new HashSet<String>(dbrecMethods.length);
-			for(Method method : dbrecMethods){
-				dbrecMethodNames.add(method.getName());
-			}
+	/**
+	 * Some g/setters like getState() can conflict with DBRecord
+	 * methods. To check for conflicts, we'll need to know
+	 * the method names.
+	 * @return the DBRecord method's names
+	 */
+	private Set<String> loadDBRecordMethodNames()
+	{
+		Method[] dbrecMethods = DBRecord.class.getMethods();
+		Set<String> names = new HashSet<String>(dbrecMethods.length);
+		for(Method method : dbrecMethods)
+		{
+			names.add(method.getName());
 		}
-		return dbrecMethodNames;
+		return names;
 	}
 
 	/**
 	 * Returns the java table class name for a given table name.
 	 */
-	public String getTableClassName(String tableName) {
-		return config.getTableClassPrefix() + javaClassName(tableName)
-				+ config.getTableClassSuffix();
+	public String getTableClassName(String tableName)
+	{
+		return config.getTableClassPrefix() + deriveClassName(tableName)
+			+ config.getTableClassSuffix();
 	}
 
 	/**
 	 * Returns the java table class name for a given view name.
 	 */
-	public String getViewClassName(String viewName) {
-		return config.getViewClassPrefix() + javaClassName(viewName)
-		+ config.getViewClassSuffix();
+	public String getViewClassName(String viewName)
+	{
+		return config.getViewClassPrefix() + deriveClassName(viewName)
+			+ config.getViewClassSuffix();
 	}
 	
 	/**
 	 * Returns the java record class name for a given table name.
+	 * 
+	 * @param tableName the table name
 	 */
-	public String getRecordClassName(String tableName) {
-		return javaClassName(tableName) + "Record";
+	public String getRecordClassName(String tableName)
+	{
+		return deriveClassName(tableName) + "Record";
 	}
 
 	/**
 	 * Returns the "getter" name for a given DBColumn.
+	 * @param column the column
 	 */
-	public String getAccessorName(DBColumn c) {
-
-		return deriveAccessorName(c.getName(), getJavaType(c).equalsIgnoreCase(
-				"Boolean"));
+	public String getAccessorName(DBColumn column) 
+	{
+		return deriveAccessorName(column.getName(), getJavaType(column));
 	}
 
 	/**
 	 * Returns the "setter" name for a given DBColumn
+	 * @param column the column
 	 */
-	public String getMutatorName(DBColumn c) {
-
-		return deriveMutatorName(c.getName(), getJavaType(c).equalsIgnoreCase("Boolean"));
+	public String getMutatorName(DBColumn column) 
+	{
+		return deriveMutatorName(column.getName(), getJavaType(column));
 	}
 	
 	/**
 	 * Returns the attribute name for a given DBColumn
 	 */
-	public String getAttributeName(DBColumn c) {
-
+	public String getAttributeName(DBColumn c)
+	{
 		return deriveAttributeName(c.getName());
 	}
 
 	/**
 	 * Returns whether the given table uses BigDecimal class or not. Velocity
 	 * uses this information to generate the neccessary import expression.
+	 * 
+	 * @param table the table to inspect
 	 */
-	public boolean hasBigDecimalField(DBTable t) {
-		
-		for (DBColumn c : t.getColumns()){
-			if (getJavaType(c).equalsIgnoreCase("BigDecimal"))
+	public boolean hasBigDecimalField(DBTable table)
+	{	
+		for (DBColumn column : table.getColumns())
+		{
+			if (getJavaType(column) == BigDecimal.class)
+			{
 				return true;
+			}
 		}
 		return false;
 	}
@@ -124,30 +140,39 @@
 	/**
 	 * Returns whether the given table uses Date class or not. Velocity
 	 * uses this information to generate the neccessary import expression.
+	 * 
+	 * @param table the table to inspect
 	 */
-	public boolean hasDateField(DBTable t) {
-
-		for (DBColumn c : t.getColumns())
+	public boolean hasDateField(DBTable table)
+	{
+		for (DBColumn column : table.getColumns())
 		{
-			if (getJavaType(c).equalsIgnoreCase("Date"))
+			if (getJavaType(column) == Date.class)
+			{
 				return true;
+			}
 		}
 		return false;
 	}
 
 	/**
 	 * Returns whether the given table has a locking column or not.
+	 * 
+	 * @param table the table to inspect
 	 */
-	public boolean hasLockingColumn(DBTable t) {
-
-		return t.getTimestampColumn() != null;
+	public boolean hasLockingColumn(DBTable table)
+	{
+		return table.getTimestampColumn() != null;
 	}
 
 	/**
 	 * Returns the corresponding java type of the given empire DataType.
+	 * 
+	 * @param column the column to get the type for
 	 */
-	public String getJavaType(DBColumn c) {
-		DataType type = getDataType(c);
+	public Class<?> getJavaType(DBColumn column)
+	{
+		DataType type = getDataType(column);
 		// We added the attribute of original datatype to AUTOINC columns
 		// in CodeGenParser.addColumn(). Now we need to use it so that
 		// the g/setters deal with the right Java type.
@@ -155,41 +180,47 @@
 		// If the original data type was not set as an attribute for some
 		// reason this will just fall through to the bottom and
 		// return "Byte[]", so no problem.
-		if (type.equals(DataType.AUTOINC) && null != c.getAttribute("AutoIncDataType"))
-			type = (DataType)c.getAttribute("AutoIncDataType");
+		if (DataType.AUTOINC.equals(type) && null != column.getAttribute("AutoIncDataType"))
+		{
+			type = (DataType)column.getAttribute("AutoIncDataType");
+		}
 		
-		if (type.equals(DataType.INTEGER))
-			return "Long";
-		else if (type.equals(DataType.TEXT))
-			return "String";
-		else if (type.equals(DataType.DATE))
-			return "Date";
-		else if (type.equals(DataType.DATETIME))
-			return "Date";
-		else if (type.equals(DataType.CHAR))
-			return "String";
-		else if (type.equals(DataType.DOUBLE))
-			return "Double";
-		else if (type.equals(DataType.DECIMAL))
-			return "BigDecimal";
-		else if (type.equals(DataType.BOOL))
-			return "Boolean";
-		else if (type.equals(DataType.CLOB))
-			return "String";
-		else if (type.equals(DataType.BLOB))
-			return "Byte[]";
-		else if (type.equals(DataType.UNKNOWN))
-			return "Byte[]";
-		else {
-			log.warn("SQL column type " + type.toString() + " not supported.");
-			return "Byte[]";
+		// TODO might be better to add this to the enum
+		// TODO use primitives for non-nullable columns?
+		switch(type){
+		case INTEGER:
+			return Long.class;
+		case TEXT:
+			return String.class;
+		case DATE:
+			return Date.class;
+		case DATETIME:
+			return Date.class;
+		case CHAR:
+			return String.class;
+		case DOUBLE:
+			return Double.class;
+		case DECIMAL:
+			return BigDecimal.class;
+		case BOOL:
+			return Boolean.class;
+		case CLOB:
+			return String.class;
+		case BLOB:
+			return Byte[].class;
+		case UNKNOWN:
+			return Byte[].class;
+		default:
+			log.warn("SQL column type " + type.toString() + " not supported, falling back to byte
array.");
+			return Byte[].class;
 		}
 	}
 
 	/**
 	 * Returns the empire DataType of the given DBColumn.
 	 */
-	public DataType getDataType(DBColumn c) {
+	public DataType getDataType(DBColumn c)
+	{
 		DBTableColumn dbC = (DBTableColumn) c;
 		return dbC.getDataType();
 	}
@@ -197,43 +228,46 @@
 	/**
 	 * Returns the default value of the given DBColumn.
 	 */
-	public String getDefaultValue(DBColumn c) {
+	public String getDefaultValue(DBColumn c)
+	{
 		DBTableColumn dbC = (DBTableColumn) c;
 		Object val = dbC.getDefaultValue();
 		if (val == null)
+		{
 			return "null";
+		}
 
 		return "\"" + val + "\"";
 	}
-
-	// ----------- private members
 	
 	
 	/**
 	 * Derives a java class name from a database table name.
 	 */
-	private static String javaClassName(String name) {
+	private String deriveClassName(String name)
+	{
 		StringBuilder sb = new StringBuilder();
 		sb.append(Character.toUpperCase(name.charAt(0)));
 		// Tables might already be camel case. Let's skip this if no '_' anywhere.
-		if(name.substring(1).indexOf('_') <= 0) {
+		if(name.substring(1).indexOf('_') <= 0)
+		{
 			if(name.length() > 1)
 				sb.append(name.substring(1));
 			return sb.toString();
 		}
-		boolean upperCase = false;
+		boolean nextCharacterUppercase = false;
 		for (int i = 1; i < name.length(); i++) 
 		{
 			char c = name.charAt(i);
 			if (c == '_') {
-				upperCase = true;
+				nextCharacterUppercase = true;
 				continue;
 			}
-			if (upperCase)
+			if (nextCharacterUppercase)
 				sb.append(Character.toUpperCase(c));
 			else
 				sb.append(Character.toLowerCase(c));
-			upperCase = false;
+			nextCharacterUppercase = false;
 		}
 		return sb.toString();
 	}
@@ -245,34 +279,46 @@
 	 * @param isBoolean
 	 * @return
 	 */
-	private static String deriveAccessorName(String attribute, boolean isBoolean) {
-		return deriveRecordMethodName(attribute, isBoolean, true);
+	private String deriveAccessorName(String attribute, Class<?> type)
+	{
+		return deriveRecordMethodName(attribute, type, true);
 	}
 	
-	// We need to alter both getter and setter if the method name will
-	// conflict with existing methods DBRecord. This will check both
-	// so that getter and setter have matching suffixes if one or 
-	// the other conflicts with an existing method.
-	private static String deriveRecordMethodName(String attribute, boolean isBoolean, boolean
isGetter) {
+	/**
+	 * We need to alter both getter and setter if the method name will
+	 * conflict with existing methods DBRecord. This will check both
+	 * so that getter and setter have matching suffixes if one or 
+	 * the other conflicts with an existing method.
+	 */
+	private String deriveRecordMethodName(String attribute, Class<?> type, boolean isGetter)
{
 		attribute = deriveAttributeName(attribute);
-		StringBuilder sb = new StringBuilder();
-		sb.append(Character.toUpperCase(attribute.charAt(0)));
-		sb.append(attribute.substring(1));
+		StringBuilder attributeName = new StringBuilder();
+		attributeName.append(Character.toUpperCase(attribute.charAt(0)));
+		attributeName.append(attribute.substring(1));
 		
-		StringBuilder sbGet = new StringBuilder();
-		if (isBoolean)
-			sbGet.append("is");
-		else
-			sbGet.append("get");
-		sbGet.append(sb);
+		StringBuilder sbGet = new StringBuilder(getGetterPrefix(type));
+		sbGet.append(attributeName);
 		
 		StringBuilder sbSet = new StringBuilder("set");
-		sbSet.append(sb);
-		sb = isGetter ? sbGet : sbSet;
-		HashSet<String> names = getDBRecordMethodNames();
-		if(names.contains(sbGet.toString()) || names.contains(sbSet.toString()))
-			sb.append("Column"); // Any change will resolve the conflict.
-		return sb.toString();
+		sbSet.append(attributeName);
+		attributeName = isGetter ? sbGet : sbSet;
+		if(dbrecMethodNames.contains(sbGet.toString()) || dbrecMethodNames.contains(sbSet.toString()))
+		{
+			// Any change will resolve the conflict.
+			attributeName.append("Column");
+		}
+		return attributeName.toString();
+	}
+	
+	private String getGetterPrefix(Class<?> type){
+		if (type == boolean.class || type == Boolean.class)
+		{
+			return "is";
+		}
+		else
+		{
+			return "get";
+		}
 	}
 
 	/**
@@ -281,8 +327,9 @@
 	 * @param attribute
 	 * @return
 	 */
-	private static String deriveMutatorName(String attribute, boolean isBoolean) {
-		return deriveRecordMethodName(attribute, isBoolean, false);
+	private String deriveMutatorName(String attribute, Class<?> type)
+	{
+		return deriveRecordMethodName(attribute, type, false);
 	}
 	
 	/**
@@ -291,7 +338,8 @@
 	 * @param attribute
 	 * @return
 	 */
-	private static String deriveAttributeName(String column) {
+	private String deriveAttributeName(String column)
+	{
 		return column.replace(' ', '_');
 	}
 

Modified: incubator/empire-db/trunk/empire-db-codegen/src/main/resources/templates/Table.vm
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/main/resources/templates/Table.vm?rev=907503&r1=907502&r2=907503&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/main/resources/templates/Table.vm (original)
+++ incubator/empire-db/trunk/empire-db-codegen/src/main/resources/templates/Table.vm Sun
Feb  7 22:20:38 2010
@@ -28,7 +28,7 @@
 #end
 
 public #if($nestTables == true)static#end class $parser.getTableClassName($table.name) extends
${baseTableClassName} {
-	// Primary key column
+
 	private List<DBTableColumn> keyColumns = new ArrayList<DBTableColumn>();
 	
 	// Regular attributes

Added: incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/WriterServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/WriterServiceTest.java?rev=907503&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/WriterServiceTest.java
(added)
+++ incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/WriterServiceTest.java
Sun Feb  7 22:20:38 2010
@@ -0,0 +1,50 @@
+package org.apache.empire.db.codegen;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.empire.data.DataType;
+import org.apache.empire.db.DBTableColumn;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class WriterServiceTest {
+
+	@Test
+	public void testGetTableClassName() {
+		CodeGenConfig config = new CodeGenConfig();
+		WriterService service = new WriterService(config);
+		assertEquals("TestTable", service.getTableClassName("test_table"));
+		assertEquals("TestTable", service.getTableClassName("TEST_TABLE"));
+		assertEquals("TestTable", service.getTableClassName("TestTable"));
+		// TODO is this correct?
+		assertEquals("TESTTABLE", service.getTableClassName("TESTTABLE"));
+	}
+	
+	@Test
+	public void testGetAccessorName(){
+		CodeGenConfig config = new CodeGenConfig();
+		WriterService service = new WriterService(config);
+		
+		DBTableColumn col = Mockito.mock(DBTableColumn.class);
+		Mockito.when(col.getDataType()).thenReturn(DataType.INTEGER);
+		Mockito.when(col.getName()).thenReturn("name");
+		assertEquals("getName",service.getAccessorName(col));
+		
+		DBTableColumn col2 = Mockito.mock(DBTableColumn.class);
+		Mockito.when(col2.getDataType()).thenReturn(DataType.BOOL);
+		Mockito.when(col2.getName()).thenReturn("name");
+		assertEquals("isName",service.getAccessorName(col2));
+	}
+	
+	@Test
+	public void testGetMutatorName(){
+		CodeGenConfig config = new CodeGenConfig();
+		WriterService service = new WriterService(config);
+		
+		DBTableColumn col = Mockito.mock(DBTableColumn.class);
+		Mockito.when(col.getDataType()).thenReturn(DataType.DECIMAL);
+		Mockito.when(col.getName()).thenReturn("name");
+		assertEquals("setName", service.getMutatorName(col));		
+	}
+
+}



Mime
View raw message