tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adrianocrest...@apache.org
Subject svn commit: r564836 - in /incubator/tuscany/java/das/rdb/src: main/java/org/apache/tuscany/das/rdb/ main/java/org/apache/tuscany/das/rdb/impl/ main/resources/ test/java/org/apache/tuscany/das/rdb/test/ test/java/org/apache/tuscany/das/rdb/test/suites/ ...
Date Sat, 11 Aug 2007 06:09:48 GMT
Author: adrianocrestani
Date: Fri Aug 10 23:09:47 2007
New Revision: 564836

URL: http://svn.apache.org/viewvc?view=rev&rev=564836
Log:
- applying patch from JIRA-1465
- modified some FORs on ReadCommandImpl.java to increase performance

Added:
    incubator/tuscany/java/das/rdb/src/test/java/org/apache/tuscany/das/rdb/test/DynamicResultDescriptorTests.java
    incubator/tuscany/java/das/rdb/src/test/resources/customerMappingWithResultDescriptor.xml
Modified:
    incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/Command.java
    incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/ReadCommandImpl.java
    incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/ResultSetShape.java
    incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/WriteCommandImpl.java
    incubator/tuscany/java/das/rdb/src/main/resources/config.xsd
    incubator/tuscany/java/das/rdb/src/test/java/org/apache/tuscany/das/rdb/test/suites/AllCommonTests.java

Modified: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/Command.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/Command.java?view=diff&rev=564836&r1=564835&r2=564836
==============================================================================
--- incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/Command.java (original)
+++ incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/Command.java Fri
Aug 10 23:09:47 2007
@@ -18,6 +18,12 @@
  */
 package org.apache.tuscany.das.rdb;
 
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.List;
+
+import org.apache.tuscany.das.rdb.config.ResultDescriptor;
+
 import commonj.sdo.DataObject;
 
 /**
@@ -65,4 +71,51 @@
      */
     int getGeneratedKey();
 
+    /**
+     * Allow set of ResultDescriptor when command is created on-the-fly
+     * @param resultDescriptorList
+     */
+    void setResultDescriptors(List resultDescriptorList);
+    
+    List getResultDescriptors();
+   
+    /**
+     * Add/replace based on columnIndex (>=0)embedded in resultDescriptor else add at
end
+     * @param resultDescriptor
+     */
+    void addResultDescriptor(ResultDescriptor resultDescriptor);
+    
+    /**
+     * remove ResultDescriptor at given columnIndex(>=0) and return same. If not
+     * present return null. For -ve index, return null
+     * @param columnIndex
+     * @return
+     */
+    ResultDescriptor removeResultDescriptor(int columnIndex);
+    
+    /**
+     * Remove resultDescriptor only if matched for index(>=0), name, type, schema
+     * name and table name and return same, else return null For -ve index, ignore  
+     *  index and if unique match for rest of the attriutes, remove/return, if multiple
+     *  matches found, throw RuntimeException
+     * @param resultDescriptor
+     * @return
+     */
+    ResultDescriptor removeResultDescriptor(ResultDescriptor resultDescriptor);
+    
+    /**
+     * Return resultDescriptor if exact match for columnIndex(>=0) found  
+     * else return null;
+     * 
+     * @param columnIndex
+     * @return
+     */
+    ResultDescriptor getResultDescriptor(int columnIndex);
+    
+    /**
+     * Utility method
+     * @param ostrm
+     * @throws IOException
+     */
+    void printResultDescriptors(OutputStream ostrm) throws IOException;
 }

Modified: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/ReadCommandImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/ReadCommandImpl.java?view=diff&rev=564836&r1=564835&r2=564836
==============================================================================
--- incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/ReadCommandImpl.java
(original)
+++ incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/ReadCommandImpl.java
Fri Aug 10 23:09:47 2007
@@ -18,9 +18,15 @@
  */
 package org.apache.tuscany.das.rdb.impl;
 
+import java.io.IOException;
+import java.io.OutputStream;
 import java.sql.SQLException;
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Iterator;
 
+import org.apache.tuscany.das.rdb.config.ResultDescriptor;
+import org.apache.tuscany.das.rdb.config.impl.ResultDescriptorImpl;
 import org.apache.tuscany.das.rdb.config.wrapper.MappingWrapper;
 import org.apache.tuscany.das.rdb.graphbuilder.impl.GraphBuilderMetadata;
 import org.apache.tuscany.das.rdb.graphbuilder.impl.ResultSetProcessor;
