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:38 UTC
[25/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/apache/metamodel/data/FirstRowDataSetTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/data/FirstRowDataSetTest.java b/core/src/test/java/org/apache/metamodel/data/FirstRowDataSetTest.java
new file mode 100644
index 0000000..a733687
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/data/FirstRowDataSetTest.java
@@ -0,0 +1,83 @@
+/**
+ * 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.data;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eobjects.metamodel.query.SelectItem;
+import org.eobjects.metamodel.schema.MutableColumn;
+
+import junit.framework.TestCase;
+
+public class FirstRowDataSetTest extends TestCase {
+
+ private List<Row> rows;
+ private SelectItem[] items = new SelectItem[] { new SelectItem(new MutableColumn("foobar")) };
+ private DataSetHeader header = new SimpleDataSetHeader(items);
+ private InMemoryDataSet dataSet;
+
+ protected void setUp() throws Exception {
+ rows = new ArrayList<Row>();
+ rows.add(new DefaultRow(header, new Object[] { 1 }));
+ rows.add(new DefaultRow(header, new Object[] { 2 }));
+ rows.add(new DefaultRow(header, new Object[] { 3 }));
+ rows.add(new DefaultRow(header, new Object[] { 4 }));
+ rows.add(new DefaultRow(header, new Object[] { 5 }));
+ dataSet = new InMemoryDataSet(header, rows);
+ };
+
+ public void testHighestPossibleOffset() throws Exception {
+ FirstRowDataSet ds = new FirstRowDataSet(dataSet, 5);
+ assertTrue(ds.next());
+ assertEquals(5, ds.getRow().getValue(0));
+ assertFalse(ds.next());
+
+ ds.close();
+ }
+
+ public void testOffsetHigherThanSize() throws Exception {
+ FirstRowDataSet ds = new FirstRowDataSet(dataSet, 8);
+ assertFalse(ds.next());
+
+ ds.close();
+ }
+
+ public void testOneOffset() throws Exception {
+ FirstRowDataSet ds = new FirstRowDataSet(dataSet, 1);
+ assertTrue(ds.next());
+ assertEquals(1, ds.getRow().getValue(0));
+ ds.close();
+ }
+
+ public void testVanillaScenario() throws Exception {
+ FirstRowDataSet ds = new FirstRowDataSet(dataSet, 2);
+ 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));
+ assertTrue(ds.next());
+ assertEquals(5, ds.getRow().getValue(0));
+ assertFalse(ds.next());
+
+ ds.close();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/data/RowPublisherDataSetTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/data/RowPublisherDataSetTest.java b/core/src/test/java/org/apache/metamodel/data/RowPublisherDataSetTest.java
new file mode 100644
index 0000000..7e8b882
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/data/RowPublisherDataSetTest.java
@@ -0,0 +1,89 @@
+/**
+ * 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.data;
+
+import junit.framework.TestCase;
+
+import org.eobjects.metamodel.query.SelectItem;
+import org.eobjects.metamodel.schema.MutableColumn;
+import org.eobjects.metamodel.util.Action;
+
+public class RowPublisherDataSetTest extends TestCase {
+
+ public void testMaxSize() throws Exception {
+ SelectItem[] selectItems = new SelectItem[2];
+ selectItems[0] = new SelectItem(new MutableColumn("foos"));
+ selectItems[1] = new SelectItem(new MutableColumn("bars"));
+ DataSet ds = new RowPublisherDataSet(selectItems, 5,
+ new Action<RowPublisher>() {
+ @Override
+ public void run(RowPublisher publisher) throws Exception {
+
+ // we want to exceed the buffer size
+ int iterations = RowPublisherImpl.BUFFER_SIZE * 2;
+
+ for (int i = 0; i < iterations; i++) {
+ publisher.publish(new Object[] { "foo" + i,
+ "bar" + i });
+ }
+ }
+ });
+
+ assertTrue(ds.next());
+ assertEquals("Row[values=[foo0, bar0]]", ds.getRow().toString());
+ assertTrue(ds.next());
+ assertTrue(ds.next());
+ assertTrue(ds.next());
+ assertTrue(ds.next());
+ assertEquals("Row[values=[foo4, bar4]]", ds.getRow().toString());
+ assertFalse(ds.next());
+
+ ds.close();
+ }
+
+ public void testExceptionInAction() throws Exception {
+ SelectItem[] selectItems = new SelectItem[2];
+ selectItems[0] = new SelectItem(new MutableColumn("foos"));
+ selectItems[1] = new SelectItem(new MutableColumn("bars"));
+ DataSet ds = new RowPublisherDataSet(selectItems, 5,
+ new Action<RowPublisher>() {
+ @Override
+ public void run(RowPublisher publisher) throws Exception {
+ publisher.publish(new Object[] { "foo0", "bar0" });
+ publisher.publish(new Object[] { "foo1", "bar1" });
+ throw new IllegalStateException("foobar!");
+ }
+ });
+
+ assertTrue(ds.next());
+ assertEquals("Row[values=[foo0, bar0]]", ds.getRow().toString());
+ assertTrue(ds.next());
+ assertEquals("Row[values=[foo1, bar1]]", ds.getRow().toString());
+
+ try {
+ ds.next();
+ fail("Exception expected");
+ } catch (Exception e) {
+ assertEquals("foobar!", e.getMessage());
+ assertEquals(IllegalStateException.class, e.getClass());
+ } finally {
+ ds.close();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/data/RowTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/data/RowTest.java b/core/src/test/java/org/apache/metamodel/data/RowTest.java
new file mode 100644
index 0000000..4c8628e
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/data/RowTest.java
@@ -0,0 +1,57 @@
+/**
+ * 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.data;
+
+import org.eobjects.metamodel.MetaModelTestCase;
+import org.eobjects.metamodel.query.SelectItem;
+import org.eobjects.metamodel.schema.Schema;
+import org.eobjects.metamodel.schema.Table;
+
+public class RowTest extends MetaModelTestCase {
+
+ public void testRow() throws Exception {
+ Schema schema = getExampleSchema();
+ Table projectTable = schema.getTableByName(TABLE_PROJECT);
+ SelectItem item = new SelectItem(projectTable.getColumns()[0]);
+ SelectItem[] items = { item };
+ DataSetHeader header = new CachingDataSetHeader(items);
+ Object[] values = { "foobar" };
+ Row row = new DefaultRow(header, values);
+ assertEquals("Row[values=[foobar]]", row.toString());
+ assertEquals("foobar", row.getValue(0));
+ assertEquals("foobar", row.getValues()[0]);
+ assertEquals("foobar", row.getValue(item));
+ assertEquals(item, row.getSelectItems()[0]);
+ }
+
+ public void testGetSubSelection() throws Exception {
+ Schema schema = getExampleSchema();
+ Table projectTable = schema.getTableByName(TABLE_PROJECT);
+ SelectItem item1 = new SelectItem(projectTable.getColumns()[0]);
+ SelectItem item2 = new SelectItem(projectTable.getColumns()[0]);
+ SelectItem[] items = { item1, item2 };
+ DataSetHeader header = new CachingDataSetHeader(items);
+ Object[] values = { "foo", "bar" };
+ Row row = new DefaultRow(header, values);
+ row = row.getSubSelection(new SimpleDataSetHeader(new SelectItem[] { item1 }));
+ assertEquals(1, row.getSelectItems().length);
+ assertEquals(1, row.getValues().length);
+ assertEquals("foo", row.getValue(0));
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/data/StyleBuilderTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/data/StyleBuilderTest.java b/core/src/test/java/org/apache/metamodel/data/StyleBuilderTest.java
new file mode 100644
index 0000000..5ad7e41
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/data/StyleBuilderTest.java
@@ -0,0 +1,60 @@
+/**
+ * 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.data;
+
+import org.eobjects.metamodel.data.Style.Color;
+
+import junit.framework.TestCase;
+
+public class StyleBuilderTest extends TestCase {
+
+ public void testDefaultColors() throws Exception {
+ StyleBuilder sb = new StyleBuilder();
+
+ sb.foreground(1, 1, 1);
+ assertEquals("color: rgb(1,1,1);", sb.create().toCSS());
+
+ sb.foreground(0, 0, 0);
+ assertEquals("", sb.create().toCSS());
+
+ sb.background(0, 0, 0);
+ assertEquals("background-color: rgb(0,0,0);", sb.create().toCSS());
+
+ sb.background(255, 255, 255);
+ assertEquals("", sb.create().toCSS());
+ }
+
+ public void testCreateNoStyle() throws Exception {
+ Style style = new StyleBuilder().create();
+ assertEquals(Style.NO_STYLE, style);
+ assertSame(Style.NO_STYLE, style);
+ }
+
+ public void testCreateColor() throws Exception {
+ Color col1 = StyleBuilder.createColor("eeEE00");
+ assertEquals("Color[238,238,0]", col1.toString());
+
+ Color col2 = StyleBuilder.createColor(238, 238, 0);
+
+ // cache should ensure that these two colors are not only equal, but
+ // also the same!
+ assertEquals(col1, col2);
+ assertSame(col1, col2);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/delete/AbstractRowDeletionCallbackTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/delete/AbstractRowDeletionCallbackTest.java b/core/src/test/java/org/apache/metamodel/delete/AbstractRowDeletionCallbackTest.java
new file mode 100644
index 0000000..d37c102
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/delete/AbstractRowDeletionCallbackTest.java
@@ -0,0 +1,76 @@
+/**
+ * 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.delete;
+
+import junit.framework.TestCase;
+
+import org.eobjects.metamodel.MockUpdateableDataContext;
+import org.eobjects.metamodel.UpdateCallback;
+import org.eobjects.metamodel.UpdateScript;
+import org.eobjects.metamodel.data.DataSet;
+import org.eobjects.metamodel.schema.Table;
+
+public class AbstractRowDeletionCallbackTest extends TestCase {
+
+ public void testDelete() throws Exception {
+ final MockUpdateableDataContext dc = new MockUpdateableDataContext();
+ final Table table = dc.getDefaultSchema().getTables()[0];
+ DataSet ds = dc.query().from(table).selectCount().execute();
+ assertTrue(ds.next());
+ assertEquals("3", ds.getRow().getValue(0).toString());
+ assertFalse(ds.next());
+ ds.close();
+
+ dc.executeUpdate(new UpdateScript() {
+ @Override
+ public void run(UpdateCallback callback) {
+ callback.update(table).value("bar", "baz").execute();
+ callback.update(table).value("foo", "4").where("foo").eq("3").execute();
+ }
+ });
+
+ ds = dc.query().from(table).select(table.getColumns()).execute();
+ assertTrue(ds.next());
+ assertEquals("Row[values=[1, baz]]", ds.getRow().toString());
+ assertTrue(ds.next());
+ assertEquals("Row[values=[2, baz]]", ds.getRow().toString());
+ assertTrue(ds.next());
+ assertEquals("Row[values=[4, baz]]", ds.getRow().toString());
+ assertFalse(ds.next());
+ ds.close();
+
+ dc.executeUpdate(new UpdateScript() {
+ @Override
+ public void run(UpdateCallback callback) {
+ RowDeletionBuilder delete = callback.deleteFrom(table);
+ assertEquals("DELETE FROM schema.table", delete.toSql());
+ delete.execute();
+
+ assertEquals("DELETE FROM schema.table WHERE table.bar = 'baz'", callback.deleteFrom(table).where("bar")
+ .eq("baz").toSql());
+ }
+ });
+
+ ds = dc.query().from(table).selectCount().execute();
+ assertTrue(ds.next());
+ assertEquals("0", ds.getRow().getValue(0).toString());
+ assertFalse(ds.next());
+ ds.close();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/insert/AbstractInsertBuilderTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/insert/AbstractInsertBuilderTest.java b/core/src/test/java/org/apache/metamodel/insert/AbstractInsertBuilderTest.java
new file mode 100644
index 0000000..27249aa
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/insert/AbstractInsertBuilderTest.java
@@ -0,0 +1,92 @@
+/**
+ * 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.insert;
+
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+import org.eobjects.metamodel.MetaModelException;
+import org.eobjects.metamodel.UpdateCallback;
+import org.eobjects.metamodel.schema.Column;
+import org.eobjects.metamodel.schema.MutableColumn;
+import org.eobjects.metamodel.schema.MutableTable;
+import org.eobjects.metamodel.util.MutableRef;
+
+public class AbstractInsertBuilderTest extends TestCase {
+
+ public void testInsertValues() throws Exception {
+ final MutableRef<Boolean> executed = new MutableRef<Boolean>(false);
+ final MutableTable table = new MutableTable("foo");
+ table.addColumn(new MutableColumn("foo"));
+ table.addColumn(new MutableColumn("bar"));
+ table.addColumn(new MutableColumn("baz"));
+ RowInsertionBuilder insertBuilder = new AbstractRowInsertionBuilder<UpdateCallback>(
+ null, table) {
+ @Override
+ public void execute() throws MetaModelException {
+ assertEquals("[1, 2, 3]", Arrays.toString(getValues()));
+ executed.set(true);
+ }
+ };
+
+ assertFalse(executed.get().booleanValue());
+
+ insertBuilder.value(0, 1).value("bar", 2)
+ .value(table.getColumnByName("baz"), 3).execute();
+
+ assertTrue(executed.get());
+
+ assertEquals("Row[values=[1, 2, 3]]", insertBuilder.toRow().toString());
+
+ }
+
+ public void testIllegalArguments() throws Exception {
+ final MutableTable table = new MutableTable("foo");
+ table.addColumn(new MutableColumn("foo"));
+ RowInsertionBuilder insertBuilder = new AbstractRowInsertionBuilder<UpdateCallback>(
+ null, table) {
+ @Override
+ public void execute() throws MetaModelException {
+ }
+ };
+
+ try {
+ insertBuilder.value((Column)null, "foo");
+ fail("Exception expected");
+ } catch (IllegalArgumentException e) {
+ assertEquals("Column cannot be null", e.getMessage());
+ }
+
+ try {
+ insertBuilder.value("hmm", "foo");
+ fail("Exception expected");
+ } catch (IllegalArgumentException e) {
+ assertEquals("No such column in table: hmm, available columns are: [Column[name=foo,columnNumber=0,type=null,nullable=null,nativeType=null,columnSize=null]]", e.getMessage());
+ }
+
+ try {
+ insertBuilder.value(4, "foo");
+ fail("Exception expected");
+ } catch (ArrayIndexOutOfBoundsException e) {
+ assertTrue("4".equals(e.getMessage())
+ || "Array index out of range: 4".equals(e.getMessage()));
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/insert/SyntaxExamplesTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/insert/SyntaxExamplesTest.java b/core/src/test/java/org/apache/metamodel/insert/SyntaxExamplesTest.java
new file mode 100644
index 0000000..e2ebb50
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/insert/SyntaxExamplesTest.java
@@ -0,0 +1,44 @@
+/**
+ * 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.insert;
+
+import junit.framework.TestCase;
+
+import org.eobjects.metamodel.UpdateScript;
+import org.eobjects.metamodel.UpdateCallback;
+import org.eobjects.metamodel.UpdateableDataContext;
+import org.eobjects.metamodel.schema.Column;
+import org.eobjects.metamodel.schema.Table;
+
+public abstract class SyntaxExamplesTest extends TestCase {
+
+ private UpdateableDataContext dc;
+ private Table table;
+ private Column col;
+
+ public void testInsertMultipleRows() throws Exception {
+ dc.executeUpdate(new UpdateScript() {
+ @Override
+ public void run(UpdateCallback cb) {
+ cb.insertInto(table).value(col, "foo").execute();
+ cb.insertInto(table).value(col, "bar").execute();
+ }
+ });
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/intercept/InterceptableDataContextTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/intercept/InterceptableDataContextTest.java b/core/src/test/java/org/apache/metamodel/intercept/InterceptableDataContextTest.java
new file mode 100644
index 0000000..96fc5dc
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/intercept/InterceptableDataContextTest.java
@@ -0,0 +1,113 @@
+/**
+ * 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.intercept;
+
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+import org.eobjects.metamodel.DataContext;
+import org.eobjects.metamodel.MockUpdateableDataContext;
+import org.eobjects.metamodel.data.DataSet;
+import org.eobjects.metamodel.data.MaxRowsDataSet;
+import org.eobjects.metamodel.query.Query;
+import org.eobjects.metamodel.schema.MutableSchema;
+import org.eobjects.metamodel.schema.Schema;
+import org.eobjects.metamodel.schema.Table;
+
+public class InterceptableDataContextTest extends TestCase {
+
+ private final MockUpdateableDataContext delegateDataContext = new MockUpdateableDataContext();
+ private final Table table = delegateDataContext.getDefaultSchema()
+ .getTables()[0];
+
+ public void testInterceptSchema() throws Exception {
+ // without an interceptor
+ {
+ DataContext dc = new InterceptableDataContext(delegateDataContext);
+
+ Schema schema = dc.getDefaultSchema();
+ Schema[] schemas = dc.getSchemas();
+
+ assertEquals("schema", schema.getName());
+ assertEquals(MutableSchema.class, schema.getClass());
+ assertEquals("[information_schema, schema]",
+ Arrays.toString(dc.getSchemaNames()));
+ assertEquals(2, schemas.length);
+ assertEquals("information_schema", schemas[0].getName());
+ assertEquals("schema", schemas[1].getName());
+ }
+
+ // with an interceptor
+ {
+ DataContext dc = new InterceptableDataContext(delegateDataContext)
+ .addSchemaInterceptor(new SchemaInterceptor() {
+ @Override
+ public Schema intercept(Schema input) {
+ return new MutableSchema(input.getName() + " foo!");
+ }
+ });
+
+ Schema schema = dc.getDefaultSchema();
+ Schema[] schemas = dc.getSchemas();
+
+ assertEquals("schema foo!", schema.getName());
+ assertEquals(MutableSchema.class, schema.getClass());
+ assertEquals("[information_schema foo!, schema foo!]",
+ Arrays.toString(dc.getSchemaNames()));
+ assertEquals(2, schemas.length);
+ assertEquals("information_schema foo!", schemas[0].getName());
+ assertEquals("schema foo!", schemas[1].getName());
+ }
+ }
+
+ public void testInterceptDataSet() throws Exception {
+ DataContext dc = new InterceptableDataContext(delegateDataContext)
+ .addDataSetInterceptor(new DataSetInterceptor() {
+ @Override
+ public DataSet intercept(DataSet dataSet) {
+ return new MaxRowsDataSet(dataSet, 1);
+ }
+ });
+
+ DataSet ds = dc.query().from(table).select("foo").execute();
+ assertEquals(MaxRowsDataSet.class, ds.getClass());
+ assertEquals(1, ds.toObjectArrays().size());
+ }
+
+ public void testInterceptQuery() throws Exception {
+
+ DataContext dc = new InterceptableDataContext(delegateDataContext)
+ .addQueryInterceptor(new QueryInterceptor() {
+ @Override
+ public Query intercept(Query input) {
+ return input.select(table.getColumnByName("foo"));
+ }
+ }).addQueryInterceptor(new QueryInterceptor() {
+ @Override
+ public Query intercept(Query input) {
+ return input.select(table.getColumnByName("bar"));
+
+ }
+ });
+
+ DataSet ds = dc.executeQuery(new Query().from(table));
+ assertEquals("[table.foo, table.bar]", Arrays.toString(ds.getSelectItems()));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/intercept/InterceptorListTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/intercept/InterceptorListTest.java b/core/src/test/java/org/apache/metamodel/intercept/InterceptorListTest.java
new file mode 100644
index 0000000..04fcecd
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/intercept/InterceptorListTest.java
@@ -0,0 +1,61 @@
+/**
+ * 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.intercept;
+
+import org.eobjects.metamodel.DataContext;
+import org.eobjects.metamodel.MockUpdateableDataContext;
+import org.eobjects.metamodel.convert.ConvertedDataSetInterceptor;
+import org.eobjects.metamodel.data.DataSet;
+
+import junit.framework.TestCase;
+
+public class InterceptorListTest extends TestCase {
+
+ public void testGetInterceptorOfType() throws Exception {
+ DataContext dc = new MockUpdateableDataContext();
+ InterceptableDataContext interceptor = Interceptors.intercept(dc);
+
+ InterceptorList<DataSet> list = interceptor.getDataSetInterceptors();
+ ConvertedDataSetInterceptor convertedDataSetInterceptor = new ConvertedDataSetInterceptor();
+ list.add(convertedDataSetInterceptor);
+
+ assertSame(convertedDataSetInterceptor, list.getInterceptorOfType(DataSetInterceptor.class));
+ assertSame(convertedDataSetInterceptor, list.getInterceptorOfType(ConvertedDataSetInterceptor.class));
+
+ class NoopDataSetInterceptor implements DataSetInterceptor {
+ @Override
+ public DataSet intercept(DataSet dataSet) {
+ return dataSet;
+ }
+ };
+
+ NoopDataSetInterceptor noopDataSetInterceptor = new NoopDataSetInterceptor();
+ list.add(noopDataSetInterceptor);
+
+ assertSame(convertedDataSetInterceptor, list.getInterceptorOfType(DataSetInterceptor.class));
+ assertSame(convertedDataSetInterceptor, list.getInterceptorOfType(ConvertedDataSetInterceptor.class));
+ assertSame(noopDataSetInterceptor, list.getInterceptorOfType(NoopDataSetInterceptor.class));
+
+ list.remove(convertedDataSetInterceptor);
+
+ assertSame(noopDataSetInterceptor, list.getInterceptorOfType(DataSetInterceptor.class));
+ assertNull(list.getInterceptorOfType(ConvertedDataSetInterceptor.class));
+ assertSame(noopDataSetInterceptor, list.getInterceptorOfType(NoopDataSetInterceptor.class));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/intercept/InterceptorsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/intercept/InterceptorsTest.java b/core/src/test/java/org/apache/metamodel/intercept/InterceptorsTest.java
new file mode 100644
index 0000000..2420370
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/intercept/InterceptorsTest.java
@@ -0,0 +1,33 @@
+/**
+ * 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.intercept;
+
+import org.eobjects.metamodel.MockUpdateableDataContext;
+
+import junit.framework.TestCase;
+
+public class InterceptorsTest extends TestCase {
+
+ public void testReuseInterceptor() throws Exception {
+ MockUpdateableDataContext original = new MockUpdateableDataContext();
+ InterceptableDataContext interceptor1 = Interceptors.intercept(original);
+ InterceptableDataContext interceptor2 = Interceptors.intercept(interceptor1);
+ assertSame(interceptor1, interceptor2);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/query/DefaultCompiledQueryTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/query/DefaultCompiledQueryTest.java b/core/src/test/java/org/apache/metamodel/query/DefaultCompiledQueryTest.java
new file mode 100644
index 0000000..7d85f5c
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/query/DefaultCompiledQueryTest.java
@@ -0,0 +1,92 @@
+/**
+ * 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.query;
+
+import org.eobjects.metamodel.schema.ColumnType;
+import org.eobjects.metamodel.schema.MutableColumn;
+import org.eobjects.metamodel.schema.MutableTable;
+import org.eobjects.metamodel.schema.TableType;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class DefaultCompiledQueryTest {
+
+ Query query;
+
+ @Before
+ public void setup() {
+ query = new Query();
+
+ MutableTable datastoreTable = new MutableTable("datastore", TableType.TABLE);
+
+ MutableColumn dataSourceIdColumn = new MutableColumn("dataSourceIdColumn", ColumnType.VARCHAR);
+ MutableColumn dataSourceNameColumn = new MutableColumn("dataSourceNameColumn", ColumnType.VARCHAR);
+ MutableColumn versionColumn = new MutableColumn("versionColumn", ColumnType.INTEGER);
+ MutableColumn changeSetColumn = new MutableColumn("changeSetColumn", ColumnType.INTEGER);
+
+ SelectItem countSelectItem = new SelectItem(FunctionType.COUNT, dataSourceIdColumn);
+ SelectItem dsIdSelectItem = new SelectItem(dataSourceIdColumn).setAlias("innerDataSourceRecordId");
+ Query leftQuery = new Query();
+ leftQuery.select(dsIdSelectItem);
+ leftQuery.groupBy(dataSourceIdColumn);
+ leftQuery.having(new FilterItem(countSelectItem.toSql() + " " + OperatorType.EQUALS_TO.toSql() + " 2"));
+ leftQuery.where(dataSourceNameColumn, OperatorType.EQUALS_TO, new QueryParameter());
+ leftQuery.from(datastoreTable);
+ FromItem leftFrom = new FromItem(leftQuery);
+ leftFrom.setAlias("innerDS");
+
+ query.select(changeSetColumn);
+ query.from(leftFrom, new FromItem(datastoreTable));
+ query.where(versionColumn, OperatorType.EQUALS_TO, 2);
+ query.where(changeSetColumn, OperatorType.EQUALS_TO, new QueryParameter());
+ // Checks if max count is 2 in order to assert that this record has not
+ // been a part of any changeSets previously and not processed by GR
+ // creation in the current run.
+ query.where(new SelectItem(dsIdSelectItem, leftFrom), OperatorType.EQUALS_TO, dsIdSelectItem);
+ query.where(dataSourceNameColumn, OperatorType.EQUALS_TO, new QueryParameter());
+ }
+
+ @Test
+ public void testGetParameterLogic() {
+
+ DefaultCompiledQuery defaultCompiledQuery = new DefaultCompiledQuery(query);
+ Assert.assertEquals(3, defaultCompiledQuery.getParameters().size());
+
+ Assert.assertEquals(
+ "DefaultCompiledQuery["
+ + "SELECT changeSetColumn FROM (SELECT dataSourceIdColumn AS innerDataSourceRecordId FROM datastore WHERE dataSourceNameColumn = ? GROUP BY dataSourceIdColumn HAVING COUNT(dataSourceIdColumn) = 2) innerDS, datastore "
+ + "WHERE versionColumn = 2 AND changeSetColumn = ? AND innerDS.innerDataSourceRecordId = dataSourceIdColumn AND dataSourceNameColumn = ?]",
+ defaultCompiledQuery.toString());
+
+ defaultCompiledQuery.close();
+ }
+
+ @Test
+ public void testCloneWithParameterValues() {
+ DefaultCompiledQuery defaultCompiledQuery = new DefaultCompiledQuery(query);
+ Query resultQuery = defaultCompiledQuery.cloneWithParameterValues(new Object[] { "BE", 1, "BE" });
+ defaultCompiledQuery.close();
+
+ defaultCompiledQuery = new DefaultCompiledQuery(resultQuery);
+ Assert.assertEquals(0, defaultCompiledQuery.getParameters().size());
+ defaultCompiledQuery.close();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/query/FilterItemTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/query/FilterItemTest.java b/core/src/test/java/org/apache/metamodel/query/FilterItemTest.java
new file mode 100644
index 0000000..9ed783c
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/query/FilterItemTest.java
@@ -0,0 +1,413 @@
+/**
+ * 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.query;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.eobjects.metamodel.DataContext;
+import org.eobjects.metamodel.MetaModelException;
+import org.eobjects.metamodel.QueryPostprocessDataContext;
+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.InMemoryDataSet;
+import org.eobjects.metamodel.data.Row;
+import org.eobjects.metamodel.data.SimpleDataSetHeader;
+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;
+import org.eobjects.metamodel.schema.TableType;
+
+public class FilterItemTest extends TestCase {
+
+ public void testExpressionBasedFilter() throws Exception {
+ FilterItem filterItem = new FilterItem("foobar");
+ assertEquals("foobar", filterItem.getExpression());
+
+ try {
+ filterItem.evaluate(null);
+ fail("Exception should have been thrown");
+ } catch (Exception e) {
+ assertEquals("Expression-based filters cannot be manually evaluated", e.getMessage());
+ }
+
+ Column col1 = new MutableColumn("Col1", ColumnType.VARCHAR);
+ assertEquals("SELECT Col1 WHERE foobar", new Query().select(col1).where(filterItem).toString());
+
+ assertEquals("SELECT Col1 WHERE YEAR(Col1) = 2008", new Query().select(col1).where("YEAR(Col1) = 2008")
+ .toString());
+ }
+
+ public void testToSqlWhereItem() throws Exception {
+ MutableColumn col1 = new MutableColumn("Col1", ColumnType.VARCHAR);
+ SelectItem selectItem = new SelectItem(col1);
+ FilterItem c = new FilterItem(selectItem, OperatorType.DIFFERENT_FROM, null);
+ assertEquals("Col1 IS NOT NULL", c.toString());
+
+ try {
+ c = new FilterItem(selectItem, OperatorType.GREATER_THAN, null);
+ fail("Exception should have been thrown");
+ } catch (IllegalArgumentException e) {
+ assertEquals("Can only use EQUALS or DIFFERENT_FROM operator with null-operand", e.getMessage());
+ }
+
+ c = new FilterItem(selectItem, OperatorType.DIFFERENT_FROM, "foo");
+ assertEquals("Col1 <> 'foo'", c.toString());
+
+ c = new FilterItem(selectItem, OperatorType.DIFFERENT_FROM, "'bar'");
+
+ // this will be rewritten so it's not an issue even though it look like
+ // it needs an escape-char
+ assertEquals("Col1 <> ''bar''", c.toSql());
+
+ c = new FilterItem(selectItem, OperatorType.DIFFERENT_FROM, "foo's bar");
+ // the same applies here
+ assertEquals("Col1 <> 'foo's bar'", c.toSql());
+
+ col1.setType(ColumnType.FLOAT);
+ c = new FilterItem(selectItem, OperatorType.EQUALS_TO, 423);
+ assertEquals("Col1 = 423", c.toString());
+
+ c = new FilterItem(selectItem, OperatorType.EQUALS_TO, 423426235423.42);
+ assertEquals("Col1 = 423426235423.42", c.toString());
+
+ c = new FilterItem(selectItem, OperatorType.EQUALS_TO, true);
+ assertEquals("Col1 = 1", c.toString());
+
+ Column timeColumn = new MutableColumn("TimeCol", ColumnType.TIME);
+ selectItem = new SelectItem(timeColumn);
+ c = new FilterItem(selectItem, OperatorType.GREATER_THAN, "02:30:05.000");
+ assertEquals("TimeCol > TIME '02:30:05'", c.toString());
+
+ Column dateColumn = new MutableColumn("DateCol", ColumnType.DATE);
+ c = new FilterItem(new SelectItem(dateColumn), OperatorType.GREATER_THAN, "2000-12-31");
+ assertEquals("DateCol > DATE '2000-12-31'", c.toString());
+ }
+
+ public void testToStringTimeStamp() throws Exception {
+ Column timestampColumn = new MutableColumn("TimestampCol", ColumnType.TIMESTAMP);
+ FilterItem c = new FilterItem(new SelectItem(timestampColumn), OperatorType.LESS_THAN,
+ "2000-12-31 02:30:05.007");
+ assertEquals("TimestampCol < TIMESTAMP '2000-12-31 02:30:05'", c.toString());
+
+ c = new FilterItem(new SelectItem(timestampColumn), OperatorType.LESS_THAN, "2000-12-31 02:30:05");
+ assertEquals("TimestampCol < TIMESTAMP '2000-12-31 02:30:05'", c.toString());
+
+ Column dateColumn = new MutableColumn("DateCol", ColumnType.DATE);
+ c = new FilterItem(new SelectItem(timestampColumn), OperatorType.GREATER_THAN, new SelectItem(dateColumn));
+ assertEquals("TimestampCol > DateCol", c.toString());
+ }
+
+ public void testEvaluateStrings() throws Exception {
+ Column col1 = new MutableColumn("Col1", ColumnType.VARCHAR);
+ Column col2 = new MutableColumn("Col2", ColumnType.VARCHAR);
+ SelectItem s1 = new SelectItem(col1);
+ SelectItem s2 = new SelectItem(col2);
+ SelectItem[] selectItems = new SelectItem[] { s1, s2 };
+ SimpleDataSetHeader header = new SimpleDataSetHeader(selectItems);
+ Row row;
+ FilterItem c;
+
+ row = new DefaultRow(header, new Object[] { "foo", "bar" });
+ c = new FilterItem(s1, OperatorType.DIFFERENT_FROM, s2);
+ assertTrue(c.evaluate(row));
+
+ row = new DefaultRow(header, new Object[] { "aaa", "bbb" });
+ c = new FilterItem(s1, OperatorType.GREATER_THAN, s2);
+ assertFalse(c.evaluate(row));
+
+ c = new FilterItem(s1, OperatorType.LESS_THAN, s2);
+ assertTrue(c.evaluate(row));
+
+ row = new DefaultRow(header, new Object[] { "aaa", "aaa" });
+ c = new FilterItem(s1, OperatorType.EQUALS_TO, s2);
+ assertTrue(c.evaluate(row));
+
+ c = new FilterItem(s1, OperatorType.LIKE, s2);
+ row = new DefaultRow(header, new Object[] { "foobar", "fo%b%r" });
+ assertTrue(c.evaluate(row));
+
+ row = new DefaultRow(header, new Object[] { "foobbdbafsdfr", "fo%b%r" });
+ assertTrue(c.evaluate(row));
+ }
+
+ public void testEvaluateNull() throws Exception {
+ Column col1 = new MutableColumn("Col1", ColumnType.INTEGER);
+ Column col2 = new MutableColumn("Col2", ColumnType.DECIMAL);
+ SelectItem s1 = new SelectItem(col1);
+ SelectItem s2 = new SelectItem(col2);
+ SelectItem[] selectItems = new SelectItem[] { s1, s2 };
+ CachingDataSetHeader header = new CachingDataSetHeader(selectItems);
+
+ FilterItem c = new FilterItem(s1, OperatorType.EQUALS_TO, null);
+
+ Row row = new DefaultRow(header, new Object[] { 1, 1 });
+ assertFalse(c.evaluate(row));
+ row = new DefaultRow(header, new Object[] { null, 1 });
+ assertTrue(c.evaluate(row));
+
+ c = new FilterItem(s1, OperatorType.EQUALS_TO, 1);
+
+ row = new DefaultRow(header, new Object[] { 1, 1 });
+ assertTrue(c.evaluate(row));
+ row = new DefaultRow(header, new Object[] { null, 1 });
+ assertFalse(c.evaluate(row));
+
+ c = new FilterItem(s1, OperatorType.DIFFERENT_FROM, 5);
+
+ row = new DefaultRow(header, new Object[] { 1, 1 });
+ assertTrue(c.evaluate(row));
+ row = new DefaultRow(header, new Object[] { null, 1 });
+ assertTrue(c.evaluate(row));
+
+ c = new FilterItem(s1, OperatorType.GREATER_THAN, s2);
+
+ row = new DefaultRow(header, new Object[] { 5, 1 });
+ assertTrue(c.evaluate(row));
+ row = new DefaultRow(header, new Object[] { null, 1 });
+ assertFalse(c.evaluate(row));
+ row = new DefaultRow(header, new Object[] { 1, null });
+ assertFalse(c.evaluate(row));
+
+ c = new FilterItem(s1, OperatorType.EQUALS_TO, s2);
+ row = new DefaultRow(header, new Object[] { 1, null });
+ assertFalse(c.evaluate(row));
+ row = new DefaultRow(header, new Object[] { null, null });
+ assertTrue(c.evaluate(row));
+ }
+
+ public void testEvaluateDates() throws Exception {
+ Column col1 = new MutableColumn("Col1", ColumnType.DATE);
+ SelectItem s1 = new SelectItem(col1);
+ SelectItem[] selectItems = new SelectItem[] { s1 };
+ CachingDataSetHeader header = new CachingDataSetHeader(selectItems);
+
+ long currentTimeMillis = System.currentTimeMillis();
+ FilterItem c = new FilterItem(s1, OperatorType.LESS_THAN, new java.sql.Date(currentTimeMillis));
+
+ Row row = new DefaultRow(header, new Object[] { new java.sql.Date(currentTimeMillis) });
+ assertFalse(c.evaluate(row));
+ row = new DefaultRow(header, new Object[] { new java.sql.Date(currentTimeMillis + 10000000) });
+ assertFalse(c.evaluate(row));
+ row = new DefaultRow(header, new Object[] { new java.sql.Date(currentTimeMillis - 10000000) });
+ assertTrue(c.evaluate(row));
+ }
+
+ public void testEvaluateBooleans() throws Exception {
+ Column col1 = new MutableColumn("Col1", ColumnType.BIT);
+ SelectItem s1 = new SelectItem(col1);
+ SelectItem[] selectItems = new SelectItem[] { s1 };
+ DataSetHeader header = new SimpleDataSetHeader(selectItems);
+
+ FilterItem c = new FilterItem(s1, OperatorType.EQUALS_TO, true);
+
+ Row row = new DefaultRow(header, new Object[] { true });
+ assertTrue(c.evaluate(row));
+ row = new DefaultRow(header, new Object[] { false });
+ assertFalse(c.evaluate(row));
+
+ c = new FilterItem(s1, OperatorType.EQUALS_TO, false);
+ row = new DefaultRow(header, new Object[] { true });
+ assertFalse(c.evaluate(row));
+ row = new DefaultRow(header, new Object[] { false });
+ assertTrue(c.evaluate(row));
+
+ c = new FilterItem(s1, OperatorType.GREATER_THAN, false);
+ row = new DefaultRow(header, new Object[] { true });
+ assertTrue(c.evaluate(row));
+ row = new DefaultRow(header, new Object[] { false });
+ assertFalse(c.evaluate(row));
+ }
+
+ /**
+ * Tests that the following (general) rules apply to the object:
+ *
+ * <li>the hashcode is the same when run twice on an unaltered object</li>
+ * <li>if o1.equals(o2) then this condition must be true: o1.hashCode() ==
+ * 02.hashCode()
+ */
+ public void testEqualsAndHashCode() throws Exception {
+ Column col1 = new MutableColumn("Col1", ColumnType.BIT);
+
+ FilterItem c1 = new FilterItem(new SelectItem(col1), OperatorType.EQUALS_TO, true);
+ FilterItem c2 = new FilterItem(new SelectItem(col1), OperatorType.EQUALS_TO, true);
+ assertEquals(c1, c2);
+ assertEquals(c1.hashCode(), c2.hashCode());
+
+ c2 = new FilterItem(new SelectItem(col1), OperatorType.GREATER_THAN, true);
+ assertFalse(c1.equals(c2));
+ assertFalse(c1.hashCode() == c2.hashCode());
+
+ Column col2 = new MutableColumn("Col2", ColumnType.VARBINARY);
+ c2 = new FilterItem(new SelectItem(col2), OperatorType.EQUALS_TO, true);
+ assertFalse(c1.equals(c2));
+ assertFalse(c1.hashCode() == c2.hashCode());
+ }
+
+ public void testOrFilterItem() throws Exception {
+ Column col1 = new MutableColumn("Col1", ColumnType.VARCHAR);
+
+ SelectItem s1 = new SelectItem(col1);
+ FilterItem c1 = new FilterItem(s1, OperatorType.EQUALS_TO, "foo");
+ FilterItem c2 = new FilterItem(s1, OperatorType.EQUALS_TO, "bar");
+ FilterItem c3 = new FilterItem(s1, OperatorType.EQUALS_TO, "foobar");
+
+ FilterItem filter = new FilterItem(c1, c2, c3);
+ assertEquals("(Col1 = 'foo' OR Col1 = 'bar' OR Col1 = 'foobar')", filter.toString());
+
+ DataSetHeader header = new SimpleDataSetHeader(new SelectItem[] { s1 });
+
+ assertTrue(filter.evaluate(new DefaultRow(header, new Object[] { "foo" })));
+ assertTrue(filter.evaluate(new DefaultRow(header, new Object[] { "bar" })));
+ assertTrue(filter.evaluate(new DefaultRow(header, new Object[] { "foobar" })));
+
+ assertFalse(filter.evaluate(new DefaultRow(header, new Object[] { "foob" })));
+ }
+
+ public void testAndFilterItem() throws Exception {
+ Column col1 = new MutableColumn("Col1", ColumnType.VARCHAR);
+
+ SelectItem s1 = new SelectItem(col1);
+ FilterItem c1 = new FilterItem(s1, OperatorType.LIKE, "foo%");
+ FilterItem c2 = new FilterItem(s1, OperatorType.LIKE, "%bar");
+ FilterItem c3 = new FilterItem(s1, OperatorType.DIFFERENT_FROM, "foobar");
+
+ FilterItem filter = new FilterItem(LogicalOperator.AND, c1, c2, c3);
+ assertEquals("(Col1 LIKE 'foo%' AND Col1 LIKE '%bar' AND Col1 <> 'foobar')", filter.toString());
+
+ SelectItem[] items = new SelectItem[] { s1 };
+ CachingDataSetHeader header = new CachingDataSetHeader(items);
+ assertTrue(filter.evaluate(new DefaultRow(header, new Object[] { "foo bar" })));
+ assertTrue(filter.evaluate(new DefaultRow(header, new Object[] { "foosenbar" })));
+ assertFalse(filter.evaluate(new DefaultRow(header, new Object[] { "foo" })));
+ assertFalse(filter.evaluate(new DefaultRow(header, new Object[] { "hello world" })));
+ assertFalse(filter.evaluate(new DefaultRow(header, new Object[] { "foobar" })));
+ }
+
+ // Ticket #410
+ public void testOrFilterItemWithoutSelectingActualItmes() throws Exception {
+
+ // define the schema
+ final MutableSchema schema = new MutableSchema("s");
+ MutableTable table = new MutableTable("persons", TableType.TABLE, schema);
+ schema.addTable(table);
+ final Column col1 = new MutableColumn("name", ColumnType.VARCHAR, table, 1, true);
+ final Column col2 = new MutableColumn("role", ColumnType.VARCHAR, table, 2, true);
+ final Column col3 = new MutableColumn("column_number", ColumnType.INTEGER, table, 3, true);
+ table.addColumn(col1);
+ table.addColumn(col2);
+ table.addColumn(col3);
+
+ Query q = new Query();
+ q.select(col3);
+ q.from(col1.getTable());
+
+ SelectItem selectItem1 = new SelectItem(col1);
+ SelectItem selectItem2 = new SelectItem(col2);
+
+ FilterItem item1 = new FilterItem(selectItem1, OperatorType.EQUALS_TO, "kasper");
+ FilterItem item2 = new FilterItem(selectItem2, OperatorType.EQUALS_TO, "user");
+
+ q.where(new FilterItem(item1, item2));
+
+ assertEquals(
+ "SELECT persons.column_number FROM s.persons WHERE (persons.name = 'kasper' OR persons.role = 'user')",
+ q.toString());
+
+ DataContext dc = new QueryPostprocessDataContext() {
+
+ @Override
+ public DataSet materializeMainSchemaTable(Table table, Column[] columns, int maxRows) {
+ assertEquals(3, columns.length);
+ assertEquals("column_number", columns[0].getName());
+ assertEquals("name", columns[1].getName());
+ assertEquals("role", columns[2].getName());
+ SelectItem[] selectItems = new SelectItem[] { new SelectItem(col1), new SelectItem(col2),
+ new SelectItem(col3) };
+ DataSetHeader header = new CachingDataSetHeader(selectItems);
+ List<Row> rows = new LinkedList<Row>();
+ rows.add(new DefaultRow(header, new Object[] { "foo", "bar", 1 }));
+ rows.add(new DefaultRow(header, new Object[] { "kasper", "developer", 2 }));
+ rows.add(new DefaultRow(header, new Object[] { "admin", "admin", 3 }));
+ rows.add(new DefaultRow(header, new Object[] { "elikeon", "user", 4 }));
+ rows.add(new DefaultRow(header, new Object[] { "someuser", "user", 5 }));
+ rows.add(new DefaultRow(header, new Object[] { "hmm", "what-the", 6 }));
+
+ return new InMemoryDataSet(header, rows);
+ }
+
+ @Override
+ protected String getMainSchemaName() throws MetaModelException {
+ return "s";
+ }
+
+ @Override
+ protected Schema getMainSchema() throws MetaModelException {
+ return schema;
+ }
+ };
+
+ DataSet result = dc.executeQuery(q);
+ List<Object[]> objectArrays = result.toObjectArrays();
+ assertEquals(3, objectArrays.size());
+ assertEquals(2, objectArrays.get(0)[0]);
+ assertEquals(4, objectArrays.get(1)[0]);
+ assertEquals(5, objectArrays.get(2)[0]);
+ }
+
+ public void testInOperandSql() throws Exception {
+ SelectItem selectItem = new SelectItem(new MutableColumn("foo", ColumnType.VARCHAR, null, 1, null, null, true,
+ null, false, null));
+ Object operand = new String[] { "foo", "bar" };
+ assertEquals("foo IN ('foo' , 'bar')", new FilterItem(selectItem, OperatorType.IN, operand).toSql());
+
+ operand = Arrays.asList("foo", "bar", "baz");
+ assertEquals("foo IN ('foo' , 'bar' , 'baz')", new FilterItem(selectItem, OperatorType.IN, operand).toSql());
+
+ operand = "foo";
+ assertEquals("foo IN ('foo')", new FilterItem(selectItem, OperatorType.IN, operand).toSql());
+
+ operand = new ArrayList<Object>();
+ assertEquals("foo IN ()", new FilterItem(selectItem, OperatorType.IN, operand).toSql());
+ }
+
+ public void testInOperandEvaluate() throws Exception {
+ SelectItem selectItem = new SelectItem(new MutableColumn("foo", ColumnType.VARCHAR, null, 1, null, null, true,
+ null, false, null));
+ Object operand = new String[] { "foo", "bar" };
+
+ FilterItem filterItem = new FilterItem(selectItem, OperatorType.IN, operand);
+ SelectItem[] selectItems = new SelectItem[] { selectItem };
+ DataSetHeader header = new CachingDataSetHeader(selectItems);
+
+ assertTrue(filterItem.evaluate(new DefaultRow(header, new Object[] { "foo" })));
+ assertTrue(filterItem.evaluate(new DefaultRow(header, new Object[] { "bar" })));
+ assertFalse(filterItem.evaluate(new DefaultRow(header, new Object[] { "foobar" })));
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/query/FromClauseTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/query/FromClauseTest.java b/core/src/test/java/org/apache/metamodel/query/FromClauseTest.java
new file mode 100644
index 0000000..074a3b4
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/query/FromClauseTest.java
@@ -0,0 +1,46 @@
+/**
+ * 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.query;
+
+import org.eobjects.metamodel.MetaModelTestCase;
+import org.eobjects.metamodel.schema.Schema;
+import org.eobjects.metamodel.schema.Table;
+
+public class FromClauseTest extends MetaModelTestCase {
+
+ public void testGetItemByReference() throws Exception {
+ Schema exampleSchema = getExampleSchema();
+ Table table = exampleSchema.getTableByName(TABLE_CONTRIBUTOR);
+
+ Query query = new Query();
+ query.from(table, "foobar");
+
+ assertNull(query.getFromClause().getItemByReference("foob"));
+ assertNull(query.getFromClause().getItemByReference(TABLE_CONTRIBUTOR));
+ assertEquals("MetaModelSchema.contributor foobar", query
+ .getFromClause().getItemByReference("foobar").toString());
+
+ query = new Query();
+ query.from(table);
+ assertNull(query.getFromClause().getItemByReference("foob"));
+ assertEquals("MetaModelSchema.contributor", query.getFromClause()
+ .getItemByReference(TABLE_CONTRIBUTOR).toString());
+ assertNull(query.getFromClause().getItemByReference("foobar"));
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/query/FromItemTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/query/FromItemTest.java b/core/src/test/java/org/apache/metamodel/query/FromItemTest.java
new file mode 100644
index 0000000..fb4c09e
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/query/FromItemTest.java
@@ -0,0 +1,101 @@
+/**
+ * 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.query;
+
+import org.eobjects.metamodel.MetaModelTestCase;
+import org.eobjects.metamodel.schema.Column;
+import org.eobjects.metamodel.schema.Relationship;
+import org.eobjects.metamodel.schema.Schema;
+import org.eobjects.metamodel.schema.Table;
+
+public class FromItemTest extends MetaModelTestCase {
+
+ private Schema _schema = getExampleSchema();
+
+ public void testExpressionBased() throws Exception {
+ FromItem fromItem = new FromItem("foobar");
+ assertEquals("foobar", fromItem.toString());
+ fromItem.setAlias("f");
+ assertEquals("foobar f", fromItem.toString());
+
+ assertEquals("SELECT COUNT(*) FROM foobar", new Query().selectCount().from(
+ "foobar").toString());
+ }
+
+ public void testRelationJoinToString() throws Exception {
+ Table contributorTable = _schema.getTableByName(TABLE_CONTRIBUTOR);
+ Table roleTable = _schema.getTableByName(TABLE_ROLE);
+ Relationship[] relationships = roleTable
+ .getRelationships(contributorTable);
+ FromItem from = new FromItem(JoinType.INNER, relationships[0]);
+ assertEquals(
+ "MetaModelSchema.contributor INNER JOIN MetaModelSchema.role ON contributor.contributor_id = role.contributor_id",
+ from.toString());
+
+ from.setAlias("myJoin");
+ assertEquals(
+ "(MetaModelSchema.contributor INNER JOIN MetaModelSchema.role ON contributor.contributor_id = role.contributor_id) myJoin",
+ from.toString());
+
+ from.getLeftSide().setAlias("a");
+ assertEquals(
+ "(MetaModelSchema.contributor a INNER JOIN MetaModelSchema.role ON a.contributor_id = role.contributor_id) myJoin",
+ from.toString());
+ }
+
+ public void testSubQueryJoinToString() throws Exception {
+ Table projectTable = _schema.getTableByName(TABLE_PROJECT);
+ Table roleTable = _schema.getTableByName(TABLE_ROLE);
+
+ Column projectIdColumn = projectTable
+ .getColumnByName(COLUMN_PROJECT_PROJECT_ID);
+
+ FromItem leftSide = new FromItem(projectTable);
+ leftSide.setAlias("a");
+ SelectItem[] leftOn = new SelectItem[] { new SelectItem(projectIdColumn) };
+
+ Column[] columns = roleTable.getColumns();
+
+ Query subQuery = new Query();
+ FromItem subQueryFrom = new FromItem(roleTable);
+ subQuery.from(subQueryFrom);
+ subQuery.select(columns);
+ SelectItem subQuerySelectItem = subQuery.getSelectClause().getItems()
+ .get(1);
+ FromItem rightSide = new FromItem(subQuery);
+ rightSide.setAlias("b");
+ SelectItem[] rightOn = new SelectItem[] { subQuerySelectItem };
+ FromItem from = new FromItem(JoinType.LEFT, leftSide, rightSide,
+ leftOn, rightOn);
+
+ assertEquals(
+ "MetaModelSchema.project a LEFT JOIN (SELECT role.contributor_id, role.project_id, role.name FROM MetaModelSchema.role) b ON a.project_id = b.project_id",
+ from.toString());
+
+ subQueryFrom.setAlias("c");
+ assertEquals(
+ "MetaModelSchema.project a LEFT JOIN (SELECT c.contributor_id, c.project_id, c.name FROM MetaModelSchema.role c) b ON a.project_id = b.project_id",
+ from.toString());
+
+ subQuerySelectItem.setAlias("foobar");
+ assertEquals(
+ "MetaModelSchema.project a LEFT JOIN (SELECT c.contributor_id, c.project_id AS foobar, c.name FROM MetaModelSchema.role c) b ON a.project_id = b.foobar",
+ from.toString());
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/query/FunctionTypeTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/query/FunctionTypeTest.java b/core/src/test/java/org/apache/metamodel/query/FunctionTypeTest.java
new file mode 100644
index 0000000..1161550
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/query/FunctionTypeTest.java
@@ -0,0 +1,42 @@
+/**
+ * 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.query;
+
+import junit.framework.TestCase;
+
+public class FunctionTypeTest extends TestCase {
+
+ public void testEvaluateNumbers() throws Exception {
+ assertEquals(2.5, FunctionType.AVG.evaluate(1.5, 2, null, 3, 3.5));
+ assertEquals(10.0, FunctionType.SUM.evaluate(1.5, 2, null, 3, 3.5));
+ assertEquals(4l, FunctionType.COUNT.evaluate(1.5, 2, null, 3, 3.5));
+ assertEquals(1.5, FunctionType.MIN.evaluate(1.5, 2, null, 3, 3.5));
+ assertEquals(3.5, FunctionType.MAX.evaluate(1.5, 2, null, 3, 3.5));
+ }
+
+ public void testEvaluateStrings() throws Exception {
+ assertEquals(2.5, FunctionType.AVG.evaluate("1.5", "2", null, "3",
+ "3.5"));
+ assertEquals(10.0, FunctionType.SUM.evaluate("1.5", "2", null, "3",
+ "3.5"));
+ assertEquals(2l, FunctionType.COUNT.evaluate("foo", "BAR", null));
+ assertEquals("a", FunctionType.MIN.evaluate("abc", "a", null, "bcd"));
+ assertEquals("bcd", FunctionType.MAX.evaluate("abc", "a", null, "bcd"));
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/query/GroupByItemTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/query/GroupByItemTest.java b/core/src/test/java/org/apache/metamodel/query/GroupByItemTest.java
new file mode 100644
index 0000000..f21d650
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/query/GroupByItemTest.java
@@ -0,0 +1,42 @@
+/**
+ * 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.query;
+
+import junit.framework.TestCase;
+
+public class GroupByItemTest extends TestCase {
+
+ public void testSimpleGroupBy() throws Exception {
+ SelectItem selectItem = new SelectItem("foo", "bar");
+ GroupByItem item = new GroupByItem(selectItem);
+ assertEquals("bar", item.toString());
+ }
+
+ public void testGroupByClause() throws Exception {
+ GroupByClause clause = new GroupByClause(new Query());
+ SelectItem selectItem = new SelectItem("foo", "foo");
+ GroupByItem item = new GroupByItem(selectItem);
+ clause.addItems(item);
+ selectItem = new SelectItem("bar", "bar");
+ item = new GroupByItem(selectItem);
+ clause.addItems(item);
+
+ assertEquals(" GROUP BY foo, bar", clause.toString());
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/query/OperatorTypeTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/query/OperatorTypeTest.java b/core/src/test/java/org/apache/metamodel/query/OperatorTypeTest.java
new file mode 100644
index 0000000..2efee46
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/query/OperatorTypeTest.java
@@ -0,0 +1,34 @@
+/**
+ * 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.query;
+
+import junit.framework.TestCase;
+
+public class OperatorTypeTest extends TestCase {
+
+ public void testConvertOperatorType() throws Exception {
+ assertEquals(OperatorType.EQUALS_TO, OperatorType.convertOperatorType("="));
+ assertEquals(OperatorType.GREATER_THAN, OperatorType.convertOperatorType(">"));
+ assertEquals(OperatorType.LESS_THAN, OperatorType.convertOperatorType("<"));
+ assertEquals(OperatorType.DIFFERENT_FROM, OperatorType.convertOperatorType("<>"));
+ assertEquals(OperatorType.LIKE, OperatorType.convertOperatorType("LIKE"));
+ assertEquals(OperatorType.IN, OperatorType.convertOperatorType("IN"));
+ assertEquals(null, OperatorType.convertOperatorType("foo"));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/query/OrderByItemTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/query/OrderByItemTest.java b/core/src/test/java/org/apache/metamodel/query/OrderByItemTest.java
new file mode 100644
index 0000000..7433177
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/query/OrderByItemTest.java
@@ -0,0 +1,33 @@
+/**
+ * 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.query;
+
+import org.eobjects.metamodel.query.OrderByItem.Direction;
+import junit.framework.TestCase;
+
+public class OrderByItemTest extends TestCase {
+
+ public void testSimpleOrderBy() throws Exception {
+ SelectItem selectItem = new SelectItem("foo", "foo");
+ OrderByItem item = new OrderByItem(selectItem, Direction.DESC);
+ assertEquals("foo DESC", item.toString());
+ item = new OrderByItem(selectItem, Direction.ASC);
+ assertEquals("foo ASC", item.toString());
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/query/QueryTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/query/QueryTest.java b/core/src/test/java/org/apache/metamodel/query/QueryTest.java
new file mode 100644
index 0000000..92e43e5
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/query/QueryTest.java
@@ -0,0 +1,245 @@
+/**
+ * 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.query;
+
+import org.eobjects.metamodel.MetaModelTestCase;
+import org.eobjects.metamodel.query.OrderByItem.Direction;
+import org.eobjects.metamodel.query.builder.InitFromBuilderImpl;
+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;
+import org.eobjects.metamodel.schema.TableType;
+
+public class QueryTest extends MetaModelTestCase {
+
+ private Schema _schema = getExampleSchema();
+
+ public void testSimpleQuery() throws Exception {
+ Table contributorTable = _schema.getTableByName(TABLE_CONTRIBUTOR);
+
+ Query q = new Query();
+ q.selectCount().from(contributorTable);
+ assertEquals("SELECT COUNT(*) FROM MetaModelSchema.contributor", q.toString());
+ }
+
+ public void testCloneGroupBy() throws Exception {
+ Table table = _schema.getTableByName(TABLE_PROJECT);
+ Column column = table.getColumnByName(COLUMN_PROJECT_NAME);
+ Query q = new Query().from(table).selectCount().select(column).groupBy(column);
+ assertEquals(q.toString(), q.clone().toString());
+
+ q.having(new FilterItem(SelectItem.getCountAllItem(), OperatorType.GREATER_THAN, 20));
+ assertEquals(q.toString(), q.clone().toString());
+ }
+
+ public void testFromItemAlias() throws Exception {
+ Query q = new Query();
+ Table contributorTable = _schema.getTableByName(TABLE_CONTRIBUTOR);
+ Column nameColumn = contributorTable.getColumnByName(COLUMN_CONTRIBUTOR_NAME);
+ Column countryColumn = contributorTable.getColumnByName(COLUMN_CONTRIBUTOR_COUNTRY);
+
+ FromItem fromContributor = new FromItem(contributorTable);
+ q.from(fromContributor);
+ q.select(nameColumn, countryColumn);
+ assertEquals("SELECT contributor.name, contributor.country FROM MetaModelSchema.contributor", q.toString());
+
+ fromContributor.setAlias("c");
+
+ assertEquals("SELECT c.name, c.country FROM MetaModelSchema.contributor c", q.toString());
+
+ q.groupBy(new GroupByItem(q.getSelectClause().getSelectItem(nameColumn)));
+ q.groupBy(new GroupByItem(q.getSelectClause().getSelectItem(countryColumn)));
+ q.select(new SelectItem(FunctionType.COUNT, "*", "total"));
+ assertEquals(2, q.getGroupByClause().getItems().size());
+ assertEquals(
+ "SELECT c.name, c.country, COUNT(*) AS total FROM MetaModelSchema.contributor c GROUP BY c.name, c.country",
+ q.toString());
+
+ Column contributorIdColumn = contributorTable.getColumnByName(COLUMN_CONTRIBUTOR_CONTRIBUTOR_ID);
+ q.where(contributorIdColumn, OperatorType.EQUALS_TO, 1);
+ assertEquals(
+ "SELECT c.name, c.country, COUNT(*) AS total FROM MetaModelSchema.contributor c WHERE c.contributor_id = 1 GROUP BY c.name, c.country",
+ q.toString());
+
+ q.where(contributorIdColumn, OperatorType.DIFFERENT_FROM, q.getSelectClause().getSelectItem(nameColumn));
+ assertEquals(
+ "SELECT c.name, c.country, COUNT(*) AS total FROM MetaModelSchema.contributor c WHERE c.contributor_id = 1 AND c.contributor_id <> c.name GROUP BY c.name, c.country",
+ q.toString());
+ }
+
+ public void testAddOrderBy() throws Exception {
+ Table contributorTable = _schema.getTableByName(TABLE_CONTRIBUTOR);
+ Column nameColumn = contributorTable.getColumnByName(COLUMN_CONTRIBUTOR_NAME);
+ Column countryColumn = contributorTable.getColumnByName(COLUMN_CONTRIBUTOR_COUNTRY);
+ FromItem fromContributor = new FromItem(contributorTable);
+ fromContributor.setAlias("a");
+
+ Query q = new Query();
+ q.select(nameColumn, countryColumn).from(fromContributor).orderBy(nameColumn)
+ .orderBy(countryColumn, Direction.DESC);
+ assertEquals(2, q.getOrderByClause().getItems().size());
+ assertEquals("SELECT a.name, a.country FROM MetaModelSchema.contributor a ORDER BY a.name ASC, a.country DESC",
+ q.toString());
+ }
+
+ public void testCloneJoinAndOrderBy() throws Exception {
+ Query q1 = new Query();
+ Table contributorTable = _schema.getTableByName(TABLE_CONTRIBUTOR);
+ Table roleTable = _schema.getTableByName(TABLE_ROLE);
+ FromItem fromItem = new FromItem(JoinType.INNER, contributorTable.getRelationships(roleTable)[0]);
+ q1.from(fromItem);
+
+ Column nameColumn = contributorTable.getColumnByName(COLUMN_CONTRIBUTOR_NAME);
+ Column countryColumn = contributorTable.getColumnByName(COLUMN_CONTRIBUTOR_COUNTRY);
+ Column roleNameColumn = roleTable.getColumnByName(COLUMN_ROLE_ROLE_NAME);
+ q1.select(nameColumn, countryColumn, roleNameColumn);
+ q1.orderBy(roleNameColumn);
+ String q1string = q1.toString();
+ assertEquals(
+ "SELECT contributor.name, contributor.country, role.name FROM MetaModelSchema.contributor INNER JOIN MetaModelSchema.role ON contributor.contributor_id = role.contributor_id ORDER BY role.name ASC",
+ q1string);
+
+ Query q2 = q1.clone();
+ assertEquals(q1string, q2.toString());
+
+ q2.getSelectClause().removeItem(1);
+ assertEquals(q1string, q1.toString());
+ assertEquals(
+ "SELECT contributor.name, role.name FROM MetaModelSchema.contributor INNER JOIN MetaModelSchema.role ON contributor.contributor_id = role.contributor_id ORDER BY role.name ASC",
+ q2.toString());
+
+ FromItem sqFromItem = new FromItem(q2).setAlias("sq");
+ SelectItem sqSelectItem = new SelectItem(q2.getSelectClause().getItem(1), sqFromItem).setAlias("foo");
+ Query q3 = new Query().from(sqFromItem);
+ q3.orderBy(new OrderByItem(sqSelectItem));
+ q3.select(sqSelectItem);
+ assertEquals(
+ "SELECT sq.name AS foo FROM (SELECT contributor.name, role.name FROM MetaModelSchema.contributor INNER JOIN MetaModelSchema.role ON contributor.contributor_id = role.contributor_id ORDER BY role.name ASC) sq ORDER BY foo ASC",
+ q3.toString());
+ Query q4 = q3.clone();
+ assertEquals(
+ "SELECT sq.name AS foo FROM (SELECT contributor.name, role.name FROM MetaModelSchema.contributor INNER JOIN MetaModelSchema.role ON contributor.contributor_id = role.contributor_id ORDER BY role.name ASC) sq ORDER BY foo ASC",
+ q4.toString());
+
+ assertTrue(q3.equals(q4));
+ }
+
+ public void testDistinctEquals() throws Exception {
+ Query q = new Query();
+ SelectClause sc1 = new SelectClause(q);
+ SelectClause sc2 = new SelectClause(q);
+ assertTrue(sc1.equals(sc2));
+ sc2.setDistinct(true);
+ assertFalse(sc1.equals(sc2));
+ sc1.setDistinct(true);
+ assertTrue(sc1.equals(sc2));
+ }
+
+ public void testSetMaxRows() throws Exception {
+ assertEquals(1, new Query().setMaxRows(1).getMaxRows().intValue());
+ try {
+ new Query().setMaxRows(0);
+ fail("Exception expected");
+ } catch (IllegalArgumentException e) {
+ assertEquals("Max rows cannot be zero", e.getMessage());
+ }
+ try {
+ new Query().setMaxRows(-1);
+ fail("Exception expected");
+ } catch (IllegalArgumentException e) {
+ assertEquals("Max rows cannot be negative", e.getMessage());
+ }
+ }
+
+ public void testSetFirstRow() throws Exception {
+ assertEquals(2, new Query().setFirstRow(2).getFirstRow().intValue());
+ assertEquals(1, new Query().setFirstRow(1).getFirstRow().intValue());
+
+ try {
+ new Query().setFirstRow(0);
+ fail("Exception expected");
+ } catch (IllegalArgumentException e) {
+ assertEquals("First row cannot be negative or zero", e.getMessage());
+ }
+
+ try {
+ new Query().setFirstRow(-1);
+ fail("Exception expected");
+ } catch (IllegalArgumentException e) {
+ assertEquals("First row cannot be negative or zero", e.getMessage());
+ }
+ }
+
+ public void testEqualsAndHashCode() throws Exception {
+ MutableSchema schema = new MutableSchema("schema");
+ MutableTable table = new MutableTable("table").setSchema(schema);
+ schema.addTable(table);
+
+ Column col1 = new MutableColumn("col1").setTable(table);
+ Column col2 = new MutableColumn("col2").setTable(table);
+ Column col3 = new MutableColumn("col3").setTable(table);
+ table.addColumn(col1);
+ table.addColumn(col2);
+ table.addColumn(col3);
+
+ Query q1 = new Query().select(col1, col2).from(table).where(col3, OperatorType.EQUALS_TO, "m'jello");
+
+ Query q2 = new InitFromBuilderImpl(null).from(table).select(col1).and(col2).where(col3).eq("m'jello").toQuery();
+
+ assertEquals(q1, q2);
+ }
+
+ public void testHavingClauseReferencingFunctionAndOperand() throws Exception {
+ MutableColumn idColumn = new MutableColumn("id", ColumnType.VARCHAR);
+ SelectItem countSelectItem = new SelectItem(FunctionType.COUNT, idColumn);
+ SelectItem idSelectItem = new SelectItem(idColumn).setAlias("innerIdColumn");
+
+ Query q = new Query();
+ q.select(idSelectItem);
+ q.groupBy(idColumn);
+ q.having(new FilterItem(countSelectItem, OperatorType.EQUALS_TO, 2));
+
+ assertEquals("SELECT id AS innerIdColumn GROUP BY id HAVING COUNT(id) = 2", q.toSql());
+ }
+
+ public void testToSqlWithFullyQualifiedColumnNames() throws Exception {
+ final MutableSchema schema = new MutableSchema("sch");
+ final MutableTable table = new MutableTable("tab", TableType.TABLE, schema);
+ final MutableColumn nameColumn = new MutableColumn("name", ColumnType.VARCHAR).setTable(table);
+ final MutableColumn ageColumn = new MutableColumn("age", ColumnType.INTEGER).setTable(table);
+ schema.addTable(table);
+ table.addColumn(nameColumn);
+ table.addColumn(ageColumn);
+
+ final Query q = new Query();
+ q.select(ageColumn).selectCount();
+ q.from(table);
+ q.where(ageColumn, OperatorType.GREATER_THAN, 18);
+ q.groupBy(ageColumn);
+ q.having(FunctionType.COUNT, nameColumn, OperatorType.LESS_THAN, 100);
+ q.orderBy(ageColumn);
+
+ assertEquals("SELECT sch.tab.age, COUNT(*) FROM sch.tab WHERE sch.tab.age > 18 "
+ + "GROUP BY sch.tab.age HAVING COUNT(sch.tab.name) < 100 ORDER BY sch.tab.age ASC", q.toSql(true));
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/query/SelectClauseTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/query/SelectClauseTest.java b/core/src/test/java/org/apache/metamodel/query/SelectClauseTest.java
new file mode 100644
index 0000000..3ef125b
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/query/SelectClauseTest.java
@@ -0,0 +1,41 @@
+/**
+ * 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.query;
+
+import org.eobjects.metamodel.schema.Column;
+import org.eobjects.metamodel.schema.MutableColumn;
+import org.eobjects.metamodel.schema.MutableTable;
+import org.eobjects.metamodel.schema.Table;
+
+import junit.framework.TestCase;
+
+public class SelectClauseTest extends TestCase {
+
+ public void testDistinctAddition() throws Exception {
+ Table table = new MutableTable("foo");
+ Column col = new MutableColumn("bar").setTable(table);
+
+ Query q = new Query();
+ q.selectDistinct();
+ q.from(table);
+ q.select(col);
+
+ assertEquals("SELECT DISTINCT foo.bar FROM foo", q.toSql());
+ }
+}