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