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.assertTrue;
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.layout.LayoutItemField;
38 import org.jooq.Condition;
39 import org.junit.Assert;
40 import org.junit.Test;
43 * @author Murray Cumming <murrayc@openismus.com>
46 public class SelfHostExampleTest {
48 private SelfHoster selfHoster = null;
51 public void test() throws SQLException {
52 final URL url = SelfHostExampleTest.class.getResource("example_music_collection.glom");
53 assertTrue(url != null);
54 final String strUri = url.toString();
56 final Document document = new Document();
57 document.setFileURI(strUri);
58 assertTrue(document.load());
60 selfHoster = new SelfHoster(document);
61 final boolean hosted = selfHoster.createAndSelfHostFromExample(HostingMode.HOSTING_MODE_POSTGRES_SELF);
64 testExampleMusiccollectionData(document);
67 private void testExampleMusiccollectionData(final Document document) throws SQLException
69 assertTrue(document != null);
71 //Check that some data is as expected:
72 final TypedDataItem quickFindValue = new TypedDataItem();
73 quickFindValue.setText("Born To Run");
74 final Condition whereClause = SqlUtils.getFindWhereClauseQuick(document, "albums", quickFindValue);
75 assertTrue(whereClause != null);
77 final List<LayoutItemField> fieldsToGet = new ArrayList<LayoutItemField>();
78 Field field = document.getField("albums", "album_id");
79 LayoutItemField layoutItemField = new LayoutItemField();
80 layoutItemField.setFullFieldDetails(field);
81 fieldsToGet.add(layoutItemField);
82 field = document.getField("albums", "name");
83 layoutItemField = new LayoutItemField();
84 layoutItemField.setFullFieldDetails(field);
85 fieldsToGet.add(layoutItemField);
87 final String sqlQuery = SqlUtils.buildSqlSelectWithWhereClause("albums", fieldsToGet, whereClause, null);
89 final Connection conn = selfHoster.createConnection(false);
90 assertTrue(conn != null);
92 final Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
93 //st.setFetchSize(length);
94 final ResultSet rs = st.executeQuery(sqlQuery);
95 assertTrue(rs != null);
97 final ResultSetMetaData rsMetaData = rs.getMetaData();
98 Assert.assertEquals(2, rsMetaData.getColumnCount());
101 final int rsRowsCount = rs.getRow();
102 Assert.assertEquals(1, rsRowsCount);
105 public void tearDown() {
106 if (selfHoster != null) {
107 selfHoster.cleanup();