summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-final/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/TypeRoundTripTest.java
blob: b14ae165dc30993e8df831e0516a2227d1131db4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tuscany.sdo.test;

import junit.framework.TestCase;

import org.apache.tuscany.sdo.util.SDOUtil;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import commonj.sdo.Type;
import commonj.sdo.helper.TypeHelper;

public class TypeRoundTripTest extends TestCase {

  private static class Test {
    Type type;
    String value;
    Object obj;
    Comparator comp;
    
    Test(Type type, String value, Object obj) {
      this.type = type;
      this.value = value;
      this.obj = obj;
    }
    
    Test(Type type, String value, Object obj, Comparator comp) {
      this.type = type;
      this.value = value;
      this.obj = obj;
      this.comp = comp;
    }    
  }
  
  private static class ListComparator implements Comparator {
    public int compare(Object list1, Object list2) {
      int answer = 1;
      if (list1 instanceof List && list2 instanceof List) {
        List l1 = (List) list1;
        List l2 = (List) list2;
        if (l1.size() == l2.size()) {
          for (int n = 0; n < l1.size(); n++) {
            if (!l1.get(n).equals(l2.get(n))) {
              answer = 0;
              break;
            }
          }
        } else {
          answer = 0;
        }
      } else {
        answer = 0;
      }
      return answer;
    }    
  }
  
  private static class BytesComparator implements Comparator {
    public int compare(Object o1, Object o2) {
      o2 = new String((byte[])o2);
      if (o1.equals(o2)) return 1;
      else return 0;
    }
  }
  
  private static class DateComparator implements Comparator {
    public int compare(Object o1, Object o2) {
      int answer = 1;
      try {
        if (o1 instanceof String && o2 instanceof Date) {
          SimpleDateFormat sdf = 
            new SimpleDateFormat("yyyy-MM-dd'Z'");
          o1 = sdf.parse((String)o1);
          if (!o1.equals(o2)) answer = 0; 
        } else {
          answer = 0;
        }
      } catch (Exception e) {}
      return answer;
    }
  }
  
  public void testTypeRoundTrips() throws Exception {
    String URI = "commonj.sdo";
    TypeHelper types = TypeHelper.INSTANCE;
    List list = new ArrayList();
    list.add("foo");
    list.add("bar");
    list.add("test");
    Test[] tests = {
      new Test(types.getType(URI, "Boolean"),      "true", new Boolean(true)),
      new Test(types.getType(URI, "Byte"),         "49", new Byte((byte)49)),
      new Test(types.getType(URI, "Bytes"),        "Zm9v", "foo", new BytesComparator()),
      new Test(types.getType(URI, "Character"),    "a", new Character('a')),
      new Test(types.getType(URI, "Date"),         "2005-12-12Z", "2005-12-12Z", new DateComparator()),
      new Test(types.getType(URI, "DateTime"),     "2005-12-12T12:12:12Z", "2005-12-12T12:12:12Z"),
      new Test(types.getType(URI, "Day"),          "---12", "---12"),
      new Test(types.getType(URI, "Decimal"),      "12.12", new BigDecimal("12.12")),
      new Test(types.getType(URI, "Double"),       "12.12", new Double(12.12)),
      new Test(types.getType(URI, "Duration"),     "P5Y2M10D", "P5Y2M10D"),
      new Test(types.getType(URI, "Float"),        "12.12", new Float(12.12f)),
      new Test(types.getType(URI, "Int"),          "12", new Integer(12)),
      new Test(types.getType(URI, "Integer"),      "12", new BigInteger("12")),
      new Test(types.getType(URI, "Long"),         "12", new Long(12l)),
      new Test(types.getType(URI, "Month"),        "--12", "--12"),
      new Test(types.getType(URI, "MonthDay"),     "--12-12", "--12-12"),
      new Test(types.getType(URI, "Object"),       "test", "test"),
      new Test(types.getType(URI, "Short"),        "12", new Short((short)12)),
      new Test(types.getType(URI, "String"),       "test", "test"),
      new Test(types.getType(URI, "Strings"),      "foo bar test", list, new ListComparator()),
      new Test(types.getType(URI, "Time"),         "12:12:12.12", "12:12:12.12"),
      new Test(types.getType(URI, "URI"),          "http://example.org", "http://example.org"),
      new Test(types.getType(URI, "Year"),         "2005", "2005"),
      new Test(types.getType(URI, "YearMonth"),    "2005-12", "2005-12"),
      new Test(types.getType(URI, "YearMonthDay"), "2005-12-12", "2005-12-12")
    };
    
    for (int n = 0; n < tests.length; n++) {
      assertEquals(
        SDOUtil.convertToString(tests[n].type, SDOUtil.createFromString(tests[n].type, tests[n].value)), 
        tests[n].value
      );
      
      //System.out.print(".");
      
      if (tests[n].comp == null) {
        assertEquals(
          SDOUtil.createFromString(tests[n].type, SDOUtil.convertToString(tests[n].type, tests[n].obj)), 
          tests[n].obj
        );
      } else {
        String o1 = SDOUtil.convertToString(tests[n].type, tests[n].obj);
        Object o2 = SDOUtil.createFromString(tests[n].type, o1);
        assertEquals(tests[n].comp.compare(tests[n].obj, o2), 1);
      }
    }
    
  }
  
}