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:36 UTC
[23/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/util/BooleanComparatorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/BooleanComparatorTest.java b/core/src/test/java/org/apache/metamodel/util/BooleanComparatorTest.java
new file mode 100644
index 0000000..1c099fd
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/BooleanComparatorTest.java
@@ -0,0 +1,53 @@
+/**
+ * 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.util;
+
+import java.util.Comparator;
+
+import junit.framework.TestCase;
+
+public class BooleanComparatorTest extends TestCase {
+
+ public void testCompare() throws Exception {
+ Comparator<Object> c = BooleanComparator.getComparator();
+ assertEquals(1, c.compare(true, false));
+ assertEquals(-1, c.compare(false, true));
+ assertEquals(0, c.compare(true, true));
+ assertEquals(0, c.compare(false, false));
+
+ assertEquals(1, c.compare("true", "false"));
+ assertEquals(1, c.compare("1", "false"));
+ assertEquals(1, c.compare("true", "0"));
+ assertEquals(1, c.compare("true", "false"));
+
+ assertEquals(1, c.compare(1, 0));
+
+ assertEquals(1, c.compare(1, "false"));
+ assertEquals(1, c.compare("yes", false));
+ assertEquals(1, c.compare("y", false));
+ assertEquals(1, c.compare("TRUE", false));
+ }
+
+ public void testComparable() throws Exception {
+ Comparable<Object> comparable = BooleanComparator.getComparable(true);
+ assertEquals(1, comparable.compareTo(false));
+ assertEquals(1, comparable.compareTo(0));
+ assertEquals(1, comparable.compareTo("false"));
+ }
+}
\ 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/util/ClasspathResourceTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/ClasspathResourceTest.java b/core/src/test/java/org/apache/metamodel/util/ClasspathResourceTest.java
new file mode 100644
index 0000000..3936092
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/ClasspathResourceTest.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.util;
+
+import java.io.InputStream;
+
+import junit.framework.TestCase;
+
+public class ClasspathResourceTest extends TestCase {
+
+ public void testGetName() throws Exception {
+ ClasspathResource resource = new ClasspathResource("folder/foo");
+ assertEquals("foo", resource.getName());
+ assertTrue(resource.isExists());
+ assertTrue(resource.isReadOnly());
+
+ resource = new ClasspathResource("/folder/foo");
+ assertEquals("foo", resource.getName());
+ assertTrue(resource.isExists());
+ assertTrue(resource.isReadOnly());
+
+ String result = resource.read(new Func<InputStream, String>() {
+ @Override
+ public String eval(InputStream inputStream) {
+ return FileHelper.readInputStreamAsString(inputStream, "UTF8");
+ }
+ });
+ assertEquals("bar-baz", result);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/util/CollectionUtilsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/CollectionUtilsTest.java b/core/src/test/java/org/apache/metamodel/util/CollectionUtilsTest.java
new file mode 100644
index 0000000..d3c5ebf
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/CollectionUtilsTest.java
@@ -0,0 +1,128 @@
+/**
+ * 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.util;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+public class CollectionUtilsTest extends TestCase {
+
+ public void testArray1() throws Exception {
+ String[] result = CollectionUtils.array(new String[] { "foo", "bar" },
+ "hello", "world");
+ assertEquals("[foo, bar, hello, world]", Arrays.toString(result));
+ }
+
+ public void testArray2() throws Exception {
+ Object[] existingArray = new Object[] { 'c' };
+ Object[] result = CollectionUtils.array(existingArray, "foo", 1, "bar");
+
+ assertEquals("[c, foo, 1, bar]", Arrays.toString(result));
+ }
+
+ public void testConcat() throws Exception {
+ List<String> list1 = new ArrayList<String>();
+ list1.add("hello");
+ list1.add("hello");
+ list1.add("world");
+ List<String> list2 = new ArrayList<String>();
+ list2.add("howdy");
+ list2.add("world");
+ List<String> list3 = new ArrayList<String>();
+ list3.add("hi");
+ list3.add("world");
+
+ List<String> result = CollectionUtils.concat(true, list1, list2, list3);
+ assertEquals("[hello, world, howdy, hi]", result.toString());
+ assertEquals(4, result.size());
+
+ result = CollectionUtils.concat(false, list1, list2, list3);
+ assertEquals("[hello, hello, world, howdy, world, hi, world]",
+ result.toString());
+ assertEquals(7, result.size());
+ }
+
+ public void testMap() throws Exception {
+ List<String> strings = new ArrayList<String>();
+ strings.add("hi");
+ strings.add("world");
+
+ List<Integer> ints = CollectionUtils.map(strings,
+ new Func<String, Integer>() {
+ @Override
+ public Integer eval(String arg) {
+ return arg.length();
+ }
+ });
+ assertEquals("[2, 5]", ints.toString());
+ }
+
+ public void testFilter() throws Exception {
+ List<String> list = new ArrayList<String>();
+ list.add("foo");
+ list.add("bar");
+ list.add("3");
+ list.add("2");
+
+ list = CollectionUtils.filter(list, new Predicate<String>() {
+ @Override
+ public Boolean eval(String arg) {
+ return arg.length() > 1;
+ }
+ });
+
+ assertEquals(2, list.size());
+ assertEquals("[foo, bar]", list.toString());
+ }
+
+ public void testArrayRemove() throws Exception {
+ String[] arr = new String[] { "a", "b", "c", "d", "e" };
+ arr = CollectionUtils.arrayRemove(arr, "c");
+ assertEquals("[a, b, d, e]", Arrays.toString(arr));
+
+ arr = CollectionUtils.arrayRemove(arr, "e");
+ assertEquals("[a, b, d]", Arrays.toString(arr));
+
+ arr = CollectionUtils.arrayRemove(arr, "e");
+ assertEquals("[a, b, d]", Arrays.toString(arr));
+
+ arr = CollectionUtils.arrayRemove(arr, "a");
+ assertEquals("[b, d]", Arrays.toString(arr));
+ }
+
+ public void testToList() throws Exception {
+ assertTrue(CollectionUtils.toList(null).isEmpty());
+ assertEquals("[foo]", CollectionUtils.toList("foo").toString());
+ assertEquals("[foo, bar]",
+ CollectionUtils.toList(new String[] { "foo", "bar" })
+ .toString());
+
+ List<Integer> ints = Arrays.asList(1, 2, 3);
+ assertSame(ints, CollectionUtils.toList(ints));
+
+ assertEquals("[1, 2, 3]", CollectionUtils.toList(ints.iterator())
+ .toString());
+ assertEquals("[1, 2, 3]",
+ CollectionUtils.toList(new HashSet<Integer>(ints)).toString());
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/util/DateUtilsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/DateUtilsTest.java b/core/src/test/java/org/apache/metamodel/util/DateUtilsTest.java
new file mode 100644
index 0000000..b1bd885
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/DateUtilsTest.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.util;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import junit.framework.TestCase;
+
+public class DateUtilsTest extends TestCase {
+
+ public void testGet() throws Exception {
+ SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+ Date christmasDay = DateUtils.get(2010, Month.DECEMBER, 24);
+ assertEquals("2010-12-24 00:00:00", f.format(christmasDay));
+ assertEquals(Weekday.FRIDAY, DateUtils.getWeekday(christmasDay));
+
+ Date date2 = DateUtils.get(christmasDay, 1);
+ assertEquals("2010-12-25 00:00:00", f.format(date2));
+
+ Date date3 = DateUtils.get(christmasDay, 10);
+ assertEquals("2011-01-03 00:00:00", f.format(date3));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/util/EqualsBuilderTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/EqualsBuilderTest.java b/core/src/test/java/org/apache/metamodel/util/EqualsBuilderTest.java
new file mode 100644
index 0000000..78649a8
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/EqualsBuilderTest.java
@@ -0,0 +1,53 @@
+/**
+ * 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.util;
+
+import junit.framework.TestCase;
+
+public class EqualsBuilderTest extends TestCase {
+
+ public void testEquals() throws Exception {
+ assertTrue(EqualsBuilder.equals(null, null));
+ assertTrue(EqualsBuilder.equals("hello", "hello"));
+ assertFalse(EqualsBuilder.equals("hello", null));
+ assertFalse(EqualsBuilder.equals(null, "hello"));
+ assertFalse(EqualsBuilder.equals("world", "hello"));
+
+ MyCloneable o1 = new MyCloneable();
+ assertTrue(EqualsBuilder.equals(o1, o1));
+ MyCloneable o2 = o1.clone();
+ assertFalse(EqualsBuilder.equals(o1, o2));
+ }
+
+ static final class MyCloneable implements Cloneable {
+ @Override
+ public boolean equals(Object obj) {
+ return false;
+ }
+
+ @Override
+ public MyCloneable clone() {
+ try {
+ return (MyCloneable) super.clone();
+ } catch (CloneNotSupportedException e) {
+ throw new UnsupportedOperationException();
+ }
+ }
+ };
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/util/ExclusionPredicateTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/ExclusionPredicateTest.java b/core/src/test/java/org/apache/metamodel/util/ExclusionPredicateTest.java
new file mode 100644
index 0000000..f2d9237
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/ExclusionPredicateTest.java
@@ -0,0 +1,36 @@
+/**
+ * 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.util;
+
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+public class ExclusionPredicateTest extends TestCase {
+
+ public void testEval() throws Exception {
+ ExclusionPredicate<String> predicate = new ExclusionPredicate<String>(Arrays.asList("foo","bar","baz"));
+
+ assertFalse(predicate.eval("foo"));
+ assertFalse(predicate.eval("bar"));
+ assertFalse(predicate.eval("baz"));
+
+ assertTrue(predicate.eval("hello world"));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/util/FileHelperTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/FileHelperTest.java b/core/src/test/java/org/apache/metamodel/util/FileHelperTest.java
new file mode 100644
index 0000000..f51553f
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/FileHelperTest.java
@@ -0,0 +1,78 @@
+/**
+ * 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.util;
+
+import java.io.File;
+
+import junit.framework.TestCase;
+
+public class FileHelperTest extends TestCase {
+
+ public void testGetTempDir() throws Exception {
+ File tempDir = FileHelper.getTempDir();
+ String property = System.getProperty("java.io.tmpdir");
+ assertEquals(normalize(property), normalize(tempDir.getPath()));
+ }
+
+ private String normalize(String path) {
+ if (path == null) {
+ return null;
+ }
+ if (path.endsWith(File.separator)) {
+ path = path.substring(0, path.length() - 1);
+ }
+ return path;
+ }
+
+ public void testWriteAndRead() throws Exception {
+ File file = new File("target/tmp/FileHelperTest.testWriteAndRead.txt");
+ if (file.exists()) {
+ file.delete();
+ }
+ file.getParentFile().mkdirs();
+ assertTrue(file.createNewFile());
+ FileHelper.writeStringAsFile(file, "foo\nbar");
+ String content = FileHelper.readFileAsString(file);
+ assertEquals("foo\nbar", content);
+ assertTrue(file.delete());
+ }
+
+ public void testByteOrderMarksInputStream() throws Exception {
+ String str1 = FileHelper.readFileAsString(new File("src/test/resources/unicode-text-utf16.txt"));
+ assertEquals("hello", str1);
+
+ String str2 = FileHelper.readFileAsString(new File("src/test/resources/unicode-text-utf8.txt"));
+ assertEquals(str1, str2);
+
+ String str3 = FileHelper.readFileAsString(new File("src/test/resources/unicode-text-utf16le.txt"));
+ assertEquals(str2, str3);
+
+ String str4 = FileHelper.readFileAsString(new File("src/test/resources/unicode-text-utf16be.txt"));
+ assertEquals(str3, str4);
+ }
+
+ public void testCannotAppendAndInsertBom() throws Exception {
+ try {
+ FileHelper.getWriter(new File("foo"), "foo", true, true);
+ fail("Exception expected");
+ } catch (IllegalArgumentException e) {
+ assertEquals("Can not insert BOM into appending writer", e.getMessage());
+ }
+ }
+}
\ 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/util/FormatHelperTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/FormatHelperTest.java b/core/src/test/java/org/apache/metamodel/util/FormatHelperTest.java
new file mode 100644
index 0000000..eebe345
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/FormatHelperTest.java
@@ -0,0 +1,63 @@
+/**
+ * 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.util;
+
+import java.text.NumberFormat;
+import java.util.Arrays;
+
+import org.eobjects.metamodel.schema.ColumnType;
+
+import junit.framework.TestCase;
+
+public class FormatHelperTest extends TestCase {
+
+ public void testNumberFormat() throws Exception {
+ NumberFormat format = FormatHelper.getUiNumberFormat();
+ assertEquals("987643.21", format.format(987643.213456343));
+ assertEquals("0.22", format.format(0.218456343));
+ assertEquals("20.1", format.format(20.1));
+ }
+
+ @SuppressWarnings("unchecked")
+ public void testFormatSqlValue() throws Exception {
+ assertEquals("'foo'", FormatHelper.formatSqlValue(null, "foo"));
+ assertEquals("1", FormatHelper.formatSqlValue(null, 1));
+ assertEquals("NULL", FormatHelper.formatSqlValue(null, null));
+ assertEquals(
+ "TIMESTAMP '2011-07-24 00:00:00'",
+ FormatHelper.formatSqlValue(ColumnType.TIMESTAMP,
+ DateUtils.get(2011, Month.JULY, 24)));
+ assertEquals(
+ "DATE '2011-07-24'",
+ FormatHelper.formatSqlValue(ColumnType.DATE,
+ DateUtils.get(2011, Month.JULY, 24)));
+ assertEquals(
+ "TIME '00:00:00'",
+ FormatHelper.formatSqlValue(ColumnType.TIME,
+ DateUtils.get(2011, Month.JULY, 24)));
+ assertEquals(
+ "('foo' , 1 , 'bar' , 0.1234)",
+ FormatHelper.formatSqlValue(null,
+ Arrays.asList("foo", 1, "bar", 0.1234)));
+ assertEquals(
+ "('foo' , 1 , 'bar' , 0.1234)",
+ FormatHelper.formatSqlValue(null, new Object[] { "foo", 1,
+ "bar", 0.1234 }));
+ }
+}
\ 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/util/InMemoryResourceTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/InMemoryResourceTest.java b/core/src/test/java/org/apache/metamodel/util/InMemoryResourceTest.java
new file mode 100644
index 0000000..d6cc1e6
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/InMemoryResourceTest.java
@@ -0,0 +1,79 @@
+/**
+ * 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.util;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import junit.framework.TestCase;
+
+public class InMemoryResourceTest extends TestCase {
+
+ public void testScenario() throws Exception {
+ InMemoryResource r = new InMemoryResource("foo/bar");
+ assertEquals("bar", r.getName());
+ assertEquals(-1, r.getLastModified());
+ assertEquals(0, r.getSize());
+ assertFalse(r.isReadOnly());
+ assertTrue(r.isExists());
+
+ r.write(new Action<OutputStream>() {
+ @Override
+ public void run(OutputStream out) throws Exception {
+ out.write(1);
+ out.write(2);
+ out.write(3);
+ }
+ });
+
+ assertEquals(3, r.getSize());
+
+ r.read(new Action<InputStream>() {
+ @Override
+ public void run(InputStream in) throws Exception {
+ assertEquals(1, in.read());
+ assertEquals(2, in.read());
+ assertEquals(3, in.read());
+ assertEquals(-1, in.read());
+ }
+ });
+
+ r.append(new Action<OutputStream>() {
+ @Override
+ public void run(OutputStream out) throws Exception {
+ out.write(4);
+ out.write(5);
+ out.write(6);
+ }
+ });
+
+ r.read(new Action<InputStream>() {
+ @Override
+ public void run(InputStream in) throws Exception {
+ assertEquals(1, in.read());
+ assertEquals(2, in.read());
+ assertEquals(3, in.read());
+ assertEquals(4, in.read());
+ assertEquals(5, in.read());
+ assertEquals(6, in.read());
+ assertEquals(-1, in.read());
+ }
+ });
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/util/InclusionPredicateTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/InclusionPredicateTest.java b/core/src/test/java/org/apache/metamodel/util/InclusionPredicateTest.java
new file mode 100644
index 0000000..0225aff
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/InclusionPredicateTest.java
@@ -0,0 +1,36 @@
+/**
+ * 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.util;
+
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+public class InclusionPredicateTest extends TestCase {
+
+ public void testEval() throws Exception {
+ InclusionPredicate<String> predicate = new InclusionPredicate<String>(Arrays.asList("foo","bar","baz"));
+
+ assertTrue(predicate.eval("foo"));
+ assertTrue(predicate.eval("bar"));
+ assertTrue(predicate.eval("baz"));
+
+ assertFalse(predicate.eval("hello world"));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/util/LazyRefTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/LazyRefTest.java b/core/src/test/java/org/apache/metamodel/util/LazyRefTest.java
new file mode 100644
index 0000000..f68fb96
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/LazyRefTest.java
@@ -0,0 +1,91 @@
+/**
+ * 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.util;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import junit.framework.TestCase;
+
+public class LazyRefTest extends TestCase {
+
+ public void testRequestLoad() throws Exception {
+ LazyRef<Integer> lazyRef = new LazyRef<Integer>() {
+ private final AtomicInteger counter = new AtomicInteger();
+
+ @Override
+ protected Integer fetch() {
+ return counter.incrementAndGet();
+ }
+ };
+
+ lazyRef.requestLoad();
+ Thread.sleep(20);
+ Integer integer = lazyRef.get();
+ assertEquals(1, integer.intValue());
+ }
+
+ public void testErrorHandling() throws Exception {
+ LazyRef<Object> ref = new LazyRef<Object>() {
+ @Override
+ protected Object fetch() throws Throwable {
+ throw new Exception("foo");
+ }
+ };
+
+ assertNull(ref.get());
+ assertEquals("foo", ref.getError().getMessage());
+
+ // now with a runtime exception (retain previous behaviour in this
+ // regard)
+ ref = new LazyRef<Object>() {
+ @Override
+ protected Object fetch() throws Throwable {
+ throw new IllegalStateException("bar");
+ }
+ };
+
+ try {
+ ref.get();
+ fail("Exception expected");
+ } catch (IllegalStateException e) {
+ assertEquals("bar", e.getMessage());
+ }
+ }
+
+ public void testGet() throws Exception {
+ final AtomicInteger counter = new AtomicInteger();
+ LazyRef<String> lazyRef = new LazyRef<String>() {
+ @Override
+ protected String fetch() {
+ counter.incrementAndGet();
+ return "foo";
+ }
+ };
+
+ assertFalse(lazyRef.isFetched());
+ assertEquals(0, counter.get());
+
+ assertEquals("foo", lazyRef.get());
+ assertEquals("foo", lazyRef.get());
+ assertEquals("foo", lazyRef.get());
+
+ assertTrue(lazyRef.isFetched());
+ assertEquals(1, counter.get());
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/util/MonthTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/MonthTest.java b/core/src/test/java/org/apache/metamodel/util/MonthTest.java
new file mode 100644
index 0000000..b70de17
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/MonthTest.java
@@ -0,0 +1,38 @@
+/**
+ * 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.util;
+
+import junit.framework.TestCase;
+
+public class MonthTest extends TestCase {
+
+ public void testGetName() throws Exception {
+ assertEquals("December", Month.DECEMBER.getName());
+ }
+
+ public void testNext() throws Exception {
+ assertEquals(Month.APRIL, Month.MARCH.next());
+ assertEquals(Month.JANUARY, Month.DECEMBER.next());
+ }
+
+ public void testPrevious() throws Exception {
+ assertEquals(Month.FEBRUARY, Month.MARCH.previous());
+ assertEquals(Month.DECEMBER, Month.JANUARY.previous());
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/util/NumberComparatorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/NumberComparatorTest.java b/core/src/test/java/org/apache/metamodel/util/NumberComparatorTest.java
new file mode 100644
index 0000000..211c967
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/NumberComparatorTest.java
@@ -0,0 +1,37 @@
+/**
+ * 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.util;
+
+import java.util.Comparator;
+
+import junit.framework.TestCase;
+
+public class NumberComparatorTest extends TestCase {
+
+ public void testDoubleAndIntegerComparison() throws Exception {
+ Comparator<Object> comparator = NumberComparator.getComparator();
+ assertEquals(0, comparator.compare(1, 1.0));
+ }
+
+ public void testComparable() throws Exception {
+ Comparable<Object> comparable = NumberComparator.getComparable("125");
+ assertEquals(0, comparable.compareTo(125));
+ assertEquals(-1, comparable.compareTo(126));
+ }
+}
\ 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/util/ObjectComparatorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/ObjectComparatorTest.java b/core/src/test/java/org/apache/metamodel/util/ObjectComparatorTest.java
new file mode 100644
index 0000000..925318c
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/ObjectComparatorTest.java
@@ -0,0 +1,63 @@
+/**
+ * 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.util;
+
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.TreeSet;
+
+import junit.framework.TestCase;
+
+public class ObjectComparatorTest extends TestCase {
+
+ public void testString() throws Exception {
+ Comparator<Object> c = ObjectComparator.getComparator();
+ assertTrue(c.compare("aaaa", "bbbb") < 0);
+
+ assertTrue(c.compare("w", "y") < 0);
+ }
+
+ public void testComparable() throws Exception {
+ Comparable<Object> comparable = ObjectComparator.getComparable("aaaa");
+ assertEquals(-1, comparable.compareTo("bbbb"));
+ }
+
+ public void testNull() throws Exception {
+ Comparator<Object> comparator = ObjectComparator.getComparator();
+ assertEquals(0, comparator.compare(null, null));
+ assertEquals(1, comparator.compare("h", null));
+ assertEquals(-1, comparator.compare(null, "h"));
+
+ TreeSet<Object> set = new TreeSet<Object>(comparator);
+ set.add("Hello");
+ set.add(null);
+ set.add(null);
+ set.add(DateUtils.get(2010, Month.SEPTEMBER, 27));
+ set.add(DateUtils.get(2010, Month.SEPTEMBER, 28));
+ set.add(DateUtils.get(2010, Month.SEPTEMBER, 26));
+
+ assertEquals(5, set.size());
+ Iterator<Object> it = set.iterator();
+ assertEquals(null, it.next());
+ assertEquals("Hello", it.next());
+ assertEquals(DateUtils.get(2010, Month.SEPTEMBER, 26), it.next());
+ assertEquals(DateUtils.get(2010, Month.SEPTEMBER, 27), it.next());
+ assertEquals(DateUtils.get(2010, Month.SEPTEMBER, 28), it.next());
+ }
+}
\ 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/util/SerializableRefTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/SerializableRefTest.java b/core/src/test/java/org/apache/metamodel/util/SerializableRefTest.java
new file mode 100644
index 0000000..2d32e54
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/SerializableRefTest.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.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.StringTokenizer;
+
+import junit.framework.TestCase;
+
+public class SerializableRefTest extends TestCase {
+
+ public void testSerialize() throws Exception {
+ SerializableRef<String> ref = new SerializableRef<String>("Foobar");
+ assertNotNull(ref.get());
+
+ SerializableRef<String> copy = copy(ref);
+ assertEquals("Foobar", copy.get());
+ }
+
+ public void testDontSerialize() throws Exception {
+ SerializableRef<StringTokenizer> ref = new SerializableRef<StringTokenizer>(new StringTokenizer("foobar"));
+ assertNotNull(ref.get());
+
+ SerializableRef<StringTokenizer> copy = copy(ref);
+ assertNull(copy.get());
+ }
+
+ @SuppressWarnings("unchecked")
+ private <E> SerializableRef<E> copy(SerializableRef<E> ref) throws Exception {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ObjectOutputStream os = new ObjectOutputStream(baos);
+ os.writeObject(ref);
+ os.flush();
+ os.close();
+
+ ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+ ObjectInputStream is = new ObjectInputStream(bais);
+ Object obj = is.readObject();
+ is.close();
+ return (SerializableRef<E>) obj;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/util/SimpleRefTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/SimpleRefTest.java b/core/src/test/java/org/apache/metamodel/util/SimpleRefTest.java
new file mode 100644
index 0000000..fcc89c3
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/SimpleRefTest.java
@@ -0,0 +1,35 @@
+/**
+ * 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.util;
+
+import junit.framework.TestCase;
+
+public class SimpleRefTest extends TestCase {
+
+ public void testGet() throws Exception {
+ Ref<String> lazyRef = new ImmutableRef<String>("foo");
+
+ assertEquals("foo", lazyRef.get());
+
+ lazyRef = ImmutableRef.of("foo");
+
+ assertEquals("foo", lazyRef.get());
+ assertEquals("foo", lazyRef.get());
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/util/TimeComparatorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/TimeComparatorTest.java b/core/src/test/java/org/apache/metamodel/util/TimeComparatorTest.java
new file mode 100644
index 0000000..4b43dcb
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/TimeComparatorTest.java
@@ -0,0 +1,79 @@
+/**
+ * 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.util;
+
+import java.text.DateFormat;
+import java.util.Comparator;
+import java.util.Date;
+
+import junit.framework.TestCase;
+
+public class TimeComparatorTest extends TestCase {
+
+ public void testCompare() throws Exception {
+ Comparator<Object> c = TimeComparator.getComparator();
+ Date d1 = new Date();
+ Thread.sleep(100);
+ Date d2 = new Date();
+ assertEquals(0, c.compare(d1, d1));
+ assertEquals(-1, c.compare(d1, d2));
+ assertEquals(1, c.compare(d2, d1));
+
+ assertEquals(1, c.compare(d2, "2005-10-08"));
+ assertEquals(1, c.compare("2006-11-09", "2005-10-08"));
+ }
+
+ public void testComparable() throws Exception {
+ Comparable<Object> comparable = TimeComparator
+ .getComparable(new Date());
+ Thread.sleep(100);
+ assertEquals(-1, comparable.compareTo(new Date()));
+ }
+
+ public void testToDate() throws Exception {
+ DateFormat dateFormat = DateUtils
+ .createDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+
+ assertEquals("2008-11-04 00:00:00.000",
+ dateFormat.format(TimeComparator.toDate("08-11-04")));
+
+ assertEquals("2010-09-21 14:06:00.000",
+ dateFormat.format(TimeComparator.toDate("2010-09-21 14:06")));
+
+ assertEquals("2010-09-21 14:06:13.000",
+ dateFormat.format(TimeComparator.toDate("2010-09-21 14:06:13")));
+
+ assertEquals("2010-09-21 14:06:13.009",
+ dateFormat.format(TimeComparator
+ .toDate("2010-09-21 14:06:13.009")));
+
+ assertEquals("2000-12-31 02:30:05.100",
+ dateFormat.format(TimeComparator
+ .toDate("2000-12-31 02:30:05.100")));
+ }
+
+ public void testToDateOfDateToString() throws Exception {
+ Date date = new Date();
+ String dateString = date.toString();
+ Date convertedDate = TimeComparator.toDate(dateString);
+
+ String convertedToString = convertedDate.toString();
+ assertEquals(dateString, convertedToString);
+ }
+}
\ 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/util/ToStringComparatorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/ToStringComparatorTest.java b/core/src/test/java/org/apache/metamodel/util/ToStringComparatorTest.java
new file mode 100644
index 0000000..684c714
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/ToStringComparatorTest.java
@@ -0,0 +1,51 @@
+/**
+ * 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.util;
+
+import java.util.Comparator;
+
+import junit.framework.TestCase;
+import org.eobjects.metamodel.util.ToStringComparator;
+
+public class ToStringComparatorTest extends TestCase {
+
+ private Comparator<Object> comparator = ToStringComparator.getComparator();
+
+ public void testNotNull() throws Exception {
+ assertEquals(4, comparator.compare("foo", "bar"));
+ assertEquals(-4, comparator.compare("bar", "foo"));
+ }
+
+ public void testNull() throws Exception {
+ int result = comparator.compare(null, null);
+ assertEquals(-1, result);
+
+ result = comparator.compare(1, null);
+ assertEquals(1, result);
+
+ result = comparator.compare(null, 1);
+ assertEquals(-1, result);
+ }
+
+ public void testComparable() throws Exception {
+ Comparable<Object> comparable = ToStringComparator
+ .getComparable("aaaa");
+ assertEquals(-1, comparable.compareTo("bbbb"));
+ }
+}
\ 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/util/UrlResourceTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/UrlResourceTest.java b/core/src/test/java/org/apache/metamodel/util/UrlResourceTest.java
new file mode 100644
index 0000000..43e6523
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/UrlResourceTest.java
@@ -0,0 +1,32 @@
+/**
+ * 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.util;
+
+import junit.framework.TestCase;
+
+public class UrlResourceTest extends TestCase {
+
+ public void testGetName() throws Exception {
+ UrlResource resource = new UrlResource("http://eobjects.org/foo.txt");
+ assertEquals("foo.txt", resource.getName());
+
+ resource = new UrlResource("http://eobjects.org/");
+ assertEquals("http://eobjects.org/", resource.getName());
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/util/WeekdayTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/WeekdayTest.java b/core/src/test/java/org/apache/metamodel/util/WeekdayTest.java
new file mode 100644
index 0000000..1f92320
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/WeekdayTest.java
@@ -0,0 +1,38 @@
+/**
+ * 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.util;
+
+import junit.framework.TestCase;
+
+public class WeekdayTest extends TestCase {
+
+ public void testGetName() throws Exception {
+ assertEquals("Monday", Weekday.MONDAY.getName());
+ }
+
+ public void testNext() throws Exception {
+ assertEquals(Weekday.TUESDAY, Weekday.MONDAY.next());
+ assertEquals(Weekday.MONDAY, Weekday.SUNDAY.next());
+ }
+
+ public void testPrevious() throws Exception {
+ assertEquals(Weekday.SUNDAY, Weekday.MONDAY.previous());
+ assertEquals(Weekday.SATURDAY, Weekday.SUNDAY.previous());
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/test/java/org/apache/metamodel/util/WildcardPatternTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/metamodel/util/WildcardPatternTest.java b/core/src/test/java/org/apache/metamodel/util/WildcardPatternTest.java
new file mode 100644
index 0000000..13da97b
--- /dev/null
+++ b/core/src/test/java/org/apache/metamodel/util/WildcardPatternTest.java
@@ -0,0 +1,45 @@
+/**
+ * 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.util;
+
+import junit.framework.TestCase;
+
+public class WildcardPatternTest extends TestCase {
+
+ public void testMatches() throws Exception {
+ WildcardPattern pattern = new WildcardPattern("foo%bar", '%');
+ assertTrue(pattern.matches("foobar"));
+ assertTrue(pattern.matches("foofoobar"));
+ assertFalse(pattern.matches("foobarbar"));
+ assertFalse(pattern.matches("w00p"));
+
+ pattern = new WildcardPattern("*foo*bar", '*');
+ assertTrue(pattern.matches("foobar"));
+ assertTrue(pattern.matches("foofoobar"));
+ assertFalse(pattern.matches("foobarbar"));
+ assertFalse(pattern.matches("w00p"));
+
+ pattern = new WildcardPattern("foo%bar%", '%');
+ assertTrue(pattern.matches("foobar"));
+ assertTrue(pattern.matches("foofoobar"));
+ assertTrue(pattern.matches("foobarbar"));
+ assertFalse(pattern.matches("w00p"));
+
+ }
+}
\ 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/AbstractDataContextTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/eobjects/metamodel/AbstractDataContextTest.java b/core/src/test/java/org/eobjects/metamodel/AbstractDataContextTest.java
deleted file mode 100644
index 4320a5b..0000000
--- a/core/src/test/java/org/eobjects/metamodel/AbstractDataContextTest.java
+++ /dev/null
@@ -1,247 +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.Arrays;
-
-import junit.framework.TestCase;
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.query.Query;
-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.Schema;
-import org.eobjects.metamodel.schema.Table;
-
-public class AbstractDataContextTest extends TestCase {
-
- private class MyDataContext extends AbstractDataContext {
- @Override
- public DataSet executeQuery(Query query) throws MetaModelException {
- throw new UnsupportedOperationException();
- }
-
- @Override
- protected String[] getSchemaNamesInternal() {
- return new String[] { "barfoo", "foobar", "foo.bar" };
- }
-
- @Override
- protected String getDefaultSchemaName() {
- return "foobar";
- }
-
- @Override
- protected Schema getSchemaByNameInternal(String name) {
- if ("barfoo".equals(name) || "foobar".equals(name)
- || "foo.bar".equals(name)) {
- return createSchema(name);
- }
- throw new IllegalStateException("No such schema: " + name);
- }
-
- private Schema createSchema(String name) {
- MutableSchema schema = new MutableSchema(name);
- MutableTable t1 = new MutableTable("table");
- MutableColumn col1 = new MutableColumn("col1");
- MutableColumn col2 = new MutableColumn("col2");
- t1.addColumn(col1).addColumn(col2);
- col1.setTable(t1);
- col2.setTable(t1);
- MutableTable t2 = new MutableTable("tab.le");
- MutableColumn col3 = new MutableColumn("col3");
- MutableColumn col4 = new MutableColumn("col4");
- t2.addColumn(col3).addColumn(col4);
- col3.setTable(t2);
- col4.setTable(t2);
- schema.addTable(t1).addTable(t2);
- t1.setSchema(schema);
- t2.setSchema(schema);
- return schema;
- }
-
- }
-
-
- public void testGetColumnByQualifiedLabel() throws Exception {
- MyDataContext dc = new MyDataContext();
- Column result;
-
- result = dc.getColumnByQualifiedLabel("foobar.tab.le.col1");
- result = dc.getColumnByQualifiedLabel("blabla.tab.le.col4");
- result = dc.getColumnByQualifiedLabel("FOOBAR.TABLE.COL3");
- assertNull(result);
-
- result = dc.getColumnByQualifiedLabel("foobar.table.col1");
- assertEquals("col1", result.getName());
- assertEquals("table", result.getTable().getName());
- assertEquals("foobar", result.getTable().getSchema().getName());
-
- result = dc.getColumnByQualifiedLabel("foo.bar.table.col1");
- assertEquals("col1", result.getName());
- assertEquals("table", result.getTable().getName());
- assertEquals("foo.bar", result.getTable().getSchema().getName());
-
- result = dc.getColumnByQualifiedLabel("foobar.tab.le.col3");
- assertEquals("col3", result.getName());
- assertEquals("tab.le", result.getTable().getName());
- assertEquals("foobar", result.getTable().getSchema().getName());
-
- result = dc.getColumnByQualifiedLabel("FOO.BAR.tab.le.col3");
- assertEquals("col3", result.getName());
- assertEquals("tab.le", result.getTable().getName());
- assertEquals("foo.bar", result.getTable().getSchema().getName());
-
- result = dc.getColumnByQualifiedLabel("tab.le.col3");
- assertEquals("col3", result.getName());
- assertEquals("tab.le", result.getTable().getName());
- assertEquals("foobar", result.getTable().getSchema().getName());
- }
-
- public void testGetTableByQualfiedLabelSchemaNameInTableName() throws Exception {
- AbstractDataContext dc = new AbstractDataContext() {
- @Override
- public DataSet executeQuery(Query query) throws MetaModelException {
- return null;
- }
-
- @Override
- protected String[] getSchemaNamesInternal() {
- return new String[] {"test"};
- }
-
- @Override
- protected Schema getSchemaByNameInternal(String name) {
- MutableSchema sch = new MutableSchema("test");
- sch.addTable(new MutableTable("test_table1").setSchema(sch));
- sch.addTable(new MutableTable("test_table2").setSchema(sch));
- sch.addTable(new MutableTable("test_table3").setSchema(sch));
- return sch;
- }
-
- @Override
- protected String getDefaultSchemaName() {
- return "test";
- }
- };
-
- assertEquals("test_table1", dc.getTableByQualifiedLabel("test_table1").getName());
- assertEquals("test_table2", dc.getTableByQualifiedLabel("test_table2").getName());
- assertEquals("test_table3", dc.getTableByQualifiedLabel("test_table3").getName());
- }
-
- public void testGetTableByQualifiedLabel() throws Exception {
- MyDataContext dc = new MyDataContext();
-
- Table result;
-
- result = dc.getTableByQualifiedLabel("FOOBAR.table");
- assertEquals("table", result.getName());
- assertEquals("foobar", result.getSchema().getName());
-
- result = dc.getTableByQualifiedLabel("table");
- assertEquals("table", result.getName());
- assertEquals("foobar", result.getSchema().getName());
-
- result = dc.getTableByQualifiedLabel("foo.bar.table");
- assertEquals("table", result.getName());
- assertEquals("foo.bar", result.getSchema().getName());
-
- result = dc.getTableByQualifiedLabel("foobar.tab.le");
- assertEquals("tab.le", result.getName());
- assertEquals("foobar", result.getSchema().getName());
-
- result = dc.getTableByQualifiedLabel("foo.bar.tab.le");
- assertEquals("tab.le", result.getName());
- assertEquals("foo.bar", result.getSchema().getName());
-
- result = dc.getTableByQualifiedLabel("foo.table");
- assertNull(result);
- }
-
- public void testGetSchemas() throws Exception {
- MyDataContext dc = new MyDataContext();
- Schema[] schemas = dc.getSchemas();
- assertEquals(
- "[Schema[name=barfoo], Schema[name=foo.bar], Schema[name=foobar]]",
- Arrays.toString(schemas));
-
- dc.refreshSchemas();
- schemas = dc.getSchemas();
- assertEquals(
- "[Schema[name=barfoo], Schema[name=foo.bar], Schema[name=foobar]]",
- Arrays.toString(schemas));
- }
-
- public void testGetColumnByQualifiedLabelWithNameOverlaps()
- throws Exception {
- AbstractDataContext dc = new AbstractDataContext() {
-
- @Override
- public DataSet executeQuery(Query query) throws MetaModelException {
- throw new UnsupportedOperationException();
- }
-
- @Override
- protected String[] getSchemaNamesInternal() {
- return new String[] { "sch" };
- }
-
- @Override
- protected Schema getSchemaByNameInternal(String name) {
- MutableSchema schema = new MutableSchema("sch");
- MutableTable table1 = new MutableTable("tab");
- MutableTable table2 = new MutableTable("tab_le");
- MutableTable table3 = new MutableTable("table");
- MutableTable table4 = new MutableTable("tabl_e");
- schema.addTable(table1.addColumn(new MutableColumn("col")
- .setTable(table1)));
- schema.addTable(table2.addColumn(new MutableColumn("col")
- .setTable(table2)));
- schema.addTable(table3.addColumn(new MutableColumn("col")
- .setTable(table3)));
- schema.addTable(table4.addColumn(new MutableColumn("col")
- .setTable(table4)));
- return schema;
- }
-
- @Override
- protected String getDefaultSchemaName() {
- return "sch";
- }
- };
-
- assertEquals("tab.col", dc.getColumnByQualifiedLabel("sch.tab.col")
- .getQualifiedLabel());
- assertEquals("table.col", dc.getColumnByQualifiedLabel("sch.table.col")
- .getQualifiedLabel());
- assertEquals("tab_le.col", dc.getColumnByQualifiedLabel(
- "sch.tab_le.col").getQualifiedLabel());
- assertEquals("tabl_e.col", dc.getColumnByQualifiedLabel(
- "sch.tabl_e.col").getQualifiedLabel());
- }
-
- public void testGetColumnByQualifiedLabelCaseInsensitive() throws Exception {
- MyDataContext dc = new MyDataContext();
- Column result = dc.getColumnByQualifiedLabel("FOOBAR.TABLE.COL1");
- assertNotNull(result);
- assertEquals("col1", result.getName());
- }
-}
\ 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/CompositeDataContextTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/eobjects/metamodel/CompositeDataContextTest.java b/core/src/test/java/org/eobjects/metamodel/CompositeDataContextTest.java
deleted file mode 100644
index 0a2e0f7..0000000
--- a/core/src/test/java/org/eobjects/metamodel/CompositeDataContextTest.java
+++ /dev/null
@@ -1,117 +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.Arrays;
-import java.util.List;
-
-import junit.framework.TestCase;
-
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.query.Query;
-import org.eobjects.metamodel.schema.CompositeSchema;
-import org.eobjects.metamodel.schema.Schema;
-import org.eobjects.metamodel.schema.Table;
-
-public class CompositeDataContextTest extends TestCase {
-
- /**
- * A "typical scenario": Use a database and a CSV file to create a query
- * that joins tables from each
- */
- public void testBaseCaseCompositeQuery() throws Exception {
- DataContext dc1 = new MockDataContext("schema1", "table1", "");
- DataContext dc2 = new MockDataContext("schema2", "table2", "");
-
- DataContext composite = new CompositeDataContext(dc1, dc2);
-
- assertEquals("[schema1, schema2]",
- Arrays.toString(composite.getSchemaNames()));
- assertSame(dc1.getDefaultSchema(), composite.getDefaultSchema());
-
- DataSet ds = composite.query()
- .from(dc1.getDefaultSchema().getTables()[0]).select("foo")
- .execute();
- List<Object[]> objectArrays = ds.toObjectArrays();
- assertEquals("1", objectArrays.get(0)[0]);
- assertEquals("2", objectArrays.get(1)[0]);
- assertEquals(4, objectArrays.size());
- }
-
- public void testSchemaNameClashes() throws Exception {
- DataContext dc1 = new MockDataContext("schema", "table1", "");
- DataContext dc2 = new MockDataContext("schema", "table2", "");
-
- DataContext composite = new CompositeDataContext(dc1, dc2);
-
- assertEquals("[schema]",
- Arrays.toString(composite.getSchemaNames()));
-
- Schema schema = composite.getDefaultSchema();
- assertEquals(4, schema.getTableCount());
- assertEquals("[table1, an_empty_table, table2, an_empty_table]",
- Arrays.toString(schema.getTableNames()));
- assertTrue(schema instanceof CompositeSchema);
- }
-
- public void testJoinSameTableNames() throws Exception {
- DataContext dc1 = new MockDataContext("schema", "table", "dc1");
- DataContext dc2 = new MockDataContext("schema", "table", "dc2");
-
- DataContext composite = new CompositeDataContext(dc1, dc2);
-
- assertEquals("[schema]",
- Arrays.toString(composite.getSchemaNames()));
-
- Schema schema = composite.getDefaultSchema();
- assertEquals(4, schema.getTableCount());
- assertEquals("[table, an_empty_table, table, an_empty_table]", Arrays.toString(schema.getTableNames()));
- assertTrue(schema instanceof CompositeSchema);
- Table[] tables = schema.getTables();
- Table table1 = tables[0];
- Table table2 = tables[2];
- assertNotSame(table1, table2);
-
- Query q = composite
- .query()
- .from(table1)
- .leftJoin(table2)
- .on(table1.getColumnByName("foo"),
- table2.getColumnByName("foo"))
- .select(table1.getColumnByName("foo"),
- table2.getColumnByName("foo"),
- table1.getColumnByName("bar"),
- table2.getColumnByName("baz")).toQuery();
- assertEquals(
- "SELECT table.foo, table.foo, table.bar, table.baz "
- + "FROM schema.table LEFT JOIN schema.table ON table.foo = table.foo",
- q.toSql());
-
- DataSet ds = composite.executeQuery(q);
- assertTrue(ds.next());
- assertEquals("Row[values=[1, 1, hello, world]]", ds.getRow().toString());
- assertTrue(ds.next());
- assertEquals("Row[values=[2, 2, dc1, world]]", ds.getRow().toString());
- assertTrue(ds.next());
- assertEquals("Row[values=[3, 3, hi, dc2]]", ds.getRow().toString());
- assertTrue(ds.next());
- assertEquals("Row[values=[4, 4, yo, world]]", 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/MetaModelHelperTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/eobjects/metamodel/MetaModelHelperTest.java b/core/src/test/java/org/eobjects/metamodel/MetaModelHelperTest.java
deleted file mode 100644
index 50c1ad1..0000000
--- a/core/src/test/java/org/eobjects/metamodel/MetaModelHelperTest.java
+++ /dev/null
@@ -1,327 +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.Arrays;
-import java.util.List;
-
-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.data.SimpleDataSetHeader;
-import org.eobjects.metamodel.data.SubSelectionDataSet;
-import org.eobjects.metamodel.query.FilterItem;
-import org.eobjects.metamodel.query.FromItem;
-import org.eobjects.metamodel.query.JoinType;
-import org.eobjects.metamodel.query.OperatorType;
-import org.eobjects.metamodel.query.OrderByItem;
-import org.eobjects.metamodel.query.Query;
-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.MutableTable;
-import org.eobjects.metamodel.schema.Schema;
-import org.eobjects.metamodel.schema.Table;
-
-public class MetaModelHelperTest extends MetaModelTestCase {
-
- public void testLeftJoin() throws Exception {
- SelectItem si1 = new SelectItem(new MutableColumn("person_id", ColumnType.INTEGER));
- SelectItem si2 = new SelectItem(new MutableColumn("person_name", ColumnType.VARCHAR));
- SelectItem si3 = new SelectItem(new MutableColumn("person_age", ColumnType.INTEGER));
- SelectItem si4 = new SelectItem(new MutableColumn("person_role_id", ColumnType.INTEGER));
- SelectItem si5 = new SelectItem(new MutableColumn("role_id", ColumnType.INTEGER));
- SelectItem si6 = new SelectItem(new MutableColumn("role_name", ColumnType.VARCHAR));
- SelectItem si7 = new SelectItem(new MutableColumn("role_code", ColumnType.VARCHAR));
- List<Object[]> data1 = new ArrayList<Object[]>();
- data1.add(new Object[] { 1, "peter", 18, 1 });
- data1.add(new Object[] { 2, "tom", 19, 2 });
- data1.add(new Object[] { 3, "betty", 19, null });
- data1.add(new Object[] { 4, "barbara", 17, 3 });
- data1.add(new Object[] { 5, "susie", 18, 4 });
-
- List<Object[]> data2 = new ArrayList<Object[]>();
- data2.add(new Object[] { 1, "class president", "clpr" });
- data2.add(new Object[] { 2, "bad boy", "bb" });
- data2.add(new Object[] { 4, "trying harder", "try" });
-
- DataSet ds1 = createDataSet(new SelectItem[] { si1, si2, si3, si4 }, data1);
- DataSet ds2 = createDataSet(new SelectItem[] { si5, si6, si7 }, data2);
- FilterItem[] onConditions = new FilterItem[1];
- onConditions[0] = new FilterItem(si4, OperatorType.EQUALS_TO, si5);
-
- DataSet result = MetaModelHelper.getLeftJoin(ds1, ds2, onConditions);
- List<Object[]> objectArrays = result.toObjectArrays();
- assertEquals("[1, peter, 18, 1, 1, class president, clpr]", Arrays.toString(objectArrays.get(0)));
- assertEquals("[2, tom, 19, 2, 2, bad boy, bb]", Arrays.toString(objectArrays.get(1)));
- assertEquals("[3, betty, 19, null, null, null, null]", Arrays.toString(objectArrays.get(2)));
- assertEquals("[4, barbara, 17, 3, null, null, null]", Arrays.toString(objectArrays.get(3)));
- assertEquals("[5, susie, 18, 4, 4, trying harder, try]", Arrays.toString(objectArrays.get(4)));
- assertEquals(5, objectArrays.size());
- }
-
- public void testRightJoin() throws Exception {
- SelectItem si1 = new SelectItem(new MutableColumn("person_id", ColumnType.INTEGER));
- SelectItem si2 = new SelectItem(new MutableColumn("person_name", ColumnType.VARCHAR));
- SelectItem si3 = new SelectItem(new MutableColumn("person_age", ColumnType.INTEGER));
- SelectItem si4 = new SelectItem(new MutableColumn("person_role_id", ColumnType.INTEGER));
- SelectItem si5 = new SelectItem(new MutableColumn("role_id", ColumnType.INTEGER));
- SelectItem si6 = new SelectItem(new MutableColumn("role_name", ColumnType.VARCHAR));
- SelectItem si7 = new SelectItem(new MutableColumn("role_code", ColumnType.VARCHAR));
- List<Object[]> data1 = new ArrayList<Object[]>();
- data1.add(new Object[] { 1, "peter", 18, 1 });
- data1.add(new Object[] { 2, "tom", 19, 2 });
- data1.add(new Object[] { 3, "betty", 19, null });
- data1.add(new Object[] { 4, "barbara", 17, 3 });
-
- List<Object[]> data2 = new ArrayList<Object[]>();
- data2.add(new Object[] { 1, "class president", "clpr" });
- data2.add(new Object[] { 2, "bad boy", "bb" });
- data2.add(new Object[] { 4, "trying harder", "try" });
-
- DataSet ds1 = createDataSet(new SelectItem[] { si1, si2, si3, si4 }, data1);
- DataSet ds2 = createDataSet(new SelectItem[] { si5, si6, si7 }, data2);
- FilterItem[] onConditions = new FilterItem[1];
- onConditions[0] = new FilterItem(si4, OperatorType.EQUALS_TO, si5);
-
- DataSet result = MetaModelHelper.getRightJoin(ds1, ds2, onConditions);
- List<Object[]> objectArrays = result.toObjectArrays();
- assertEquals("[1, peter, 18, 1, 1, class president, clpr]", Arrays.toString(objectArrays.get(0)));
- assertEquals("[2, tom, 19, 2, 2, bad boy, bb]", Arrays.toString(objectArrays.get(1)));
- assertEquals("[null, null, null, null, 4, trying harder, try]", Arrays.toString(objectArrays.get(2)));
- assertEquals(3, objectArrays.size());
- }
-
- public void testSimpleCarthesianProduct() throws Exception {
- DataSet dataSet = MetaModelHelper.getCarthesianProduct(createDataSet1(), createDataSet2());
-
- assertEquals(2, dataSet.getSelectItems().length);
- assertTrue(dataSet.next());
- assertEquals("Row[values=[f, b]]", dataSet.getRow().toString());
- assertTrue(dataSet.next());
- assertEquals("Row[values=[f, a]]", dataSet.getRow().toString());
- assertTrue(dataSet.next());
- assertTrue(dataSet.next());
- assertTrue(dataSet.next());
- assertTrue(dataSet.next());
- assertTrue(dataSet.next());
- assertTrue(dataSet.next());
- assertTrue(dataSet.next());
- assertEquals("Row[values=[o, r]]", dataSet.getRow().toString());
- assertFalse(dataSet.next());
- }
-
- public void testTripleCarthesianProduct() throws Exception {
- DataSet dataSet = MetaModelHelper.getCarthesianProduct(createDataSet1(), createDataSet2(), createDataSet3());
- assertEquals(4, dataSet.getSelectItems().length);
- for (int i = 0; i < 3 * 3 * 2; i++) {
- assertTrue("Assertion failed at i=" + i, dataSet.next());
- }
- assertFalse(dataSet.next());
- }
-
- public void testTripleCarthesianProductWithWhereItems() throws Exception {
- DataSet ds1 = createDataSet1();
- DataSet ds2 = createDataSet2();
- DataSet[] dataSets = new DataSet[] { ds1, ds2, };
- FilterItem w1 = new FilterItem(ds1.getSelectItems()[0], OperatorType.EQUALS_TO, "f");
- DataSet dataSet = MetaModelHelper.getCarthesianProduct(dataSets, w1);
- assertEquals(2, dataSet.getSelectItems().length);
- for (int i = 0; i < 1 * 3; i++) {
- assertTrue("Assertion failed at i=" + i, dataSet.next());
- assertEquals("f", dataSet.getRow().getValue(0));
- }
- assertFalse(dataSet.next());
- }
-
- public void testGetCarthesianProductNoRows() throws Exception {
- DataSet dataSet = MetaModelHelper.getCarthesianProduct(createDataSet4(), createDataSet2(), createDataSet3());
- assertEquals(4, dataSet.getSelectItems().length);
- assertFalse(dataSet.next());
-
- dataSet = MetaModelHelper.getCarthesianProduct(createDataSet1(), createDataSet4(), createDataSet3());
- assertEquals(4, dataSet.getSelectItems().length);
- assertFalse(dataSet.next());
-
- dataSet = MetaModelHelper.getCarthesianProduct(createDataSet1(), createDataSet2(), createDataSet4());
- assertEquals(3, dataSet.getSelectItems().length);
- assertFalse(dataSet.next());
- }
-
- public void testGetOrdered() throws Exception {
- DataSet dataSet = createDataSet3();
- List<OrderByItem> orderByItems = new ArrayList<OrderByItem>();
- orderByItems.add(new OrderByItem(dataSet.getSelectItems()[0]));
-
- dataSet = MetaModelHelper.getOrdered(dataSet, orderByItems);
- assertTrue(dataSet.next());
- assertEquals("Row[values=[w00p, true]]", dataSet.getRow().toString());
- assertTrue(dataSet.next());
- assertEquals("Row[values=[yippie, false]]", dataSet.getRow().toString());
- assertFalse(dataSet.next());
- }
-
- private DataSet createDataSet1() {
- List<Object[]> data1 = new ArrayList<Object[]>();
- data1.add(new Object[] { "f" });
- data1.add(new Object[] { "o" });
- data1.add(new Object[] { "o" });
- DataSet dataSet1 = createDataSet(
- new SelectItem[] { new SelectItem(new MutableColumn("foo", ColumnType.VARCHAR)) }, data1);
- return dataSet1;
- }
-
- private DataSet createDataSet2() {
- List<Object[]> data2 = new ArrayList<Object[]>();
- data2.add(new Object[] { "b" });
- data2.add(new Object[] { "a" });
- data2.add(new Object[] { "r" });
- DataSet dataSet2 = createDataSet(new SelectItem[] { new SelectItem("bar", "bar") }, data2);
- return dataSet2;
- }
-
- private DataSet createDataSet3() {
- List<Object[]> data3 = new ArrayList<Object[]>();
- data3.add(new Object[] { "w00p", true });
- data3.add(new Object[] { "yippie", false });
- DataSet dataSet3 = createDataSet(new SelectItem[] { new SelectItem("expression", "e"),
- new SelectItem("webish?", "w") }, data3);
- return dataSet3;
- }
-
- private DataSet createDataSet4() {
- List<Object[]> data4 = new ArrayList<Object[]>();
- DataSet dataSet4 = createDataSet(new SelectItem[] { new SelectItem("abc", "abc") }, data4);
- return dataSet4;
- }
-
- public void testGetTables() throws Exception {
- MutableTable table1 = new MutableTable("table1");
- MutableTable table2 = new MutableTable("table2");
- MutableColumn t1column1 = new MutableColumn("t1c1", ColumnType.BIGINT);
- MutableColumn t2column1 = new MutableColumn("t2c1", ColumnType.BIGINT);
- MutableColumn t2column2 = new MutableColumn("t2c2", ColumnType.BIGINT);
- table1.addColumn(t1column1);
- t1column1.setTable(table1);
- table2.addColumn(t2column1);
- t2column1.setTable(table2);
- table2.addColumn(t2column2);
- t2column2.setTable(table2);
-
- ArrayList<Table> tableList = new ArrayList<Table>();
- tableList.add(table1);
-
- ArrayList<Column> columnList = new ArrayList<Column>();
- columnList.add(t2column1);
-
- Table[] tables = MetaModelHelper.getTables(tableList, columnList);
- assertEquals(2, tables.length);
- assertTrue(Arrays.asList(tables).contains(table1));
- assertTrue(Arrays.asList(tables).contains(table2));
- }
-
- public void testGetTableColumns() throws Exception {
- MutableTable table1 = new MutableTable("table1");
- MutableColumn column1 = new MutableColumn("c1", ColumnType.BIGINT);
- MutableColumn column2 = new MutableColumn("c2", ColumnType.BIGINT);
- MutableColumn column3 = new MutableColumn("c3", ColumnType.BIGINT);
- table1.addColumn(column1);
- column1.setTable(table1);
- table1.addColumn(column2);
- column2.setTable(table1);
- table1.addColumn(column3);
- column3.setTable(table1);
-
- ArrayList<Column> columnList = new ArrayList<Column>();
-
- Column[] columns = MetaModelHelper.getTableColumns(table1, columnList);
- assertEquals(0, columns.length);
-
- columnList.add(column1);
- columnList.add(column3);
-
- columns = MetaModelHelper.getTableColumns(table1, columnList);
- assertEquals(2, columns.length);
- assertSame(column1, columns[0]);
- assertSame(column3, columns[1]);
- }
-
- public void testGetTableFromItems() throws Exception {
- Schema schema = getExampleSchema();
- Table contributorTable = schema.getTableByName(TABLE_CONTRIBUTOR);
- Table projectTable = schema.getTableByName(TABLE_PROJECT);
- Table projectContributorTable = schema.getTableByName(TABLE_PROJECT_CONTRIBUTOR);
-
- FromItem sqFromItem = new FromItem(new Query().from(projectTable).from(projectContributorTable));
- FromItem fromItem = new FromItem(JoinType.INNER, new FromItem(contributorTable), sqFromItem, new SelectItem[0],
- new SelectItem[0]);
- Query q = new Query().from(fromItem);
-
- FromItem[] fromItems = MetaModelHelper.getTableFromItems(q);
- assertEquals(3, fromItems.length);
- assertEquals("[MetaModelSchema.contributor, MetaModelSchema.project, MetaModelSchema.project_contributor]",
- Arrays.toString(fromItems));
- }
-
- public void testGetSelectionNoRows() throws Exception {
- SelectItem item1 = new SelectItem("foo", "f");
- SelectItem item2 = new SelectItem("bar", "b");
- SelectItem item3 = new SelectItem("baz", "bz");
- List<SelectItem> selectItems1 = Arrays.asList(item1, item2, item3);
- List<SelectItem> selectItems2 = Arrays.asList(item2, item1);
-
- DataSet ds = MetaModelHelper.getSelection(selectItems2, new EmptyDataSet(selectItems1));
- assertEquals(SubSelectionDataSet.class, ds.getClass());
-
- assertEquals("[bar AS b, foo AS f]", Arrays.toString(ds.getSelectItems()));
- }
-
- public void testLeftJoinNoRowsOrSingleRow() throws Exception {
- SelectItem item1 = new SelectItem("foo", "f");
- SelectItem item2 = new SelectItem("bar", "b");
- SelectItem item3 = new SelectItem("baz", "z");
- List<SelectItem> selectItems1 = Arrays.asList(item1, item2);
- List<SelectItem> selectItems2 = Arrays.asList(item3);
-
- DataSet ds1 = new EmptyDataSet(selectItems1);
- DataSet ds2 = new EmptyDataSet(selectItems2);
-
- DataSet joinedDs = MetaModelHelper.getLeftJoin(ds1, ds2, new FilterItem[] { new FilterItem(item2,
- OperatorType.EQUALS_TO, item3) });
-
- assertEquals(SubSelectionDataSet.class, joinedDs.getClass());
- assertEquals("[foo AS f, bar AS b, baz AS z]", Arrays.toString(joinedDs.getSelectItems()));
-
- DataSetHeader header1 = new SimpleDataSetHeader(selectItems1);
- Row row = new DefaultRow(header1, new Object[] { 1, 2 }, null);
- ds1 = new InMemoryDataSet(header1, row);
-
- joinedDs = MetaModelHelper.getLeftJoin(ds1, ds2, new FilterItem[] { new FilterItem(item2,
- OperatorType.EQUALS_TO, item3) });
- assertEquals("[foo AS f, bar AS b, baz AS z]", Arrays.toString(joinedDs.getSelectItems()));
- assertTrue(joinedDs.next());
- assertEquals("Row[values=[1, 2, null]]", joinedDs.getRow().toString());
- assertFalse(joinedDs.next());
- }
-}