[jbpm-commits] JBoss JBPM SVN: r5655 - in jbpm4/trunk/modules/test-base/src/main/java/org/jbpm/test: assertion and 1 other directory.
do-not-reply at jboss.org
do-not-reply at jboss.org
Thu Sep 17 08:08:54 EDT 2009
Author: jbarrez
Date: 2009-09-17 08:08:53 -0400 (Thu, 17 Sep 2009)
New Revision: 5655
Added:
jbpm4/trunk/modules/test-base/src/main/java/org/jbpm/test/assertion/
jbpm4/trunk/modules/test-base/src/main/java/org/jbpm/test/assertion/CollectionAssertions.java
jbpm4/trunk/modules/test-base/src/main/java/org/jbpm/test/assertion/QueryAssertions.java
Log:
Unit test helper classes
Added: jbpm4/trunk/modules/test-base/src/main/java/org/jbpm/test/assertion/CollectionAssertions.java
===================================================================
--- jbpm4/trunk/modules/test-base/src/main/java/org/jbpm/test/assertion/CollectionAssertions.java (rev 0)
+++ jbpm4/trunk/modules/test-base/src/main/java/org/jbpm/test/assertion/CollectionAssertions.java 2009-09-17 12:08:53 UTC (rev 5655)
@@ -0,0 +1,61 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+/**
+ *
+ */
+package org.jbpm.test.assertion;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import junit.framework.Assert;
+
+/**
+ * Utility class with assertions for collections.
+ *
+ * @author Joram Barrez
+ */
+public class CollectionAssertions {
+
+ public static <T> void assertElementsEqual(Collection<T> collection1, Collection<T> collection2) {
+
+ Assert.assertTrue( (collection1 == null && collection2 == null)
+ || (collection1 != null && collection2 != null) );
+
+ if (collection1 != null && collection2 != null) {
+
+ Assert.assertEquals(collection1.size(), collection2.size());
+
+ Iterator<T> it1 = collection1.iterator();
+ Iterator<T> it2 = collection2.iterator();
+
+ while (it1.hasNext()) {
+ T t1 = it1.next();
+ T t2 = it2.next();
+ Assert.assertEquals(t1, t2);
+ }
+
+ }
+
+ }
+
+}
Added: jbpm4/trunk/modules/test-base/src/main/java/org/jbpm/test/assertion/QueryAssertions.java
===================================================================
--- jbpm4/trunk/modules/test-base/src/main/java/org/jbpm/test/assertion/QueryAssertions.java (rev 0)
+++ jbpm4/trunk/modules/test-base/src/main/java/org/jbpm/test/assertion/QueryAssertions.java 2009-09-17 12:08:53 UTC (rev 5655)
@@ -0,0 +1,150 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+/**
+ *
+ */
+package org.jbpm.test.assertion;
+
+import java.lang.reflect.Method;
+import java.util.List;
+
+import junit.framework.Assert;
+
+/**
+ * Utility class with assertions for query test.
+ *
+ * @author Joram Barrez
+ */
+public class QueryAssertions {
+
+ // No need to instantiate
+ private QueryAssertions() {
+ }
+
+ /**
+ * Assertion to be used to check if the result of a certain XXXQuery produces
+ * the correct result.
+ *
+ * Example usage:
+ *
+ * List<Task> taskListAsc = taskService.createTaskQuery().orderAsc(property).list(); List<Task>
+ * taskListDesc = taskService.createTaskQuery().orderDesc(property).list();
+ * QueryAssertions.assertOrderOnProperty(Task.class, property, taskListAsc, taskListDesc, Arrays.asList("a", "b", "c"));
+ *
+ * The assertion will do the following assertions:
+ *
+ * @param clazz
+ * Due to generics mumbo-jumbo, it's required to provide the class
+ * of the generic type T. If only we could do T.getClass() ...
+ * @param <T>
+ * The return type of the XXXQuery
+ * @param property
+ * The property on which the query was ordered
+ * @param listOrderedAsc
+ * The list resulting from executing the query with an 'order by asc'
+ * @param listOrderedDesc
+ * The list resulting from executing the query with an 'order by
+ * desc'
+ * @param expectedValuesAsc
+ * The hard coded list of expected elements, in ascending order. The
+ * assertion for the descending case will be done by reversing this
+ * list
+ */
+ public static <T> void assertOrderOnProperty(Class<T> clazz, String property,
+ List<T> listOrderedAsc, List<T> listOrderedDesc, List<Object> expectedValuesAsc) {
+
+ Assert.assertNotNull(listOrderedAsc);
+ Assert.assertEquals(expectedValuesAsc.size(), expectedValuesAsc.size());
+
+ Assert.assertNotNull(listOrderedDesc);
+ Assert.assertEquals(expectedValuesAsc.size(), listOrderedDesc.size());
+
+ for (int i = 0; i < expectedValuesAsc.size(); i++) {
+
+ Object realValueAsc = null;
+ Object realValueDesc = null;
+
+ try {
+
+ realValueAsc = invokeGetter(property, clazz, listOrderedAsc.get(i));
+ realValueDesc = invokeGetter(property, clazz, listOrderedDesc.get(i));
+
+ } catch (Exception e) {
+ Assert.fail(e.getMessage());
+ }
+
+ Assert.assertEquals("Incorrect order by ASC for property " + property,
+ expectedValuesAsc.get(i), realValueAsc);
+ Assert.assertEquals("Incorrect order by DESC for property " + property,
+ expectedValuesAsc.get((listOrderedDesc.size() -1) - i), realValueDesc);
+
+ }
+ }
+
+ /**
+ * Does the same as the assertOrderOnProperty assertion, but instead of
+ * checking if the resulting list have the correct elements now it is checked
+ * if the elements are naturally ordered ascending and descending.
+ *
+ * IMPORTANT: the type T must be implementing the {@link Comparable} interface!
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> void assertOrderIsNatural(Class<T> clazz, String property, List<T> listOrderedAsc, List<T> listOrderedDesc, int nrOfExpectedElements) {
+
+ Assert.assertEquals(nrOfExpectedElements, listOrderedAsc.size());
+ Assert.assertEquals(nrOfExpectedElements, listOrderedDesc.size());
+
+ if (nrOfExpectedElements > 1) {
+
+ for (int i = 1; i < listOrderedAsc.size(); i++) {
+
+ // ascending check
+ Comparable c1 = (Comparable) invokeGetter(property, clazz, listOrderedAsc.get(i - 1));
+ Comparable c2 = (Comparable) invokeGetter(property, clazz, listOrderedAsc.get(i));
+ if (c1 != null && c2 != null) {
+ Assert.assertTrue("The ascending list does not have a natural order",c1.compareTo(c2) <= 0); // c1 <= c2 when ascending
+ }
+
+ // ascending check
+ Comparable c3 = (Comparable) invokeGetter(property, clazz, listOrderedDesc.get(i - 1));
+ Comparable c4 = (Comparable) invokeGetter(property, clazz, listOrderedDesc.get(i));
+ if (c3 != null && c4 != null) {
+ Assert.assertTrue("The descending list does not have a natural order", c3.compareTo(c4) >= 0); // c3 >= c4 when ascending
+ }
+
+ }
+
+ }
+
+ }
+
+ private static <T> Object invokeGetter(String property, Class<T> clazz, T obj) {
+ try {
+ Method getter = clazz.getMethod("get" + property.substring(0, 1).toUpperCase() + property.substring(1));
+ return getter.invoke(obj);
+ } catch (Exception e) {
+ throw new RuntimeException("Couldn't invoke getter for property " + property);
+ }
+ }
+
+
+}
More information about the jbpm-commits
mailing list