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 static org.junit.Assert.*;
8   import java.util.ArrayList;
9   import java.util.Collection;
10  import java.util.Collections;
11  import java.util.Comparator;
12  import java.util.List;
13  
14  import org.apache.commons.collections.CollectionUtils;
15  import org.apache.commons.collections.Transformer;
16  import org.apache.commons.lang.StringUtils;
17  import org.kuali.student.r2.common.dto.AttributeInfo;
18  import org.mortbay.log.Log;
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  
22  /**
23   * Helps create a dynamic 
24   * @author nwright
25   */
26  public class AttributeTester {
27  
28      private static final Logger log = LoggerFactory.getLogger(AttributeTester.class);
29      
30      public void add2ForCreate(List<AttributeInfo> expected) {
31          AttributeInfo attr = new AttributeInfo();
32          attr.setKey("attribute.key1");
33          attr.setValue("attribute value1");
34          expected.add(attr);
35          attr = new AttributeInfo();
36          attr.setKey("attribute.key2");
37          attr.setValue("attribute value2");
38          expected.add(attr);
39      }
40  
41      public void delete1Update1Add1ForUpdate(List<AttributeInfo> expected) {
42          expected.remove(0);
43          expected.get(0).setValue("updated value");
44          AttributeInfo attr = new AttributeInfo();
45          attr.setKey("attribute.key3");
46          attr.setValue("attribute value3");
47          expected.add(attr);
48      }
49  
50      public AttributeInfo toAttribute(String key, String value) {
51          if (key == null) {
52              return null;
53          }
54          AttributeInfo actual = new AttributeInfo();
55          actual.setKey(key);
56          actual.setValue(value);
57          return actual;
58      }
59  
60      public List<AttributeInfo> findAttributes(List<AttributeInfo> attrs, String key) {
61          List<AttributeInfo> list = new ArrayList<AttributeInfo>();
62          for (AttributeInfo actual : attrs) {
63              if (key.equals(actual.getKey())) {
64                  list.add(actual);
65              }
66          }
67          return list;
68      }
69      
70      
71      public void check(List<AttributeInfo> expectedList, List<AttributeInfo> actualList) {
72          if (expectedList.size() != actualList.size()) {
73              this.dump(expectedList, actualList);
74          }
75          assertEquals(expectedList.size(), actualList.size());
76          List<AttributeInfo> expectedSorted = new ArrayList(expectedList);
77          Collections.sort(expectedSorted, new AttributeInfoComparator());
78          List<AttributeInfo> actualSorted = new ArrayList(actualList);
79          Collections.sort(actualSorted, new AttributeInfoComparator());
80          for (int i = 0; i < expectedSorted.size(); i++) {
81              AttributeInfo expected = expectedSorted.get(i);
82              AttributeInfo actual = actualSorted.get(i);
83              if (expected.getId() != null) {
84                  assertEquals(i + "", expected.getId(), actual.getId());
85              }
86              
87             if (log.isDebugEnabled()) {
88              
89                  Transformer valueTransformer = new Transformer() {
90  
91                      @Override
92                      public Object transform(Object input) {
93                          AttributeInfo attr = (AttributeInfo) input;
94                          return attr.getValue();
95                      }
96                  };
97  
98                  if (!expected.getValue().equals(actual.getValue())) {
99  
100                     /*
101                      * On miss match print out the values in each collection so we can get
102                      * a better sense of why the test failed..
103                      */
104                     Collection<String> actualValues = CollectionUtils.collect(
105                             actualSorted, valueTransformer);
106                     Collection<String> expectedValues = CollectionUtils
107                             .collect(expectedSorted, valueTransformer);
108 
109                     log.debug("expected = "
110                             + StringUtils.join(expectedValues, ", ")
111                             + ", actual = "
112                             + StringUtils.join(actualValues, ", "));
113 
114                 }
115            }
116             
117             
118             assertEquals(i + "", expected.getKey(), actual.getKey());
119             assertEquals(i + "", expected.getValue(), actual.getValue());
120         }
121     }
122 
123     public void dump(List<AttributeInfo> expectedList, List<AttributeInfo> actualList) {
124         System.out.println("Expected List");
125         this.dump(expectedList);
126         System.out.println("Actual List");
127         this.dump(actualList);
128     }
129 
130     public void dump(List<AttributeInfo> list) {
131         for (int i = 0; i < list.size(); i++) {
132             AttributeInfo expected = list.get(i);
133             System.out.println(i + ".) " + expected.getKey() + "=" + expected.getValue() + "\t" + expected.getId());
134         }
135     }
136 
137     private static class AttributeInfoComparator implements Comparator<AttributeInfo> {
138 
139         @Override
140         public int compare(AttributeInfo o1, AttributeInfo o2) {
141             return calcSortKey(o1).compareTo(calcSortKey(o2));
142         }
143 
144         private String calcSortKey(AttributeInfo attr) {
145             StringBuilder sb = new StringBuilder();
146             sb.append(attr.getKey());
147             sb.append("\t");
148             if (attr.getId() != null) {
149                 sb.append(attr.getId());
150             }
151             sb.append("\t");
152             if (attr.getValue() != null) {
153                 sb.append(attr.getValue());
154             }
155             return sb.toString();
156         }
157     }
158 
159 }