summaryrefslogtreecommitdiffstats
path: root/das-java/trunk/rdb/src/test/java/org/apache/tuscany/das/rdb/test/NamedParameterTests.java
blob: 88e9b201eacc0c802a3cd88422017032825f6b38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * 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.List;

import org.apache.tuscany.das.rdb.Command;
import org.apache.tuscany.das.rdb.ConfigHelper;
import org.apache.tuscany.das.rdb.DAS;
import org.apache.tuscany.das.rdb.config.Config;
import org.apache.tuscany.das.rdb.config.ConfigFactory;
import org.apache.tuscany.das.rdb.config.Parameter;
import org.apache.tuscany.das.rdb.test.data.CustomerData;
import org.apache.tuscany.das.rdb.test.framework.DasTest;

import commonj.sdo.DataObject;

public class NamedParameterTests  extends DasTest {
    protected void setUp() throws Exception {    
        super.setUp();
        new CustomerData(getAutoConnection()).refresh();
    }
    
    protected void tearDown() throws Exception {
    	super.tearDown();
    }
 
    /**
     * Test passing <Parameters><Parameter></Paramerers> to create/update/delete
     * through config, with duplicate indexes
     * @throws Exception
     */
    public void testCUDNamedParameters() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("namedParameter.xml"), getConnection());
        // Read customer 1
        Command select = das.createCommand("Select * from CUSTOMER where ID = 1");
        DataObject root = select.executeQuery();
        assertFalse(root.get("CUSTOMER[1]/LASTNAME").equals("Pavick"));

        // Modify customer
        DataObject customer = (DataObject) root.get("CUSTOMER[1]");
        customer.set("LASTNAME", "Pavick");
        customer.set("ADDRESS", "Pavick's Address");
        // Build apply changes command
        try{
        	das.applyChanges(root);
        	fail("Expected exception");
        }catch(RuntimeException e){
        	assertTrue(e.getMessage().startsWith("Parameters with improper indexes!"));
        }
        
        //create
        root = select.executeQuery();
        DataObject newCustomer = root.createDataObject("CUSTOMER");
        newCustomer.setInt("ID", 6);
        newCustomer.setString("LASTNAME", "Louise");
        newCustomer.setString("ADDRESS", "U.K.");
        try{
        	das.applyChanges(root);
        	fail("Expected exception");
        }catch(RuntimeException e){
        	assertTrue(e.getMessage().startsWith("Parameters with duplicate indexes!"));
        }
        
        //delete
        root = select.executeQuery();
        DataObject toBeDeleted = root.getDataObject("CUSTOMER[1]");
        toBeDeleted.setString("LASTNAME", "Pavick");
        toBeDeleted.delete();
        das.applyChanges(root);
        root = select.executeQuery();
        assertEquals(null, root.getDataObject("CUSTOMER[1]"));
        
    }
        
    /**
     * Test passing <Parameter>List to Command through config
     * @throws Exception
     */    
    public void testCommandNamedParameters() throws Exception {
    	DAS das = DAS.FACTORY.createDAS(getConfig("namedParameter.xml"), getConnection());
    	//select
    	Command select = das.getCommand("getCustomer");
    	select.setParameter("ID", new Integer(1));
    	DataObject root = select.executeQuery();
    	List customers = root.getList("CUSTOMER");
    	assertEquals(1, customers.size());
    	DataObject customer = (DataObject)customers.get(0);
    	assertEquals("Williams", customer.get("LASTNAME"));
    	
    	//insert - command in config has <Parameter>
    	Command insert = das.getCommand("createCustomer");
    	//see order is not maintained, but config depicts it through index attrb
    	//if index is missing , auto increment logic will be followed
    	insert.setParameter("LASTNAME", "Louise");
    	insert.setParameter("ADDRESS", "TPO");
    	insert.setParameter("ID", new Integer(6));
    	insert.execute();
    	select.setParameter("ID", new Integer(6));
    	root = select.executeQuery();
    	customers = root.getList("CUSTOMER");
    	assertEquals(1, customers.size());
    	customer = (DataObject)customers.get(0);
    	assertEquals("Louise", customer.get("LASTNAME"));
    	
    	//insert - command in config has no <Parameter>
    	Command insertNoParam = das.getCommand("createCustomerNoParam");
    	//if param/index is missing in cfg, auto increment logic will be followed
    	insertNoParam.setParameter("ID", new Integer(7));
    	insertNoParam.setParameter("LASTNAME", "Louise7");
    	insertNoParam.setParameter("ADDRESS", "TPO7");
    	insertNoParam.execute();
    	select.setParameter("ID", new Integer(7));
    	root = select.executeQuery();
    	customers = root.getList("CUSTOMER");
    	assertEquals(1, customers.size());
    	customer = (DataObject)customers.get(0);
    	assertEquals("Louise7", customer.get("LASTNAME"));    	
    	
    	//update
    	Command update = das.getCommand("updateCustomer");
    	update.setParameter("LASTNAME", "NoLouise");
    	update.setParameter("ID", new Integer(6));
    	update.execute();
    	select.setParameter("ID", new Integer(6));
    	root = select.executeQuery();
    	customers = root.getList("CUSTOMER");
    	assertEquals(1, customers.size());
    	customer = (DataObject)customers.get(0);
    	assertEquals("NoLouise", customer.get("LASTNAME"));
    	
    	//update - no param in config
    	Command updateNoParam = das.getCommand("updateCustomerNoParam");
    	updateNoParam.setParameter("LASTNAME", "YesLouise");
    	updateNoParam.setParameter("ID", new Integer(6));
    	updateNoParam.execute();
    	select.setParameter("ID", new Integer(6));
    	root = select.executeQuery();
    	customers = root.getList("CUSTOMER");
    	assertEquals(1, customers.size());
    	customer = (DataObject)customers.get(0);
    	assertEquals("YesLouise", customer.get("LASTNAME"));
    	
    	//delete
    	Command delete = das.getCommand("deleteCustomer");
    	delete.setParameter("ID", new Integer(6));
    	delete.execute();
    	select.setParameter("ID", new Integer(6));
    	root = select.executeQuery();
    	customers = root.getList("CUSTOMER");
    	assertEquals(0, customers.size());

    	//delete - cfg command with no param
    	Command deleteNoParam = das.getCommand("deleteCustomerNoParam");
    	deleteNoParam.setParameter("ID", new Integer(7));
    	deleteNoParam.execute();
    	select.setParameter("ID", new Integer(7));
    	root = select.executeQuery();
    	customers = root.getList("CUSTOMER");
    	assertEquals(0, customers.size());    	
    }
    
    /**
     * Test passing <Parameter>List to Command through program
     * @throws Exception
     */    
    public void testProgrammaticCommandNamedParameters() throws Exception {
    	ConfigHelper helper = new ConfigHelper();    	
	    //Command insertAdhoc = das.createCommand("insert into CUSTOMER values (?, ?, ?)");
    	org.apache.tuscany.das.rdb.config.Command insertAdhoc = ConfigFactory.INSTANCE.createCommand();
    	insertAdhoc.setKind("insert");
    	insertAdhoc.setSQL("insert into CUSTOMER values (?, ?, ?)");
    	insertAdhoc.setName("insertCustomer");
	    Parameter parameter1 = ConfigFactory.INSTANCE.createParameter();
		parameter1.setName("ID");
		Parameter parameter2 = ConfigFactory.INSTANCE.createParameter();
		parameter2.setName("LASTNAME");
		Parameter parameter3 = ConfigFactory.INSTANCE.createParameter();
		parameter3.setName("ADDRESS");
		insertAdhoc.getParameter().add(parameter1);
		insertAdhoc.getParameter().add(parameter2);
		insertAdhoc.getParameter().add(parameter3);
		Config cfg = helper.getConfig();
		cfg.getCommand().add(insertAdhoc);
		DAS das = DAS.FACTORY.createDAS(cfg, getConnection());
		
		//setup is over, now set values in params and execute
		Command insert = das.getCommand("insertCustomer");
		insert.setParameter("ID", new Integer(6));
		insert.setParameter("LASTNAME", "Anthony");
		insert.setParameter("ADDRESS", "IND");
		insert.execute();
		
		Command select = das.createCommand("select * from CUSTOMER where ID = ?");
    	select.setParameter("ID", new Integer(6));
    	DataObject root = select.executeQuery();
    	List customers = root.getList("CUSTOMER");
    	assertEquals(1, customers.size());
    	DataObject customer = (DataObject)customers.get(0);
    	assertEquals("Anthony", customer.get("LASTNAME"));
    }
    
    /**
     * Test set/getParameter(name) on Command
     * @throws Exception
     */    
    public void testNamedParameters() throws Exception {
	    DAS das = DAS.FACTORY.createDAS(getConnection());
	    Command insertAdhoc = das.createCommand("insert into CUSTOMER values (?, ?, ?)");
	    insertAdhoc.setParameter("ID", new Integer(6));
	    insertAdhoc.setParameter("LASTNAME", "MyLastName");
	    insertAdhoc.setParameter("ADDRESS", "MyLastAddress");
	    assertEquals("MyLastAddress", insertAdhoc.getParameter("ADDRESS"));
	    insertAdhoc.execute();
	    
	    Command select = das.createCommand("select * from CUSTOMER where ID = ?");
    	select.setParameter("ID", new Integer(6));
    	DataObject root = select.executeQuery();
    	List customers = root.getList("CUSTOMER");
    	assertEquals(1, customers.size());
    	DataObject customer = (DataObject)customers.get(0);
    	assertEquals("MyLastName", customer.get("LASTNAME"));	    
    	
    }    
}