1 /*****************************************************************************
5 * Creation date 12 Oct 2012
8 *****************************************************************************/
10 package test.splat.common;
12 import java.lang.reflect.InvocationTargetException;
13 import java.lang.reflect.Method;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.Comparator;
17 import java.util.List;
19 import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
20 import org.testng.Assert;
21 import org.testng.annotations.Test;
24 * Abstract class for specific search functionality tests. It is necessary to
25 * define method, returning expected search list size, array of properties.
27 * @author <a href="mailto:vera.polyanskikh@opencascade.com">Vera POLYANSKIKH (VPH)</a>
28 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
30 @Test(sequential=true, groups = {"service", "functional", "business"})
31 public class TestListingAndOrder extends AbstractTransactionalTestNGSpringContextTests {
36 * Current Specific method name.
38 protected String _methodName;
40 * Common message suffix used to inform that corresponding method should be overridden if this common functionality is used.
42 private static final String NO_METHOD_OVERRIDEN_MSG_SUFFIX = " should be overridden in test class if common test pagination and order engine is used";
45 * Error message has been appeared when null object exist in business method result set.
47 private static final String NULL_OBJECT_EXIST_IN_RESULTSET = "Null object exist in business method result set";
49 //////////////////////////////////////////////////////////////////////////////////////////
50 //////////////////////////////////////////////////////////////////////////////////////////
51 /**METHODS TO BE OVERRIDDEN**/
52 /**Throw UnsupportedOperationException if not**/
53 //////////////////////////////////////////////////////////////////////////////////////////
54 //////////////////////////////////////////////////////////////////////////////////////////
57 * Return expected size of the whole retrieved list or a page of the objects.
58 * @param methodName - specific search method to be called
59 * @return size of list
61 @Test(enabled = false)
62 public int getExpectedWholeListSize(final String methodName){
63 // empty default implementation
64 // should be overridden if test of pagination or order is necessary
65 throw new UnsupportedOperationException("TestListingAndOrder.getExpectedWholeListSize"+NO_METHOD_OVERRIDEN_MSG_SUFFIX);
70 * Returns array of DTO properties which can be used to test order of page or list.
71 * @param methodName - specific search method to be called
72 * @return array of order criteria.
74 @Test(enabled = false)
75 public String[][] getOrderCriteria(final String methodName){
76 // empty default implementation
77 // should be overridden if test of pagination or order is necessary
78 throw new UnsupportedOperationException("TestListingAndOrder.getOrderCriteria"+NO_METHOD_OVERRIDEN_MSG_SUFFIX);
82 * Wrapper for call of specific not paginated search method.
83 * @param <T> object type class
84 * @param methodName - specific search method to be called
85 * @return template list
87 @Test(enabled = false)
88 public <T>List<T> searchListWrapper(final String methodName){
89 // empty default implementation
90 // should be overridden if test of pagination or order is necessary
91 throw new UnsupportedOperationException("TestListingAndOrder.searchListWrapper"+NO_METHOD_OVERRIDEN_MSG_SUFFIX);
94 //////////////////////////////////////////////////////////////////////////////////////////
95 //////////////////////////////////////////////////////////////////////////////////////////
97 //////////////////////////////////////////////////////////////////////////////////////////
98 //////////////////////////////////////////////////////////////////////////////////////////
102 * Returns result of default comparison of two objects by given array of properties.
103 * @param o1 - object 1
104 * @param o2 - object 2
105 * @param methodName - specific search method name
106 * @param propertyNames - properties to be compared by
107 * @param ascDirection - order direction (asc if true, desc - otherwise)
108 * @return result of comparison.
110 @Test(enabled = false)
111 protected int specificCompare(final Object o1, final Object o2, final String methodName,final String[] propertyNames,final boolean ascDirection){
114 while(result==0 && i<propertyNames.length){
115 result =internalCompare(o1,o2, propertyNames[i]);
120 result = result*(-1);
126 * Perform test of not paginated search method.
127 * @param <T> object type class
128 * @param methodName - specific search method to be called
130 @Test(enabled = false)
131 protected <T> void internalTestSearchList(final String methodName) {
132 setMethodName(methodName);
134 // compare the whole set size with expected
135 List<T> list = searchList();
136 Assert.assertEquals(list.size(), getExpectedWholeListSize(methodName),"The whole list size is unexpected!");
138 // check ordering by all given criteria
139 String[] orderCriteria = getOrderCriteria(_methodName)[0];
141 Assert.assertTrue(checkOrdering(list,orderCriteria,true),"Asc Ordering of list by properties "+orderCriteria[0].toString()+" is wrong!");
143 } catch (Exception e) {
144 Assert.fail("Exception occured", e);
149 * Get the methodName.
150 * @return the methodName
152 @Test(enabled = false)
153 private String getMethodName() {
161 * Set the methodName.
162 * @param methodName the methodName to set
164 @Test(enabled = false)
165 private void setMethodName(final String methodName) {
166 this._methodName = methodName;
171 * Test of not paginated search method. Specific service method to be called for tests.
172 * @param <T> object type class
173 * @return list -template list
175 @Test(enabled = false)
176 private <T>List<T> searchList() {
177 return searchListWrapper(_methodName);
181 * Order list given list by given order criteria.
182 * @param <T> - object type class
183 * @param list - list of the objects
184 * @param orderCriteria - property to order by
185 * @param ascDirection - order direction (asc if true, desc - otherwise)
186 * @return ordered list
188 @Test(enabled = false)
189 private <T>List<T> getOrderedList(final List<T> list,
190 final String[] orderCriteria,
191 final boolean ascDirection){
194 Collections.sort(list,new Comparator<T>(){
195 public int compare(final T o1, final T o2){
196 return specificCompare(o1,o2,null,orderCriteria,ascDirection);
203 * Check if given list is ordered by given criteria.
204 * @param <T> - object type class
205 * @param list - list of the objects
206 * @param orderCriteria - property to order by
207 * @param ascDirection - order direction (asc if true, desc - otherwise)
210 @Test(enabled = false)
211 private <T> boolean checkOrdering(final List<T> list,
212 final String[] orderCriteria,
213 final boolean ascDirection){
214 boolean result = true;
216 List<T> oldList = new ArrayList<T>();
220 List<T> sortedList = getOrderedList(list,orderCriteria,ascDirection);
222 for(i=0;i<sortedList.size();i++){
223 if(oldList.get(i)==null){
224 Assert.fail(NULL_OBJECT_EXIST_IN_RESULTSET);
226 if(!oldList.get(i).equals(sortedList.get(i))){
236 * Performs internal default compare by given property.
237 * @param o1 - object 1
238 * @param o2 - object 2
239 * @param propertyName - property to be compared by
242 @Test(enabled = false)
243 private int internalCompare(final Object o1, final Object o2, final String propertyName){
246 // Use getter method for the given property name.
247 Method getter = o1.getClass().getMethod(
248 "get" + propertyName.substring(0, 1).toUpperCase()
249 + propertyName.substring(1));
250 // Get property values by means of the getter method.
251 Object v1 = getter.invoke(o1);
252 Object v2 = getter.invoke(o2);
254 if(v1==null && v2==null) {
257 if(v1==null && v2!=null) {
260 if(v1!=null && v2==null){
263 if (v1 instanceof Comparable) {
264 result = ((Comparable)v1).compareTo(v2);
269 }catch (IllegalAccessException e) {
271 Assert.fail("IllegalAccessException was thrown by BeanUtils.getProperty ", e);
272 } catch (InvocationTargetException e) {
274 Assert.fail("InvocationTargetException was thrown by BeanUtils.getProperty ", e);
275 } catch (NoSuchMethodException e) {
277 Assert.fail("NoSuchMethodException was thrown by BeanUtils.getProperty: " + e.getMessage(), e);