001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.test.formatter;
018:
019: import java.math.BigDecimal;
020: import java.math.BigInteger;
021: import java.sql.Time;
022: import java.sql.Timestamp;
023: import java.text.SimpleDateFormat;
024: import java.util.Date;
025: import java.util.Locale;
026:
027: import org.compass.core.CompassSession;
028: import org.compass.core.CompassTransaction;
029: import org.compass.core.Resource;
030: import org.compass.core.config.CompassEnvironment;
031: import org.compass.core.config.CompassSettings;
032: import org.compass.core.converter.basic.DateConverter;
033: import org.compass.core.test.AbstractTestCase;
034:
035: /**
036: * @author kimchy
037: */
038: public class FormatterTests extends AbstractTestCase {
039:
040: protected String[] getMappings() {
041: return new String[] { "formatter/formatter.cpm.xml" };
042: }
043:
044: protected void addSettings(CompassSettings settings) {
045: Locale.setDefault(Locale.ENGLISH);
046: settings
047: .setGroupSettings(
048: CompassEnvironment.Converter.PREFIX,
049: "myint",
050: new String[] {
051: CompassEnvironment.Converter.TYPE,
052: CompassEnvironment.Converter.Format.FORMAT },
053: new String[] {
054: CompassEnvironment.Converter.DefaultTypes.Simple.INTEGER,
055: "0,000" });
056: settings
057: .setGroupSettings(
058: CompassEnvironment.Converter.PREFIX,
059: "myshort",
060: new String[] {
061: CompassEnvironment.Converter.TYPE,
062: CompassEnvironment.Converter.Format.FORMAT },
063: new String[] {
064: CompassEnvironment.Converter.DefaultTypes.Simple.SHORT,
065: "0,000" });
066: settings
067: .setGroupSettings(
068: CompassEnvironment.Converter.PREFIX,
069: "mylong",
070: new String[] {
071: CompassEnvironment.Converter.TYPE,
072: CompassEnvironment.Converter.Format.FORMAT },
073: new String[] {
074: CompassEnvironment.Converter.DefaultTypes.Simple.LONG,
075: "0,000" });
076: settings
077: .setGroupSettings(
078: CompassEnvironment.Converter.PREFIX,
079: "mybigdecimal",
080: new String[] {
081: CompassEnvironment.Converter.TYPE,
082: CompassEnvironment.Converter.Format.FORMAT },
083: new String[] {
084: CompassEnvironment.Converter.DefaultTypes.Simple.BIGDECIMAL,
085: "0,000.00" });
086: settings
087: .setGroupSettings(
088: CompassEnvironment.Converter.PREFIX,
089: "mybiginteger",
090: new String[] {
091: CompassEnvironment.Converter.TYPE,
092: CompassEnvironment.Converter.Format.FORMAT },
093: new String[] {
094: CompassEnvironment.Converter.DefaultTypes.Simple.BIGINTEGER,
095: "0,000" });
096: settings
097: .setGroupSettings(
098: CompassEnvironment.Converter.PREFIX,
099: "myfloat",
100: new String[] {
101: CompassEnvironment.Converter.TYPE,
102: CompassEnvironment.Converter.Format.FORMAT },
103: new String[] {
104: CompassEnvironment.Converter.DefaultTypes.Simple.FLOAT,
105: "0,000.00" });
106: settings
107: .setGroupSettings(
108: CompassEnvironment.Converter.PREFIX,
109: "mydouble",
110: new String[] {
111: CompassEnvironment.Converter.TYPE,
112: CompassEnvironment.Converter.Format.FORMAT },
113: new String[] {
114: CompassEnvironment.Converter.DefaultTypes.Simple.DOUBLE,
115: "0,000.00" });
116: settings
117: .setGroupSettings(
118: CompassEnvironment.Converter.PREFIX,
119: "mydate",
120: new String[] {
121: CompassEnvironment.Converter.TYPE,
122: CompassEnvironment.Converter.Format.FORMAT },
123: new String[] {
124: CompassEnvironment.Converter.DefaultTypes.Simple.DATE,
125: "yyyy-MM-dd-HH" });
126: }
127:
128: public void testNumberFormatUsingLookupConverters() {
129: CompassSession session = openSession();
130: CompassTransaction tr = session.beginTransaction();
131:
132: Long id = new Long(1);
133: A a = new A();
134: a.setId(id);
135: a.setIntVal(12);
136: a.setShortVal((short) 12);
137: a.setLongVal(12);
138: a.setDoubleVal(12.56789);
139: a.setFloatVal(12.56789f);
140: Date date = new Date();
141: a.setDateVal(date);
142: a.setBigIntegerVal(new BigInteger("12"));
143: BigDecimal bigDecimal = new BigDecimal(12.56789d);
144: a.setBigDecimalVal(bigDecimal);
145:
146: session.save("a", a);
147:
148: Resource r = session.loadResource("a", id);
149: assertEquals("12", r.getValue("intSimple"));
150: assertEquals("0,012", r.getValue("intFormatted"));
151: assertEquals("12", r.getValue("shortSimple"));
152: assertEquals("0,012", r.getValue("shortFormatted"));
153: assertEquals("12", r.getValue("longSimple"));
154: assertEquals("0,012", r.getValue("longFormatted"));
155: assertEquals("12", r.getValue("bigIntegerSimple"));
156: assertEquals("0,012", r.getValue("bigIntegerFormatted"));
157: assertEquals("12.56789", r.getValue("doubleSimple"));
158: assertEquals("0,012.57", r.getValue("doubleFormatted"));
159: assertEquals("12.56789", r.getValue("floatSimple"));
160: assertEquals("0,012.57", r.getValue("floatFormatted"));
161: assertEquals(bigDecimal.toString(), r
162: .getValue("bigDecimalSimple"));
163: assertEquals("0,012.57", r.getValue("bigDecimalFormatted"));
164: SimpleDateFormat sdf = new SimpleDateFormat(
165: DateConverter.DEFAULT_DATE_FORMAT);
166: assertEquals(sdf.format(date), r.getValue("dateSimple"));
167: sdf = new SimpleDateFormat("yyyy-MM-dd-HH");
168: assertEquals(sdf.format(date), r.getValue("dateFormatted"));
169:
170: tr.commit();
171: }
172:
173: public void testIntFormattedAsUnmarshallValue() throws Exception {
174: CompassSession session = openSession();
175: CompassTransaction tr = session.beginTransaction();
176:
177: Long id = new Long(1);
178: A a = new A();
179: a.setId(id);
180: a.setIntVal(12);
181: a.setShortVal((short) 12);
182: a.setLongVal(12);
183: a.setDoubleVal(12.56789);
184: a.setFloatVal(12.56789f);
185: Date date = new Date();
186: a.setDateVal(date);
187: a.setTimeVal(new Time(date.getTime()));
188: a.setTimestampVal(new Timestamp(date.getTime()));
189: a.setBigIntegerVal(new BigInteger("12"));
190: BigDecimal bigDecimal = new BigDecimal(12.56789d);
191: a.setBigDecimalVal(bigDecimal);
192:
193: session.save("a1", a);
194:
195: a = (A) session.load("a1", id);
196: assertEquals(12, a.getIntVal());
197: assertEquals(12, a.getShortVal());
198: assertEquals(12, a.getLongVal());
199: assertEquals(12.57, a.getDoubleVal(), 0.0000001);
200: assertEquals(12.57f, a.getFloatVal(), 0.0000001);
201:
202: tr.commit();
203: }
204:
205: public void testNumberFormatUsingFormatAttribute() {
206: CompassSession session = openSession();
207: CompassTransaction tr = session.beginTransaction();
208:
209: Long id = new Long(1);
210: A a = new A();
211: a.setId(id);
212: a.setIntVal(12);
213: a.setShortVal((short) 12);
214: a.setLongVal(12);
215: a.setDoubleVal(12.56789);
216: a.setFloatVal(12.56789f);
217: Date date = new Date();
218: a.setDateVal(date);
219: a.setTimeVal(new Time(date.getTime()));
220: a.setTimestampVal(new Timestamp(date.getTime()));
221: a.setBigIntegerVal(new BigInteger("12"));
222: BigDecimal bigDecimal = new BigDecimal(12.56789d);
223: a.setBigDecimalVal(bigDecimal);
224:
225: session.save("a2", a);
226:
227: Resource r = session.loadResource("a2", id);
228: assertEquals("12", r.getValue("intSimple"));
229: assertEquals("0,012", r.getValue("intFormatted"));
230: assertEquals("12", r.getValue("shortSimple"));
231: assertEquals("0,012", r.getValue("shortFormatted"));
232: assertEquals("12", r.getValue("longSimple"));
233: assertEquals("0,012", r.getValue("longFormatted"));
234: assertEquals("12", r.getValue("bigIntegerSimple"));
235: assertEquals("0,012", r.getValue("bigIntegerFormatted"));
236: assertEquals("12.56789", r.getValue("doubleSimple"));
237: assertEquals("0,012.57", r.getValue("doubleFormatted"));
238: assertEquals("12.56789", r.getValue("floatSimple"));
239: assertEquals("0,012.57", r.getValue("floatFormatted"));
240: assertEquals(bigDecimal.toString(), r
241: .getValue("bigDecimalSimple"));
242: assertEquals("0,012.57", r.getValue("bigDecimalFormatted"));
243: SimpleDateFormat sdf = new SimpleDateFormat(
244: DateConverter.DEFAULT_DATE_FORMAT);
245: assertEquals(sdf.format(date), r.getValue("dateSimple"));
246: sdf = new SimpleDateFormat("yyyy-MM-dd-HH");
247: assertEquals(sdf.format(date), r.getValue("dateFormatted"));
248: assertEquals(sdf.format(date), r.getValue("timeFormatted"));
249: assertEquals(sdf.format(date), r.getValue("timestampFormatted"));
250:
251: tr.commit();
252: }
253: }
|