|
From: <gca...@us...> - 2014-08-01 17:09:45
|
Revision: 4587
http://openutils.svn.sourceforge.net/openutils/?rev=4587&view=rev
Author: gcatania
Date: 2014-08-01 17:09:42 +0000 (Fri, 01 Aug 2014)
Log Message:
-----------
BSHD-26 improve test coverage
Modified Paths:
--------------
trunk/openutils-bshd5/pom.xml
Added Paths:
-----------
trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/example/
trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/example/ExampleTreeUtilsTest.java
Modified: trunk/openutils-bshd5/pom.xml
===================================================================
--- trunk/openutils-bshd5/pom.xml 2014-08-01 17:03:08 UTC (rev 4586)
+++ trunk/openutils-bshd5/pom.xml 2014-08-01 17:09:42 UTC (rev 4587)
@@ -179,6 +179,12 @@
<version>6.8.8</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
+ <version>1.9.5</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<properties>
<slf4j.version>1.7.7</slf4j.version>
Added: trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/example/ExampleTreeUtilsTest.java
===================================================================
--- trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/example/ExampleTreeUtilsTest.java (rev 0)
+++ trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/example/ExampleTreeUtilsTest.java 2014-08-01 17:09:42 UTC (rev 4587)
@@ -0,0 +1,195 @@
+/**
+ * Copyright (c) Energeya LLC. All rights reserved. http://www.energeya.com
+ */
+package it.openutils.hibernate.example;
+
+import static it.openutils.hibernate.example.ExampleTreeUtils.addIdentifierRestriction;
+import static it.openutils.hibernate.example.ExampleTreeUtils.alreadyWalked;
+import static it.openutils.hibernate.example.ExampleTreeUtils.append;
+import static it.openutils.hibernate.example.ExampleTreeUtils.getClassMetadata;
+import static it.openutils.hibernate.example.ExampleTreeUtils.getPath;
+import static it.openutils.hibernate.example.ExampleTreeUtils.getValueFromCollection;
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+import org.apache.commons.lang3.StringUtils;
+import org.hamcrest.Matcher;
+import org.hibernate.Criteria;
+import org.hibernate.SessionFactory;
+import org.hibernate.criterion.Criterion;
+import org.hibernate.criterion.Restrictions;
+import org.hibernate.event.spi.EventSource;
+import org.hibernate.metadata.ClassMetadata;
+import org.mockito.ArgumentMatcher;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+
+/**
+ * @author gcatania
+ * @version $Id$
+ */
+public class ExampleTreeUtilsTest
+{
+
+ @Test
+ public void testAlreadyWalked()
+ {
+ Assert.assertFalse(alreadyWalked(new String[0], "prop"));
+ Assert.assertFalse(alreadyWalked(new String[]{"prop" }, "prop"));
+ Assert.assertFalse(alreadyWalked(new String[]{"prop", "childProp" }, "childProp"));
+ Assert.assertFalse(alreadyWalked(new String[]{"prop", "childProp", "grandchildProp" }, "childProp"));
+ Assert.assertFalse(alreadyWalked(new String[]{"prop", "childProp", "grandchildProp" }, "grandchildProp"));
+ Assert.assertFalse(alreadyWalked(new String[]{"prop", "childProp", "parentProp" }, "childProp"));
+ Assert.assertFalse(alreadyWalked(new String[]{"prop", "childProp", "parentProp", "childProp" }, "childProp"));
+ Assert.assertFalse(alreadyWalked(new String[]{"a", "b", "c", "b" }, "b"));
+ Assert.assertTrue(alreadyWalked(new String[]{"a", "b", "a", "b" }, "a"));
+ }
+
+ @Test
+ public void testGetValueFromColl()
+ {
+ Assert.assertNull(getValueFromCollection("prop", null));
+ Assert.assertNull(getValueFromCollection("prop", Collections.emptySet()));
+ Assert.assertNull(getValueFromCollection("prop", Collections.emptyList()));
+ Assert.assertNull(getValueFromCollection("prop", new Object[0]));
+ Assert.assertNull(getValueFromCollection("prop", new double[0]));
+
+ Object singleResult = new Object();
+ Assert.assertEquals(getValueFromCollection("prop", Collections.singleton(singleResult)), singleResult);
+ Assert.assertEquals(getValueFromCollection("prop", Collections.singletonList(singleResult)), singleResult);
+ Assert.assertEquals(getValueFromCollection("prop", new Object[]{singleResult }), singleResult);
+ Assert.assertEquals(getValueFromCollection("prop", new double[]{3d }), 3d);
+
+ try
+ {
+ getValueFromCollection("wasabi", Arrays.asList("a", "b"));
+ Assert.fail("Should have thrown exception");
+ }
+ catch (IllegalArgumentException e)
+ {
+ Assert.assertTrue(e.getMessage().contains("wasabi"), "exception message did not report property name.\n"
+ + e.getMessage());
+ }
+ try
+ {
+ getValueFromCollection("wasabi", new String[]{"a", "b" });
+ Assert.fail("Should have thrown exception");
+ }
+ catch (IllegalArgumentException e)
+ {
+ Assert.assertTrue(e.getMessage().contains("wasabi"), "exception message did not report property name.\n"
+ + e.getMessage());
+ }
+ }
+
+ @Test
+ public void testGetClassMetadata()
+ {
+ SessionFactory sess = mock(SessionFactory.class);
+ ClassMetadata clm = mock(ClassMetadata.class);
+ when(sess.getClassMetadata(String.class)).thenReturn(clm);
+
+ ClassMetadata returned = getClassMetadata("pippo", sess);
+ Assert.assertEquals(returned, clm);
+ }
+
+ @Test
+ public void testGetClassMetadataNotFound()
+ {
+ SessionFactory sess = mock(SessionFactory.class);
+ when(sess.getClassMetadata(String.class)).thenReturn(null);
+
+ try
+ {
+ getClassMetadata("pippo", sess);
+ Assert.fail("Should have thrown exception");
+ }
+ catch (IllegalStateException e)
+ {
+ Assert.assertTrue(
+ e.getMessage().contains(String.class.toString()),
+ "exception message did not report entity type.\n" + e.getMessage());
+ }
+ }
+
+ @Test
+ public void testAppend()
+ {
+ Assert.assertEquals(append(new String[0], "a"), new String[]{"a" });
+ Assert.assertEquals(append(new String[]{"a" }, "b"), new String[]{"a", "b" });
+ Assert.assertEquals(append(new String[]{null }, null), new String[]{null, null });
+ }
+
+ @Test
+ public void testGetPath()
+ {
+ Assert.assertEquals(getPath(new String[0]), StringUtils.EMPTY);
+ Assert.assertEquals(getPath(new String[]{"pippo" }), "pippo");
+ Assert.assertEquals(getPath(new String[]{"a", "b" }), "a.b");
+ Assert.assertEquals(getPath(new String[]{"a", "b", "c" }), "a.b.c");
+ }
+
+ @Test
+ public void testAddIdentifierRestrictionWhenAdded()
+ {
+ Criteria crit = mock(Criteria.class);
+ Object entity = new Object();
+ // mock EventSource because it needs to implement both Session and SessionImplementor
+ EventSource sess = mock(EventSource.class);
+ ClassMetadata clm = mock(ClassMetadata.class);
+ when(clm.getIdentifierPropertyName()).thenReturn("id");
+ when(clm.getIdentifier(entity, sess)).thenReturn(1);
+
+ boolean added = addIdentifierRestriction(crit, entity, clm, sess);
+ Assert.assertTrue(added, "identifier restriction not added");
+
+ Matcher<Criterion> hasSameToStringAsIdEqualsOneRestriction = new ArgumentMatcher<Criterion>()
+ {
+
+ @Override
+ public boolean matches(Object argument)
+ {
+ // all this mess because Restriction does not support equals
+ return argument != null && Restrictions.idEq(1).toString().equals(argument.toString());
+ }
+
+ };
+ verify(crit).add(argThat(hasSameToStringAsIdEqualsOneRestriction));
+ }
+
+ @Test
+ public void testAddIdentifierRestrictionWhenNotAdded1()
+ {
+ Criteria crit = mock(Criteria.class);
+ Object entity = new Object();
+ // mock EventSource because it needs to implement both Session and SessionImplementor
+ EventSource sess = mock(EventSource.class);
+ ClassMetadata clm = mock(ClassMetadata.class);
+ when(clm.getIdentifierPropertyName()).thenReturn(null);
+
+ boolean added = addIdentifierRestriction(crit, entity, clm, sess);
+ Assert.assertFalse(added, "identifier restriction added");
+ }
+
+ @Test
+ public void testAddIdentifierRestrictionWhenNotAdded2()
+ {
+ Criteria crit = mock(Criteria.class);
+ Object entity = new Object();
+ // mock EventSource because it needs to implement both Session and SessionImplementor
+ EventSource sess = mock(EventSource.class);
+ ClassMetadata clm = mock(ClassMetadata.class);
+ when(clm.getIdentifierPropertyName()).thenReturn("id");
+ when(clm.getIdentifier(entity, sess)).thenReturn(null);
+
+ boolean added = addIdentifierRestriction(crit, entity, clm, sess);
+ Assert.assertFalse(added, "identifier restriction added");
+ }
+
+}
Property changes on: trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/example/ExampleTreeUtilsTest.java
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Author Date Id Revision
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|