tests: Test translations moe.
[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 junit.framework.Assert;
31
32 import org.apache.commons.lang3.StringUtils;
33 import org.glom.web.server.libglom.Document;
34 import org.glom.web.server.libglom.DocumentTest;
35 import org.glom.web.server.libglom.Document.HostingMode;
36 import org.glom.web.shared.DataItem;
37 import org.glom.web.shared.TypedDataItem;
38 import org.glom.web.shared.libglom.layout.LayoutGroup;
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
84         @Test
85         public void testGetListViewData() {
86                 ArrayList<DataItem[]> list = configuredDoc.getListViewData("albums", defaultLocale, 0, 10, false, 0, false);
87                 Assert.assertNotNull(list);
88                 Assert.assertEquals(5, list.size());
89                 
90                 DataItem[] data = list.get(2);
91                 Assert.assertNotNull(data);
92                 Assert.assertEquals(8, data.length);
93                 
94                 DataItem dataItem = data[0];
95                 Assert.assertNotNull(dataItem);
96                 Assert.assertEquals("The Wild, the Innocent, & the E-Street Shuffle", dataItem.getText());
97                 
98                 dataItem = data[1];
99                 Assert.assertEquals(1973.0, dataItem.getNumber());
100                 
101                 dataItem = data[2];
102                 Assert.assertEquals(0.0, dataItem.getNumber());
103                 
104                 dataItem = data[3];
105                 Assert.assertEquals("Bruce Springsteen", dataItem.getText());
106                 
107                 dataItem = data[4];
108                 Assert.assertEquals(0.0, dataItem.getNumber());
109                 
110                 dataItem = data[5];
111                 Assert.assertEquals("Sony", dataItem.getText());
112                 
113                 dataItem = data[6];
114                 Assert.assertEquals("", dataItem.getText());
115                 
116                 dataItem = data[7];
117                 Assert.assertEquals(2.0, dataItem.getNumber());
118         }
119
120         @Test
121         public void testGetDetailsData() {
122                 final TypedDataItem primaryKeyValue = new TypedDataItem();
123                 primaryKeyValue.setNumber(1);
124                 final DataItem[] data = configuredDoc.getDetailsData("albums", primaryKeyValue);
125                 Assert.assertNotNull(data);
126                 Assert.assertEquals(8, data.length);
127                 
128                 DataItem dataItem = data[0];
129                 Assert.assertNotNull(dataItem);
130                 Assert.assertEquals(1.0, dataItem.getNumber());
131                 
132                 dataItem = data[1];
133                 Assert.assertEquals("True Blue", dataItem.getText());
134                 
135                 dataItem = data[2];
136                 Assert.assertEquals(1.0, dataItem.getNumber());
137                 
138                 dataItem = data[3];
139                 Assert.assertEquals("Madonna", dataItem.getText());
140                 
141                 dataItem = data[4];
142                 Assert.assertEquals(1.0, dataItem.getNumber());
143                 
144                 dataItem = data[5];
145                 Assert.assertEquals("Warner Bros", dataItem.getText());
146                 
147                 dataItem = data[6];
148                 Assert.assertEquals(1987.0, dataItem.getNumber());
149                 
150                 dataItem = data[7];
151                 Assert.assertEquals("", dataItem.getText());
152         }
153
154         @Test
155         public void testGetRelatedListData() {
156                 //TODO: final ArrayList<DataItem[]> list = configuredDoc.getRelatedListData(tableName, portal, foreignKeyValue, start, length, sortColumnIndex, isAscending)
157         }
158
159         public void testGetDetailsLayoutGroup(final String locale, final String detailsTitle) {;
160                 final List<LayoutGroup> list = configuredDoc.getDetailsLayoutGroup("albums", locale);
161                 Assert.assertNotNull(list);
162                 Assert.assertEquals(2, list.size());
163                 Assert.assertNotNull(list.get(0));
164                 
165                 LayoutGroup layoutGroup = list.get(1);
166                 Assert.assertNotNull(layoutGroup);
167                 assertEquals("details", layoutGroup.getName());
168                 assertEquals("Details", layoutGroup.getTitle()); //We don't need to specify locale again.
169                 //assertEquals("Details", layoutGroup.getTitle(germanLocale));
170         }
171         
172         @Test
173         public void testGetDetailsLayoutGroup() {;
174                 testGetDetailsLayoutGroup(defaultLocale, "Details");
175                 testGetDetailsLayoutGroup(germanLocale, "Details");
176         }
177
178         @Test
179         public void testGetRelatedListRowCount() {
180                 //final TypedDataItem foreignKeyValue = new TypedDataItem();
181                 //foreignKeyValue.setNumber(1);
182                 //TODO: configuredDoc.getRelatedListRowCount(tableName, portal, foreignKeyValue);
183         }
184 }