@@ -36,6 +42,8 @@
 
     private int endRow = Integer.MAX_VALUE;   
 
+    private List resultDescriptors = null;
+    
     public ReadCommandImpl(String sqlString, MappingWrapper mapping, List resultDescriptor)
{
         super(sqlString);
         this.configWrapper = mapping;
@@ -45,7 +53,204 @@
         }
     }
 
-   
+    private void refreshResultSetShape(){
+		//sort descriptor and use in ResultSetShape
+        sortResultDescriptors();    		
+		this.resultSetShape = new ResultSetShape(this.resultDescriptors, configWrapper.getConfig());

+    }
+    
+	private void sortResultDescriptors(){
+		if(this.resultDescriptors == null) {
+			return;
+		}
+		
+		if( this.resultDescriptors.size()==0) {
+			return;
+		}
+		
+		//when any index is found not set, do not sort	
+		for(Iterator it =  this.resultDescriptors.iterator() ; it.hasNext();){
+			ResultDescriptor resultDescriptor = (ResultDescriptor) it.next();
+			if(resultDescriptor.getColumnIndex() <= -1){
+				return;
+			}
+		}
+
+		//now is time to sort
+		Object[] resultDescAry = this.resultDescriptors.toArray();
+		for(int i=0; i<resultDescAry.length; i++){
+			for(int j=i+1; j<resultDescAry.length; j++){
+				if( ((ResultDescriptor)resultDescAry[j]).getColumnIndex()
+						< ((ResultDescriptor)resultDescAry[i]).getColumnIndex()){
+					ResultDescriptor tmpResDesc = (ResultDescriptor)resultDescAry[i];
+					resultDescAry[i] = resultDescAry[j];
+					resultDescAry[j] = tmpResDesc;
+				}
+				
+				if( ((ResultDescriptor)resultDescAry[j]).getColumnIndex()
+						== ((ResultDescriptor)resultDescAry[i]).getColumnIndex()){
+					throw new RuntimeException("Two columns in Result Descriptor can not have same index");
			
+				}
+			}
+		}
+		
+		this.resultDescriptors.clear();
+		for(int i=0; i<resultDescAry.length; i++){
+			this.resultDescriptors.add(resultDescAry[i]);
+		}
+		
+		return;
+	}
+	
+	private ResultDescriptor deepCopyResultDescriptor(ResultDescriptor inObj){
+		ResultDescriptorImpl outObj = new ResultDescriptorImpl();
+		outObj.setColumnIndex(inObj.getColumnIndex());
+		outObj.setColumnName(inObj.getColumnName());
+		outObj.setColumnType(inObj.getColumnType());
+		outObj.setTableName(inObj.getTableName());
+		outObj.setSchemaName(inObj.getSchemaName());	
+		return outObj;
+	}
+	
+	private List deepCopyResultDescriptors(List resultDescriptors){
+		if(resultDescriptors == null || resultDescriptors.size() == 0)
+			return null;
+		
+		ArrayList copyList = new ArrayList();
+		
+		for(Iterator it =  resultDescriptors.iterator() ; it.hasNext();){
+			copyList.add( deepCopyResultDescriptor( (ResultDescriptorImpl) it.next()));
+		}
+		return copyList;
+	}
+	
+    /**
+     * When any columnIndex == -ve, sorting will not happen in ResultShapeSorter (old way)
+     * When null is passed, set this.resultSetShape to null, this will later trigger, dbms
metadata
+     * based shaping of result
+     */
+    public void setResultDescriptors(List resultDescriptors){    	
+    	this.resultDescriptors = deepCopyResultDescriptors(resultDescriptors);
+    	if(this.resultDescriptors == null || this.resultDescriptors.size()==0){
+    		this.resultSetShape = null;
+    	}
+    	else{
+    		//below will go away with List<> JDK5
+    		for(Iterator it =  this.resultDescriptors.iterator() ; it.hasNext();){
+
+    			if(!(it.next() instanceof ResultDescriptor)){
+    				throw new RuntimeException("Elements in List not of type ResultDescriptor!");
+    			}
+
+    		}
+    		refreshResultSetShape();
+    	}
+    }
+      
+    public List getResultDescriptors(){
+    	return this.resultDescriptors;
+    }
+    
+    public void addResultDescriptor(ResultDescriptor resultDescriptor){
+    	//if >= 0 columnIndex, add/replace for given index 
+    	//if < 0 columnIndex, add at end of current list
+    	if(resultDescriptor == null) {
+    		return;
+    	}
+    	
+		if(this.resultDescriptors == null){
+			this.resultDescriptors = new ArrayList();
+		}
+		
+    	if(resultDescriptor.getColumnIndex() <0){
+    		this.resultDescriptors.add(deepCopyResultDescriptor(resultDescriptor));//dont care
about columnIndex,add at end,  old way
+    	}
+    	else{
+    		ResultDescriptor existing = getResultDescriptor(resultDescriptor.getColumnIndex());
+    		if(existing != null){
+    			removeResultDescriptor(resultDescriptor.getColumnIndex());
+    		}
+    		this.resultDescriptors.add(deepCopyResultDescriptor(resultDescriptor));//add at end,
sorting will happen below
+    	}
+    	
+    	refreshResultSetShape();
+    }
+    
+
+    public ResultDescriptor removeResultDescriptor(int columnIndex){
+    	//if < 0 index return null
+    	//if >=0 index and available at given index, remove and return same
+    	//if >=0 index and not available at given index, return null
+    	ResultDescriptor existing = null;
+    	if(columnIndex >=0 && ((existing = getResultDescriptor(columnIndex)) != null)
){
+    		this.resultDescriptors.remove(existing);    	
+    		refreshResultSetShape();			
+    		return existing;
+    	}    	
+    	return null;
+    }
+    
+    public ResultDescriptor removeResultDescriptor(ResultDescriptor resultDescriptor){
+    	//remove and return only if matched for index, name, type, table name, schema name
+    	//else return null
+    	if(resultDescriptor != null){
+    		ResultDescriptor existing = getResultDescriptor(resultDescriptor.getColumnIndex());
+    		if(existing != null &&
+    		   existing.getColumnName().equals(resultDescriptor.getColumnName()) &&
+    		   existing.getColumnType().equals(resultDescriptor.getColumnType()) &&
+    		   existing.getTableName().equals(resultDescriptor.getTableName()) ) {
+    		   if(this.configWrapper.getConfig().isDatabaseSchemaNameSupported()){//multi schema
support
+    			   if(resultDescriptor.getSchemaName() != null && existing.getSchemaName()
!= null
+    				&& resultDescriptor.getSchemaName().equals(existing.getSchemaName())){
+    				   this.resultDescriptors.remove(existing);    				   
+    				   refreshResultSetShape();    					
+    				   return existing;
+    			   }
+    			   return null;
+    		   }
+    		   else{
+    			   this.resultDescriptors.remove(existing);    			   
+    			   refreshResultSetShape();    				
+				   return existing;
+    		   }    			
+    		}    				
+    	}
+    	return null;
+    }
+    
+    public ResultDescriptor getResultDescriptor(int columnIndex){
+    	//if <0 index return null
+    	//if >=0 index and available at given index,  return same
+    	//if >=0 index and not available at given index, return null
+    	if(columnIndex >=0 && this.resultDescriptors != null){
+			
+			for(Iterator it =  this.resultDescriptors.iterator() ; it.hasNext();){
+				ResultDescriptor rs = (ResultDescriptor) it.next();
+
+				if( rs.getColumnIndex() == columnIndex){
+					return rs;
+				}
+
+			}    		
+    	}
+
+    	return null;
+    }
+    
+    //Utility method
+    public void printResultDescriptors(OutputStream ostrm) throws IOException{
+    	if(this.resultDescriptors != null && this.resultDescriptors.size() != 0){
+
+		for(Iterator it =  this.resultDescriptors.iterator() ; it.hasNext();){
+    			ostrm.write( it.next().toString().getBytes() );
+    			ostrm.write('\n');
+
+    		}
+    		ostrm.flush();
+
+    	}
+    }
+    
     public void execute() {
         throw new UnsupportedOperationException();
     }

