View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   package org.kuali.student.common.test.util;
6   
7   import java.util.ArrayList;
8   import java.util.Collections;
9   import java.util.List;
10  import java.util.Random;
11  
12  import org.junit.Assert;
13  
14  import static org.junit.Assert.assertEquals;
15  
16  /**
17   * Helps create a dynamic 
18   * @author nwright
19   */
20  public class ListOfStringTester {
21  
22  	/**
23  	 * Verify that the expected elements exist in the actual list.  
24  	 * order is not important
25  	 * @param expectedList
26  	 * @param actualList
27  	 * @param failForExtraElements true if we should fail if there are other elements in the actual list.
28  	 */
29  	public void checkExistsAnyOrder (List<String>expectedList, List<String>actualList, boolean failForExtraElements) {
30  		
31  		if (failForExtraElements) 
32  			Assert.assertFalse(expectedList.size() != actualList.size());
33  		
34  		List<String>unmatchedList = new ArrayList<String>(expectedList);
35  		
36  		for (String string : actualList) {
37  
38  			boolean removed = unmatchedList.remove(string);
39  		
40  			if (failForExtraElements)
41  				Assert.assertTrue(removed);
42  		}
43  		
44  		Assert.assertTrue(unmatchedList.size() == 0);
45  	
46  	}
47  	
48      public void check(List<String> expectedList, List<String> actualList) {
49          if (expectedList.size () != actualList.size ()) {
50              this.dump(expectedList, actualList);
51          }
52          assertEquals(expectedList.size(), actualList.size());
53          List<String> expectedSorted = new ArrayList(expectedList);
54          Collections.sort(expectedSorted);
55          List<String> actualSorted = new ArrayList(actualList);
56          Collections.sort(actualSorted);
57          for (int i = 0; i < expectedSorted.size(); i++) {
58              String expected = expectedSorted.get(i);
59              String actual = actualSorted.get(i);
60              assertEquals(i + "", expected, actual);
61          }
62      }
63  
64      public void dump (List<String> expectedList, List<String> actualList) {
65           System.out.println ("Original List");
66          this.dump(expectedList);
67          System.out.println ("Updated List");
68          this.dump(actualList);
69      }
70      
71      public void dump(List<String> list) {
72          for (int i = 0; i < list.size(); i++) {
73              String expected = list.get(i);
74              System.out.println(i + ".) " + expected);
75          }
76      }
77  
78      /*
79       * Generates a List<String> object, where each element is the String representation
80       * of a randomly generated int.
81       * @param length the number of elements to return in the List<String> object.
82       * @param upperBound the maximum value to be generated
83       */
84      public static List<String> generateRandomListOfStringIds (int length, int upperBound) {
85          Random generator = new Random();
86          List<String> arr = new ArrayList<String> ();
87          for (int i=0; i<length; i++) {
88              int randomNum = generator.nextInt(upperBound);
89              arr.add("" + randomNum);
90          }
91          return arr;
92      }
93  
94  }