DetailsPlace: Use a date-formatting API available in GWT client code.
[online-glom:gwt-glom.git] / src / test / java / org / glom / web / client / place / GwtTestDetailsPlace.java
1 package org.glom.web.client.place;
2
3 import static org.junit.Assert.*;
4
5 import java.util.Calendar;
6 import java.util.Date;
7 import java.util.GregorianCalendar;
8
9 import org.glom.web.server.Utils;
10 import org.glom.web.shared.TypedDataItem;
11 import org.glom.web.shared.libglom.Field.GlomFieldType;
12 import org.junit.Test;
13
14 import com.googlecode.gwt.test.GwtModule;
15 import com.googlecode.gwt.test.GwtTest;
16
17 @GwtModule("org.glom.web.OnlineGlom")
18 public class GwtTestDetailsPlace extends GwtTest {
19
20         public GwtTestDetailsPlace() {
21         }
22
23         @Test
24         public void testGetPlaceNoParameters() {
25                 checkTokenWithoutParameters("");
26                 checkTokenWithoutParameters("something");
27                 checkTokenWithoutParameters("list:a=1");
28                 checkTokenWithoutParameters("value1=123");
29         }
30
31         @Test
32         public void testGetPlaceParametersTextPrimaryKey() {
33                 // Create a DetailsPlace, testing getPlace():
34                 final String documentId = "somedocument";
35                 final String tableName = "sometable";
36                 final String primaryKeyValue = "123";
37                 DetailsPlace place = getDetailsPlaceFromToken("document=" + documentId + "&table=" + tableName + "&value="
38                                 + primaryKeyValue);
39                 checkParameters(place, documentId, tableName, primaryKeyValue);
40
41                 // Recreate it, testing getToken(),
42                 // checking that the same parameters are read back:
43                 final DetailsPlace.Tokenizer tokenizer = new DetailsPlace.Tokenizer();
44                 final String token = tokenizer.getToken(place);
45                 place = getDetailsPlaceFromToken(token);
46                 checkParameters(place, documentId, tableName, primaryKeyValue);
47         }
48         
49         @Test
50         public void testGetPlaceParametersNumericPrimaryKey() {
51                 // Create a DetailsPlace, testing getPlace():
52                 final String documentId = "somedocument";
53                 final String tableName = "sometable";
54                 final double primaryKeyValue = 123;
55                 final String primaryKeyValueText = "123";
56                 DetailsPlace place = getDetailsPlaceFromToken("document=" + documentId + "&table=" + tableName + "&value="
57                                 + primaryKeyValueText);
58                 checkParameters(place, documentId, tableName, primaryKeyValue);
59
60                 // Recreate it, testing getToken(),
61                 // checking that the same parameters are read back:
62                 final DetailsPlace.Tokenizer tokenizer = new DetailsPlace.Tokenizer();
63                 final String token = tokenizer.getToken(place);
64                 place = getDetailsPlaceFromToken(token);
65                 checkParameters(place, documentId, tableName, primaryKeyValue);
66         }
67
68         @Test
69         public void testGetPlaceParametersBooleanPrimaryKey() {
70                 // Create a DetailsPlace, testing getPlace():
71                 final String documentId = "somedocument";
72                 final String tableName = "sometable";
73                 final boolean primaryKeyValue = false;
74                 final String primaryKeyValueText = "false";
75                 DetailsPlace place = getDetailsPlaceFromToken("document=" + documentId + "&table=" + tableName + "&value="
76                                 + primaryKeyValueText);
77                 checkParameters(place, documentId, tableName, primaryKeyValue);
78
79                 // Recreate it, testing getToken(),
80                 // checking that the same parameters are read back:
81                 final DetailsPlace.Tokenizer tokenizer = new DetailsPlace.Tokenizer();
82                 final String token = tokenizer.getToken(place);
83                 place = getDetailsPlaceFromToken(token);
84                 checkParameters(place, documentId, tableName, primaryKeyValue);
85         }
86
87         @Test
88         public void testGetPlaceParametersDatePrimaryKey() {
89                 // Create a DetailsPlace, testing getPlace():
90                 final String documentId = "somedocument";
91                 final String tableName = "sometable";
92                 
93                 //TODO: Is there no easier (and non-deprecated) way to create
94                 //a Date intance?
95                 Calendar cal = new GregorianCalendar();
96                 cal.setTime(new Date());
97                 cal.set(Calendar.HOUR_OF_DAY, 0);
98                 cal.set(Calendar.MINUTE, 0);
99                 cal.set(Calendar.SECOND, 0);
100                 cal.set(Calendar.MILLISECOND, 0);
101                 cal.set(Calendar.YEAR, 1973);
102                 cal.set(Calendar.MONTH, 4); /* The month is 0 indexed. */
103                 cal.set(Calendar.DAY_OF_MONTH, 11);
104                 final Date primaryKeyValue = cal.getTime();
105                 //final Date primaryKeyValue = new Date(1973, 4, 11); /* The month is 0-indexed. */
106                 
107                 final String primaryKeyValueText = "1973-05-11";
108                 DetailsPlace place = getDetailsPlaceFromToken("document=" + documentId + "&table=" + tableName + "&value="
109                                 + primaryKeyValueText);
110                 checkParameters(place, documentId, tableName, primaryKeyValue);
111
112                 // Recreate it, testing getToken(),
113                 // checking that the same parameters are read back:
114                 final DetailsPlace.Tokenizer tokenizer = new DetailsPlace.Tokenizer();
115                 final String token = tokenizer.getToken(place);
116                 place = getDetailsPlaceFromToken(token);
117                 checkParameters(place, documentId, tableName, primaryKeyValue);
118         }
119
120         /**
121          * @param place
122          * @param documentId
123          * @param tableName
124          * @param primaryKeyValue
125          */
126         private void checkParameters(final DetailsPlace place, final String documentID, final String tableName, double primaryKeyValue) {
127                 checkParametersBasic(place, documentID, tableName);
128                 
129                 //Check the value as a number:
130                 final TypedDataItem dataItem = place.getPrimaryKeyValue();
131                 Utils.transformUnknownToActualType(dataItem, GlomFieldType.TYPE_NUMERIC);
132                 assertEquals(primaryKeyValue, dataItem.getNumber(), 0.1);
133         }
134
135         private void checkParameters(final DetailsPlace place, final String documentID, final String tableName,
136                         final String primaryKeyValue) {
137                 checkParametersBasic(place, documentID, tableName);
138
139                 //Check the value as a string:
140                 final TypedDataItem dataItem = place.getPrimaryKeyValue();
141                 Utils.transformUnknownToActualType(dataItem, GlomFieldType.TYPE_TEXT);
142                 assertEquals(primaryKeyValue, dataItem.getText());
143         }
144         
145         private void checkParameters(final DetailsPlace place, final String documentID, final String tableName,
146                         final Date primaryKeyValue) {
147                 checkParametersBasic(place, documentID, tableName);
148
149                 //Check the value as a string:
150                 final TypedDataItem dataItem = place.getPrimaryKeyValue();
151                 Utils.transformUnknownToActualType(dataItem, GlomFieldType.TYPE_DATE);
152                 assertEquals(primaryKeyValue, dataItem.getDate());
153         }
154         
155         private void checkParameters(final DetailsPlace place, final String documentID, final String tableName,
156                         final boolean primaryKeyValue) {
157                 checkParametersBasic(place, documentID, tableName);
158
159                 //Check the value as a string:
160                 final TypedDataItem dataItem = place.getPrimaryKeyValue();
161                 Utils.transformUnknownToActualType(dataItem, GlomFieldType.TYPE_BOOLEAN);
162                 assertEquals(primaryKeyValue, dataItem.getBoolean());
163         }
164
165         /**
166          * @param place
167          * @param documentID
168          * @param tableName
169          */
170         private void checkParametersBasic(final DetailsPlace place, final String documentID, final String tableName) {
171                 assertNotNull(place);
172
173                 assertEquals(documentID, place.getDocumentID());
174                 assertEquals(tableName, place.getTableName());
175         }
176
177         private DetailsPlace getDetailsPlaceFromToken(final String token) {
178                 final DetailsPlace.Tokenizer tokenizer = new DetailsPlace.Tokenizer();
179                 final DetailsPlace place = tokenizer.getPlace(token);
180                 assertNotNull(place);
181                 return place;
182         }
183
184         private void checkTokenWithoutParameters(final String token) {
185                 final DetailsPlace place = getDetailsPlaceFromToken(token);
186
187                 assertNotNull(place.getDocumentID());
188                 assertTrue(place.getDocumentID().isEmpty());
189
190                 assertNotNull(place.getTableName());
191                 assertTrue(place.getTableName().isEmpty());
192
193                 assertNotNull(place.getPrimaryKeyValue());
194                 assertTrue(place.getPrimaryKeyValue().isEmpty());
195                 assertEquals(null, place.getPrimaryKeyValue().getUnknown());
196                 assertEquals(0.0, place.getPrimaryKeyValue().getNumber(), 0.0); //TODO: Handle other types.
197                 assertEquals(null, place.getPrimaryKeyValue().getText());
198         }
199
200 }