2 * Copyright (C) 2012 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;
22 import static org.junit.Assert.*;
25 import java.sql.Connection;
26 import java.sql.ResultSet;
27 import java.sql.ResultSetMetaData;
28 import java.sql.SQLException;
29 import java.sql.Statement;
30 import java.util.ArrayList;
31 import java.util.List;
33 import org.glom.web.server.libglom.Document;
34 import org.glom.web.server.libglom.Document.HostingMode;
35 import org.glom.web.shared.TypedDataItem;
36 import org.glom.web.shared.libglom.Field;
37 import org.glom.web.shared.libglom.Relationship;
38 import org.glom.web.shared.libglom.layout.LayoutItemField;
39 import org.jooq.Condition;
40 import org.junit.AfterClass;
41 import org.junit.Assert;
42 import org.junit.Test;
45 * @author Murray Cumming <murrayc@openismus.com>
48 public class SelfHostExampleTest {
50 private static SelfHoster selfHoster = null;
53 public void test() throws SQLException {
54 final URL url = SelfHostExampleTest.class.getResource("example_music_collection.glom");
55 assertTrue(url != null);
56 final String strUri = url.toString();
58 final Document document = new Document();
59 document.setFileURI(strUri);
60 assertTrue(document.load());
62 selfHoster = new SelfHoster(document);
63 final boolean hosted = selfHoster.createAndSelfHostFromExample(HostingMode.HOSTING_MODE_POSTGRES_SELF);
66 testExampleMusiccollectionData(document);
69 private void testExampleMusiccollectionData(final Document document) throws SQLException
71 assertTrue(document != null);
73 //Check that some data is as expected:
74 final TypedDataItem quickFindValue = new TypedDataItem();
75 quickFindValue.setText("Born To Run");
76 final String tableName = "albums";
77 final Condition whereClause = SqlUtils.getFindWhereClauseQuick(document, tableName, quickFindValue);
78 assertTrue(whereClause != null);
80 final List<LayoutItemField> fieldsToGet = new ArrayList<LayoutItemField>();
81 Field field = document.getField(tableName, "album_id");
82 final LayoutItemField layoutItemFieldAlbumID = new LayoutItemField();
83 layoutItemFieldAlbumID.setFullFieldDetails(field);
84 fieldsToGet.add(layoutItemFieldAlbumID);
85 field = document.getField(tableName, "name");
86 LayoutItemField layoutItemField = new LayoutItemField();
87 layoutItemField.setFullFieldDetails(field);
88 fieldsToGet.add(layoutItemField);
90 final String sqlQuery = SqlUtils.buildSqlSelectWithWhereClause(tableName, fieldsToGet, whereClause, null);
92 final Connection conn = selfHoster.createConnection(false);
93 assertTrue(conn != null);
95 final Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
96 //st.setFetchSize(length);
97 final ResultSet rs = st.executeQuery(sqlQuery);
98 assertTrue(rs != null);
100 final ResultSetMetaData rsMetaData = rs.getMetaData();
101 Assert.assertEquals(2, rsMetaData.getColumnCount());
104 final int rsRowsCount = rs.getRow();
105 Assert.assertEquals(1, rsRowsCount);
107 final TypedDataItem albumID = new TypedDataItem();
108 SqlUtils.fillDataItemFromResultSet(albumID, layoutItemFieldAlbumID, 1,
109 rs, "fake-document-id", tableName, null);
110 testExampleMusiccollectionDataRelated(document, albumID);
113 /** Check that we can get data via a relationship.
116 * @throws SQLException
118 private void testExampleMusiccollectionDataRelated(Document document, TypedDataItem albumID) throws SQLException {
119 final String tableName = "albums";
122 final List<LayoutItemField> fieldsToGet = new ArrayList<LayoutItemField>();
123 final Field fieldAlbumID = document.getField(tableName, "album_id");
124 assertNotNull(fieldAlbumID);
125 LayoutItemField layoutItemField = new LayoutItemField();
126 layoutItemField.setFullFieldDetails(fieldAlbumID);
127 fieldsToGet.add(layoutItemField);
128 Field field = document.getField(tableName, "name");
129 assertNotNull(field);
130 layoutItemField = new LayoutItemField();
131 layoutItemField.setFullFieldDetails(field);
132 fieldsToGet.add(layoutItemField);
135 final Relationship relationship = document.getRelationship(tableName, "artist");
136 assertNotNull(relationship);
137 layoutItemField = new LayoutItemField();
138 layoutItemField.setRelationship(relationship);
139 field = document.getField("artists", "name");
140 assertNotNull(field);
141 layoutItemField.setFullFieldDetails(field);
142 fieldsToGet.add(layoutItemField);
145 final String sqlQuery = SqlUtils.buildSqlSelectWithKey(tableName, fieldsToGet, fieldAlbumID, albumID);
147 final Connection conn = selfHoster.createConnection(false);
148 assertTrue(conn != null);
150 final Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
151 //st.setFetchSize(length);
152 final ResultSet rs = st.executeQuery(sqlQuery);
153 assertTrue(rs != null);
155 final ResultSetMetaData rsMetaData = rs.getMetaData();
156 Assert.assertEquals(3, rsMetaData.getColumnCount());
159 final int rsRowsCount = rs.getRow();
160 Assert.assertEquals(1, rsRowsCount);
164 public static void tearDown() {
165 if (selfHoster != null) {
166 selfHoster.cleanup();