Remove an unused variable
[online-glom:gwt-glom.git] / src / test / java / org / glom / web / server / libglom / DocumentTest.java
1 /*
2  * Copyright (C) 2009, 2010, 2011 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.libglom;
21
22 import static org.hamcrest.CoreMatchers.is;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertThat;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.glom.web.shared.libglom.Field;
31 import org.glom.web.shared.libglom.NumericFormat;
32 import org.glom.web.shared.libglom.Relationship;
33 import org.glom.web.shared.libglom.Report;
34 import org.glom.web.shared.libglom.layout.LayoutGroup;
35 import org.glom.web.shared.libglom.layout.LayoutItem;
36 import org.glom.web.shared.libglom.layout.LayoutItemField;
37 import org.glom.web.shared.libglom.layout.LayoutItemNotebook;
38 import org.glom.web.shared.libglom.layout.LayoutItemPortal;
39 import org.glom.web.shared.libglom.layout.SortClause;
40 import org.glom.web.shared.libglom.layout.reportparts.LayoutItemGroupBy;
41 import org.junit.AfterClass;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44
45 /**
46  * Simple test to ensure that the generated bindings are working.
47  */
48 public class DocumentTest {
49
50         private static Document document;
51         private static String locale = ""; // This means the original locale.
52         final static String testUriMusicCollection = "src/test/java/org/glom/web/server/libglom/example_music_collection.glom";
53         final static String testUriFilmManager = "src/test/java/org/glom/web/server/libglom/example_film_manager.glom";
54
55         @BeforeClass
56         static public void setUp() {
57                 document = new Document();
58                 document.setFileURI(testUriMusicCollection);
59                 final boolean retval = document.load();
60                 assertTrue(retval);
61         }
62
63         @AfterClass
64         static public void tearDown() {
65         }
66
67         @Test
68         public void testDocumentInfo() {
69                 assertThat(document.getDatabaseTitleOriginal(), is("Music Collection"));
70                 assertThat(document.getDefaultTable(), is("artists"));
71         }
72
73         @Test
74         public void testReadTableNames() {
75                 final List<String> tableNames = document.getTableNames();
76                 assertEquals(4, tableNames.size());
77
78                 String tables = tableNames.get(0);
79                 for (int i = 1; i < tableNames.size(); i++) {
80                         tables += ", " + tableNames.get(i);
81                 }
82                 assertThat(tables, is("artists, albums, songs, publishers"));
83         }
84
85         @Test
86         public void testReadTableFieldSizes() {
87
88                 List<Field> fields = document.getTableFields("albums");
89                 assertEquals(6, fields.size());
90
91                 Field field = fields.get(0);
92                 String titles = field.getTitleOrName(locale);
93                 for (int i = 1; i < fields.size(); i++) {
94                         field = fields.get(i);
95                         titles += ", " + field.getTitleOrName(locale);
96                 }
97
98                 // TODO: The sequence is not important. It's only important that they are all there.
99                 assertThat(titles, is("Publisher ID, Artist ID, Album ID, Name, Year, Comments"));
100
101                 fields = document.getTableFields("artists");
102                 assertEquals(4, fields.size());
103
104                 field = fields.get(0);
105                 titles = field.getTitleOrName(locale);
106                 for (int i = 1; i < fields.size(); i++) {
107                         field = fields.get(i);
108                         titles += ", " + field.getTitleOrName(locale);
109                 }
110
111                 // TODO: The sequence is not important. It's only important that they are all there.
112                 assertThat(titles, is("Artist ID, Name, Description, Comments"));
113
114                 fields = document.getTableFields("publishers");
115                 assertEquals(3, fields.size());
116
117                 field = fields.get(0);
118                 titles = field.getTitleOrName(locale);
119                 for (int i = 1; i < fields.size(); i++) {
120                         field = fields.get(i);
121                         titles += ", " + field.getTitleOrName(locale);
122                 }
123
124                 // TODO: The sequence is not important. It's only important that they are all there.
125                 assertThat(titles, is("Name, Publisher ID, Comments"));
126
127                 fields = document.getTableFields("songs");
128                 assertEquals(4, fields.size());
129
130                 field = fields.get(0);
131                 titles = field.getTitleOrName(locale);
132                 for (int i = 1; i < fields.size(); i++) {
133                         field = fields.get(i);
134                         titles += ", " + field.getTitleOrName(locale);
135                 }
136
137                 // TODO: The sequence is not important. It's only important that they are all there.
138                 assertThat(titles, is("Song ID, Album ID, Name, Comments"));
139         }
140
141         @Test
142         public void testReadLayoutListInfo() {
143                 final String[] tables = { "albums", "artists", "publishers", "songs" };
144                 final int[] sortClauseSizes = { 0, 1, 1, 1 };
145                 final int[] layoutFieldSizes = { 7, 4, 3, 4 };
146
147                 for (int i = 0; i < tables.length; i++) {
148                         final List<LayoutGroup> layoutList = document.getDataLayoutGroups("list", tables[i]);
149                         assertTrue(!layoutList.isEmpty());
150                         final List<LayoutItem> layoutItems = layoutList.get(0).getItems();
151                         final List<LayoutItemField> layoutFields = new ArrayList<LayoutItemField>();
152                         final SortClause sortClause = new SortClause(); // TODO: Why use a SortClause instead of a List?
153                         final int numItems = safeLongToInt(layoutItems.size());
154                         for (int j = 0; j < numItems; j++) {
155                                 final LayoutItem item = layoutItems.get(j);
156
157                                 if (item instanceof LayoutItemField) {
158                                         final LayoutItemField field = (LayoutItemField) item;
159                                         layoutFields.add(field);
160                                         final Field details = field.getFullFieldDetails();
161                                         if (details != null && details.getPrimaryKey()) {
162                                                 sortClause.add(new SortClause.SortField(field, true)); // ascending
163                                         }
164                                 }
165                         }
166                         assertEquals(sortClauseSizes[i], sortClause.size());
167                         assertEquals(layoutFieldSizes[i], safeLongToInt(layoutFields.size()));
168                 }
169         }
170
171         /*
172          * This tests if getting values from a NumericFormat object is working. This test was failing with a JVM crash when
173          * using the glom_sharedptr macro with Glom::UsesRelationship and Glom::Formatting.
174          */
175         @Test
176         public void testGetNumericFormat() {
177                 final List<String> tableNames = document.getTableNames();
178
179                 for (int i = 0; i < tableNames.size(); i++) {
180                         final String table = tableNames.get(i);
181                         final List<LayoutGroup> layoutList = document.getDataLayoutGroups("list", table);
182                         assertTrue(!layoutList.isEmpty());
183                         final List<LayoutItem> layoutItems = layoutList.get(0).getItems();
184                         final int numItems = safeLongToInt(layoutItems.size());
185                         for (int j = 0; j < numItems; j++) {
186                                 final LayoutItem item = layoutItems.get(j);
187                                 assertTrue(item != null);
188
189                                 if (item instanceof LayoutItemField) {
190                                         final LayoutItemField itemField = (LayoutItemField) item;
191                                         // don't keep a reference to the FeildFormatting object
192                                         final NumericFormat numFormat = itemField.getFormattingUsed().getNumericFormat();
193                                         assertTrue(numFormat != null);
194
195                                         // get the values
196                                         final boolean altForegroundColorForNegatives = numFormat.getUseAltForegroundColorForNegatives();
197                                         final String currencySymbol = numFormat.getCurrencySymbol();
198                                         final long decimalPlaces = numFormat.getDecimalPlaces();
199                                         final boolean decimalPlacesRestricted = numFormat.getDecimalPlacesRestricted();
200                                         final boolean useThousandsSepator = numFormat.getUseThousandsSeparator();
201                                         final String alternativeColorForNegatives = NumericFormat.getAlternativeColorForNegatives();
202                                         final long defaultPrecision = NumericFormat.getDefaultPrecision();
203
204                                         // Simulate a garbage collection
205                                         System.gc();
206                                         System.runFinalization();
207
208                                         // re-get the values and test
209                                         assertEquals(altForegroundColorForNegatives, numFormat.getUseAltForegroundColorForNegatives());
210                                         assertEquals(currencySymbol, numFormat.getCurrencySymbol());
211                                         assertEquals(decimalPlaces, numFormat.getDecimalPlaces());
212                                         assertEquals(decimalPlacesRestricted, numFormat.getDecimalPlacesRestricted());
213                                         assertEquals(useThousandsSepator, numFormat.getUseThousandsSeparator());
214                                         assertEquals(alternativeColorForNegatives, NumericFormat.getAlternativeColorForNegatives());
215                                         assertEquals(defaultPrecision, NumericFormat.getDefaultPrecision());
216
217                                 }
218                         }
219                 }
220         }
221
222         /*
223          * A smoke test for the methods added to LayoutItemField for accessing methods in Glom::UsesRelationship.
224          */
225         @Test
226         public void testUsesRelationshipMethods() {
227                 final String table = "albums";
228                 final List<LayoutGroup> layoutList = document.getDataLayoutGroups("list", table);
229                 final List<LayoutItem> layoutItems = layoutList.get(0).getItems();
230
231                 String names = null, hasRelationshipNames = null, tablesUsed = null;
232                 final LayoutItem firstItem = layoutItems.get(0);
233
234                 if (firstItem instanceof LayoutItemField) {
235                         final LayoutItemField firstItemField = (LayoutItemField) firstItem;
236                         names = firstItemField.getName();
237                         hasRelationshipNames = "" + firstItemField.getHasRelationshipName();
238                         tablesUsed = firstItemField.getTableUsed(table);
239                 }
240                 final int numItems = safeLongToInt(layoutItems.size());
241                 for (int j = 1; j < numItems; j++) {
242                         final LayoutItem item = layoutItems.get(j);
243
244                         if (item instanceof LayoutItemField) {
245                                 final LayoutItemField itemField = (LayoutItemField) item;
246                                 names += ", " + itemField.getName();
247                                 hasRelationshipNames += ", " + itemField.getHasRelationshipName();
248                                 tablesUsed += ", " + itemField.getTableUsed(table);
249                         }
250                 }
251                 assertEquals("name, year, artist_id, name, publisher_id, name, comments", names);
252                 assertEquals("false, false, false, true, false, true, false", hasRelationshipNames);
253                 assertEquals("albums, albums, albums, artists, albums, publishers, albums", tablesUsed);
254         }
255
256         @Test
257         public void testGetSuitableTableToViewDetails() {
258
259                 // Create a new document for the film manager
260                 final Document filmManagerDocument = new Document();
261                 filmManagerDocument.setFileURI(testUriFilmManager);
262                 final boolean retval = filmManagerDocument.load();
263                 assertTrue(retval);
264
265                 // Get the "Scene Cast" related list portal. This relies on specific details of the film manager details
266                 // view layout. I've included safety checks that will fail if the layout changes.
267                 final List<LayoutGroup> detailsLayout = filmManagerDocument.getDataLayoutGroups("details", "scenes");
268                 assertEquals(3, detailsLayout.size());
269
270                 LayoutGroup layoutGroup = detailsLayout.get(1);
271                 assertEquals("details", layoutGroup.getName());
272
273                 layoutGroup = detailsLayout.get(2);
274                 assertEquals("details_lower", layoutGroup.getName());
275
276                 List<LayoutItem> items = layoutGroup.getItems();
277                 assertEquals(2, items.size());
278
279                 final LayoutItem notebookItem = items.get(0);
280                 assertEquals("notebook", notebookItem.getName());
281                 assertTrue(notebookItem instanceof LayoutItemNotebook);
282                 final LayoutItemNotebook notebook = (LayoutItemNotebook) notebookItem;
283                 items = notebook.getItems();
284                 assertEquals(7, items.size());
285                 final LayoutItem portalItem = items.get(0);
286                 assertTrue(portalItem instanceof LayoutItemPortal);
287                 final LayoutItemPortal portal = (LayoutItemPortal) portalItem;
288                 assertTrue(portal != null);
289
290                 assertEquals(portal.getRelationshipNameUsed(), "scene_cast");
291
292                 // call getSuitableTableToCiewDetails
293                 final Document.TableToViewDetails viewDetails = filmManagerDocument.getPortalSuitableTableToViewDetails(portal);
294                 assertTrue(viewDetails != null);
295
296                 // Simulate a garbage collection
297                 System.gc();
298                 System.runFinalization();
299
300                 // Check if things are working like we expect
301                 assertEquals("characters", viewDetails.tableName);
302                 assertTrue(viewDetails.usesRelationship != null);
303                 final Relationship relationship = viewDetails.usesRelationship.getRelationship();
304                 assertTrue(relationship != null);
305                 assertEquals("cast", relationship.getName());
306                 assertTrue(viewDetails.usesRelationship.getRelatedRelationship() == null);
307
308         }
309
310         @Test
311         public void testReadReportNames() {
312                 final List<String> reportNames = document.getReportNames("albums");
313                 assertEquals(1, reportNames.size()); // TODO: Test something with more reports.
314
315                 String reports = reportNames.get(0);
316                 for (int i = 1; i < reportNames.size(); i++) {
317                         reports += ", " + reportNames.get(i);
318                 }
319                 assertThat(reports, is("albums_by_artist"));
320         }
321
322         @Test
323         public void testReadReportStructure() {
324                 final Report report = document.getReport("albums", "albums_by_artist");
325                 assertTrue(report != null);
326                 final LayoutGroup layoutGroup = report.getLayoutGroup();
327                 assertTrue(layoutGroup != null);
328                 final List<LayoutItem> layoutItems = layoutGroup.getItems();
329                 final int numItems = safeLongToInt(layoutItems.size());
330                 assertEquals(1, numItems);
331
332                 LayoutItem layoutItem = layoutItems.get(0);
333                 assertTrue(layoutItem != null);
334                 final LayoutGroup asGroup = (LayoutGroup) layoutItem;
335                 assertTrue(asGroup != null);
336                 final LayoutItemGroupBy groupby = (LayoutItemGroupBy) layoutItem;
337                 assertTrue(groupby != null);
338
339                 assertTrue(groupby.getHasFieldGroupBy());
340                 final LayoutItemField fieldGroupBy = groupby.getFieldGroupBy();
341                 assertTrue(fieldGroupBy != null);
342                 assertThat(fieldGroupBy.getName(), is("artist_id"));
343
344                 final LayoutGroup groupSecondaries = groupby.getSecondaryFields();
345                 assertTrue(groupSecondaries != null);
346
347                 final List<LayoutItem> innerItems = groupby.getItems();
348                 assertTrue(innerItems != null);
349                 final int numInnerItems = safeLongToInt(innerItems.size());
350                 assertEquals(2, numInnerItems);
351
352                 layoutItem = innerItems.get(0);
353                 assertTrue(layoutItem != null);
354                 assertTrue(layoutItem instanceof LayoutItemField);
355                 LayoutItemField field = (LayoutItemField) layoutItem;
356                 assertTrue(field != null);
357                 assertThat(field.getName(), is("name"));
358                 assertThat(field.getGlomType(), is(Field.GlomFieldType.TYPE_TEXT));
359
360                 layoutItem = innerItems.get(1);
361                 assertTrue(layoutItem != null);
362                 assertTrue(layoutItem instanceof LayoutItemField);
363                 field = (LayoutItemField) layoutItem;
364                 assertTrue(field != null);
365                 assertThat(field.getName(), is("year"));
366                 assertThat(field.getGlomType(), is(Field.GlomFieldType.TYPE_NUMERIC));
367         }
368
369         // Test thread class that runs all the tests.
370         private class TestThread implements Runnable {
371
372                 @Override
373                 public void run() {
374                         for (int i = 0; i < 20; i++) {
375                                 testDocumentInfo();
376                                 testGetNumericFormat();
377                                 testGetSuitableTableToViewDetails();
378                                 testReadLayoutListInfo();
379                                 testReadTableFieldSizes();
380                                 testReadTableNames();
381                                 testUsesRelationshipMethods();
382                         }
383                 }
384         }
385
386         /*
387          * Tests threaded access.
388          */
389         @Test
390         public void testThreadedAccess() throws InterruptedException {
391                 // create the threads
392                 final Thread thread1 = new Thread(new TestThread());
393                 final Thread thread2 = new Thread(new TestThread());
394                 final Thread thread3 = new Thread(new TestThread());
395                 final Thread thread4 = new Thread(new TestThread());
396
397                 // start the threads
398                 thread1.start();
399                 thread2.start();
400                 thread3.start();
401                 thread4.start();
402
403                 // wait for the treads to finish
404                 try {
405                         thread1.join();
406                 } catch (final InterruptedException e) {
407                         System.out.println("Thread 1 had a problem finishing. " + e);
408                         throw e;
409                 }
410
411                 try {
412                         thread2.join();
413                 } catch (final InterruptedException e) {
414                         System.out.println("Thread 2 had a problem finishing. " + e);
415                         throw e;
416                 }
417
418                 try {
419                         thread3.join();
420                 } catch (final InterruptedException e) {
421                         System.out.println("Thread 3 had a problem finishing. " + e);
422                         throw e;
423                 }
424
425                 try {
426                         thread4.join();
427                 } catch (final InterruptedException e) {
428                         System.out.println("Thread 4 had a problem finishing. " + e);
429                         throw e;
430                 }
431         }
432
433         /*
434          * This method safely converts longs from libglom into ints. This method was taken from stackoverflow:
435          * 
436          * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
437          */
438         private static int safeLongToInt(final long l) {
439                 if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
440                         throw new IllegalArgumentException(l + " cannot be cast to int without changing its value.");
441                 }
442                 return (int) l;
443         }
444
445 }