View Javadoc
1   package org.itracker;
2   
3   import org.apache.log4j.Logger;
4   import org.dom4j.*;
5   import org.itracker.model.Entity;
6   
7   import java.util.Comparator;
8   import java.util.List;
9   
10  /**
11   * Itracker specific assertions for include static to test-classes.
12   */
13  public class Assert extends org.junit.Assert {
14  
15      private static final Logger log = Logger.getLogger(Assert.class);
16  
17      /**
18       * asserts true when comparator compares for lhs being < rhs and rhs > lhs.
19       */
20      @SuppressWarnings("unchecked")
21      public static void assertEntityComparator(String message, Comparator comparator, Entity lhs, Entity rhs) {
22          if (log.isDebugEnabled()) {
23              log.debug("assertEntityComparator: " + message + " " + comparator);
24          }
25          if (null == lhs) {
26              throw new IllegalArgumentException("lhs must not both be null.");
27          }
28          // test nullpointer
29  
30          if (null == rhs) {
31  
32              try {
33                  assertNull(message + " lhs, null: " + comparator, comparator.compare(lhs, rhs));
34                  fail();
35              } catch (NullPointerException npe) {
36              } // ok
37  
38              try {
39                  assertNull(message + " null, lhs: " + comparator, comparator.compare(rhs, lhs));
40                  fail();
41              } catch (NullPointerException npe) {
42              } // ok
43  
44              return;
45          }
46  
47          assertTrue(message + " lhs, rhs: " + comparator, 0 > comparator.compare(lhs, rhs));
48          assertTrue(message + " rhs, lhs: " + comparator, 0 < comparator.compare(rhs, lhs));
49      }
50  
51      /**
52       * asserts true when comparator compares for lhs being equal to rhs and rhs equal lhs.
53       */
54  
55      @SuppressWarnings("unchecked")
56      public static void assertEntityComparatorEquals(String message, Comparator comparator, Entity lhs, Entity rhs) {
57          if (log.isDebugEnabled()) {
58              log.debug("assertEntityComparatorEquals: " + message + " " + comparator);
59          }
60          if (null == lhs || null == rhs) {
61              throw new IllegalArgumentException("rhs and lhs must not be null.");
62          }
63  
64          assertTrue(message + " lhs, rhs: " + comparator, 0 == comparator.compare(lhs, rhs));
65          assertTrue(message + " rhs, lhs: " + comparator, 0 == comparator.compare(rhs, lhs));
66      }
67  
68  
69  
70      public static void assertContainsAll(String message, Node expected, Node actual) {
71          if (expected == actual) {
72              return;
73          }
74          assertNotNull(message, actual);
75          if (expected.equals(actual)) {
76              return;
77          }
78          if (expected instanceof Document && actual instanceof Document) {
79              assertContainsAll(message + " rootElement", ((Document) expected).getRootElement(),
80                      ((Document) actual).getRootElement());
81          }
82          if (expected instanceof Element && actual instanceof Element) {
83              Element elExp = (Element)expected;
84              Element elAct = (Element)actual;
85              assertEquals(message + " nodeCount", elExp.nodeCount(), elAct.nodeCount());
86              for (int i = 0; i < elExp.nodeCount(); i++) {
87                  Node node = elExp.node(i);
88                  if (node instanceof Element) {
89                      XPath x = DocumentFactory.getInstance().createXPath(node.getPath());
90                      List found = x.selectNodes(actual);
91                      if (!found.isEmpty()) {
92                          if (found.size() > 1) {
93                              assertEquals("found size " + x, x.selectNodes(expected).size(), found.size());
94                              assertContainsOne(message + " contains found " + x, (Element)node, found);
95                          } else {
96                              Node foundExp = x.selectSingleNode(expected);
97                              if (foundExp instanceof Element && found.get(0) instanceof Element) {
98                                  assertElementEquals(message + " element " + x, (Element)foundExp, (Element)found.get(0));
99                              }
100                         }
101                     }
102                 }
103             }
104         }
105 
106     }
107 
108     private static boolean equalsNodes(Node left, Node right) {
109         if (left == right)
110             return true;
111         if (left == null)
112             return false;
113         if (right == null)
114             return false;
115 
116         if (left.equals(right))
117             return true;
118 
119         if (left.getNodeType() != right.getNodeType())
120             return false;
121 
122         if (left instanceof Element) {
123             if (((Element) left).attributeCount() != ((Element)right).attributeCount())
124                 return false;
125 
126             for (Object aLeft:((Element) left).attributes()) {
127                 Object aRight = ((Element) right).attribute(((Attribute) aLeft).getName());
128                 if (!((Attribute) aLeft).getValue()
129                         .equals(((Attribute)aRight).getValue())) {
130                     return false;
131                 }
132             }
133             if (((Element) left).isTextOnly()) {
134                 if (!left.getText().equals(right.getText())) {
135                     return false;
136                 }
137             } else {
138                 if (((Element) left).elements().size() != ((Element) right).elements().size())
139                     return false;
140                 for (Object e: ((Element) left).elements()) {
141                     int sizeLeft = ((Element) left).elements(((Element)e).getName()).size();
142                     int sizeRight = ((Element) right).elements(((Element)e).getName()).size();
143                     if (sizeLeft != sizeRight)
144                         return false;
145                     if (sizeLeft == 1) {
146                         return equalsNodes(((Element) left).element(((Element) e).getName()),
147                                 ((Element) right).element(((Element) e).getName()));
148                     }
149                 }
150             }
151         }
152 
153         // TODO assume equals?
154         return true;
155     }
156     public static void assertContainsOne(String message, Element expected, List nodes) {
157         if (null == expected) {
158             return;
159         }
160         assertNotNull(message + " nodes", nodes);
161         for (Object el: nodes) {
162             if (el instanceof Element) {
163                 Element e = (Element)el;
164                 if (equalsNodes(expected, e))
165                     return;
166             }
167         }
168         fail(message + " did not find containing");
169     }
170 
171     /**
172      * Some highlevel equality test on JDOM elements.
173      *
174      * @param message the message
175      * @param expected expected element
176      * @param actual the actual element
177      */
178     public static void assertElementEquals(String message, Element expected, Element actual) {
179         assertEquals(message, expected.isTextOnly(), actual.isTextOnly());
180         assertEquals("tagname", expected.getName(), actual.getName());
181 
182         if (expected.isTextOnly()) {
183             assertEquals(message + " text", expected.getText(), actual.getText());
184         } else {
185             assertContainsAll(message + " containsAll", expected, actual);
186         }
187     }
188 
189 }