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);
263 final boolean retval = filmManagerDocument.load();
266 // Get the "Scene Cast" related list portal. This relies on specific details of the film manager details
267 // view layout. I've included safety checks that will fail if the layout changes.
268 final List<LayoutGroup> detailsLayout = filmManagerDocument.getDataLayoutGroups("details", "scenes");
269 assertEquals(3, detailsLayout.size());
271 LayoutGroup layoutGroup = detailsLayout.get(1);
272 assertEquals("details", layoutGroup.getName());
274 layoutGroup = detailsLayout.get(2);
275 assertEquals("details_lower", layoutGroup.getName());
277 List<LayoutItem> items = layoutGroup.getItems();
278 assertEquals(2, items.size());
280 final LayoutItem notebookItem = items.get(0);
281 assertEquals("notebook", notebookItem.getName());
282 assertTrue(notebookItem instanceof LayoutItemNotebook);
283 final LayoutItemNotebook notebook = (LayoutItemNotebook) notebookItem;
284 items = notebook.getItems();
285 assertEquals(7, items.size());
286 final LayoutItem portalItem = items.get(0);
287 assertTrue(portalItem instanceof LayoutItemPortal);
288 final LayoutItemPortal portal = (LayoutItemPortal) portalItem;
289 assertTrue(portal != null);
291 assertEquals(portal.getRelationshipNameUsed(), "scene_cast");
293 // call getSuitableTableToCiewDetails
294 final Document.TableToViewDetails viewDetails = filmManagerDocument.getPortalSuitableTableToViewDetails(portal);
295 assertTrue(viewDetails != null);
297 // Simulate a garbage collection
299 System.runFinalization();
301 // Check if things are working like we expect
302 assertEquals("characters", viewDetails.tableName);
303 assertTrue(viewDetails.usesRelationship != null);
304 final Relationship relationship = viewDetails.usesRelationship.getRelationship();
305 assertTrue(relationship != null);
306 assertEquals("cast", relationship.getName());
307 assertTrue(viewDetails.usesRelationship.getRelatedRelationship() == null);
312 public void testReadReportNames() {
313 final List<String> reportNames = document.getReportNames("albums");
314 assertEquals(1, reportNames.size()); // TODO: Test something with more reports.
316 String reports = reportNames.get(0);
317 for (int i = 1; i < reportNames.size(); i++) {
318 reports += ", " + reportNames.get(i);
320 assertThat(reports, is("albums_by_artist"));
324 public void testReadReportStructure() {
325 final Report report = document.getReport("albums", "albums_by_artist");
326 assertTrue(report != null);
327 final LayoutGroup layoutGroup = report.getLayoutGroup();
328 assertTrue(layoutGroup != null);
329 final List<LayoutItem> layoutItems = layoutGroup.getItems();
330 final int numItems = safeLongToInt(layoutItems.size());
331 assertEquals(1, numItems);
333 LayoutItem layoutItem = layoutItems.get(0);
334 assertTrue(layoutItem != null);
335 final LayoutGroup asGroup = (LayoutGroup) layoutItem;
336 assertTrue(asGroup != null);
337 final LayoutItemGroupBy groupby = (LayoutItemGroupBy) layoutItem;
338 assertTrue(groupby != null);
340 assertTrue(groupby.getHasFieldGroupBy());
341 final LayoutItemField fieldGroupBy = groupby.getFieldGroupBy();
342 assertTrue(fieldGroupBy != null);
343 assertThat(fieldGroupBy.getName(), is("artist_id"));
345 final LayoutGroup groupSecondaries = groupby.getSecondaryFields();
346 assertTrue(groupSecondaries != null);
348 final List<LayoutItem> innerItems = groupby.getItems();
349 assertTrue(innerItems != null);
350 final int numInnerItems = safeLongToInt(innerItems.size());
351 assertEquals(2, numInnerItems);
353 layoutItem = innerItems.get(0);
354 assertTrue(layoutItem != null);
355 assertTrue(layoutItem instanceof LayoutItemField);
356 LayoutItemField field = (LayoutItemField) layoutItem;
357 assertTrue(field != null);
358 assertThat(field.getName(), is("name"));
359 assertThat(field.getGlomType(), is(Field.GlomFieldType.TYPE_TEXT));
361 layoutItem = innerItems.get(1);
362 assertTrue(layoutItem != null);
363 assertTrue(layoutItem instanceof LayoutItemField);
364 field = (LayoutItemField) layoutItem;
365 assertTrue(field != null);
366 assertThat(field.getName(), is("year"));
367 assertThat(field.getGlomType(), is(Field.GlomFieldType.TYPE_NUMERIC));
370 // Test thread class that runs all the tests.
371 private class TestThread implements Runnable {
375 for (int i = 0; i < 20; i++) {
377 testGetNumericFormat();
378 testGetSuitableTableToViewDetails();
379 testReadLayoutListInfo();
380 testReadTableFieldSizes();
381 testReadTableNames();
382 testUsesRelationshipMethods();
388 * Tests threaded access.
391 public void testThreadedAccess() throws InterruptedException {
392 // create the threads
393 final Thread thread1 = new Thread(new TestThread());
394 final Thread thread2 = new Thread(new TestThread());
395 final Thread thread3 = new Thread(new TestThread());
396 final Thread thread4 = new Thread(new TestThread());
404 // wait for the treads to finish
407 } catch (final InterruptedException e) {
408 System.out.println("Thread 1 had a problem finishing. " + e);
414 } catch (final InterruptedException e) {
415 System.out.println("Thread 2 had a problem finishing. " + e);
421 } catch (final InterruptedException e) {
422 System.out.println("Thread 3 had a problem finishing. " + e);
428 } catch (final InterruptedException e) {
429 System.out.println("Thread 4 had a problem finishing. " + e);
435 * This method safely converts longs from libglom into ints. This method was taken from stackoverflow:
437 * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
439 private static int safeLongToInt(final long l) {
440 if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
441 throw new IllegalArgumentException(l + " cannot be cast to int without changing its value.");