KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > soda > SodaNumberCoercion


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.test.soda;
22
23 import com.db4o.query.*;
24 import com.db4o.test.*;
25
26 public class SodaNumberCoercion {
27     private static final String JavaDoc DOUBLEFIELD = "_doubleValue";
28     private static final String JavaDoc FLOATFIELD = "_floatValue";
29     private static final String JavaDoc LONGFIELD = "_longValue";
30     private static final String JavaDoc INTFIELD = "_intValue";
31     private static final String JavaDoc SHORTFIELD = "_shortValue";
32     private static final String JavaDoc BYTEFIELD = "_byteValue";
33     private static final Float JavaDoc ROUNDFLOATVALUE = new Float JavaDoc(100);
34     private static final Double JavaDoc ROUNDDOUBLEVALUE = new Double JavaDoc(100);
35     private static final Long JavaDoc LONGVALUE = new Long JavaDoc(100);
36     private static final Integer JavaDoc INTVALUE = new Integer JavaDoc(100);
37     private static final Short JavaDoc SHORTVALUE = new Short JavaDoc((short)100);
38     private static final Byte JavaDoc BYTEVALUE = new Byte JavaDoc((byte)100);
39
40     public static class Thing {
41         public byte _byteValue;
42         public short _shortValue;
43         public int _intValue;
44         public long _longValue;
45         public float _floatValue;
46         public double _doubleValue;
47
48         public Thing(byte byteValue,short shortValue,int intValue,long longValue,float floatValue,double doubleValue) {
49             _byteValue=byteValue;
50             _shortValue=shortValue;
51             _intValue=intValue;
52             _longValue=longValue;
53             _floatValue=floatValue;
54             _doubleValue=doubleValue;
55         }
56     }
57
58     public void store() {
59         Test.deleteAllInstances(Thing.class);
60         Test.store(new Thing((byte)10,(short)10,10,10L,10f,10d));
61         Test.store(new Thing((byte)100,(short)100,100,100L,100f,100d));
62         Test.store(new Thing((byte)42,(short)42,42,42L,42f,42d));
63         Test.store(new Thing((byte)111,(short)111,111,111L,111.11f,111.11d));
64     }
65
66     public void testIntegerTypes() {
67         assertSingleCoercionResult(BYTEFIELD,SHORTVALUE);
68         assertSingleCoercionResult(BYTEFIELD,INTVALUE);
69         assertSingleCoercionResult(BYTEFIELD,LONGVALUE);
70
71         assertSingleCoercionResult(SHORTFIELD,BYTEVALUE);
72         assertSingleCoercionResult(SHORTFIELD,INTVALUE);
73         assertSingleCoercionResult(SHORTFIELD,LONGVALUE);
74
75         assertSingleCoercionResult(INTFIELD,BYTEVALUE);
76         assertSingleCoercionResult(INTFIELD,SHORTVALUE);
77         assertSingleCoercionResult(INTFIELD,LONGVALUE);
78
79         assertSingleCoercionResult(LONGFIELD,BYTEVALUE);
80         assertSingleCoercionResult(LONGFIELD,SHORTVALUE);
81         assertSingleCoercionResult(LONGFIELD,INTVALUE);
82     }
83
84     public void testFloatingPointTypes() {
85         assertSingleCoercionResult(FLOATFIELD,ROUNDDOUBLEVALUE);
86         assertSingleCoercionResult(DOUBLEFIELD,ROUNDFLOATVALUE);
87     }
88
89     public void testMixed() {
90         assertSingleCoercionResult(BYTEFIELD, ROUNDFLOATVALUE);
91         assertSingleCoercionResult(BYTEFIELD, ROUNDDOUBLEVALUE);
92         assertSingleCoercionResult(SHORTFIELD, ROUNDFLOATVALUE);
93         assertSingleCoercionResult(SHORTFIELD, ROUNDDOUBLEVALUE);
94         assertSingleCoercionResult(INTFIELD, ROUNDFLOATVALUE);
95         assertSingleCoercionResult(INTFIELD, ROUNDDOUBLEVALUE);
96         assertSingleCoercionResult(LONGFIELD, ROUNDFLOATVALUE);
97         assertSingleCoercionResult(LONGFIELD, ROUNDDOUBLEVALUE);
98         assertSingleCoercionResult(FLOATFIELD, BYTEVALUE);
99         assertSingleCoercionResult(FLOATFIELD, SHORTVALUE);
100         assertSingleCoercionResult(FLOATFIELD, INTVALUE);
101         assertSingleCoercionResult(FLOATFIELD, LONGVALUE);
102         assertSingleCoercionResult(DOUBLEFIELD, BYTEVALUE);
103         assertSingleCoercionResult(DOUBLEFIELD, SHORTVALUE);
104         assertSingleCoercionResult(DOUBLEFIELD, INTVALUE);
105         assertSingleCoercionResult(DOUBLEFIELD, LONGVALUE);
106     }
107     
108     public void testRounding() {
109         assertCoercionResult(FLOATFIELD, new Double JavaDoc(111.11), 0); // 111.11f!=111.11d
110
assertCoercionResult(FLOATFIELD, new Integer JavaDoc(111), 0);
111         assertCoercionResult(INTFIELD, new Float JavaDoc(111.11), 0);
112     }
113     
114     public void testOverflow() {
115         long cmpval=((long)Integer.MAX_VALUE)-Integer.MIN_VALUE+1+INTVALUE.intValue();
116         assertCoercionResult(INTFIELD, new Long JavaDoc(cmpval), 0);
117     }
118     
119     private void assertSingleCoercionResult(String JavaDoc fieldName,Number JavaDoc value) {
120         assertCoercionResult(fieldName,value,1);
121     }
122     
123     private void assertCoercionResult(String JavaDoc fieldName,Number JavaDoc value,int expCount) {
124         Query q = Test.query();
125         q.constrain(Thing.class);
126         Constraint constr=q.descend(fieldName).constrain(value);
127         Test.ensure(constr!=null);
128         Test.ensureEquals(expCount, q.execute().size());
129     }
130 }
131
Popular Tags