tests: Add SelfHostConfiguredDocumentTest
[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.glom.web.shared.libglom.layout.LayoutItem;
40 import org.glom.web.shared.libglom.layout.LayoutItemNotebook;
41 import org.glom.web.shared.libglom.layout.LayoutItemPortal;
42 import org.glom.web.shared.libglom.layout.TableToViewDetails;
43 import org.junit.AfterClass;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46
47 /**
48  * @author Murray Cumming <murrayc@openismus.com>
49  *
50  */
51 public class SelfHostConfiguredDocumentTest {
52
53         private static SelfHoster selfHoster = null;
54         private static ConfiguredDocument configuredDoc;
55         private static Document document;
56         private static String defaultLocale = "";
57         private static String germanLocale = "de";
58         
59         @BeforeClass
60         static public void setUp() throws PropertyVetoException, SQLException {
61                 URL url = DocumentTest.class.getResource("example_music_collection.glom");
62                 assertTrue(url != null);
63                 final String testUriMusicCollection = url.toString();
64                 assertTrue(!StringUtils.isEmpty(testUriMusicCollection));
65
66                 document = new Document();
67                 document.setFileURI(testUriMusicCollection);
68                 final boolean retval = document.load();
69                 assertTrue(retval);
70
71                 selfHoster = new SelfHoster(document);
72                 final boolean hosted = selfHoster.createAndSelfHostFromExample(HostingMode.HOSTING_MODE_POSTGRES_SELF);
73                 assertTrue(hosted);
74                 
75                 configuredDoc = new ConfiguredDocument(document);
76                 configuredDoc.setUsernameAndPassword(selfHoster.getUsername(), selfHoster.getPassword());
77                 assertTrue(configuredDoc.isAuthenticated());
78         }
79
80         @AfterClass
81         static public void tearDown() {
82                 if (selfHoster != null) {
83                         selfHoster.cleanup();
84                 }
85         }
86         
87
88         @Test
89         public void testGetListViewData() {
90                 ArrayList<DataItem[]> list = configuredDoc.getListViewData("albums", defaultLocale, 0, 10, false, 0, false);
91                 Assert.assertNotNull(list);
92                 Assert.assertEquals(5, list.size());
93                 
94                 DataItem[] data = list.get(2);
95                 Assert.assertNotNull(data);
96                 Assert.assertEquals(8, data.length);
97                 
98                 DataItem dataItem = data[0];
99                 Assert.assertNotNull(dataItem);
100                 Assert.assertEquals("The Wild, the Innocent, & the E-Street Shuffle", dataItem.getText());
101                 
102                 dataItem = data[1];
103                 Assert.assertEquals(1973.0, dataItem.getNumber());
104                 
105                 dataItem = data[2];
106                 Assert.assertEquals(0.0, dataItem.getNumber());
107                 
108                 dataItem = data[3];
109                 Assert.assertEquals("Bruce Springsteen", dataItem.getText());
110                 
111                 dataItem = data[4];
112                 Assert.assertEquals(0.0, dataItem.getNumber());
113                 
114                 dataItem = data[5];
115                 Assert.assertEquals("Sony", dataItem.getText());
116                 
117                 dataItem = data[6];
118                 Assert.assertEquals("", dataItem.getText());
119                 
120                 dataItem = data[7];
121                 Assert.assertEquals(2.0, dataItem.getNumber());
122         }
123
124         @Test
125         public void testGetDetailsData() {
126                 final TypedDataItem primaryKeyValue = new TypedDataItem();
127                 primaryKeyValue.setNumber(1);
128                 final DataItem[] data = configuredDoc.getDetailsData("albums", primaryKeyValue);
129                 Assert.assertNotNull(data);
130                 Assert.assertEquals(8, data.length);
131                 
132                 DataItem dataItem = data[0];
133                 Assert.assertNotNull(dataItem);
134                 Assert.assertEquals(1.0, dataItem.getNumber());
135                 
136                 dataItem = data[1];
137                 Assert.assertEquals("True Blue", dataItem.getText());
138                 
139                 dataItem = data[2];
140                 Assert.assertEquals(1.0, dataItem.getNumber());
141                 
142                 dataItem = data[3];
143                 Assert.assertEquals("Madonna", dataItem.getText());
144                 
145                 dataItem = data[4];
146                 Assert.assertEquals(1.0, dataItem.getNumber());
147                 
148                 dataItem = data[5];
149                 Assert.assertEquals("Warner Bros", dataItem.getText());
150                 
151                 dataItem = data[6];
152                 Assert.assertEquals(1987.0, dataItem.getNumber());
153                 
154                 dataItem = data[7];
155                 Assert.assertEquals("", dataItem.getText());
156         }
157
158         @Test
159         public void testGetRelatedListData() {
160                 //TODO: final ArrayList<DataItem[]> list = configuredDoc.getRelatedListData(tableName, portal, foreignKeyValue, start, length, sortColumnIndex, isAscending)
161         }
162
163         @Test
164         public void testGetDetailsLayoutGroup() {;
165                 final List<LayoutGroup> list = configuredDoc.getDetailsLayoutGroup("albums", defaultLocale);
166                 Assert.assertNotNull(list);
167                 Assert.assertEquals(2, list.size());
168                 Assert.assertNotNull(list.get(0));
169                 
170                 LayoutGroup layoutGroup = list.get(1);
171                 Assert.assertNotNull(layoutGroup);
172                 assertEquals("details", layoutGroup.getName());
173                 assertEquals("Details", layoutGroup.getTitle(defaultLocale));
174                 //assertEquals("Details", layoutGroup.getTitle(germanLocale));
175         }
176
177         @Test
178         public void testGetRelatedListRowCount() {
179                 //final TypedDataItem foreignKeyValue = new TypedDataItem();
180                 //foreignKeyValue.setNumber(1);
181                 //TODO: configuredDoc.getRelatedListRowCount(tableName, portal, foreignKeyValue);
182         }
183 }