add some more testing add configuration for clover plugin
Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/a789e499 Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/a789e499 Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/a789e499 Branch: refs/heads/ldp Commit: a789e4994d422a691f219543353a3c3a1ad53a66 Parents: 85a5993 Author: Sebastian Schaffert <[email protected]> Authored: Tue Apr 8 16:41:27 2014 +0200 Committer: Sebastian Schaffert <[email protected]> Committed: Tue Apr 8 16:41:27 2014 +0200 ---------------------------------------------------------------------- .../collections/BaseEquivalenceHashMapTest.java | 98 +++++++++ .../collections/BaseEquivalenceHashSetTest.java | 211 +++++++++++++++++++ .../collections/EquivalenceHashSet2Test.java | 62 ------ .../collections/EquivalenceHashSetTest.java | 138 ------------ .../collections/FastEquivalenceHashSetTest.java | 62 ++++++ .../StandardEquivalenceHashMapTest.java | 35 +++ .../StandardEquivalenceHashSetTest.java | 35 +++ parent/pom.xml | 10 + 8 files changed, 451 insertions(+), 200 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/a789e499/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/BaseEquivalenceHashMapTest.java ---------------------------------------------------------------------- diff --git a/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/BaseEquivalenceHashMapTest.java b/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/BaseEquivalenceHashMapTest.java new file mode 100644 index 0000000..29d1890 --- /dev/null +++ b/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/BaseEquivalenceHashMapTest.java @@ -0,0 +1,98 @@ +/* + * 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.apache.marmotta.commons.collections; + +import com.google.common.base.Equivalence; +import org.junit.Test; + +import java.util.Map; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +/** + * Add file description here! + * + * @author Sebastian Schaffert ([email protected]) + */ +public abstract class BaseEquivalenceHashMapTest { + + // a simple equivalence function on strings, saying they are equal if their first character is the same + Equivalence<String> equivalence = new Equivalence<String>() { + @Override + protected boolean doEquivalent(String a, String b) { + return a.charAt(0) == b.charAt(0); + } + + @Override + protected int doHash(String s) { + return s.charAt(0) * 31; + } + }; + + public abstract Map<String,String> createHashMap(Equivalence<String> equivalence); + + @Test + public void testPutGet() { + Map<String,String> map = createHashMap(equivalence); + + map.put("abc","a"); + map.put("axy","a"); + map.put("xyz","x"); + + assertEquals(2, map.size()); + assertTrue(map.containsKey("abc")); + assertTrue(map.containsKey("axy")); + assertTrue(map.containsKey("aef")); + + assertEquals("a", map.get("abc")); + assertEquals("a", map.get("aef")); + + assertTrue(map.containsValue("a")); + assertTrue(map.containsValue("x")); + } + + @Test + public void testKeySet() { + Map<String,String> map = createHashMap(equivalence); + + map.put("abc","a"); + map.put("axy","a"); + map.put("xyz","x"); + + assertThat(map.keySet(), contains(startsWith("a"), startsWith("x"))); + } + + @Test + public void testIteration() { + Map<String,String> map = createHashMap(equivalence); + + map.put("abc","a"); + map.put("axy","a"); + map.put("xyz","x"); + + int count = 0; + for(Map.Entry<String,String> e : map.entrySet()) { + assertThat(e.getKey(), anyOf(equalTo("abc"), equalTo("axy"), equalTo("xyz"))); + assertThat(e.getValue(), anyOf(equalTo("a"), equalTo("x"))); + count++; + } + assertEquals(2, count); + } + +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/a789e499/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/BaseEquivalenceHashSetTest.java ---------------------------------------------------------------------- diff --git a/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/BaseEquivalenceHashSetTest.java b/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/BaseEquivalenceHashSetTest.java new file mode 100644 index 0000000..95cba91 --- /dev/null +++ b/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/BaseEquivalenceHashSetTest.java @@ -0,0 +1,211 @@ +/* + * 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.apache.marmotta.commons.collections; + +import com.google.common.base.Equivalence; +import com.google.common.collect.Sets; +import org.junit.Test; + +import java.util.Set; + +import static org.hamcrest.Matchers.hasItemInArray; +import static org.hamcrest.Matchers.startsWith; +import static org.junit.Assert.*; + +/** + * Add file description here! + * + * @author Sebastian Schaffert ([email protected]) + */ +public abstract class BaseEquivalenceHashSetTest { + + // a simple equivalence function on strings, saying they are equal if their first character is the same + Equivalence<String> equivalence = new Equivalence<String>() { + @Override + protected boolean doEquivalent(String a, String b) { + return a.charAt(0) == b.charAt(0); + } + + @Override + protected int doHash(String s) { + return s.charAt(0) * 31; + } + }; + + public abstract Set<String> createHashSet(Equivalence<String> equivalence); + + @Test + public void testEquivalence() { + assertTrue(equivalence.equivalent("abc", "axy")); + assertFalse(equivalence.equivalent("abc", "xyz")); + + assertTrue(equivalence.hash("abc") == equivalence.hash("axy")); + assertFalse(equivalence.hash("abc") == equivalence.hash("xyz")); + } + + @Test + public void testSetContains() { + String a = "abc"; + String b = "axy"; + String c = "xyz"; + + Set<String> set = createHashSet(equivalence); + set.add(a); + + // set should now also contain b (because first character the same) + assertTrue(set.contains(b)); + + set.add(b); + + // adding b should not change the set + assertEquals(1, set.size()); + + set.add(c); + + assertEquals(2, set.size()); + + assertTrue(set.containsAll(Sets.newHashSet(a, b, c))); + } + + + @Test + public void testSetEquals() { + String a1 = "abc"; + String a2 = "axy"; + String b1 = "bcd"; + String b2 = "bxy"; + String c1 = "cde"; + + Set<String> set1 = createHashSet(equivalence); + Set<String> set2 = createHashSet(equivalence); + + // test empty sets + assertEquals(set1, set2); + + set1.add(a1); + set1.add(b1); + + set2.add(b2); + set2.add(a2); + + + assertEquals(2, set1.size()); + assertEquals(2, set2.size()); + + + // test sets with elements, insertion order different + assertEquals(set1, set2); + assertEquals(set1.hashCode(), set2.hashCode()); + + set1.add(c1); + + assertNotEquals(set1, set2); + assertNotEquals(set1.hashCode(), set2.hashCode()); + + + } + + @Test + public void testIteration() { + String a = "abc"; + String b = "axy"; + String c = "xyz"; + + Set<String> set = createHashSet(equivalence); + set.add(a); + set.add(b); + set.add(c); + + int count = 0; + for(String x : set) { + count++; + } + assertEquals(2, count); + } + + + @Test + public void testEmpty() { + Set<String> set = createHashSet(equivalence); + + assertTrue(set.isEmpty()); + + set.add("abc"); + + assertFalse(set.isEmpty()); + } + + + @Test + public void testToArray() { + String a = "abc"; + String b = "axy"; + String c = "xyz"; + + Set<String> set = createHashSet(equivalence); + set.add(a); + set.add(b); + set.add(c); + + String[] arr = new String[4]; + + arr = set.toArray(arr); + + assertEquals(2, countNotNull(arr)); + assertThat(arr, hasItemInArray(startsWith("a"))); + assertThat(arr, hasItemInArray(startsWith("x"))); + } + + + @Test + public void testRemove() { + String a = "abc"; + String b = "axy"; + String c = "xyz"; + + Set<String> set = createHashSet(equivalence); + set.add(a); + set.add(b); + set.add(c); + + assertEquals(2, set.size()); + + set.remove(a); + + assertEquals(1, set.size()); + assertTrue(set.contains(c)); + assertFalse(set.contains(b)); + + set.remove(c); + + assertEquals(0, set.size()); + assertFalse(set.contains(c)); + assertFalse(set.contains(b)); + } + + + private static <T> int countNotNull(T[] arr) { + int count = 0; + for(int i=0; i<arr.length; i++) { + if(arr[i] != null) { + count++; + } + } + return count; + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/a789e499/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/EquivalenceHashSet2Test.java ---------------------------------------------------------------------- diff --git a/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/EquivalenceHashSet2Test.java b/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/EquivalenceHashSet2Test.java deleted file mode 100644 index 185b0be..0000000 --- a/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/EquivalenceHashSet2Test.java +++ /dev/null @@ -1,62 +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.apache.marmotta.commons.collections; - -import com.google.common.base.Equivalence; -import javolution.util.FastSet; -import javolution.util.function.Equality; - -import java.util.Set; - -/** - * Add file description here! - * - * @author Sebastian Schaffert ([email protected]) - */ -public class EquivalenceHashSet2Test extends EquivalenceHashSetTest { - - @Override - public Set<String> createHashSet(final Equivalence<String> equivalence) { - return new FastSet<>(new Equality<String>() { - @Override - public int hashCodeOf(String object) { - return equivalence.hash(object); - } - - @Override - public boolean areEqual(String left, String right) { - return equivalence.equivalent(left, right); - } - - @Override - public int compare(String left, String right) { - return equivalence.hash(left) - equivalence.hash(right); - } - - @Override - public int hashCode() { - return equivalence.hashCode(); - } - - @Override - public boolean equals(Object obj) { - return obj.hashCode() == hashCode(); - } - }); - } -} http://git-wip-us.apache.org/repos/asf/marmotta/blob/a789e499/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/EquivalenceHashSetTest.java ---------------------------------------------------------------------- diff --git a/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/EquivalenceHashSetTest.java b/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/EquivalenceHashSetTest.java deleted file mode 100644 index a01813c..0000000 --- a/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/EquivalenceHashSetTest.java +++ /dev/null @@ -1,138 +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.apache.marmotta.commons.collections; - -import com.google.common.base.Equivalence; -import org.junit.Assert; -import org.junit.Test; - -import java.util.Set; - -/** - * Add file description here! - * - * @author Sebastian Schaffert ([email protected]) - */ -public class EquivalenceHashSetTest { - - // a simple equivalence function on strings, saying they are equal if their first character is the same - Equivalence<String> equivalence = new Equivalence<String>() { - @Override - protected boolean doEquivalent(String a, String b) { - return a.charAt(0) == b.charAt(0); - } - - @Override - protected int doHash(String s) { - return s.charAt(0) * 31; - } - }; - - public Set<String> createHashSet(Equivalence<String> equivalence) { - return new EquivalenceHashSet<>(equivalence); - } - - @Test - public void testEquivalence() { - Assert.assertTrue(equivalence.equivalent("abc","axy")); - Assert.assertFalse(equivalence.equivalent("abc", "xyz")); - - Assert.assertTrue(equivalence.hash("abc") == equivalence.hash("axy")); - Assert.assertFalse(equivalence.hash("abc") == equivalence.hash("xyz")); - } - - @Test - public void testSetContains() { - String a = "abc"; - String b = "axy"; - String c = "xyz"; - - Set<String> set = createHashSet(equivalence); - set.add(a); - - // set should now also contain b (because first character the same) - Assert.assertTrue(set.contains(b)); - - set.add(b); - - // adding b should not change the set - Assert.assertEquals(1, set.size()); - - set.add(c); - - Assert.assertEquals(2, set.size()); - - } - - - @Test - public void testSetEquals() { - String a1 = "abc"; - String a2 = "axy"; - String b1 = "bcd"; - String b2 = "bxy"; - String c1 = "cde"; - - Set<String> set1 = createHashSet(equivalence); - Set<String> set2 = createHashSet(equivalence); - - // test empty sets - Assert.assertEquals(set1,set2); - - set1.add(a1); - set1.add(b1); - - set2.add(b2); - set2.add(a2); - - - Assert.assertEquals(2, set1.size()); - Assert.assertEquals(2, set2.size()); - - - // test sets with elements, insertion order different - Assert.assertEquals(set1,set2); - Assert.assertEquals(set1.hashCode(), set2.hashCode()); - - set1.add(c1); - - Assert.assertNotEquals(set1,set2); - Assert.assertNotEquals(set1.hashCode(), set2.hashCode()); - - - } - - @Test - public void testIteration() { - String a = "abc"; - String b = "axy"; - String c = "xyz"; - - Set<String> set = createHashSet(equivalence); - set.add(a); - set.add(b); - set.add(c); - - int count = 0; - for(String x : set) { - count++; - } - Assert.assertEquals(2,count); - } - -} http://git-wip-us.apache.org/repos/asf/marmotta/blob/a789e499/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/FastEquivalenceHashSetTest.java ---------------------------------------------------------------------- diff --git a/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/FastEquivalenceHashSetTest.java b/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/FastEquivalenceHashSetTest.java new file mode 100644 index 0000000..232615d --- /dev/null +++ b/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/FastEquivalenceHashSetTest.java @@ -0,0 +1,62 @@ +/* + * 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.apache.marmotta.commons.collections; + +import com.google.common.base.Equivalence; +import javolution.util.FastSet; +import javolution.util.function.Equality; + +import java.util.Set; + +/** + * Tests for HashSet with custom equivalence function (FastSet implementation) + * + * @author Sebastian Schaffert ([email protected]) + */ +public class FastEquivalenceHashSetTest extends BaseEquivalenceHashSetTest { + + @Override + public Set<String> createHashSet(final Equivalence<String> equivalence) { + return new FastSet<>(new Equality<String>() { + @Override + public int hashCodeOf(String object) { + return equivalence.hash(object); + } + + @Override + public boolean areEqual(String left, String right) { + return equivalence.equivalent(left, right); + } + + @Override + public int compare(String left, String right) { + return equivalence.hash(left) - equivalence.hash(right); + } + + @Override + public int hashCode() { + return equivalence.hashCode(); + } + + @Override + public boolean equals(Object obj) { + return obj.hashCode() == hashCode(); + } + }); + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/a789e499/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/StandardEquivalenceHashMapTest.java ---------------------------------------------------------------------- diff --git a/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/StandardEquivalenceHashMapTest.java b/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/StandardEquivalenceHashMapTest.java new file mode 100644 index 0000000..ab52f3a --- /dev/null +++ b/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/StandardEquivalenceHashMapTest.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.apache.marmotta.commons.collections; + +import com.google.common.base.Equivalence; + +import java.util.Map; + +/** + * Tests for HashSet with custom equivalence function (standard implementation) + * + * @author Sebastian Schaffert ([email protected]) + */ +public class StandardEquivalenceHashMapTest extends BaseEquivalenceHashMapTest { + + @Override + public Map<String, String> createHashMap(Equivalence<String> equivalence) { + return new EquivalenceHashMap<>(equivalence); + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/a789e499/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/StandardEquivalenceHashSetTest.java ---------------------------------------------------------------------- diff --git a/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/StandardEquivalenceHashSetTest.java b/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/StandardEquivalenceHashSetTest.java new file mode 100644 index 0000000..04bf93b --- /dev/null +++ b/commons/marmotta-commons/src/test/java/org/apache/marmotta/commons/collections/StandardEquivalenceHashSetTest.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.apache.marmotta.commons.collections; + +import com.google.common.base.Equivalence; + +import java.util.Set; + +/** + * Tests for HashSet with custom equivalence function (standard implementation) + * + * @author Sebastian Schaffert ([email protected]) + */ +public class StandardEquivalenceHashSetTest extends BaseEquivalenceHashSetTest { + + public Set<String> createHashSet(Equivalence<String> equivalence) { + return new EquivalenceHashSet<>(equivalence); + } + +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/a789e499/parent/pom.xml ---------------------------------------------------------------------- diff --git a/parent/pom.xml b/parent/pom.xml index 309bc45..5bbfffc 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -146,6 +146,16 @@ </configuration> </plugin> <plugin> + <groupId>com.atlassian.maven.plugins</groupId> + <artifactId>maven-clover2-plugin</artifactId> + <version>3.3.0</version> + <configuration> + <excludes> + <exclude>**/org/apache/marmotta/maven/plugins/**</exclude> + </excludes> + </configuration> + </plugin> + <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.16</version>