Modified: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/ResultSetShape.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/ResultSetShape.java?view=diff&rev=564836&r1=564835&r2=564836
==============================================================================
--- incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/ResultSetShape.java
(original)
+++ incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/ResultSetShape.java
Fri Aug 10 23:09:47 2007
@@ -79,14 +79,14 @@
     }
 
     //JIRA-952
-    public ResultSetShape(List resultDescriptor, Config model) {
+    protected ResultSetShape(List resultDescriptor, Config model) {
         TypeHelper helper = TypeHelper.INSTANCE;
         int size = resultDescriptor.size();
         columns = new String[size];
         tables = new String[size];
         types = new Type[size];
         schema = new String[size];
-
+        
         for (int i = 0; i < size; i++) {
             ResultDescriptor desc = (ResultDescriptor) resultDescriptor.get(i);
             if(model.isDatabaseSchemaNameSupported()){

Modified: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/WriteCommandImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/WriteCommandImpl.java?view=diff&rev=564836&r1=564835&r2=564836
==============================================================================
--- incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/WriteCommandImpl.java
(original)
+++ incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/WriteCommandImpl.java
Fri Aug 10 23:09:47 2007
@@ -18,10 +18,14 @@
  */
 package org.apache.tuscany.das.rdb.impl;
 
+import java.io.IOException;
+import java.io.OutputStream;
 import java.sql.SQLException;
+import java.util.List;
 import java.util.StringTokenizer;
 
 import org.apache.tuscany.das.rdb.config.Config;
+import org.apache.tuscany.das.rdb.config.ResultDescriptor;
 
 import commonj.sdo.DataObject;
 
@@ -31,6 +35,36 @@
         super(sqlString);
     }
 
+    public void setResultDescriptors(List resultDescriptorList){
+    	//ignore , applicable for ReadCommand only
+    }
+    
+    public List getResultDescriptors(){
+    	//ignore, applicable for ReadCommand only
+    	return null;
+    }
+    
+    public void addResultDescriptor(ResultDescriptor resultDescriptor){
+    	//ignore
+    }
+    
+
+    public ResultDescriptor removeResultDescriptor(int index){
+    	return null;
+    }
+    
+    public ResultDescriptor removeResultDescriptor(ResultDescriptor resultDescriptor){
+    	return null;
+    }
+    
+    public ResultDescriptor getResultDescriptor(int index){
+    	return null;
+    }
+    
+    public void printResultDescriptors(OutputStream ostrm) throws IOException{
+    //ignore	
+    }
+    
     public void execute() {
 
         boolean success = false;

Modified: incubator/tuscany/java/das/rdb/src/main/resources/config.xsd
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/main/resources/config.xsd?view=diff&rev=564836&r1=564835&r2=564836
==============================================================================
--- incubator/tuscany/java/das/rdb/src/main/resources/config.xsd (original)
+++ incubator/tuscany/java/das/rdb/src/main/resources/config.xsd Fri Aug 10 23:09:47 2007
@@ -130,6 +130,7 @@
       <xsd:attribute name="managed" type="xsd:boolean" default="true"/>
    </xsd:complexType>
    <xsd:complexType name="ResultDescriptor">
+      <xsd:attribute name="columnIndex" type="xsd:int" default="-1"/>
       <xsd:attribute name="columnName" type="xsd:string"/>
       <xsd:attribute name="tableName" type="xsd:string"/>
       <xsd:attribute name="schemaName" type="xsd:string"/>      

Added: incubator/tuscany/java/das/rdb/src/test/java/org/apache/tuscany/das/rdb/test/DynamicResultDescriptorTests.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/test/java/org/apache/tuscany/das/rdb/test/DynamicResultDescriptorTests.java?view=auto&rev=564836
==============================================================================
--- incubator/tuscany/java/das/rdb/src/test/java/org/apache/tuscany/das/rdb/test/DynamicResultDescriptorTests.java
(added)
+++ incubator/tuscany/java/das/rdb/src/test/java/org/apache/tuscany/das/rdb/test/DynamicResultDescriptorTests.java
Fri Aug 10 23:09:47 2007
@@ -0,0 +1,352 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.tuscany.das.rdb.test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.tuscany.das.rdb.Command;
+import org.apache.tuscany.das.rdb.DAS;
+import org.apache.tuscany.das.rdb.config.ConfigFactory;
+import org.apache.tuscany.das.rdb.config.ResultDescriptor;
+import org.apache.tuscany.das.rdb.test.data.CustomerData;
+import org.apache.tuscany.das.rdb.test.framework.DasTest;
+
+import commonj.sdo.DataObject;
+
+public class DynamicResultDescriptorTests extends DasTest {
+
+    protected void setUp() throws Exception {    
+        super.setUp();
+        new CustomerData(getAutoConnection()).refresh();
+    }
+    
+    protected void tearDown() throws Exception {
+    	super.tearDown();
+    }
+	
+    /**
+     * Set result descriptor on Command dynamically
+     */
+    public void testSetResultDescriptors() throws Exception {
+    	ConfigFactory factory = ConfigFactory.INSTANCE;
+    	DAS das = DAS.FACTORY.createDAS(getConnection());
+        // Read a customer
+        Command select = das.createCommand("SELECT ID, LASTNAME, ADDRESS FROM CUSTOMER WHERE
CUSTOMER.ID = 1");
+        
+        List resultDescriptorList = new ArrayList();
+        //as long as columnIndex is correct, order in ArrayList does not matter
+        ResultDescriptor desc3 = factory.createResultDescriptor();
+        desc3.setColumnIndex(3);
+        desc3.setColumnName("ADDRESS");
+        desc3.setColumnType("commonj.sdo.String");
+        desc3.setTableName("CUSTOMER");
+
+        ResultDescriptor desc1 = factory.createResultDescriptor();
+        desc1.setColumnIndex(1);
+        desc1.setColumnName("ID");
+        desc1.setColumnType("commonj.sdo.Int");
+        desc1.setTableName("CUSTOMER");
+        
+        ResultDescriptor desc2 = factory.createResultDescriptor();
+        desc2.setColumnIndex(2);
+        desc2.setColumnName("LASTNAME");
+        desc2.setColumnType("commonj.sdo.String");
+        desc2.setTableName("CUSTOMER");
+                
+        resultDescriptorList.add(desc3);
+        resultDescriptorList.add(desc1);
+        resultDescriptorList.add(desc2);
+        
+        select.setResultDescriptors(resultDescriptorList);
+        DataObject root = select.executeQuery();
+        assertEquals(1, root.getList("CUSTOMER").size());
+    }
+    
+    /**
+     * Set a result descriptor on Command dynamically
+     * and later replace it with another invalid one, and check failure
+     */
+    public void testReplaceResultDescriptors() throws Exception {
+    	ConfigFactory factory = ConfigFactory.INSTANCE;
+    	DAS das = DAS.FACTORY.createDAS(getConnection());
+        // Read a customer
+        Command select = das.createCommand("SELECT ID, LASTNAME, ADDRESS FROM CUSTOMER WHERE
CUSTOMER.ID = 1");
+        
+        List resultDescriptorList = new ArrayList();
+        //as long as columnIndex is correct, order in ArrayList does not matter
+        ResultDescriptor desc3 = factory.createResultDescriptor();
+        desc3.setColumnIndex(3);
+        desc3.setColumnName("ADDRESS");
+        desc3.setColumnType("commonj.sdo.String");
+        desc3.setTableName("CUSTOMER");
+
+        ResultDescriptor desc1 = factory.createResultDescriptor();
+        desc1.setColumnIndex(1);
+        desc1.setColumnName("ID");
+        desc1.setColumnType("commonj.sdo.Int");
+        desc1.setTableName("CUSTOMER");
+        
+        ResultDescriptor desc2 = factory.createResultDescriptor();
+        desc2.setColumnIndex(2);
+        desc2.setColumnName("LASTNAME");
+        desc2.setColumnType("commonj.sdo.String");
+        desc2.setTableName("CUSTOMER");
+                
+        resultDescriptorList.add(desc3);
+        resultDescriptorList.add(desc1);
+        resultDescriptorList.add(desc2);
+        
+        select.setResultDescriptors(resultDescriptorList);
+        DataObject root = select.executeQuery();
+        assertEquals(1, root.getList("CUSTOMER").size());
+        
+        //Now use invalid one
+        resultDescriptorList.clear();
+        
+        desc3.setColumnIndex(3);
+        desc3.setColumnName("ADDRESS");
+        desc3.setColumnType("commonj.sdo.Int");//incompatible type
+        desc3.setTableName("CUSTOMER");
+
+        desc1.setColumnIndex(1);
+        desc1.setColumnName("ID");
+        desc1.setColumnType("commonj.sdo.Int");
+        desc1.setTableName("CUSTOMER");
+        
+        desc2 = factory.createResultDescriptor();
+        desc2.setColumnIndex(2);
+        desc2.setColumnName("LASTNAME");
+        desc2.setColumnType("commonj.sdo.String");
+        desc2.setTableName("CUSTOMER");
+        
+        resultDescriptorList.add(desc3);
+        resultDescriptorList.add(desc2);
+        resultDescriptorList.add(desc1);
+        
+        select.setResultDescriptors(resultDescriptorList);
+        try{
+        	root = select.executeQuery();
+        	System.out.println(((DataObject)root.getList("CUSTOMER").get(0)).getString("LASTNAME"));
+        	fail("Expected exception");
+        }catch(Exception e){
+        	assertTrue(e instanceof ClassCastException);
+        }
+    }
+    
+    /**
+     * Set result descriptor on Command dynamically
+     * , overriding the one set from static config.
+     */
+    public void testOverrideResultDescriptorsFromConfig() throws Exception {
+    	ConfigFactory factory = ConfigFactory.INSTANCE;
+    	DAS das = DAS.FACTORY.createDAS(getConfig("customerMappingWithResultDescriptor.xml"),
getConnection());
+        Command select = das.getCommand("testSelectCustomer");
+        DataObject root = select.executeQuery();
+        List custList = root.getList("CUSTOMER");
+        assertEquals("Williams", ((DataObject)custList.get(0)).getString("LASTNAME"));
+        
+        List resultDescriptorList = new ArrayList();
+
+        ResultDescriptor desc3 = factory.createResultDescriptor();
+        desc3.setColumnIndex(3);
+        desc3.setColumnName("ADDRESS");
+        desc3.setColumnType("commonj.sdo.Int");//incompatible type
+        desc3.setTableName("CUSTOMER");
+
+        ResultDescriptor desc1 = factory.createResultDescriptor();
+        desc1.setColumnIndex(1);
+        desc1.setColumnName("ID");
+        desc1.setColumnType("commonj.sdo.Int");
+        desc1.setTableName("CUSTOMER");
+        
+        ResultDescriptor desc2 = factory.createResultDescriptor();
+        desc2.setColumnIndex(2);
+        desc2.setColumnName("LASTNAME");
+        desc2.setColumnType("commonj.sdo.String");
+        desc2.setTableName("CUSTOMER");
+                
+        resultDescriptorList.add(desc3);
+        resultDescriptorList.add(desc1);
+        resultDescriptorList.add(desc2);
+        
+        select.setResultDescriptors(resultDescriptorList);
+        try{
+        	select.executeQuery();
+        	fail("Expected exception");
+        }catch(Exception e){
+        	assertTrue(e instanceof ClassCastException);
+        }    	
+    }
+    
+    /**
+     * Set null result descriptor on Command dynamically 
+     */
+    public void testNullResultDescriptors() throws Exception {
+    	DAS das = DAS.FACTORY.createDAS(getConnection());
+        // Read a customer
+        Command select = das.createCommand("SELECT ID, LASTNAME, ADDRESS FROM CUSTOMER WHERE
CUSTOMER.ID = 1");
+        
+        List resultDescriptorList = null;
+
+        try{
+        	select.setResultDescriptors(resultDescriptorList);
+
+        	select.executeQuery();
+        	this.assertTrue("Derby has DBMS metadata, so it will be used", true);
+        }catch(RuntimeException e){
+        	fail("Expected to succeed with no exception");
+        } 
+    }
+    
+    /**
+     * Set -ve columnIndex in result descriptor on Command dynamically
+     * Set will not be sorted and Type mismatch wi (Int and String for ID)
+     * will throw exception 
+     */
+    public void testNegativeIndexResultDescriptors() throws Exception {
+    	ConfigFactory factory = ConfigFactory.INSTANCE;
+    	DAS das = DAS.FACTORY.createDAS(getConnection());
+        // Read a customer
+        Command select = das.createCommand("SELECT ID, LASTNAME, ADDRESS FROM CUSTOMER WHERE
CUSTOMER.ID = 1");
+        
+        List resultDescriptorList = new ArrayList();
+        
+        ResultDescriptor desc3 = factory.createResultDescriptor();
+        desc3.setColumnIndex(-3);//invalid
+        desc3.setColumnName("ADDRESS");
+        desc3.setColumnType("commonj.sdo.String");
+        desc3.setTableName("CUSTOMER");
+
+        ResultDescriptor desc1 = factory.createResultDescriptor();
+        desc1.setColumnIndex(1);
+        desc1.setColumnName("ID");
+        desc1.setColumnType("commonj.sdo.Int");
+        desc1.setTableName("CUSTOMER");
+        
+        ResultDescriptor desc2 = factory.createResultDescriptor();
+        desc2.setColumnIndex(2);
+        desc2.setColumnName("LASTNAME");
+        desc2.setColumnType("commonj.sdo.String");
+        desc2.setTableName("CUSTOMER");
+                
+        resultDescriptorList.add(desc3);
+        resultDescriptorList.add(desc1);
+        resultDescriptorList.add(desc2);
+
+        try{
+        	select.setResultDescriptors(resultDescriptorList);
+
+        	select.executeQuery();
+        	fail("Expected exception");
+        }catch(RuntimeException e){
+        	assertTrue("Exception as sorting wil not happen due to -ve columnIndex", true);
+        } 
+    }
+    
+    /**
+     * Set matching columnIndices in result descriptor on Command dynamically 
+     */
+    public void testMatchingIndexResultDescriptor() throws Exception {
+    	ConfigFactory factory = ConfigFactory.INSTANCE;
+    	DAS das = DAS.FACTORY.createDAS(getConnection());
+        // Read a customer
+        Command select = das.createCommand("SELECT ID, LASTNAME, ADDRESS FROM CUSTOMER WHERE
CUSTOMER.ID = 1");
+        
+        List resultDescriptorList = new ArrayList();
+        
+        ResultDescriptor desc3 = factory.createResultDescriptor();
+        desc3.setColumnIndex(1);//two times index is 1
+        desc3.setColumnName("ADDRESS");
+        desc3.setColumnType("commonj.sdo.String");
+        desc3.setTableName("CUSTOMER");
+
+        ResultDescriptor desc1 = factory.createResultDescriptor();
+        desc1.setColumnIndex(1);
+        desc1.setColumnName("ID");
+        desc1.setColumnType("commonj.sdo.Int");
+        desc1.setTableName("CUSTOMER");
+        
+        ResultDescriptor desc2 = factory.createResultDescriptor();
+        desc2.setColumnIndex(2);
+        desc2.setColumnName("LASTNAME");
+        desc2.setColumnType("commonj.sdo.String");
+        desc2.setTableName("CUSTOMER");
+                
+        resultDescriptorList.add(desc3);
+        resultDescriptorList.add(desc1);
+        resultDescriptorList.add(desc2);
+
+        try{
+        	select.setResultDescriptors(resultDescriptorList);
+
+        	select.executeQuery();
+        	fail("Expected exception");
+        }catch(RuntimeException e){
+        	assertTrue(e.getMessage().indexOf("Two columns in Result Descriptor can not have
same index")!= -1);
+        } 
+    }    
+    
+    public void testAddRemoveResultDescriptor() throws Exception {
+    	ConfigFactory factory = ConfigFactory.INSTANCE;
+
+    	ResultDescriptor desc3 = factory.createResultDescriptor();
+        desc3.setColumnIndex(3);//two times index is 1
+        desc3.setColumnName("ADDRESS");
+        desc3.setColumnType("commonj.sdo.String");
+        desc3.setTableName("CUSTOMER");
+
+    	ResultDescriptor desc1 = factory.createResultDescriptor();
+        desc1.setColumnIndex(1);
+        desc1.setColumnName("ID");
+        desc1.setColumnType("commonj.sdo.Int");
+        desc1.setTableName("CUSTOMER");
+        
+        ResultDescriptor desc2 = factory.createResultDescriptor();
+        desc2.setColumnIndex(2);
+        desc2.setColumnName("LASTNAME");
+        desc2.setColumnType("commonj.sdo.String");
+        desc2.setTableName("CUSTOMER");
+
+    	DAS das = DAS.FACTORY.createDAS(getConnection());
+        // Read a customer
+        Command select = das.createCommand("SELECT ID, LASTNAME, ADDRESS FROM CUSTOMER WHERE
CUSTOMER.ID = 1");
+        select.addResultDescriptor(desc3);
+        select.addResultDescriptor(desc1);
+        select.addResultDescriptor(desc2);
+        //select.printResultDescriptors(System.out);
+        assertEquals(1, ((ResultDescriptor)select.getResultDescriptors().get(0)).getColumnIndex());
+        assertEquals(2, ((ResultDescriptor)select.getResultDescriptors().get(1)).getColumnIndex());
+        assertEquals(3, ((ResultDescriptor)select.getResultDescriptors().get(2)).getColumnIndex());
+        
+        select.removeResultDescriptor(2);
+        //select.printResultDescriptors(System.out);
+        assertEquals(1, ((ResultDescriptor)select.getResultDescriptors().get(0)).getColumnIndex());
+        assertEquals(3, ((ResultDescriptor)select.getResultDescriptors().get(1)).getColumnIndex());
+        
+        select.removeResultDescriptor(desc3);
+        //select.printResultDescriptors(System.out);
+        assertEquals(1, ((ResultDescriptor)select.getResultDescriptors().get(0)).getColumnIndex());
+        
+        select.addResultDescriptor(null);
+        select.removeResultDescriptor(null);
+        //select.printResultDescriptors(System.out);
+        assertEquals(1, ((ResultDescriptor)select.getResultDescriptors().get(0)).getColumnIndex());
       
+    }
+}

Modified: incubator/tuscany/java/das/rdb/src/test/java/org/apache/tuscany/das/rdb/test/suites/AllCommonTests.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/test/java/org/apache/tuscany/das/rdb/test/suites/AllCommonTests.java?view=diff&rev=564836&r1=564835&r2=564836
==============================================================================
--- incubator/tuscany/java/das/rdb/src/test/java/org/apache/tuscany/das/rdb/test/suites/AllCommonTests.java
(original)
+++ incubator/tuscany/java/das/rdb/src/test/java/org/apache/tuscany/das/rdb/test/suites/AllCommonTests.java
Fri Aug 10 23:09:47 2007
@@ -31,6 +31,7 @@
 import org.apache.tuscany.das.rdb.test.ConverterTests;
 import org.apache.tuscany.das.rdb.test.CorrectedDefectTests;
 import org.apache.tuscany.das.rdb.test.CrudWithChangeHistory;
+import org.apache.tuscany.das.rdb.test.DynamicResultDescriptorTests;
 import org.apache.tuscany.das.rdb.test.ExceptionTests;
 import org.apache.tuscany.das.rdb.test.GeneratedCommandTests;
 import org.apache.tuscany.das.rdb.test.GeneratedId;
@@ -109,6 +110,7 @@
         suite.addTest(new TestSuite(TransactionTests.class));
         suite.addTest(new TestSuite(MultiSchemaTests.class));
     
+        suite.addTest(new TestSuite(DynamicResultDescriptorTests.class));
         //$JUnit-END$
         return suite;
     }

Added: incubator/tuscany/java/das/rdb/src/test/resources/customerMappingWithResultDescriptor.xml
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/test/resources/customerMappingWithResultDescriptor.xml?view=auto&rev=564836
==============================================================================
--- incubator/tuscany/java/das/rdb/src/test/resources/customerMappingWithResultDescriptor.xml
(added)
+++ incubator/tuscany/java/das/rdb/src/test/resources/customerMappingWithResultDescriptor.xml
Fri Aug 10 23:09:47 2007
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="ASCII"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied.  See the License for the
+  specific language governing permissions and limitations
+  under the License.
+ -->
+<Config xmlns="http:///org.apache.tuscany.das.rdb/config.xsd"> 
+  <Command name="testSelectCustomer" SQL="select * from CUSTOMER where CUSTOMER.ID = 1"
kind="Select">
+ 		<ResultDescriptor columnName="ID" tableName="CUSTOMER" columnType="commonj.sdo.IntObject"/>
+ 		<ResultDescriptor columnName="LASTNAME" tableName="CUSTOMER" columnType="commonj.sdo.String"/>
+ 		<ResultDescriptor columnName="ADDRESS" tableName="CUSTOMER" columnType="commonj.sdo.String"/>
+  </Command>
+
+  <Table tableName="CUSTOMER">
+    <Column columnName="ID" primaryKey="true"/>
+  </Table>
+  
+</Config>



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org


Mime
View raw message