2 Copyright 2010 Sun Microsystems, Inc.
3 All rights reserved. Use is subject to license terms.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 package testsuite.clusterj;
21 import java.math.BigDecimal;
22 import testsuite.clusterj.model.DecimalTypes;
23 import testsuite.clusterj.model.IdBase;
25 public class QueryDecimalTypesTest extends AbstractQueryTest {
28 public Class getInstanceType() {
29 return DecimalTypes.class;
33 void createInstances(int number) {
34 createAllDecimalTypesInstances(number);
37 /** Test all single- and double-predicate queries using DecimalTypes.
38 drop table if exists decimaltypes;
39 create table decimaltypes (
40 id int not null primary key,
42 decimal_null_hash decimal(10,5),
43 decimal_null_btree decimal(10,5),
44 decimal_null_both decimal(10,5),
45 decimal_null_none decimal(10,5)
47 ) ENGINE=ndbcluster DEFAULT CHARSET=latin1;
49 create unique index idx_decimal_null_hash using hash on decimaltypes(decimal_null_hash);
50 create index idx_decimal_null_btree on decimaltypes(decimal_null_btree);
51 create unique index idx_decimal_null_both on decimaltypes(decimal_null_both);
55 /** These tests require implementation of
56 * setBoundDecimal for btree index scans, and cmpDecimal for non-index
60 btreeIndexScanDecimal();
61 hashIndexScanDecimal();
62 bothIndexScanDecimal();
63 noneIndexScanDecimal();
67 public void btreeIndexScanDecimal() {
68 equalQuery("decimal_null_btree", "idx_decimal_null_btree", getDecimal(8), 8);
69 greaterEqualQuery("decimal_null_btree", "idx_decimal_null_btree", getDecimal(7), 7, 8, 9);
70 greaterThanQuery("decimal_null_btree", "idx_decimal_null_btree", getDecimal(6), 7, 8, 9);
71 lessEqualQuery("decimal_null_btree", "idx_decimal_null_btree", getDecimal(4), 4, 3, 2, 1, 0);
72 lessThanQuery("decimal_null_btree", "idx_decimal_null_btree", getDecimal(4), 3, 2, 1, 0);
73 betweenQuery("decimal_null_btree", "idx_decimal_null_btree", getDecimal(4), getDecimal(6), 4, 5, 6);
74 greaterEqualAndLessEqualQuery("decimal_null_btree", "idx_decimal_null_btree", getDecimal(4), getDecimal(6), 4, 5, 6);
75 greaterThanAndLessEqualQuery("decimal_null_btree", "idx_decimal_null_btree", getDecimal(4), getDecimal(6), 5, 6);
76 greaterEqualAndLessThanQuery("decimal_null_btree", "idx_decimal_null_btree", getDecimal(4), getDecimal(6), 4, 5);
77 greaterThanAndLessThanQuery("decimal_null_btree", "idx_decimal_null_btree", getDecimal(4), getDecimal(6), 5);
80 public void hashIndexScanDecimal() {
81 equalQuery("decimal_null_hash", "idx_decimal_null_hash", getDecimal(8), 8);
82 greaterEqualQuery("decimal_null_hash", "none", getDecimal(7), 7, 8, 9);
83 greaterThanQuery("decimal_null_hash", "none", getDecimal(6), 7, 8, 9);
84 lessEqualQuery("decimal_null_hash", "none", getDecimal(4), 4, 3, 2, 1, 0);
85 lessThanQuery("decimal_null_hash", "none", getDecimal(4), 3, 2, 1, 0);
86 betweenQuery("decimal_null_hash", "none", getDecimal(4), getDecimal(6), 4, 5, 6);
87 greaterEqualAndLessEqualQuery("decimal_null_hash", "none", getDecimal(4), getDecimal(6), 4, 5, 6);
88 greaterThanAndLessEqualQuery("decimal_null_hash", "none", getDecimal(4), getDecimal(6), 5, 6);
89 greaterEqualAndLessThanQuery("decimal_null_hash", "none", getDecimal(4), getDecimal(6), 4, 5);
90 greaterThanAndLessThanQuery("decimal_null_hash", "none", getDecimal(4), getDecimal(6), 5);
93 public void bothIndexScanDecimal() {
94 equalQuery("decimal_null_both", "idx_decimal_null_both", getDecimal(8), 8);
95 greaterEqualQuery("decimal_null_both", "idx_decimal_null_both", getDecimal(7), 7, 8, 9);
96 greaterThanQuery("decimal_null_both", "idx_decimal_null_both", getDecimal(6), 7, 8, 9);
97 lessEqualQuery("decimal_null_both", "idx_decimal_null_both", getDecimal(4), 4, 3, 2, 1, 0);
98 lessThanQuery("decimal_null_both", "idx_decimal_null_both", getDecimal(4), 3, 2, 1, 0);
99 betweenQuery("decimal_null_both", "idx_decimal_null_both", getDecimal(4), getDecimal(6), 4, 5, 6);
100 greaterEqualAndLessEqualQuery("decimal_null_both", "idx_decimal_null_both", getDecimal(4), getDecimal(6), 4, 5, 6);
101 greaterThanAndLessEqualQuery("decimal_null_both", "idx_decimal_null_both", getDecimal(4), getDecimal(6), 5, 6);
102 greaterEqualAndLessThanQuery("decimal_null_both", "idx_decimal_null_both", getDecimal(4), getDecimal(6), 4, 5);
103 greaterThanAndLessThanQuery("decimal_null_both", "idx_decimal_null_both", getDecimal(4), getDecimal(6), 5);
106 public void noneIndexScanDecimal() {
107 equalQuery("decimal_null_none", "none", getDecimal(8), 8);
108 greaterEqualQuery("decimal_null_none", "none", getDecimal(7), 7, 8, 9);
109 greaterThanQuery("decimal_null_none", "none", getDecimal(6), 7, 8, 9);
110 lessEqualQuery("decimal_null_none", "none", getDecimal(4), 4, 3, 2, 1, 0);
111 lessThanQuery("decimal_null_none", "none", getDecimal(4), 3, 2, 1, 0);
112 betweenQuery("decimal_null_none", "none", getDecimal(4), getDecimal(6), 4, 5, 6);
113 greaterEqualAndLessEqualQuery("decimal_null_none", "none", getDecimal(4), getDecimal(6), 4, 5, 6);
114 greaterThanAndLessEqualQuery("decimal_null_none", "none", getDecimal(4), getDecimal(6), 5, 6);
115 greaterEqualAndLessThanQuery("decimal_null_none", "none", getDecimal(4), getDecimal(6), 4, 5);
116 greaterThanAndLessThanQuery("decimal_null_none", "none", getDecimal(4), getDecimal(6), 5);
120 private void createAllDecimalTypesInstances(int number) {
121 for (int i = 0; i < number; ++i) {
122 DecimalTypes instance = session.newInstance(DecimalTypes.class);
124 instance.setDecimal_null_hash(getDecimal(i));
125 instance.setDecimal_null_btree(getDecimal(i));
126 instance.setDecimal_null_both(getDecimal(i));
127 instance.setDecimal_null_none(getDecimal(i));
128 instances.add(instance);
132 /** Create a BigDecimal value from an int value by multiplying by 0.00001.
134 * @param number the sequence number
135 * @return the value corresponding to the number
137 protected BigDecimal getDecimal(int number) {
138 return BigDecimal.valueOf(number, 5);
141 /** Print the results of a query for debugging.
143 * @param instance the instance to print
146 protected void printResultInstance(IdBase instance) {
147 if (instance instanceof DecimalTypes) {
148 DecimalTypes decimalType = (DecimalTypes)instance;
149 // System.out.println(toString(decimalType));
153 public static String toString(IdBase idBase) {
154 DecimalTypes instance = (DecimalTypes)idBase;
155 StringBuffer buffer = new StringBuffer("DecimalTypes id: ");
156 buffer.append(instance.getId());
157 buffer.append("; decimal_null_both: ");
158 buffer.append(instance.getDecimal_null_both().toString());
159 buffer.append("; decimal_null_btree: ");
160 buffer.append(instance.getDecimal_null_btree().toString());
161 buffer.append("; decimal_null_hash: ");
162 buffer.append(instance.getDecimal_null_hash().toString());
163 buffer.append("; decimal_null_none: ");
164 buffer.append(instance.getDecimal_null_none().toString());
165 return buffer.toString();