KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > test > DirectSQLQueryTest


1 /*
2  * Copyright 2003 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: DirectSQLQueryTest.java,v 1.3 2003/03/17 07:02:52 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.test;
12
13 import com.triactive.jdo.store.NoExtentException;
14 import java.util.Collection JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import javax.jdo.Extent;
17 import javax.jdo.PersistenceManager;
18 import javax.jdo.Query;
19 import javax.jdo.Transaction;
20 import org.apache.log4j.Category;
21
22
23 /**
24  * Tests the functionality of TJDOSQL queries.
25  *
26  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
27  * @version $Revision: 1.3 $
28  */

29
30 public class DirectSQLQueryTest extends StorageTestCase
31 {
32     private static final Category LOG = Category.getInstance(DirectSQLQueryTest.class);
33
34     private boolean schemaInitialized = false;
35
36
37     /**
38      * Used by the JUnit framework to construct tests. Normally, programmers
39      * would never explicitly use this constructor.
40      *
41      * @param name Name of the <tt>TestCase</tt>.
42      */

43
44     public DirectSQLQueryTest(String JavaDoc name)
45     {
46         super(name);
47     }
48
49
50     protected void setUp() throws Exception JavaDoc
51     {
52         super.setUp();
53
54         if (!schemaInitialized)
55         {
56             addClassesToSchema(new Class JavaDoc[]
57                 {
58                     Widget.class,
59                 }
60             );
61
62             schemaInitialized = true;
63         }
64     }
65
66
67     public void testTJDOSQLQueryOfWidgets() throws Exception JavaDoc
68     {
69         /*
70          * Can't run this test on SQL Server because it doesn't allow you to
71          * GROUP BY a bit column.
72          */

73         if ("sqlserver".equals(vendorID))
74             return;
75
76         LOG.info("Testing TJDOSQL query against " + TEST_OBJECT_COUNT + " " + Widget.class.getName() + " objects");
77         insertObjects(Widget.class);
78
79         PersistenceManager pm = pmf.getPersistenceManager();
80         Transaction tx = pm.currentTransaction();
81
82         try
83         {
84             tx.begin();
85
86             String JavaDoc sqlText;
87
88             if ("db2".equals(vendorID))
89             {
90                 /*
91                  * In DB2 the AVG() function has to work on FLOAT values to
92                  * avoid an arithmetic overflow.
93                  */

94                 sqlText = "SELECT W.{Widget.booleanField} AS {this.booleanValue},"
95                         + " INTEGER(AVG(FLOAT(W.{Widget.intField}))) AS {this.avgIntValue},"
96                         + " SMALLINT(AVG(FLOAT(W.{Widget.shortField}))) AS {this.avgShortValue}"
97                         + " FROM {Widget} W"
98                         + " WHERE W.{Widget.shortField} BETWEEN ?loVal? AND ?hiVal?"
99                         + " GROUP BY W.{Widget.booleanField}";
100             }
101             else if ("mysql".equals(vendorID))
102             {
103                 /*
104                  * In MySQL the GROUP BY clause has to reference columns by
105                  * their alias.
106                  */

107                 sqlText = "SELECT W.{Widget.booleanField} AS {this.booleanValue},"
108                         + " AVG(W.{Widget.intField}) AS {this.avgIntValue},"
109                         + " AVG(W.{Widget.shortField}) AS {this.avgShortValue}"
110                         + " FROM {Widget} W"
111                         + " WHERE W.{Widget.shortField} BETWEEN ?loVal? AND ?hiVal?"
112                         + " GROUP BY {this.booleanValue}";
113             }
114             else if ("postgresql".equals(vendorID))
115             {
116                 /* This is to get around a broken JDBC driver. */
117                 sqlText = "SELECT W.{Widget.booleanField} AS {this.booleanValue},"
118                         + " int8(AVG(W.{Widget.intField})) AS {this.avgIntValue},"
119                         + " int4(AVG(W.{Widget.shortField})) AS {this.avgShortValue}"
120                         + " FROM {Widget} W"
121                         + " WHERE W.{Widget.shortField} BETWEEN ?loVal? AND ?hiVal?"
122                         + " GROUP BY W.{Widget.booleanField}";
123             }
124             else
125             {
126                 sqlText = "SELECT W.{Widget.booleanField} AS {this.booleanValue},"
127                         + " AVG(W.{Widget.intField}) AS {this.avgIntValue},"
128                         + " AVG(W.{Widget.shortField}) AS {this.avgShortValue}"
129                         + " FROM {Widget} W"
130                         + " WHERE W.{Widget.shortField} BETWEEN ?loVal? AND ?hiVal?"
131                         + " GROUP BY W.{Widget.booleanField}";
132             }
133
134             short loVal = 0;
135             short hiVal = Short.MAX_VALUE / 2;
136
137             Query query = pm.newQuery("javax.jdo.query.TJDOSQL", sqlText);
138             query.setClass(WidgetAverages.class);
139             query.declareImports("import com.triactive.jdo.test.Widget");
140             query.declareParameters("short loVal, short hiVal");
141             Collection JavaDoc results = (Collection JavaDoc)query.execute(new Short JavaDoc(loVal), new Short JavaDoc(hiVal));
142
143             Iterator JavaDoc i = results.iterator();
144             int count = 0;
145
146             while (i.hasNext())
147             {
148                 WidgetAverages wa = (WidgetAverages)i.next();
149
150                 assertTrue("", wa.getAvgShortValue() >= loVal && wa.getAvgShortValue() <= hiVal);
151                 ++count;
152             }
153
154             assertEquals("TJDOSQL query returned wrong number of rows", 2, count);
155
156             tx.commit();
157
158             /*
159              * Negative test #1. Ensure that an attempt to write a field
160              * throws the proper exception.
161              */

162
163             try
164             {
165                 tx.begin();
166
167                 WidgetAverages wa = (WidgetAverages)results.iterator().next();
168                 wa.fillRandom();
169
170                 tx.commit();
171
172                 fail("Writing to a TJDOSQL query result object succeeded");
173             }
174             catch (NoExtentException e)
175             {
176                 if (tx.isActive())
177                     tx.rollback();
178             }
179
180             /*
181              * Negative test #2. Ensure that an attempt to make a query result
182              * object persistent throws the proper exception.
183              */

184
185             try
186             {
187                 tx.begin();
188
189                 WidgetAverages wa = new WidgetAverages();
190                 pm.makePersistent(wa);
191
192                 tx.commit();
193
194                 fail("Making a query result object persistent succeeded");
195             }
196             catch (NoExtentException e)
197             {
198                 if (tx.isActive())
199                     tx.rollback();
200             }
201
202             /*
203              * Negative test #3. Ensure that an attempt to delete a view object
204              * throws the proper exception.
205              */

206
207             try
208             {
209                 tx.begin();
210
211                 WidgetAverages wa = (WidgetAverages)results.iterator().next();
212                 pm.deletePersistent(wa);
213
214                 tx.commit();
215
216                 fail("Deleting a TJDOSQL query result object succeeded");
217             }
218             catch (NoExtentException e)
219             {
220                 if (tx.isActive())
221                     tx.rollback();
222             }
223         }
224         finally
225         {
226             if (tx.isActive())
227                 tx.rollback();
228
229             pm.close();
230         }
231
232         removeObjects();
233     }
234 }
235
Popular Tags