001: package org.dbbrowser.ui;
002:
003: import java.text.Format;
004: import java.text.MessageFormat;
005: import java.text.NumberFormat;
006: import java.text.ParseException;
007: import java.util.Date;
008: import java.util.HashMap;
009: import java.util.Map;
010: import org.dbbrowser.db.engine.model.ColumnInfo;
011: import org.dbbrowser.db.engine.model.DBTableCell;
012: import junit.framework.TestCase;
013:
014: /**
015: * Tests message formatter. The formatter is used to parse user input and creates a java object from the parsed string
016: * @author amangat
017: *
018: */
019: public class MessageFormatterTest extends TestCase {
020: private Map mapOfDataTypeNameToFormatter = new HashMap();
021:
022: public MessageFormatterTest(String name) {
023: super (name);
024: mapOfDataTypeNameToFormatter.put(
025: ColumnInfo.COLUMN_TYPE_VARCHAR,
026: new MessageFormat("{0}")); //Format varchar datatype using the message formatter
027: mapOfDataTypeNameToFormatter.put(ColumnInfo.COLUMN_TYPE_CHAR,
028: new MessageFormat("{0}")); //Format char datatype using the message formatter
029: mapOfDataTypeNameToFormatter.put(ColumnInfo.COLUMN_TYPE_DATE,
030: DBTableCell.getDateFormat()); //Format date datatype using the message formatter
031: mapOfDataTypeNameToFormatter.put(ColumnInfo.COLUMN_TYPE_NUMBER,
032: DBTableCell.getDecimalFormat()); //Format date datatype using the message formatter
033: }
034:
035: public void testVarChar() {
036: String stringToParse = "a string";
037: Format formatter = (Format) mapOfDataTypeNameToFormatter
038: .get(ColumnInfo.COLUMN_TYPE_VARCHAR);
039:
040: try {
041: Object o = formatter.parseObject(stringToParse);
042:
043: Object stringObject = (((Object[]) o)[0]).toString();
044:
045: //Test
046: assertTrue("Object should be of type String", stringObject
047: .getClass().getName().equals("java.lang.String"));
048: assertTrue("Formatting of string failed",
049: ((String) stringObject).equals(stringToParse));
050: } catch (ParseException exc) {
051: fail(exc.getMessage());
052: }
053: }
054:
055: public void testChar() {
056: String charToParse = "a";
057: Format formatter = (Format) mapOfDataTypeNameToFormatter
058: .get(ColumnInfo.COLUMN_TYPE_CHAR);
059:
060: try {
061: Object o = formatter.parseObject(charToParse);
062:
063: Object stringObject = (((Object[]) o)[0]).toString();
064:
065: //Test
066: assertTrue("Object should be of type String", stringObject
067: .getClass().getName().equals("java.lang.String"));
068: assertTrue("Formatting of char failed",
069: ((String) stringObject).equals(charToParse));
070: } catch (ParseException exc) {
071: fail(exc.getMessage());
072: }
073: }
074:
075: public void testDate() {
076: String dateToParse = "26/12/2005";
077: Format formatter = (Format) mapOfDataTypeNameToFormatter
078: .get(ColumnInfo.COLUMN_TYPE_DATE);
079:
080: try {
081: Object o = formatter.parseObject(dateToParse);
082:
083: //Test
084: assertTrue("Object should be of type Date", o.getClass()
085: .getName().equals("java.util.Date"));
086: String d = DBTableCell.getDateFormat().format((Date) o);
087: assertTrue("Formatting of Date failed", d
088: .equals(dateToParse));
089: } catch (ParseException exc) {
090: fail(exc.getMessage());
091: }
092: }
093:
094: public void testNumber() {
095: String numberToParse = "100";
096: Format formatter = (Format) mapOfDataTypeNameToFormatter
097: .get(ColumnInfo.COLUMN_TYPE_NUMBER);
098:
099: try {
100: Object o = formatter.parseObject(numberToParse);
101:
102: //Test
103: assertTrue("Object should be of type Number",
104: o instanceof Number);
105: } catch (ParseException exc) {
106: fail(exc.getMessage());
107: }
108: }
109: }
|