2 * Copyright (C) 2009, 2010, 2011 Openismus GmbH
4 * This file is part of gwt-glom
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.
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
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/>.
20 package org.glom.web.server.libglom;
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;
27 import java.util.ArrayList;
28 import java.util.List;
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;
46 * Simple test to ensure that the generated bindings are working.
48 public class DocumentTest {
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";
56 static public void setUp() {
57 document = new Document();
58 document.setFileURI(testUriMusicCollection);
59 final boolean retval = document.load();
64 static public void tearDown() {
68 public void testDocumentInfo() {
69 assertThat(document.getDatabaseTitleOriginal(), is("Music Collection"));
70 assertThat(document.getDefaultTable(), is("artists"));
74 public void testReadTableNames() {
75 final List<String> tableNames = document.getTableNames();
76 assertEquals(4, tableNames.size());
78 String tables = tableNames.get(0);
79 for (int i = 1; i < tableNames.size(); i++) {
80 tables += ", " + tableNames.get(i);
82 assertThat(tables, is("artists, albums, songs, publishers"));
86 public void testReadTableFieldSizes() {
88 List<Field> fields = document.getTableFields("albums");
89 assertEquals(6, fields.size());
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);
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"));
101 fields = document.getTableFields("artists");
102 assertEquals(4, fields.size());
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);
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"));
114 fields = document.getTableFields("publishers");
115 assertEquals(3, fields.size());
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);
124 // TODO: The sequence is not important. It's only important that they are all there.
125 assertThat(titles, is("Name, Publisher ID, Comments"));
127 fields = document.getTableFields("songs");
128 assertEquals(4, fields.size());
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);
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"));
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 };
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);
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
166 assertEquals(sortClauseSizes[i], sortClause.size());
167 assertEquals(layoutFieldSizes[i], safeLongToInt(layoutFields.size()));
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.
176 public void testGetNumericFormat() {
177 final List<String> tableNames = document.getTableNames();
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);
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);
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();
204 // Simulate a garbage collection
206 System.runFinalization();
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());
223 * A smoke test for the methods added to LayoutItemField for accessing methods in Glom::UsesRelationship.
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();
231 String names = null, hasRelationshipNames = null, tablesUsed = null;
232 final LayoutItem firstItem = layoutItems.get(0);
234 if (firstItem instanceof LayoutItemField) {
235 final LayoutItemField firstItemField = (LayoutItemField) firstItem;
236 names = firstItemField.getName();
237 hasRelationshipNames = "" + firstItemField.getHasRelationshipName();
238 tablesUsed = firstItemField.getTableUsed(table);
240 final int numItems = safeLongToInt(layoutItems.size());
241 for (int j = 1; j < numItems; j++) {
242 final LayoutItem item = layoutItems.get(j);
244 if (item instanceof LayoutItemField) {
245 final LayoutItemField itemField = (LayoutItemField) item;
246 names += ", " + itemField.getName();
247 hasRelationshipNames += ", " + itemField.getHasRelationshipName();
248 tablesUsed += ", " + itemField.getTableUsed(table);
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);
257 public void testGetSuitableTableToViewDetails() {
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();
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());
270 LayoutGroup layoutGroup = detailsLayout.get(1);
271 assertEquals("details", layoutGroup.getName());
273 layoutGroup = detailsLayout.get(2);
274 assertEquals("details_lower", layoutGroup.getName());
276 List<LayoutItem> items = layoutGroup.getItems();
277 assertEquals(2, items.size());
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);
290 assertEquals(portal.getRelationshipNameUsed(), "scene_cast");
292 // call getSuitableTableToCiewDetails
293 final Document.TableToViewDetails viewDetails = filmManagerDocument.getPortalSuitableTableToViewDetails(portal);
294 assertTrue(viewDetails != null);
296 // Simulate a garbage collection
298 System.runFinalization();
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);
311 public void testReadReportNames() {
312 final List<String> reportNames = document.getReportNames("albums");
313 assertEquals(1, reportNames.size()); // TODO: Test something with more reports.
315 String reports = reportNames.get(0);
316 for (int i = 1; i < reportNames.size(); i++) {
317 reports += ", " + reportNames.get(i);
319 assertThat(reports, is("albums_by_artist"));
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);
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);
339 assertTrue(groupby.getHasFieldGroupBy());
340 final LayoutItemField fieldGroupBy = groupby.getFieldGroupBy();
341 assertTrue(fieldGroupBy != null);
342 assertThat(fieldGroupBy.getName(), is("artist_id"));
344 final LayoutGroup groupSecondaries = groupby.getSecondaryFields();
345 assertTrue(groupSecondaries != null);
347 final List<LayoutItem> innerItems = groupby.getItems();
348 assertTrue(innerItems != null);
349 final int numInnerItems = safeLongToInt(innerItems.size());
350 assertEquals(2, numInnerItems);
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));
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));
369 // Test thread class that runs all the tests.
370 private class TestThread implements Runnable {
374 for (int i = 0; i < 20; i++) {
376 testGetNumericFormat();
377 testGetSuitableTableToViewDetails();
378 testReadLayoutListInfo();
379 testReadTableFieldSizes();
380 testReadTableNames();
381 testUsesRelationshipMethods();
387 * Tests threaded access.
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());
403 // wait for the treads to finish
406 } catch (final InterruptedException e) {
407 System.out.println("Thread 1 had a problem finishing. " + e);
413 } catch (final InterruptedException e) {
414 System.out.println("Thread 2 had a problem finishing. " + e);
420 } catch (final InterruptedException e) {
421 System.out.println("Thread 3 had a problem finishing. " + e);
427 } catch (final InterruptedException e) {
428 System.out.println("Thread 4 had a problem finishing. " + e);
434 * This method safely converts longs from libglom into ints. This method was taken from stackoverflow:
436 * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
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.");