You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@metamodel.apache.org by ka...@apache.org on 2013/07/22 10:10:35 UTC
[22/64] [partial] Hard rename of all 'org/eobjects' folders to
'org/apache'.
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/eobjects/metamodel/MetaModelTestCase.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/eobjects/metamodel/MetaModelTestCase.java b/core/src/test/java/org/eobjects/metamodel/MetaModelTestCase.java
deleted file mode 100644
index 6f3e5b1..0000000
--- a/core/src/test/java/org/eobjects/metamodel/MetaModelTestCase.java
+++ /dev/null
@@ -1,199 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel;
-
-import java.io.File;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import junit.framework.TestCase;
-
-import org.easymock.EasyMock;
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.data.DefaultRow;
-import org.eobjects.metamodel.data.EmptyDataSet;
-import org.eobjects.metamodel.data.InMemoryDataSet;
-import org.eobjects.metamodel.data.Row;
-import org.eobjects.metamodel.data.SimpleDataSetHeader;
-import org.eobjects.metamodel.query.SelectItem;
-import org.eobjects.metamodel.schema.Column;
-import org.eobjects.metamodel.schema.ColumnType;
-import org.eobjects.metamodel.schema.MutableColumn;
-import org.eobjects.metamodel.schema.MutableRelationship;
-import org.eobjects.metamodel.schema.MutableSchema;
-import org.eobjects.metamodel.schema.MutableTable;
-import org.eobjects.metamodel.schema.Schema;
-import org.eobjects.metamodel.schema.TableType;
-
-/**
- * Convenient super-class to use for unittesting
- */
-public abstract class MetaModelTestCase extends TestCase {
-
- public static final String COLUMN_CONTRIBUTOR_COUNTRY = "country";
- public static final String COLUMN_CONTRIBUTOR_NAME = "name";
- public static final String COLUMN_CONTRIBUTOR_CONTRIBUTOR_ID = "contributor_id";
-
- public static final String COLUMN_PROJECT_PROJECT_ID = "project_id";
- public static final String COLUMN_PROJECT_NAME = "name";
- public static final String COLUMN_PROJECT_LINES_OF_CODE = "lines_of_code";
- public static final String COLUMN_PROJECT_PARENT_PROJECT_ID = "parent_project_id";
-
- public static final String COLUMN_ROLE_PROJECT_ID = "project_id";
- public static final String COLUMN_ROLE_CONTRIBUTOR_ID = "contributor_id";
- public static final String COLUMN_ROLE_ROLE_NAME = "name";
-
- public static final String COLUMN_PROJECT_CONTRIBUTOR_CONTRIBUTOR = "contributor";
- public static final String COLUMN_PROJECT_CONTRIBUTOR_ROLE = "role";
- public static final String COLUMN_PROJECT_CONTRIBUTOR_PROJECT = "project";
-
- public static final String TABLE_PROJECT_CONTRIBUTOR = "project_contributor";
- public static final String TABLE_ROLE = "role";
- public static final String TABLE_PROJECT = "project";
- public static final String TABLE_CONTRIBUTOR = "contributor";
-
- /**
- * Creates an example schema with three tables and a view:
- * <ul>
- * <li>contributor[contributor_id,name,country] (TABLE)</li>
- * <li>project[project_id,name,lines_of_code,parent_project_id] (TABLE)</li>
- * <li>role[contributor_id,project_id,role_name] (TABLE)</li>
- * <li>project_contributor[contributor,project,role] (VIEW)</li>
- * </ul>
- * The example schema is good for testing purposes and possess various
- * features of the schema model:
- * <ul>
- * <li>Relations between tables: one-Contributor-to-many-Role's and
- * many-Role's-to-one-Project</li>
- * <li>Recursive relations: A project can have a parent project</li>
- * <li>Views: The ProjectContributor view</li>
- * </ul>
- */
- protected Schema getExampleSchema() {
- MutableSchema schema = new MutableSchema("MetaModelSchema");
-
- MutableTable table1 = new MutableTable(TABLE_CONTRIBUTOR, TableType.TABLE, schema);
- Column column1 = new MutableColumn(COLUMN_CONTRIBUTOR_CONTRIBUTOR_ID, ColumnType.INTEGER, table1, 0, false)
- .setIndexed(true).setPrimaryKey(true);
- Column column2 = new MutableColumn(COLUMN_CONTRIBUTOR_NAME, ColumnType.VARCHAR, table1, 1, false);
- Column column3 = new MutableColumn(COLUMN_CONTRIBUTOR_COUNTRY, ColumnType.VARCHAR, table1, 2, true);
- table1.setColumns(column1, column2, column3);
-
- MutableTable table2 = new MutableTable(TABLE_PROJECT, TableType.TABLE, schema);
- Column column4 = new MutableColumn(COLUMN_PROJECT_PROJECT_ID, ColumnType.INTEGER, table2, 0, false)
- .setPrimaryKey(true);
- Column column5 = new MutableColumn(COLUMN_PROJECT_NAME, ColumnType.VARCHAR, table2, 1, false);
- Column column6 = new MutableColumn(COLUMN_PROJECT_LINES_OF_CODE, ColumnType.BIGINT, table2, 2, true);
- Column column7 = new MutableColumn(COLUMN_PROJECT_PARENT_PROJECT_ID, ColumnType.INTEGER, table2, 3, true);
- table2.setColumns(column4, column5, column6, column7);
-
- MutableTable table3 = new MutableTable(TABLE_ROLE, TableType.TABLE, schema);
- Column column8 = new MutableColumn(COLUMN_ROLE_CONTRIBUTOR_ID, ColumnType.INTEGER, table3, 0, false)
- .setPrimaryKey(true);
- Column column9 = new MutableColumn(COLUMN_ROLE_PROJECT_ID, ColumnType.INTEGER, table3, 1, false)
- .setPrimaryKey(true);
- Column column10 = new MutableColumn(COLUMN_ROLE_ROLE_NAME, ColumnType.VARCHAR, table3, 2, false);
- table3.setColumns(column8, column9, column10);
-
- MutableTable table4 = new MutableTable(TABLE_PROJECT_CONTRIBUTOR, TableType.VIEW, schema);
- Column column11 = new MutableColumn(COLUMN_PROJECT_CONTRIBUTOR_CONTRIBUTOR, ColumnType.VARCHAR, table4, 0,
- false);
- Column column12 = new MutableColumn(COLUMN_PROJECT_CONTRIBUTOR_PROJECT, ColumnType.VARCHAR, table4, 1, false);
- Column column13 = new MutableColumn(COLUMN_PROJECT_CONTRIBUTOR_ROLE, ColumnType.VARCHAR, table4, 2, false);
- ArrayList<Column> columnList = new ArrayList<Column>();
- columnList.add(column11);
- columnList.add(column12);
- columnList.add(column13);
- table4.setColumns(columnList);
-
- // one-Contributor-to-many-Role's
- MutableRelationship.createRelationship(new Column[] { column1 }, new Column[] { column8 });
-
- // one-Project-to-many-Role's
- MutableRelationship.createRelationship(new Column[] { column4 }, new Column[] { column9 });
-
- // view relation [contributor -> contributor_name]
- MutableRelationship.createRelationship(new Column[] { column2 }, new Column[] { column11 });
-
- // view relation [project -> project_name]
- MutableRelationship.createRelationship(new Column[] { column5 }, new Column[] { column12 });
-
- // view relation [role -> role_name]
- MutableRelationship.createRelationship(new Column[] { column10 }, new Column[] { column13 });
-
- schema.setTables(table1, table2, table3, table4);
- return schema;
- }
-
- protected static DataSet createDataSet(SelectItem[] selectItems, List<Object[]> data) {
- if (data.isEmpty()) {
- return new EmptyDataSet(selectItems);
- }
-
- SimpleDataSetHeader header = new SimpleDataSetHeader(selectItems);
-
- List<Row> rows = new ArrayList<Row>();
- for (Object[] objects : data) {
- rows.add(new DefaultRow(header, objects));
- }
- return new InMemoryDataSet(header, rows);
- }
-
- private List<Object> _mocks = new ArrayList<Object>();
-
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- _mocks.clear();
- }
-
- public <T extends Object> T createMock(Class<T> clazz) {
- T mock = EasyMock.createMock(clazz);
- _mocks.add(mock);
- return mock;
- }
-
- public void verifyMocks() {
- EasyMock.verify(_mocks.toArray());
- }
-
- public void replayMocks() {
- EasyMock.replay(_mocks.toArray());
- }
-
- public void assertEquals(DataSet ds1, DataSet ds2) {
- assertEquals(Arrays.toString(ds1.getSelectItems()), Arrays.toString(ds2.getSelectItems()));
- boolean ds1next = true;
- while (ds1next) {
- ds1next = ds1.next();
- boolean ds2next = ds2.next();
- assertEquals("DataSet 1 next=" + ds1next, ds1next, ds2next);
- if (ds1next) {
- Row row1 = ds1.getRow();
- Row row2 = ds2.getRow();
- assertEquals(row1, row2);
- }
- }
- }
-
- protected File getTestResourceAsFile(String filename) {
- return new File("src/test/resources/" + filename);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/eobjects/metamodel/MockDataContext.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/eobjects/metamodel/MockDataContext.java b/core/src/test/java/org/eobjects/metamodel/MockDataContext.java
deleted file mode 100644
index 5d95034..0000000
--- a/core/src/test/java/org/eobjects/metamodel/MockDataContext.java
+++ /dev/null
@@ -1,99 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.eobjects.metamodel.data.CachingDataSetHeader;
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.data.DataSetHeader;
-import org.eobjects.metamodel.data.DefaultRow;
-import org.eobjects.metamodel.data.EmptyDataSet;
-import org.eobjects.metamodel.data.InMemoryDataSet;
-import org.eobjects.metamodel.data.Row;
-import org.eobjects.metamodel.query.SelectItem;
-import org.eobjects.metamodel.schema.Column;
-import org.eobjects.metamodel.schema.ColumnType;
-import org.eobjects.metamodel.schema.MutableColumn;
-import org.eobjects.metamodel.schema.MutableSchema;
-import org.eobjects.metamodel.schema.MutableTable;
-import org.eobjects.metamodel.schema.Schema;
-import org.eobjects.metamodel.schema.Table;
-
-public class MockDataContext extends QueryPostprocessDataContext {
-
- private final String _schemaName;
- private final String _tableName;
- private final String _value;
-
- public MockDataContext(String schemaName, String tableName, String value) {
- _schemaName = schemaName;
- _tableName = tableName;
- _value = value;
- }
-
- @Override
- protected Schema getMainSchema() throws MetaModelException {
-
- final MutableSchema schema = new MutableSchema(_schemaName);
- final MutableTable primaryTable = new MutableTable(_tableName).setSchema(schema);
- primaryTable.addColumn(new MutableColumn("foo").setColumnNumber(0).setType(ColumnType.VARCHAR).setTable(primaryTable));
- primaryTable.addColumn(new MutableColumn("bar").setColumnNumber(1).setType(ColumnType.VARCHAR).setTable(primaryTable));
- primaryTable.addColumn(new MutableColumn("baz").setColumnNumber(2).setType(ColumnType.VARCHAR).setTable(primaryTable));
-
- final MutableTable emptyTable = new MutableTable("an_empty_table").setSchema(schema);
- emptyTable.addColumn(new MutableColumn("foo").setColumnNumber(0).setType(ColumnType.VARCHAR).setTable(emptyTable));
- emptyTable.addColumn(new MutableColumn("bar").setColumnNumber(1).setType(ColumnType.VARCHAR).setTable(emptyTable));
-
- schema.addTable(primaryTable);
- schema.addTable(emptyTable);
-
- return schema;
- }
-
- @Override
- protected String getMainSchemaName() throws MetaModelException {
- return _schemaName;
- }
-
- @Override
- protected DataSet materializeMainSchemaTable(Table table, Column[] columns, int maxRows) {
- if (_tableName.equals(table.getName())) {
- final SelectItem[] allSelectItems = MetaModelHelper.createSelectItems(table.getColumns());
- final DataSetHeader header = new CachingDataSetHeader(allSelectItems);
- final List<Row> data = new ArrayList<Row>();
- data.add(new DefaultRow(header, new Object[] { "1", "hello", "world" }, null));
- data.add(new DefaultRow(header, new Object[] { "2", _value, "world" }, null));
- data.add(new DefaultRow(header, new Object[] { "3", "hi", _value }, null));
- data.add(new DefaultRow(header, new Object[] { "4", "yo", "world" }, null));
-
- DataSet ds = new InMemoryDataSet(header, data);
-
- SelectItem[] columnSelectItems = MetaModelHelper.createSelectItems(columns);
- ds = MetaModelHelper.getSelection(columnSelectItems, ds);
-
- return ds;
- } else if ("an_empty_table".equals(table.getName())) {
- return new EmptyDataSet(columns);
- }
- throw new UnsupportedOperationException();
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/eobjects/metamodel/MockUpdateableDataContext.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/eobjects/metamodel/MockUpdateableDataContext.java b/core/src/test/java/org/eobjects/metamodel/MockUpdateableDataContext.java
deleted file mode 100644
index 60f3d95..0000000
--- a/core/src/test/java/org/eobjects/metamodel/MockUpdateableDataContext.java
+++ /dev/null
@@ -1,181 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-import org.eobjects.metamodel.create.TableCreationBuilder;
-import org.eobjects.metamodel.data.CachingDataSetHeader;
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.data.DefaultRow;
-import org.eobjects.metamodel.data.EmptyDataSet;
-import org.eobjects.metamodel.data.InMemoryDataSet;
-import org.eobjects.metamodel.data.Row;
-import org.eobjects.metamodel.delete.AbstractRowDeletionBuilder;
-import org.eobjects.metamodel.delete.RowDeletionBuilder;
-import org.eobjects.metamodel.drop.TableDropBuilder;
-import org.eobjects.metamodel.insert.AbstractRowInsertionBuilder;
-import org.eobjects.metamodel.insert.RowInsertionBuilder;
-import org.eobjects.metamodel.query.FilterItem;
-import org.eobjects.metamodel.query.SelectItem;
-import org.eobjects.metamodel.schema.Column;
-import org.eobjects.metamodel.schema.ColumnType;
-import org.eobjects.metamodel.schema.MutableColumn;
-import org.eobjects.metamodel.schema.MutableSchema;
-import org.eobjects.metamodel.schema.MutableTable;
-import org.eobjects.metamodel.schema.Schema;
-import org.eobjects.metamodel.schema.Table;
-
-public class MockUpdateableDataContext extends QueryPostprocessDataContext implements UpdateableDataContext {
-
- private final List<Object[]> _values = new ArrayList<Object[]>();
-
- private final MutableTable _table;
- private final MutableSchema _schema;
-
- public MockUpdateableDataContext() {
- _values.add(new Object[] { "1", "hello" });
- _values.add(new Object[] { "2", "there" });
- _values.add(new Object[] { "3", "world" });
-
- _table = new MutableTable("table");
- _table.addColumn(new MutableColumn("foo", ColumnType.VARCHAR).setTable(_table).setColumnNumber(0));
- _table.addColumn(new MutableColumn("bar", ColumnType.VARCHAR).setTable(_table).setColumnNumber(1));
- _schema = new MutableSchema("schema", _table);
- _table.setSchema(_schema);
- }
-
- public MutableTable getTable() {
- return _table;
- }
-
- @Override
- protected DataSet materializeMainSchemaTable(Table table, Column[] columns, int maxRows) {
-
- List<Row> rows = new ArrayList<Row>();
- SelectItem[] items = MetaModelHelper.createSelectItems(columns);
- CachingDataSetHeader header = new CachingDataSetHeader(items);
-
- for (final Object[] values : _values) {
- Object[] rowValues = new Object[columns.length];
- for (int i = 0; i < columns.length; i++) {
- int columnNumber = columns[i].getColumnNumber();
- rowValues[i] = values[columnNumber];
- }
- rows.add(new DefaultRow(header, rowValues));
- }
-
- if (rows.isEmpty()) {
- return new EmptyDataSet(items);
- }
- return new InMemoryDataSet(header, rows);
- }
-
- @Override
- protected String getMainSchemaName() throws MetaModelException {
- return _schema.getName();
- }
-
- @Override
- protected Schema getMainSchema() throws MetaModelException {
- return _schema;
- }
-
- @Override
- public void executeUpdate(UpdateScript update) {
- update.run(new AbstractUpdateCallback(this) {
-
- @Override
- public boolean isDeleteSupported() {
- return true;
- }
-
- @Override
- public RowDeletionBuilder deleteFrom(Table table) throws IllegalArgumentException, IllegalStateException,
- UnsupportedOperationException {
- return new AbstractRowDeletionBuilder(table) {
- @Override
- public void execute() throws MetaModelException {
- delete(getWhereItems());
- }
- };
- }
-
- @Override
- public RowInsertionBuilder insertInto(Table table) throws IllegalArgumentException, IllegalStateException,
- UnsupportedOperationException {
- return new AbstractRowInsertionBuilder<UpdateCallback>(this, table) {
-
- @Override
- public void execute() throws MetaModelException {
- Object[] values = toRow().getValues();
- _values.add(values);
- }
- };
- }
-
- @Override
- public boolean isDropTableSupported() {
- return false;
- }
-
- @Override
- public boolean isCreateTableSupported() {
- return false;
- }
-
- @Override
- public TableDropBuilder dropTable(Table table) throws IllegalArgumentException, IllegalStateException,
- UnsupportedOperationException {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public TableCreationBuilder createTable(Schema schema, String name) throws IllegalArgumentException,
- IllegalStateException {
- throw new UnsupportedOperationException();
- }
- });
- }
-
- private void delete(List<FilterItem> whereItems) {
- final SelectItem[] selectItems = MetaModelHelper.createSelectItems(_table.getColumns());
- final CachingDataSetHeader header = new CachingDataSetHeader(selectItems);
- for (Iterator<Object[]> it = _values.iterator(); it.hasNext();) {
- Object[] values = (Object[]) it.next();
- DefaultRow row = new DefaultRow(header, values);
- boolean delete = true;
- for (FilterItem filterItem : whereItems) {
- if (!filterItem.evaluate(row)) {
- delete = false;
- break;
- }
- }
- if (delete) {
- it.remove();
- }
- }
- }
-
- public List<Object[]> getValues() {
- return _values;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/eobjects/metamodel/QueryPostprocessDataContextTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/eobjects/metamodel/QueryPostprocessDataContextTest.java b/core/src/test/java/org/eobjects/metamodel/QueryPostprocessDataContextTest.java
deleted file mode 100644
index 0562bc5..0000000
--- a/core/src/test/java/org/eobjects/metamodel/QueryPostprocessDataContextTest.java
+++ /dev/null
@@ -1,861 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel;
-
-import java.nio.channels.UnsupportedAddressTypeException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import javax.swing.table.TableModel;
-
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.data.DataSetTableModel;
-import org.eobjects.metamodel.data.Row;
-import org.eobjects.metamodel.query.CompiledQuery;
-import org.eobjects.metamodel.query.FilterItem;
-import org.eobjects.metamodel.query.FromItem;
-import org.eobjects.metamodel.query.FunctionType;
-import org.eobjects.metamodel.query.GroupByItem;
-import org.eobjects.metamodel.query.JoinType;
-import org.eobjects.metamodel.query.OperatorType;
-import org.eobjects.metamodel.query.OrderByItem;
-import org.eobjects.metamodel.query.QueryParameter;
-import org.eobjects.metamodel.query.OrderByItem.Direction;
-import org.eobjects.metamodel.query.Query;
-import org.eobjects.metamodel.query.SelectItem;
-import org.eobjects.metamodel.schema.Column;
-import org.eobjects.metamodel.schema.MutableColumn;
-import org.eobjects.metamodel.schema.MutableSchema;
-import org.eobjects.metamodel.schema.MutableTable;
-import org.eobjects.metamodel.schema.Relationship;
-import org.eobjects.metamodel.schema.Schema;
-import org.eobjects.metamodel.schema.Table;
-
-public class QueryPostprocessDataContextTest extends MetaModelTestCase {
-
- private final Schema schema = getExampleSchema();
- private final Table table1 = schema.getTableByName(TABLE_CONTRIBUTOR);
- private final Table table2 = schema.getTableByName(TABLE_ROLE);
-
- public void testAggregateQueryNoWhereClause() throws Exception {
- MockDataContext dc = new MockDataContext("sch", "tab", "1");
- Table table = dc.getDefaultSchema().getTables()[0];
- assertSingleRowResult("Row[values=[4]]", dc.query().from(table).selectCount().execute());
- }
-
- public void testAggregateQueryRegularWhereClause() throws Exception {
- MockDataContext dc = new MockDataContext("sch", "tab", "1");
- Table table = dc.getDefaultSchema().getTables()[0];
- assertSingleRowResult("Row[values=[3]]", dc.query().from(table).selectCount().where("baz").eq("world")
- .execute());
- }
-
- public void testAggregateQueryWhereClauseExcludingAll() throws Exception {
- MockDataContext dc = new MockDataContext("sch", "tab", "1");
- assertSingleRowResult("Row[values=[0]]",
- dc.query().from("tab").selectCount().where("baz").eq("non_existing_value").execute());
- }
-
- public void testMixedAggregateAndRawQueryOnEmptyTable() throws Exception {
- MockDataContext dc = new MockDataContext("sch", "tab", "1");
- Table emptyTable = dc.getTableByQualifiedLabel("an_empty_table");
-
- assertSingleRowResult("Row[values=[0, null]]", dc.query().from(emptyTable).selectCount().and("foo").execute());
- }
-
- private void assertSingleRowResult(String rowStr, DataSet ds) {
- assertTrue("DataSet had no rows", ds.next());
- Row row = ds.getRow();
- assertEquals(rowStr, row.toString());
- assertFalse("DataSet had more than a single row!", ds.next());
- ds.close();
- }
-
- public void testMixedAggregateAndRawQuery() throws Exception {
- MockDataContext dc = new MockDataContext("sch", "tab", "1");
- Table table = dc.getDefaultSchema().getTables()[0];
- Column[] columns = table.getColumns();
-
- Query query = dc.query().from(table).select(FunctionType.MAX, columns[0]).and(columns[1]).toQuery();
- assertEquals("SELECT MAX(tab.foo), tab.bar FROM sch.tab", query.toSql());
-
- DataSet ds = dc.executeQuery(query);
- assertTrue(ds.next());
- assertEquals("Row[values=[4, hello]]", ds.getRow().toString());
- assertTrue(ds.next());
- assertEquals("Row[values=[4, 1]]", ds.getRow().toString());
- assertTrue(ds.next());
- assertEquals("Row[values=[4, hi]]", ds.getRow().toString());
- assertTrue(ds.next());
- assertEquals("Row[values=[4, yo]]", ds.getRow().toString());
- assertFalse(ds.next());
- }
-
- public void testSelectItemReferencesToFromItems() throws Exception {
- MockDataContext dc = new MockDataContext("sch", "tab", "1");
-
- Table table = dc.getDefaultSchema().getTables()[0];
-
- Query q = new Query();
- FromItem fromItem1 = q.from(table, "t1").getFromClause().getItem(0);
- FromItem fromItem2 = q.from(table, "t2").getFromClause().getItem(1);
- q.select(table.getColumnByName("foo"), fromItem1);
- q.select(table.getColumnByName("foo"), fromItem2);
- q.where(q.getSelectClause().getItem(0), OperatorType.EQUALS_TO, "2");
- assertEquals("SELECT t1.foo, t2.foo FROM sch.tab t1, sch.tab t2 WHERE t1.foo = '2'", q.toSql());
-
- DataSet ds = dc.executeQuery(q);
- SelectItem[] selectItems = ds.getSelectItems();
- assertEquals(2, selectItems.length);
- assertEquals("t1.foo", selectItems[0].toSql());
- assertEquals("t2.foo", selectItems[1].toSql());
- assertTrue(ds.next());
- assertEquals("Row[values=[2, 1]]", ds.getRow().toString());
- assertTrue(ds.next());
- assertEquals("Row[values=[2, 2]]", ds.getRow().toString());
- assertTrue(ds.next());
- assertEquals("Row[values=[2, 3]]", ds.getRow().toString());
- assertTrue(ds.next());
- assertEquals("Row[values=[2, 4]]", ds.getRow().toString());
- assertFalse(ds.next());
- ds.close();
- }
-
- private DataContext getDataContext() {
- QueryPostprocessDataContext dataContext = new QueryPostprocessDataContext() {
-
- @Override
- public DataSet materializeMainSchemaTable(Table table, Column[] columns, int maxRows) {
- if (table == table1) {
- Column[] columns1 = table1.getColumns();
- SelectItem[] selectItems = new SelectItem[columns1.length];
- for (int i = 0; i < selectItems.length; i++) {
- SelectItem selectItem = new SelectItem(columns1[i]);
- selectItems[i] = selectItem;
- }
- List<Object[]> data = new ArrayList<Object[]>();
- data.add(new Object[] { 1, "kasper", "denmark" });
- data.add(new Object[] { 2, "asbjorn", "denmark" });
- data.add(new Object[] { 3, "johny", "israel" });
- data.add(new Object[] { 4, "daniel", "canada" });
- data.add(new Object[] { 5, "sasidhar", "unknown" });
- data.add(new Object[] { 6, "jesper", "denmark" });
- if (maxRows != -1) {
- for (int i = data.size() - 1; i >= maxRows; i--) {
- data.remove(i);
- }
- }
- return createDataSet(selectItems, data);
- } else if (table == table2) {
- Column[] columns2 = table2.getColumns();
- SelectItem[] selectItems = new SelectItem[columns2.length];
- for (int i = 0; i < selectItems.length; i++) {
- SelectItem selectItem = new SelectItem(columns2[i]);
- selectItems[i] = selectItem;
- }
- List<Object[]> data = new ArrayList<Object[]>();
- data.add(new Object[] { 1, 1, "founder" });
- data.add(new Object[] { 1, 1, "developer" });
- data.add(new Object[] { 1, 2, "developer" });
- data.add(new Object[] { 2, 1, "developer" });
- data.add(new Object[] { 2, 3, "developer" });
- data.add(new Object[] { 4, 1, "advisor" });
- data.add(new Object[] { 5, 2, "developer" });
- data.add(new Object[] { 6, 1, "founder" });
- if (maxRows != -1) {
- for (int i = data.size() - 1; i >= maxRows; i--) {
- data.remove(i);
- }
- }
- return createDataSet(selectItems, data);
- }
- throw new IllegalArgumentException("This test only accepts table1 and table2");
- }
-
- @Override
- protected String getMainSchemaName() throws MetaModelException {
- return schema.getName();
- }
-
- @Override
- protected Schema getMainSchema() throws MetaModelException {
- return schema;
- }
- };
- return dataContext;
- }
-
- public void testDistinct() throws Exception {
-
- Column roleColumn = table2.getColumnByName(COLUMN_ROLE_ROLE_NAME);
-
- Query q = new Query().select(roleColumn).from(table2).orderBy(roleColumn);
- q.getSelectClause().setDistinct(true);
-
- DataContext dc = getDataContext();
- DataSet data = dc.executeQuery(q);
- assertTrue(data.next());
- assertEquals("advisor", data.getRow().getValue(roleColumn));
- assertTrue(data.next());
- assertEquals("developer", data.getRow().getValue(roleColumn));
- assertTrue(data.next());
- assertEquals("founder", data.getRow().getValue(roleColumn));
- assertFalse(data.next());
- }
-
- public void testInformationSchema() throws Exception {
- DataContext dc = getDataContext();
- assertEquals("[information_schema, MetaModelSchema]", Arrays.toString(dc.getSchemaNames()));
- Schema informationSchema = dc.getSchemaByName("information_schema");
- assertEquals(
- "[Table[name=tables,type=TABLE,remarks=null], Table[name=columns,type=TABLE,remarks=null], Table[name=relationships,type=TABLE,remarks=null]]",
- Arrays.toString(informationSchema.getTables()));
- assertEquals(
- "[Relationship[primaryTable=tables,primaryColumns=[name],foreignTable=columns,foreignColumns=[table]], "
- + "Relationship[primaryTable=tables,primaryColumns=[name],foreignTable=relationships,foreignColumns=[primary_table]], "
- + "Relationship[primaryTable=tables,primaryColumns=[name],foreignTable=relationships,foreignColumns=[foreign_table]], "
- + "Relationship[primaryTable=columns,primaryColumns=[name],foreignTable=relationships,foreignColumns=[primary_column]], "
- + "Relationship[primaryTable=columns,primaryColumns=[name],foreignTable=relationships,foreignColumns=[foreign_column]]]",
- Arrays.toString(informationSchema.getRelationships()));
- Table tablesTable = informationSchema.getTableByName("tables");
- assertEquals(
- "[Column[name=name,columnNumber=0,type=VARCHAR,nullable=false,nativeType=null,columnSize=null], "
- + "Column[name=type,columnNumber=1,type=VARCHAR,nullable=true,nativeType=null,columnSize=null], "
- + "Column[name=num_columns,columnNumber=2,type=INTEGER,nullable=true,nativeType=null,columnSize=null], "
- + "Column[name=remarks,columnNumber=3,type=VARCHAR,nullable=true,nativeType=null,columnSize=null]]",
- Arrays.toString(tablesTable.getColumns()));
- Table columnsTable = informationSchema.getTableByName("columns");
- assertEquals(
- "[Column[name=name,columnNumber=0,type=VARCHAR,nullable=false,nativeType=null,columnSize=null], "
- + "Column[name=type,columnNumber=1,type=VARCHAR,nullable=true,nativeType=null,columnSize=null], "
- + "Column[name=native_type,columnNumber=2,type=VARCHAR,nullable=true,nativeType=null,columnSize=null], "
- + "Column[name=size,columnNumber=3,type=INTEGER,nullable=true,nativeType=null,columnSize=null], "
- + "Column[name=nullable,columnNumber=4,type=BOOLEAN,nullable=true,nativeType=null,columnSize=null], "
- + "Column[name=indexed,columnNumber=5,type=BOOLEAN,nullable=true,nativeType=null,columnSize=null], "
- + "Column[name=table,columnNumber=6,type=VARCHAR,nullable=false,nativeType=null,columnSize=null], "
- + "Column[name=remarks,columnNumber=7,type=VARCHAR,nullable=true,nativeType=null,columnSize=null]]",
- Arrays.toString(columnsTable.getColumns()));
- Table relationshipsTable = informationSchema.getTableByName("relationships");
- assertEquals(
- "[Column[name=primary_table,columnNumber=0,type=VARCHAR,nullable=false,nativeType=null,columnSize=null], "
- + "Column[name=primary_column,columnNumber=1,type=VARCHAR,nullable=false,nativeType=null,columnSize=null], "
- + "Column[name=foreign_table,columnNumber=2,type=VARCHAR,nullable=false,nativeType=null,columnSize=null], "
- + "Column[name=foreign_column,columnNumber=3,type=VARCHAR,nullable=false,nativeType=null,columnSize=null]]",
- Arrays.toString(relationshipsTable.getColumns()));
-
- DataSet dataSet = dc.query().from(tablesTable).select(tablesTable.getColumns()).execute();
- assertTrue(dataSet.next());
- assertEquals("Row[values=[contributor, TABLE, 3, null]]", dataSet.getRow().toString());
- assertTrue(dataSet.next());
- assertEquals("Row[values=[project, TABLE, 4, null]]", dataSet.getRow().toString());
- assertTrue(dataSet.next());
- assertEquals("Row[values=[role, TABLE, 3, null]]", dataSet.getRow().toString());
- assertTrue(dataSet.next());
- assertEquals("Row[values=[project_contributor, VIEW, 3, null]]", dataSet.getRow().toString());
- assertFalse(dataSet.next());
- dataSet.close();
-
- Relationship relationship = tablesTable.getRelationships(columnsTable)[0];
- FromItem joinFromItem = new FromItem(JoinType.INNER, relationship);
- Query q = new Query().select(tablesTable.getColumnByName("name")).select(columnsTable.getColumnByName("name"))
- .select(columnsTable.getBooleanColumns()).from(joinFromItem);
-
- assertEquals("SELECT tables.name, columns.name, columns.nullable, columns.indexed "
- + "FROM information_schema.tables INNER JOIN information_schema.columns "
- + "ON tables.name = columns.table", q.toString());
-
- dataSet = dc.executeQuery(q);
- assertTrue(dataSet.next());
- assertEquals("Row[values=[contributor, contributor_id, false, true]]", dataSet.getRow().toString());
- assertTrue(dataSet.next());
- assertEquals("Row[values=[contributor, name, false, false]]", dataSet.getRow().toString());
- assertTrue(dataSet.next());
- assertEquals("Row[values=[contributor, country, true, false]]", dataSet.getRow().toString());
- assertTrue(dataSet.next());
- assertEquals("Row[values=[project, project_id, false, false]]", dataSet.getRow().toString());
- assertTrue(dataSet.next());
- assertEquals("Row[values=[project, name, false, false]]", dataSet.getRow().toString());
- dataSet.close();
- }
-
- public void testOrderByWithoutSelecting() throws Exception {
- Query q = new Query();
- q.from(new FromItem(table2).setAlias("r"));
- Column roleColumn = table2.getColumnByName(COLUMN_ROLE_ROLE_NAME);
- Column projectIdColumn = table2.getColumnByName(COLUMN_ROLE_PROJECT_ID);
- q.select(new SelectItem(projectIdColumn));
- q.orderBy(roleColumn);
- assertEquals("SELECT r.project_id FROM MetaModelSchema.role r ORDER BY r.name ASC", q.toString());
-
- DataContext dc = getDataContext();
- DataSet data = dc.executeQuery(q);
- assertEquals(1, data.getSelectItems().length);
-
- @SuppressWarnings("deprecation")
- TableModel tableModel = data.toTableModel();
-
- // should correspond to these lines:
-
- // data.add(new Object[] { 4, 1, "advisor" });
- // data.add(new Object[] { 1, 1, "developer" });
- // data.add(new Object[] { 1, 2, "developer" });
- // data.add(new Object[] { 2, 1, "developer" });
- // data.add(new Object[] { 2, 3, "developer" });
- // data.add(new Object[] { 5, 2, "developer" });
- // data.add(new Object[] { 1, 1, "founder" });
- // data.add(new Object[] { 6, 1, "founder" });
-
- assertEquals(8, tableModel.getRowCount());
- assertEquals(1, tableModel.getColumnCount());
- assertEquals(1, tableModel.getValueAt(0, 0));
- assertEquals(1, tableModel.getValueAt(1, 0));
- assertEquals(2, tableModel.getValueAt(2, 0));
- assertEquals(1, tableModel.getValueAt(3, 0));
- assertEquals(3, tableModel.getValueAt(4, 0));
- assertEquals(2, tableModel.getValueAt(5, 0));
- assertEquals(1, tableModel.getValueAt(6, 0));
- assertEquals(1, tableModel.getValueAt(7, 0));
- }
-
- public void testGroupByWithoutSelecting() throws Exception {
- Query q = new Query();
- q.from(new FromItem(table2).setAlias("r"));
- Column roleColumn = table2.getColumnByName(COLUMN_ROLE_ROLE_NAME);
- Column projectIdColumn = table2.getColumnByName(COLUMN_ROLE_PROJECT_ID);
- q.select(new SelectItem(FunctionType.SUM, projectIdColumn));
- q.groupBy(new GroupByItem(new SelectItem(roleColumn)));
- q.orderBy(roleColumn);
- assertEquals("SELECT SUM(r.project_id) FROM MetaModelSchema.role r GROUP BY r.name ORDER BY r.name ASC",
- q.toString());
-
- DataContext dc = getDataContext();
- DataSet data = dc.executeQuery(q);
- assertEquals(1, data.getSelectItems().length);
- assertEquals("SUM(r.project_id)", data.getSelectItems()[0].toString());
-
- @SuppressWarnings("deprecation")
- TableModel tableModel = data.toTableModel();
- assertEquals(3, tableModel.getRowCount());
- assertEquals(1, tableModel.getColumnCount());
- assertEquals(1.0, tableModel.getValueAt(0, 0));
- assertEquals(9.0, tableModel.getValueAt(1, 0));
- assertEquals(2.0, tableModel.getValueAt(2, 0));
-
- q = dc.query().from(table2).select("name").orderBy("name").toQuery();
- q.getSelectClause().setDistinct(true);
-
- tableModel = new DataSetTableModel(dc.executeQuery(q));
- assertEquals(3, tableModel.getRowCount());
- assertEquals(1, tableModel.getColumnCount());
- assertEquals("advisor", tableModel.getValueAt(0, 0));
- assertEquals("developer", tableModel.getValueAt(1, 0));
- assertEquals("founder", tableModel.getValueAt(2, 0));
- }
-
- public void testSimpleGroupBy() throws Exception {
- Query q = new Query();
- q.from(new FromItem(table2).setAlias("r"));
- Column roleColumn = table2.getColumnByName(COLUMN_ROLE_ROLE_NAME);
- q.select(new SelectItem(roleColumn));
- q.groupBy(new GroupByItem(new SelectItem(roleColumn)));
- assertEquals("SELECT r.name FROM MetaModelSchema.role r GROUP BY r.name", q.toString());
-
- DataContext dc = getDataContext();
- DataSet data = dc.executeQuery(q);
- assertEquals(1, data.getSelectItems().length);
- assertEquals("r.name", data.getSelectItems()[0].toString());
- TableModel tableModel = new DataSetTableModel(data);
- assertEquals(3, tableModel.getRowCount());
-
- q.select(new SelectItem(FunctionType.COUNT, "*", "c"));
- q.where(new FilterItem(new SelectItem(roleColumn), OperatorType.EQUALS_TO, "founder"));
- data = dc.executeQuery(q);
- assertEquals(2, data.getSelectItems().length);
- assertEquals("r.name", data.getSelectItems()[0].toString());
- assertEquals("COUNT(*) AS c", data.getSelectItems()[1].toString());
- tableModel = new DataSetTableModel(data);
- assertEquals(1, tableModel.getRowCount());
- assertEquals("founder", tableModel.getValueAt(0, 0));
- assertEquals(2l, tableModel.getValueAt(0, 1));
-
- q.select(new SelectItem(FunctionType.SUM, table2.getColumns()[0]));
- assertEquals(
- "SELECT r.name, COUNT(*) AS c, SUM(r.contributor_id) FROM MetaModelSchema.role r WHERE r.name = 'founder' GROUP BY r.name",
- q.toString());
- data = dc.executeQuery(q);
- assertEquals(3, data.getSelectItems().length);
- assertEquals("r.name", data.getSelectItems()[0].toString());
- assertEquals("COUNT(*) AS c", data.getSelectItems()[1].toString());
- assertEquals("SUM(r.contributor_id)", data.getSelectItems()[2].toString());
- tableModel = new DataSetTableModel(data);
- assertEquals(1, tableModel.getRowCount());
- assertEquals("founder", tableModel.getValueAt(0, 0));
- assertEquals(2l, tableModel.getValueAt(0, 1));
- assertEquals(7.0, tableModel.getValueAt(0, 2));
- }
-
- public void testSimpleHaving() throws Exception {
- Query q = new Query();
- q.from(table2, "c");
- Column roleColumn = table2.getColumnByName(COLUMN_ROLE_ROLE_NAME);
- Column contributorIdColumn = table2.getColumnByName(COLUMN_ROLE_CONTRIBUTOR_ID);
-
- q.groupBy(roleColumn);
- SelectItem countSelectItem = new SelectItem(FunctionType.COUNT, contributorIdColumn).setAlias("my_count");
- q.select(new SelectItem(roleColumn), countSelectItem);
- q.having(new FilterItem(countSelectItem, OperatorType.GREATER_THAN, 1));
- q.orderBy(new OrderByItem(countSelectItem));
- assertEquals(
- "SELECT c.name, COUNT(c.contributor_id) AS my_count FROM MetaModelSchema.role c GROUP BY c.name HAVING COUNT(c.contributor_id) > 1 ORDER BY COUNT(c.contributor_id) ASC",
- q.toString());
-
- DataSet data = getDataContext().executeQuery(q);
- assertTrue(data.next());
- assertEquals("Row[values=[founder, 2]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[developer, 5]]", data.getRow().toString());
- assertFalse(data.next());
- }
-
- public void testHavingFunctionNotSelected() throws Exception {
- Query q = new Query();
- q.from(table2, "c");
- Column roleColumn = table2.getColumnByName(COLUMN_ROLE_ROLE_NAME);
- Column contributorIdColumn = table2.getColumnByName(COLUMN_ROLE_CONTRIBUTOR_ID);
-
- q.groupBy(roleColumn);
- SelectItem countSelectItem = new SelectItem(FunctionType.COUNT, contributorIdColumn).setAlias("my_count");
- q.select(new SelectItem(roleColumn));
- q.having(new FilterItem(countSelectItem, OperatorType.GREATER_THAN, 3));
- assertEquals("SELECT c.name FROM MetaModelSchema.role c GROUP BY c.name HAVING COUNT(c.contributor_id) > 3",
- q.toString());
-
- DataSet data = getDataContext().executeQuery(q);
- assertTrue(data.next());
- assertEquals("Row[values=[developer]]", data.getRow().toString());
- assertFalse(data.next());
- data.close();
-
- q.getHavingClause().removeItems();
- q.having(new FilterItem(SelectItem.getCountAllItem(), OperatorType.GREATER_THAN, 3));
- assertEquals("SELECT c.name FROM MetaModelSchema.role c GROUP BY c.name HAVING COUNT(*) > 3", q.toString());
- data = getDataContext().executeQuery(q);
- assertTrue(data.next());
- assertEquals("Row[values=[developer]]", data.getRow().toString());
- assertFalse(data.next());
- data.close();
- }
-
- public void testCompiledQueryParameterInWhereClause() throws Exception {
- DataContext dc = getDataContext();
- QueryParameter param1 = new QueryParameter();
- CompiledQuery compiledQuery = dc.query().from(table1).select("name").where(COLUMN_CONTRIBUTOR_COUNTRY)
- .eq(param1).compile();
- try {
- assertEquals(1, compiledQuery.getParameters().size());
- assertSame(param1, compiledQuery.getParameters().get(0));
-
- DataSet ds = dc.executeQuery(compiledQuery, "denmark");
- try {
- assertTrue(ds.next());
- assertEquals("Row[values=[kasper]]", ds.getRow().toString());
- assertTrue(ds.next());
- assertEquals("Row[values=[asbjorn]]", ds.getRow().toString());
- assertTrue(ds.next());
- assertEquals("Row[values=[jesper]]", ds.getRow().toString());
- assertFalse(ds.next());
- } finally {
- ds.close();
- }
-
- try {
- ds = dc.executeQuery(compiledQuery, "canada");
- assertTrue(ds.next());
- assertEquals("Row[values=[daniel]]", ds.getRow().toString());
- assertFalse(ds.next());
- } finally {
- ds.close();
- }
- } finally {
- compiledQuery.close();
- }
- }
-
- public void testCompiledQueryParameterInSubQuery() throws Exception {
- final DataContext dc = getDataContext();
-
- final QueryParameter param1 = new QueryParameter();
- final Query subQuery = dc.query().from(table1).select("name").where(COLUMN_CONTRIBUTOR_COUNTRY).eq(param1)
- .toQuery();
-
- final FromItem subQueryFromItem = new FromItem(subQuery);
- final Query query = new Query().select(new SelectItem(subQuery.getSelectClause().getItem(0), subQueryFromItem))
- .from(subQueryFromItem);
-
- final CompiledQuery compiledQuery = dc.compileQuery(query);
-
- try {
- assertEquals(1, compiledQuery.getParameters().size());
- assertSame(param1, compiledQuery.getParameters().get(0));
-
- DataSet ds = dc.executeQuery(compiledQuery, "denmark");
- List<Object[]> objectArrays = ds.toObjectArrays();
- assertEquals(3, objectArrays.size());
-
- } finally {
- compiledQuery.close();
- }
- }
-
- public void testSelectCount() throws Exception {
- DataContext dc = getDataContext();
- Query q = new Query();
- q.from(table1);
- q.selectCount();
-
- Row row = MetaModelHelper.executeSingleRowQuery(dc, q);
- assertEquals("6", row.getValue(0).toString());
- }
-
- public void testSimpleSelect() throws Exception {
- DataContext dc = getDataContext();
- Query q = new Query();
- q.from(table1);
- q.select(table1.getColumns());
- DataSet dataSet = dc.executeQuery(q);
- assertTrue(dataSet.next());
- Row row = dataSet.getRow();
- assertEquals("Row[values=[1, kasper, denmark]]", row.toString());
- assertTrue(dataSet.next());
- assertTrue(dataSet.next());
- assertTrue(dataSet.next());
- assertTrue(dataSet.next());
- assertTrue(dataSet.next());
- assertFalse(dataSet.next());
- }
-
- public void testCarthesianProduct() throws Exception {
- DataContext dc = getDataContext();
- Query q = new Query();
- q.from(table1);
- q.from(table2);
- q.select(table1.getColumns());
- q.select(table2.getColumns());
- DataSet data = dc.executeQuery(q);
- assertEquals(table1.getColumnCount() + table2.getColumnCount(), data.getSelectItems().length);
- for (int i = 0; i < 6 * 8; i++) {
- assertTrue(data.next());
- if (i == 0) {
- assertEquals("Row[values=[1, kasper, denmark, 1, 1, founder]]", data.getRow().toString());
- } else if (i == 1) {
- assertEquals("Row[values=[1, kasper, denmark, 1, 1, developer]]", data.getRow().toString());
- }
- }
- assertFalse(data.next());
- }
-
- public void testJoinAndFirstRow() throws Exception {
- DataSet data;
-
- DataContext dc = getDataContext();
- Query q = new Query();
- q.from(table1);
- q.from(table2);
- q.select(table1.getColumns());
- q.select(table2.getColumns());
- data = dc.executeQuery(q);
- assertEquals(48, data.toObjectArrays().size());
-
- q.setFirstRow(3);
- data = dc.executeQuery(q);
- assertEquals(46, data.toObjectArrays().size());
- }
-
- public void testSimpleWhere() throws Exception {
- DataContext dc = getDataContext();
- Query q = new Query();
- q.from(table1);
- q.select(table1.getColumns());
- SelectItem countrySelectItem = q.getSelectClause().getSelectItem(
- table1.getColumnByName(COLUMN_CONTRIBUTOR_COUNTRY));
- q.where(new FilterItem(countrySelectItem, OperatorType.EQUALS_TO, "denmark"));
-
- DataSet data = dc.executeQuery(q);
- for (int i = 0; i < 3; i++) {
- assertTrue("Assertion failed at i=" + i, data.next());
- }
- assertFalse(data.next());
- }
-
- public void testMaxRows() throws Exception {
- DataContext dc = getDataContext();
- Query q = new Query();
- q.from(table1);
- q.select(table1.getColumns());
- q.setMaxRows(3);
- DataSet data1 = dc.executeQuery(q);
-
- assertTrue(data1.next());
- assertEquals("Row[values=[1, kasper, denmark]]", data1.getRow().toString());
- assertTrue(data1.next());
- assertEquals("Row[values=[2, asbjorn, denmark]]", data1.getRow().toString());
- assertTrue(data1.next());
- assertEquals("Row[values=[3, johny, israel]]", data1.getRow().toString());
-
- assertFalse(data1.next());
- data1.close();
-
- q = new Query();
- q.from(table1);
- q.select(table1.getColumns());
- q.setFirstRow(2);
- q.setMaxRows(2);
- DataSet data2 = dc.executeQuery(q);
- assertTrue(data2.next());
- assertEquals("Row[values=[2, asbjorn, denmark]]", data2.getRow().toString());
- assertTrue(data2.next());
- assertEquals("Row[values=[3, johny, israel]]", data2.getRow().toString());
-
- assertFalse(data2.next());
- data2.close();
- }
-
- public void testCarthesianProductWithWhere() throws Exception {
- DataContext dc = getDataContext();
-
- SelectItem s1 = new SelectItem(table1.getColumnByName(COLUMN_CONTRIBUTOR_NAME));
- SelectItem s2 = new SelectItem(table2.getColumnByName(COLUMN_ROLE_ROLE_NAME));
- FromItem f1 = new FromItem(table1);
- FromItem f2 = new FromItem(table2);
-
- Query q = new Query();
- q.select(s1);
- q.select(s2);
- q.from(f1);
- q.from(f2);
- SelectItem s3 = new SelectItem(table1.getColumnByName(COLUMN_CONTRIBUTOR_CONTRIBUTOR_ID));
- SelectItem s4 = new SelectItem(table2.getColumnByName(COLUMN_ROLE_CONTRIBUTOR_ID));
- q.where(new FilterItem(s3, OperatorType.EQUALS_TO, s4));
- assertEquals(
- "SELECT contributor.name, role.name FROM MetaModelSchema.contributor, MetaModelSchema.role WHERE contributor.contributor_id = role.contributor_id",
- q.toString());
-
- DataSet data = dc.executeQuery(q);
- assertEquals(2, data.getSelectItems().length);
- assertTrue(data.next());
- assertEquals("Row[values=[kasper, founder]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[kasper, developer]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[kasper, developer]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[asbjorn, developer]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[asbjorn, developer]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[daniel, advisor]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[sasidhar, developer]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[jesper, founder]]", data.getRow().toString());
- assertFalse(data.next());
- }
-
- public void testSelectDistinct() throws Exception {
- // there will be three distinct values in bar column: hello (x2), hi,
- // howdy
- MockDataContext dc = new MockDataContext("sch", "tab", "hello");
-
- Table table = dc.getTableByQualifiedLabel("sch.tab");
- Query q = dc.query().from(table).select("bar").toQuery();
- q.getSelectClause().setDistinct(true);
- q.orderBy(table.getColumnByName("bar"));
-
- DataSet ds = dc.executeQuery(q);
- assertTrue(ds.next());
- assertEquals("Row[values=[hello]]", ds.getRow().toString());
- assertTrue(ds.next());
- assertEquals("Row[values=[hi]]", ds.getRow().toString());
- assertTrue(ds.next());
- assertEquals("Row[values=[yo]]", ds.getRow().toString());
- assertFalse(ds.next());
- }
-
- public void testSubSelectionAndInnerJoin() throws Exception {
- DataContext dc = getDataContext();
-
- SelectItem s1 = new SelectItem(table1.getColumnByName(COLUMN_CONTRIBUTOR_NAME));
- SelectItem s2 = new SelectItem(table2.getColumnByName(COLUMN_ROLE_ROLE_NAME));
- FromItem fromItem = new FromItem(JoinType.INNER, table1.getRelationships(table2)[0]);
-
- Query q = new Query();
- q.select(s1);
- q.select(s2);
- q.from(fromItem);
- assertEquals(
- "SELECT contributor.name, role.name FROM MetaModelSchema.contributor INNER JOIN MetaModelSchema.role ON contributor.contributor_id = role.contributor_id",
- q.toString());
-
- DataSet data = dc.executeQuery(q);
- assertEquals(2, data.getSelectItems().length);
- assertTrue(data.next());
- assertEquals("Row[values=[kasper, founder]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[kasper, developer]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[kasper, developer]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[asbjorn, developer]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[asbjorn, developer]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[daniel, advisor]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[sasidhar, developer]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[jesper, founder]]", data.getRow().toString());
- assertFalse(data.next());
- }
-
- public void testSubquery() throws Exception {
- Query q1 = new Query();
- q1.from(table1);
- q1.select(table1.getColumns());
-
- Query q2 = new Query();
- FromItem fromItem = new FromItem(q1);
- q2.from(fromItem);
- SelectItem selectItem = new SelectItem(q1.getSelectClause().getItems().get(1), fromItem);
- selectItem.setAlias("e");
- q2.select(selectItem);
- assertEquals(
- "SELECT name AS e FROM (SELECT contributor.contributor_id, contributor.name, contributor.country FROM MetaModelSchema.contributor)",
- q2.toString());
-
- fromItem.setAlias("c");
- assertEquals(
- "SELECT c.name AS e FROM (SELECT contributor.contributor_id, contributor.name, contributor.country FROM MetaModelSchema.contributor) c",
- q2.toString());
-
- DataContext dc = getDataContext();
- DataSet data = dc.executeQuery(q2);
- assertEquals(1, data.getSelectItems().length);
- assertTrue(data.next());
- assertEquals("Row[values=[kasper]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[asbjorn]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[johny]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[daniel]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[sasidhar]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[jesper]]", data.getRow().toString());
- assertFalse(data.next());
-
- // Create a sub-query for a sub-query
- Query q3 = new Query();
- fromItem = new FromItem(q2);
- q3.from(fromItem);
- selectItem = new SelectItem(q2.getSelectClause().getItems().get(0), fromItem);
- selectItem.setAlias("f");
- q3.select(selectItem);
- fromItem.setAlias("d");
- assertEquals(
- "SELECT d.e AS f FROM (SELECT c.name AS e FROM (SELECT contributor.contributor_id, contributor.name, contributor.country FROM MetaModelSchema.contributor) c) d",
- q3.toString());
- data = dc.executeQuery(q3);
- assertEquals(1, data.getSelectItems().length);
- assertTrue(data.next());
- assertEquals("Row[values=[kasper]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[asbjorn]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[johny]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[daniel]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[sasidhar]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[jesper]]", data.getRow().toString());
- assertFalse(data.next());
- }
-
- public void testOrderBy() throws Exception {
- Query q = new Query();
- q.from(new FromItem(table1).setAlias("c"));
- q.select(table1.getColumns());
- OrderByItem countryOrderBy = new OrderByItem(q.getSelectClause().getItem(2), Direction.DESC);
- OrderByItem nameOrderBy = new OrderByItem(q.getSelectClause().getItem(1));
- q.orderBy(countryOrderBy, nameOrderBy);
-
- assertEquals(
- "SELECT c.contributor_id, c.name, c.country FROM MetaModelSchema.contributor c ORDER BY c.country DESC, c.name ASC",
- q.toString());
-
- DataSet data = getDataContext().executeQuery(q);
- assertTrue(data.next());
- assertEquals("Row[values=[5, sasidhar, unknown]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[3, johny, israel]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[2, asbjorn, denmark]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[6, jesper, denmark]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[1, kasper, denmark]]", data.getRow().toString());
- assertTrue(data.next());
- assertEquals("Row[values=[4, daniel, canada]]", data.getRow().toString());
- assertFalse(data.next());
- }
-
- public void testExecuteCount() throws Exception {
- QueryPostprocessDataContext dc = new QueryPostprocessDataContext() {
- @Override
- protected DataSet materializeMainSchemaTable(Table table, Column[] columns, int maxRows) {
- throw new UnsupportedAddressTypeException();
- }
-
- @Override
- protected Number executeCountQuery(Table table, List<FilterItem> whereItems,
- boolean functionApproximationAllowed) {
- return 1337;
- }
-
- @Override
- protected String getMainSchemaName() throws MetaModelException {
- return "sch";
- }
-
- @Override
- protected Schema getMainSchema() throws MetaModelException {
- MutableSchema schema = new MutableSchema(getMainSchemaName());
- MutableTable table = new MutableTable("tabl").setSchema(schema);
- return schema.addTable(table.addColumn(new MutableColumn("col").setTable(table)));
- }
- };
-
- DataSet ds = dc.query().from("sch.tabl").selectCount().execute();
- assertTrue(ds.next());
- assertEquals("Row[values=[1337]]", ds.getRow().toString());
- assertFalse(ds.next());
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/eobjects/metamodel/SchemaNameComparatorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/eobjects/metamodel/SchemaNameComparatorTest.java b/core/src/test/java/org/eobjects/metamodel/SchemaNameComparatorTest.java
deleted file mode 100644
index 201c911..0000000
--- a/core/src/test/java/org/eobjects/metamodel/SchemaNameComparatorTest.java
+++ /dev/null
@@ -1,40 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel;
-
-import java.util.Comparator;
-
-import junit.framework.TestCase;
-
-public class SchemaNameComparatorTest extends TestCase {
-
- final Comparator<? super String> comp = SchemaNameComparator.getInstance();
-
- public void testNormalComparison() throws Exception {
- assertTrue(comp.compare("foo", "bar") > 0);
- assertTrue(comp.compare("bar", "foo") < 0);
- assertTrue(comp.compare("bar", "bar") == 0);
- }
-
- public void testNull() throws Exception {
- assertTrue(comp.compare(null, null) == 0);
- assertTrue(comp.compare("foo", null) > 0);
- assertTrue(comp.compare(null, "foo") < 0);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/eobjects/metamodel/convert/ColumnTypeDetectorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/eobjects/metamodel/convert/ColumnTypeDetectorTest.java b/core/src/test/java/org/eobjects/metamodel/convert/ColumnTypeDetectorTest.java
deleted file mode 100644
index 0e9cef7..0000000
--- a/core/src/test/java/org/eobjects/metamodel/convert/ColumnTypeDetectorTest.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel.convert;
-
-import junit.framework.TestCase;
-
-public class ColumnTypeDetectorTest extends TestCase {
-
- public void testBooleanConverter() throws Exception {
- ColumnTypeDetector d = new ColumnTypeDetector();
-
- d.registerValue("1");
- d.registerValue("true");
- d.registerValue("0");
-
- assertEquals(StringToBooleanConverter.class, d.createConverter()
- .getClass());
-
- d.registerValue("2");
-
- assertNull(d.createConverter());
- }
-
- public void testIntegerAndDoubleConverter() throws Exception {
- ColumnTypeDetector d = new ColumnTypeDetector();
-
- d.registerValue("123");
- d.registerValue("0");
-
- assertEquals(StringToIntegerConverter.class, d.createConverter()
- .getClass());
-
- d.registerValue("1123.23");
- d.registerValue("0.0");
-
- assertEquals(StringToDoubleConverter.class, d.createConverter()
- .getClass());
-
- d.registerValue("abc");
-
- assertNull(d.createConverter());
- }
-
- public void testDateConverter() throws Exception {
- ColumnTypeDetector d = new ColumnTypeDetector();
-
- d.registerValue("2010-12-30");
-
- assertEquals(StringToDateConverter.class, d.createConverter()
- .getClass());
-
- d.registerValue("2 abc");
-
- assertNull(d.createConverter());
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/eobjects/metamodel/convert/ConvertedDataSetInterceptorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/eobjects/metamodel/convert/ConvertedDataSetInterceptorTest.java b/core/src/test/java/org/eobjects/metamodel/convert/ConvertedDataSetInterceptorTest.java
deleted file mode 100644
index e096283..0000000
--- a/core/src/test/java/org/eobjects/metamodel/convert/ConvertedDataSetInterceptorTest.java
+++ /dev/null
@@ -1,92 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel.convert;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import junit.framework.TestCase;
-
-import org.eobjects.metamodel.DataContext;
-import org.eobjects.metamodel.MockUpdateableDataContext;
-import org.eobjects.metamodel.UpdateableDataContext;
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.data.InMemoryDataSet;
-import org.eobjects.metamodel.query.Query;
-import org.eobjects.metamodel.schema.Column;
-import org.eobjects.metamodel.schema.Table;
-
-public class ConvertedDataSetInterceptorTest extends TestCase {
-
- public void testConvertedQuery() throws Exception {
- UpdateableDataContext dc = new MockUpdateableDataContext();
- Column fooColumn = dc.getColumnByQualifiedLabel("schema.table.foo");
- assertNotNull(fooColumn);
-
- dc = Converters.addTypeConverter(dc, fooColumn,
- new StringToIntegerConverter());
-
- Table table = dc.getDefaultSchema().getTableByName("table");
- Query query = dc.query().from(table).select(table.getColumns())
- .toQuery();
- assertEquals("SELECT table.foo, table.bar FROM schema.table",
- query.toSql());
-
- DataSet ds = dc.executeQuery(query);
- assertEquals(ConvertedDataSet.class, ds.getClass());
-
- assertTrue(ds.next());
- assertEquals("Row[values=[1, hello]]", ds.getRow().toString());
- assertEquals(Integer.class, ds.getRow().getValue(0).getClass());
- assertEquals(String.class, ds.getRow().getValue(1).getClass());
-
- assertTrue(ds.next());
- assertEquals("Row[values=[2, there]]", ds.getRow().toString());
- assertEquals(Integer.class, ds.getRow().getValue(0).getClass());
- assertEquals(String.class, ds.getRow().getValue(1).getClass());
-
- assertTrue(ds.next());
- assertEquals("Row[values=[3, world]]", ds.getRow().toString());
- assertEquals(Integer.class, ds.getRow().getValue(0).getClass());
- assertEquals(String.class, ds.getRow().getValue(1).getClass());
-
- assertFalse(ds.next());
- ds.close();
- }
-
- public void testNonConvertedQuery() throws Exception {
- MockUpdateableDataContext source = new MockUpdateableDataContext();
- Column fooColumn = source.getColumnByQualifiedLabel("schema.table.foo");
- assertNotNull(fooColumn);
-
- Map<Column, TypeConverter<?, ?>> converters = new HashMap<Column, TypeConverter<?, ?>>();
- converters.put(fooColumn, new StringToIntegerConverter());
- DataContext converted = Converters.addTypeConverter(source, fooColumn,
- new StringToIntegerConverter());
-
- // only select "bar" which is not converted
- Table table = converted.getDefaultSchema().getTableByName("table");
- Query query = converted.query().from(table).select("bar").toQuery();
- assertEquals("SELECT table.bar FROM schema.table", query.toSql());
-
- DataSet ds = converted.executeQuery(query);
- assertEquals(InMemoryDataSet.class, ds.getClass());
-
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/eobjects/metamodel/convert/ConvertedRowInsertionInterceptorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/eobjects/metamodel/convert/ConvertedRowInsertionInterceptorTest.java b/core/src/test/java/org/eobjects/metamodel/convert/ConvertedRowInsertionInterceptorTest.java
deleted file mode 100644
index aad70c2..0000000
--- a/core/src/test/java/org/eobjects/metamodel/convert/ConvertedRowInsertionInterceptorTest.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel.convert;
-
-import java.util.List;
-
-import junit.framework.TestCase;
-
-import org.eobjects.metamodel.MockUpdateableDataContext;
-import org.eobjects.metamodel.UpdateCallback;
-import org.eobjects.metamodel.UpdateScript;
-import org.eobjects.metamodel.UpdateableDataContext;
-import org.eobjects.metamodel.schema.Column;
-
-public class ConvertedRowInsertionInterceptorTest extends TestCase {
-
- public void testConvertedInsert() throws Exception {
- MockUpdateableDataContext source = new MockUpdateableDataContext();
- Column fooColumn = source.getColumnByQualifiedLabel("schema.table.foo");
- assertNotNull(fooColumn);
-
- UpdateableDataContext intercepted = Converters.addTypeConverter(source,
- fooColumn, new StringToIntegerConverter());
-
- final List<Object[]> values = source.getValues();
-
- assertEquals(3, values.size());
-
- intercepted.executeUpdate(new UpdateScript() {
- @Override
- public void run(UpdateCallback callback) {
- callback.insertInto("schema.table").value(0, 1).value(1, "2")
- .execute();
- callback.insertInto("schema.table").value(0, 3).value(1, "4")
- .execute();
- }
- });
-
- assertEquals(5, values.size());
- assertEquals("1", values.get(3)[0]);
- assertEquals("2", values.get(3)[1]);
- assertEquals("3", values.get(4)[0]);
- assertEquals("4", values.get(4)[1]);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/eobjects/metamodel/convert/ConvertersTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/eobjects/metamodel/convert/ConvertersTest.java b/core/src/test/java/org/eobjects/metamodel/convert/ConvertersTest.java
deleted file mode 100644
index 7ff2f7c..0000000
--- a/core/src/test/java/org/eobjects/metamodel/convert/ConvertersTest.java
+++ /dev/null
@@ -1,160 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel.convert;
-
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-
-import junit.framework.TestCase;
-
-import org.eobjects.metamodel.MockUpdateableDataContext;
-import org.eobjects.metamodel.UpdateCallback;
-import org.eobjects.metamodel.UpdateScript;
-import org.eobjects.metamodel.UpdateableDataContext;
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.query.Query;
-import org.eobjects.metamodel.schema.Column;
-import org.eobjects.metamodel.schema.Table;
-
-public class ConvertersTest extends TestCase {
-
- public void testAutoDetectConverters() throws Exception {
- final MockUpdateableDataContext decoratedDataContext = new MockUpdateableDataContext();
- final Table table = decoratedDataContext.getDefaultSchema().getTables()[0];
- Map<Column, TypeConverter<?, ?>> converters = Converters.autoDetectConverters(decoratedDataContext, table, 2);
- assertEquals(1, converters.size());
- assertEquals(
- "[Column[name=foo,columnNumber=0,type=VARCHAR,nullable=null,nativeType=null,columnSize=null]]",
- converters.keySet().toString());
- assertEquals(StringToIntegerConverter.class, converters.values().iterator().next().getClass());
-
- final UpdateableDataContext dc = Converters.addTypeConverters(decoratedDataContext, converters);
-
- DataSet ds = dc.query().from(table).select(table.getColumns()).execute();
- assertEquals(ConvertedDataSet.class, ds.getClass());
- assertTrue(ds.next());
- assertEquals("Row[values=[1, hello]]", ds.getRow().toString());
- assertEquals(Integer.class, ds.getRow().getValue(0).getClass());
- assertEquals(1, ds.getRow().getValue(0));
- assertTrue(ds.next());
- assertEquals(Integer.class, ds.getRow().getValue(0).getClass());
- assertEquals(2, ds.getRow().getValue(0));
- assertTrue(ds.next());
- assertEquals(Integer.class, ds.getRow().getValue(0).getClass());
- assertEquals(3, ds.getRow().getValue(0));
- assertFalse(ds.next());
- ds.close();
-
- dc.executeUpdate(new UpdateScript() {
- @Override
- public void run(UpdateCallback callback) {
- callback.insertInto(table).value("foo", 4).value("bar", "mrrrrh").execute();
- }
- });
-
- // query the decorator
- ds = dc.query().from(table).select(table.getColumns()).where("foo").eq(4).execute();
- assertTrue(ds.next());
- assertEquals("Row[values=[4, mrrrrh]]", ds.getRow().toString());
- assertEquals(Integer.class, ds.getRow().getValue(0).getClass());
- assertEquals(4, ds.getRow().getValue(0));
- assertFalse(ds.next());
- ds.close();
-
- // query the decorated
- Object[] physicalRow = decoratedDataContext.getValues().get(3);
- assertEquals("[4, mrrrrh]", Arrays.toString(physicalRow));
- assertEquals(String.class, physicalRow[0].getClass());
- }
-
- public void testScenario() throws Exception {
- UpdateableDataContext dc = new MockUpdateableDataContext();
- List<Object[]> physicalValuesList = ((MockUpdateableDataContext) dc).getValues();
- assertEquals(3, physicalValuesList.size());
- for (Object[] physicalValues : physicalValuesList) {
- assertEquals("foo is expected to be string", String.class, physicalValues[0].getClass());
- }
-
- final Table table = dc.getDefaultSchema().getTables()[0];
- Map<Column, TypeConverter<?, ?>> converters = Converters.autoDetectConverters(dc, table, 1000);
- assertEquals(1, converters.size());
- dc = Converters.addTypeConverters(dc, converters);
-
- final Query q = dc.query().from(table).select("foo").toQuery();
- assertEquals("SELECT table.foo FROM schema.table", q.toSql());
- DataSet ds = dc.executeQuery(q);
- assertTrue(ds.next());
- assertEquals(1, ds.getRow().getValue(0));
- assertTrue(ds.next());
- assertEquals(2, ds.getRow().getValue(0));
- assertTrue(ds.next());
- assertEquals(3, ds.getRow().getValue(0));
- assertFalse(ds.next());
-
- dc.executeUpdate(new UpdateScript() {
- @Override
- public void run(UpdateCallback callback) {
- callback.insertInto(table).value("foo", 4).value("bar", "heidiho!").execute();
- }
- });
-
- ds = dc.executeQuery(q);
- assertTrue(ds.next());
- assertEquals(1, ds.getRow().getValue(0));
- assertTrue(ds.next());
- assertEquals(2, ds.getRow().getValue(0));
- assertTrue(ds.next());
- assertEquals(3, ds.getRow().getValue(0));
- assertTrue(ds.next());
- assertEquals(4, ds.getRow().getValue(0));
- assertFalse(ds.next());
-
- assertEquals(4, physicalValuesList.size());
- for (Object[] physicalValues : physicalValuesList) {
- assertEquals("foo is expected to be string", String.class, physicalValues[0].getClass());
- }
-
- dc.executeUpdate(new UpdateScript() {
- @Override
- public void run(UpdateCallback callback) {
- callback.insertInto(table).value("foo", 5).value("bar", "hejsa...").execute();
- callback.update(table).where("foo").lessThan(3).value("foo", 100).execute();
- }
- });
-
- ds = dc.executeQuery(q);
- assertTrue(ds.next());
- assertEquals(3, ds.getRow().getValue(0));
- assertTrue(ds.next());
- assertEquals(4, ds.getRow().getValue(0));
- assertTrue(ds.next());
- assertEquals(5, ds.getRow().getValue(0));
- assertTrue(ds.next());
- assertEquals(100, ds.getRow().getValue(0));
- assertTrue(ds.next());
- assertEquals(100, ds.getRow().getValue(0));
- assertFalse(ds.next());
-
- assertEquals(5, physicalValuesList.size());
- for (Object[] physicalValues : physicalValuesList) {
- assertEquals("foo is expected to be string", String.class, physicalValues[0].getClass());
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/eobjects/metamodel/convert/StringToBooleanConverterTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/eobjects/metamodel/convert/StringToBooleanConverterTest.java b/core/src/test/java/org/eobjects/metamodel/convert/StringToBooleanConverterTest.java
deleted file mode 100644
index d173e1e..0000000
--- a/core/src/test/java/org/eobjects/metamodel/convert/StringToBooleanConverterTest.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel.convert;
-
-import junit.framework.TestCase;
-
-public class StringToBooleanConverterTest extends TestCase {
-
- private StringToBooleanConverter conv = new StringToBooleanConverter();
-
- public void testToVirtual() throws Exception {
- assertNull(conv.toVirtualValue(null));
- assertNull(conv.toVirtualValue(""));
- assertEquals(true, conv.toVirtualValue("true").booleanValue());
- }
-
- public void testToPhysical() throws Exception {
- assertNull(conv.toPhysicalValue(null));
- assertEquals("true", conv.toPhysicalValue(true));
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/eobjects/metamodel/convert/StringToDateConverterTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/eobjects/metamodel/convert/StringToDateConverterTest.java b/core/src/test/java/org/eobjects/metamodel/convert/StringToDateConverterTest.java
deleted file mode 100644
index 5288f9d..0000000
--- a/core/src/test/java/org/eobjects/metamodel/convert/StringToDateConverterTest.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel.convert;
-
-import java.text.DateFormat;
-import java.util.Date;
-
-import junit.framework.TestCase;
-
-import org.eobjects.metamodel.util.DateUtils;
-import org.eobjects.metamodel.util.Month;
-
-public class StringToDateConverterTest extends TestCase {
-
- public void testToVirtualSimpleDateFormat() throws Exception {
- StringToDateConverter conv = new StringToDateConverter("yyyy-MM-dd");
- assertNull(conv.toVirtualValue(null));
- assertNull(conv.toVirtualValue(""));
-
- assertEquals(DateUtils.get(2010, Month.DECEMBER, 31),
- conv.toVirtualValue("2010-12-31"));
- }
-
- public void testToVirtualNoArgs() throws Exception {
- StringToDateConverter conv = new StringToDateConverter();
- assertNull(conv.toVirtualValue(null));
- assertNull(conv.toVirtualValue(""));
-
- assertEquals(DateUtils.get(2010, Month.DECEMBER, 31),
- conv.toVirtualValue("2010-12-31"));
- }
-
- public void testToPhysicalSimpleDateFormat() throws Exception {
- StringToDateConverter conv = new StringToDateConverter("yyyy-MM-dd");
- assertNull(conv.toPhysicalValue(null));
- Date input = DateUtils.get(2010, Month.DECEMBER, 31);
- String physicalValue = conv.toPhysicalValue(input);
- assertEquals("2010-12-31", physicalValue);
- }
-
- public void testToPhysicalNoArgs() throws Exception {
- StringToDateConverter conv = new StringToDateConverter();
- assertNull(conv.toPhysicalValue(null));
- Date input = DateUtils.get(2010, Month.DECEMBER, 31);
- String physicalValue = conv.toPhysicalValue(input);
- Date virtualValue = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
- DateFormat.MEDIUM).parse(physicalValue);
- assertEquals(virtualValue, input);
- }
-}