Replace deprecated DOM.setElementAttribute().
[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.shared.DataItem;
34 import org.glom.web.shared.TypedDataItem;
35 import org.glom.web.shared.libglom.layout.LayoutGroup;
36 import org.glom.web.shared.libglom.layout.LayoutItem;
37 import org.glom.web.shared.libglom.layout.LayoutItemField;
38 import org.junit.AfterClass;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41
42 import com.mchange.v2.c3p0.ComboPooledDataSource;
43
44 /**
45  * @author Murray Cumming <murrayc@openismus.com>
46  *
47  */
48 public class SelfHostConfiguredDocumentTest {
49
50         private static SelfHosterPostgreSQL selfHosterPostgreSQL = null;
51         private static ConfiguredDocument configuredDoc;
52         private static ComboPooledDataSource authenticatedConnection;
53         private static Document document;
54         private static String defaultLocale = "";
55         private static String germanLocale = "de";
56         
57         @BeforeClass
58         static public void setUp() throws PropertyVetoException, SQLException {
59                 URL url = DocumentTest.class.getResource("example_music_collection.glom");
60                 assertTrue(url != null);
61                 final String testUriMusicCollection = url.toString();
62                 assertTrue(!StringUtils.isEmpty(testUriMusicCollection));
63
64                 document = new Document();
65                 document.setFileURI(testUriMusicCollection);
66                 final boolean retval = document.load();
67                 assertTrue(retval);
68
69                 selfHosterPostgreSQL = new SelfHosterPostgreSQL(document);
70                 final boolean hosted = selfHosterPostgreSQL.createAndSelfHostFromExample();
71                 assertTrue(hosted);
72                 
73                 configuredDoc = new ConfiguredDocument(document);
74                 authenticatedConnection = SqlUtils.tryUsernameAndPassword(document, selfHosterPostgreSQL.getUsername(), selfHosterPostgreSQL.getPassword());
75                 assertNotNull(authenticatedConnection);
76         }
77
78         @AfterClass
79         static public void tearDown() {
80                 if (selfHosterPostgreSQL != null) {
81                         selfHosterPostgreSQL.cleanup();
82                 }
83         }
84         
85         public void testGetListViewLayoutGroup(final String locale, final String field0Title, final String field1Title) {
86                 final LayoutGroup group = configuredDoc.getListViewLayoutGroup("albums", defaultLocale);
87                 assertNotNull(group);
88                 
89                 List<LayoutItem> items = group.getItems();
90                 assertNotNull(items);
91                 assertEquals(8, items.size());
92                 
93                 LayoutItem item = items.get(0);
94                 assertTrue(item instanceof LayoutItemField);
95                 
96                 assertEquals("name", item.getName());
97                 assertEquals(field0Title, item.getTitle());
98                 
99                 item = items.get(1);
100                 assertTrue(item instanceof LayoutItemField);
101                 
102                 assertEquals("year", item.getName());
103                 assertEquals(field1Title, item.getTitle());
104         }
105         
106         /**
107          * Test method for {@link org.glom.web.server.ConfiguredDocument#getListViewLayoutGroup(java.lang.String, java.lang.String)}.
108          */
109         @Test
110         public void testGetListViewLayoutGroup() {
111                 testGetListViewLayoutGroup(defaultLocale, "Name", "Year");
112                 testGetListViewLayoutGroup(germanLocale, "Name", "Year");
113         }
114
115         @Test
116         public void testGetListViewData() {
117                 ArrayList<DataItem[]> list = configuredDoc.getListViewData(authenticatedConnection, "albums", defaultLocale, 0, 10, false, 0, false);
118                 assertNotNull(list);
119                 assertEquals(5, list.size());
120                 
121                 DataItem[] data = list.get(2);
122                 assertNotNull(data);
123                 assertEquals(8, data.length);
124                 
125                 DataItem dataItem = data[0];
126                 assertNotNull(dataItem);
127                 assertEquals("The Wild, the Innocent, & the E-Street Shuffle", dataItem.getText());
128                 
129                 dataItem = data[1];
130                 assertEquals(1973.0, dataItem.getNumber(), 0);
131                 
132                 dataItem = data[2];
133                 assertEquals(0.0, dataItem.getNumber(), 0);
134                 
135                 dataItem = data[3];
136                 assertEquals("Bruce Springsteen", dataItem.getText());
137                 
138                 dataItem = data[4];
139                 assertEquals(0.0, dataItem.getNumber(), 0);
140                 
141                 dataItem = data[5];
142                 assertEquals("Sony", dataItem.getText());
143                 
144                 dataItem = data[6];
145                 assertEquals("", dataItem.getText());
146                 
147                 dataItem = data[7];
148                 assertEquals(2.0, dataItem.getNumber(), 0);
149         }
150
151         @Test
152         public void testGetDetailsData() {
153                 final TypedDataItem primaryKeyValue = new TypedDataItem();
154                 primaryKeyValue.setNumber(1);
155                 final DataItem[] data = configuredDoc.getDetailsData(authenticatedConnection, "albums", primaryKeyValue);
156                 assertNotNull(data);
157                 assertEquals(8, data.length);
158                 
159                 DataItem dataItem = data[0];
160                 assertNotNull(dataItem);
161                 assertEquals(1.0, dataItem.getNumber(), 0);
162                 
163                 dataItem = data[1];
164                 assertEquals("True Blue", dataItem.getText());
165                 
166                 dataItem = data[2];
167                 assertEquals(1.0, dataItem.getNumber(), 0);
168                 
169                 dataItem = data[3];
170                 assertEquals("Madonna", dataItem.getText());
171                 
172                 dataItem = data[4];
173                 assertEquals(1.0, dataItem.getNumber(), 0);
174                 
175                 dataItem = data[5];
176                 assertEquals("Warner Bros", dataItem.getText());
177                 
178                 dataItem = data[6];
179                 assertEquals(1987.0, dataItem.getNumber(), 0);
180                 
181                 dataItem = data[7];
182                 assertEquals("", dataItem.getText());
183         }
184
185         @Test
186         public void testGetRelatedListData() {
187                 //TODO: final ArrayList<DataItem[]> list = configuredDoc.getRelatedListData(tableName, portal, foreignKeyValue, start, length, sortColumnIndex, isAscending)
188         }
189
190         public void testGetDetailsLayoutGroup(final String locale, final String detailsTitle) {;
191                 final List<LayoutGroup> list = configuredDoc.getDetailsLayoutGroup("albums", locale);
192                 assertNotNull(list);
193                 assertEquals(2, list.size());
194                 assertNotNull(list.get(0));
195                 
196                 LayoutGroup layoutGroup = list.get(1);
197                 assertNotNull(layoutGroup);
198                 assertEquals(Document.LAYOUT_NAME_DETAILS, layoutGroup.getName());
199                 assertEquals("Details", layoutGroup.getTitle()); //We don't need to specify locale again.
200                 //assertEquals("Details", layoutGroup.getTitle(germanLocale));
201         }
202         
203         @Test
204         public void testGetDetailsLayoutGroup() {;
205                 testGetDetailsLayoutGroup(defaultLocale, "Details");
206                 testGetDetailsLayoutGroup(germanLocale, "Details");
207         }
208
209         @Test
210         public void testGetRelatedListRowCount() {
211                 //final TypedDataItem foreignKeyValue = new TypedDataItem();
212                 //foreignKeyValue.setNumber(1);
213                 //TODO: configuredDoc.getRelatedListRowCount(tableName, portal, foreignKeyValue);
214         }
215 }