DataItemTest: Test the date type too, and improve the asserts.
[online-glom:gwt-glom.git] / src / test / java / org / glom / web / server / SelfHostConfiguredDocumentTest.java
1 /*
2  * Copyright (C) 2012 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
6  * GWT-Glom is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with GWT-Glom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package org.glom.web.server;
21
22 import static org.junit.Assert.*;
23
24 import java.beans.PropertyVetoException;
25 import java.net.URL;
26 import java.sql.SQLException;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.apache.commons.lang3.StringUtils;
31 import org.glom.web.server.libglom.Document;
32 import org.glom.web.server.libglom.DocumentTest;
33 import org.glom.web.server.libglom.Document.HostingMode;
34 import org.glom.web.shared.DataItem;
35 import org.glom.web.shared.TypedDataItem;
36 import org.glom.web.shared.libglom.layout.LayoutGroup;
37 import org.glom.web.shared.libglom.layout.LayoutItem;
38 import org.glom.web.shared.libglom.layout.LayoutItemField;
39 import org.junit.AfterClass;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42
43 /**
44  * @author Murray Cumming <murrayc@openismus.com>
45  *
46  */
47 public class SelfHostConfiguredDocumentTest {
48
49         private static SelfHoster selfHoster = null;
50         private static ConfiguredDocument configuredDoc;
51         private static Document document;
52         private static String defaultLocale = "";
53         private static String germanLocale = "de";
54         
55         @BeforeClass
56         static public void setUp() throws PropertyVetoException, SQLException {
57                 URL url = DocumentTest.class.getResource("example_music_collection.glom");
58                 assertTrue(url != null);
59                 final String testUriMusicCollection = url.toString();
60                 assertTrue(!StringUtils.isEmpty(testUriMusicCollection));
61
62                 document = new Document();
63                 document.setFileURI(testUriMusicCollection);
64                 final boolean retval = document.load();
65                 assertTrue(retval);
66
67                 selfHoster = new SelfHoster(document);
68                 final boolean hosted = selfHoster.createAndSelfHostFromExample(HostingMode.HOSTING_MODE_POSTGRES_SELF);
69                 assertTrue(hosted);
70                 
71                 configuredDoc = new ConfiguredDocument(document);
72                 configuredDoc.setUsernameAndPassword(selfHoster.getUsername(), selfHoster.getPassword());
73                 assertTrue(configuredDoc.isAuthenticated());
74         }
75
76         @AfterClass
77         static public void tearDown() {
78                 if (selfHoster != null) {
79                         selfHoster.cleanup();
80                 }
81         }
82         
83         public void testGetListViewLayoutGroup(final String locale, final String field0Title, final String field1Title) {
84                 final LayoutGroup group = configuredDoc.getListViewLayoutGroup("albums", defaultLocale);
85                 assertNotNull(group);
86                 
87                 List<LayoutItem> items = group.getItems();
88                 assertNotNull(items);
89                 assertEquals(8, items.size());
90                 
91                 LayoutItem item = items.get(0);
92                 assertTrue(item instanceof LayoutItemField);
93                 
94                 assertEquals("name", item.getName());
95                 assertEquals(field0Title, item.getTitle());
96                 
97                 item = items.get(1);
98                 assertTrue(item instanceof LayoutItemField);
99                 
100                 assertEquals("year", item.getName());
101                 assertEquals(field1Title, item.getTitle());
102         }
103         
104         /**
105          * Test method for {@link org.glom.web.server.ConfiguredDocument#getListViewLayoutGroup(java.lang.String, java.lang.String)}.
106          */
107         @Test
108         public void testGetListViewLayoutGroup() {
109                 testGetListViewLayoutGroup(defaultLocale, "Name", "Year");
110                 testGetListViewLayoutGroup(germanLocale, "Name", "Year");
111         }
112
113         @Test
114         public void testGetListViewData() {
115                 ArrayList<DataItem[]> list = configuredDoc.getListViewData("albums", defaultLocale, 0, 10, false, 0, false);
116                 assertNotNull(list);
117                 assertEquals(5, list.size());
118                 
119                 DataItem[] data = list.get(2);
120                 assertNotNull(data);
121                 assertEquals(8, data.length);
122                 
123                 DataItem dataItem = data[0];
124                 assertNotNull(dataItem);
125                 assertEquals("The Wild, the Innocent, & the E-Street Shuffle", dataItem.getText());
126                 
127                 dataItem = data[1];
128                 assertEquals(1973.0, dataItem.getNumber(), 0);
129                 
130                 dataItem = data[2];
131                 assertEquals(0.0, dataItem.getNumber(), 0);
132                 
133                 dataItem = data[3];
134                 assertEquals("Bruce Springsteen", dataItem.getText());
135                 
136                 dataItem = data[4];
137                 assertEquals(0.0, dataItem.getNumber(), 0);
138                 
139                 dataItem = data[5];
140                 assertEquals("Sony", dataItem.getText());
141                 
142                 dataItem = data[6];
143                 assertEquals("", dataItem.getText());
144                 
145                 dataItem = data[7];
146                 assertEquals(2.0, dataItem.getNumber(), 0);
147         }
148
149         @Test
150         public void testGetDetailsData() {
151                 final TypedDataItem primaryKeyValue = new TypedDataItem();
152                 primaryKeyValue.setNumber(1);
153                 final DataItem[] data = configuredDoc.getDetailsData("albums", primaryKeyValue);
154                 assertNotNull(data);
155                 assertEquals(8, data.length);
156                 
157                 DataItem dataItem = data[0];
158                 assertNotNull(dataItem);
159                 assertEquals(1.0, dataItem.getNumber(), 0);
160                 
161                 dataItem = data[1];
162                 assertEquals("True Blue", dataItem.getText());
163                 
164                 dataItem = data[2];
165                 assertEquals(1.0, dataItem.getNumber(), 0);
166                 
167                 dataItem = data[3];
168                 assertEquals("Madonna", dataItem.getText());
169                 
170                 dataItem = data[4];
171                 assertEquals(1.0, dataItem.getNumber(), 0);
172                 
173                 dataItem = data[5];
174                 assertEquals("Warner Bros", dataItem.getText());
175                 
176                 dataItem = data[6];
177                 assertEquals(1987.0, dataItem.getNumber(), 0);
178                 
179                 dataItem = data[7];
180                 assertEquals("", dataItem.getText());
181         }
182
183         @Test
184         public void testGetRelatedListData() {
185                 //TODO: final ArrayList<DataItem[]> list = configuredDoc.getRelatedListData(tableName, portal, foreignKeyValue, start, length, sortColumnIndex, isAscending)
186         }
187
188         public void testGetDetailsLayoutGroup(final String locale, final String detailsTitle) {;
189                 final List<LayoutGroup> list = configuredDoc.getDetailsLayoutGroup("albums", locale);
190                 assertNotNull(list);
191                 assertEquals(2, list.size());
192                 assertNotNull(list.get(0));
193                 
194                 LayoutGroup layoutGroup = list.get(1);
195                 assertNotNull(layoutGroup);
196                 assertEquals(Document.LAYOUT_NAME_DETAILS, layoutGroup.getName());
197                 assertEquals("Details", layoutGroup.getTitle()); //We don't need to specify locale again.
198                 //assertEquals("Details", layoutGroup.getTitle(germanLocale));
199         }
200         
201         @Test
202         public void testGetDetailsLayoutGroup() {;
203                 testGetDetailsLayoutGroup(defaultLocale, "Details");
204                 testGetDetailsLayoutGroup(germanLocale, "Details");
205         }
206
207         @Test
208         public void testGetRelatedListRowCount() {
209                 //final TypedDataItem foreignKeyValue = new TypedDataItem();
210                 //foreignKeyValue.setNumber(1);
211                 //TODO: configuredDoc.getRelatedListRowCount(tableName, portal, foreignKeyValue);
212         }
213 }