1/*
2 * Copyright (c) 2001-2003 World Wide Web Consortium,
3 * (Massachusetts Institute of Technology, Institut National de
4 * Recherche en Informatique et en Automatique, Keio University). All
5 * Rights Reserved. This program is distributed under the W3C's Software
6 * Intellectual Property License. This program is distributed in the
7 * hope that it will be useful, but WITHOUT ANY WARRANTY; without even
8 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9 * PURPOSE.
10 * See W3C License http://www.w3.org/Consortium/Legal/ for more details.
11 */
12
13
14package org.w3c.domts;
15
16import java.lang.reflect.Constructor;
17import java.lang.reflect.InvocationTargetException;
18import java.net.URI;
19import java.net.URISyntaxException;
20import java.util.ArrayList;
21import java.util.Collection;
22import java.util.Iterator;
23import java.util.List;
24import java.util.logging.Logger;
25
26import javax.xml.parsers.DocumentBuilder;
27
28import junit.framework.TestCase;
29
30import org.w3c.dom.NamedNodeMap;
31import org.w3c.dom.Node;
32import org.w3c.dom.NodeList;
33
34
35public class JUnitTestCaseAdapter extends TestCase implements DOMTestFramework {
36
37  private DOMTestCase test;
38
39
40  private static DOMTestDocumentBuilderFactory defaultFactory = null;
41
42  public JUnitTestCaseAdapter(DOMTestCase test) {
43    super(test.getTargetURI());
44    test.setFramework(this);
45    this.test = test;
46  }
47//BEGIN android-added
48  public JUnitTestCaseAdapter() {
49
50  }
51
52  private String errorMessage = null;
53  private boolean failed = false;
54
55  @Override
56    public void setName(String name) {
57        super.setName(name);
58        if (test == null) {
59            try {
60                URI uri = new URI(name);
61                String path = uri.getPath();
62                path = path.replaceAll("/", ".");
63                Class<?> clazz = null;
64                int pos = path.indexOf('.');
65                while (pos != -1) {
66                    try {
67                        clazz = Class.forName("org.w3c.domts." + path);
68                        break;
69                    } catch (ClassNotFoundException e) {
70                        // do nothing
71                    }
72                    path = path.substring(pos + 1);
73                }
74                if (clazz == null) {
75                    errorMessage = "class not found for test: " + name;
76                    failed = true;
77                    return;
78                }
79
80                if (defaultFactory == null) {
81                    defaultFactory = new JAXPDOMTestDocumentBuilderFactory(null,
82                            JAXPDOMTestDocumentBuilderFactory.getConfiguration1());
83                }
84
85                Constructor<?> constructor = clazz.getConstructor(new Class<?>[] {
86                    DOMTestDocumentBuilderFactory.class
87                });
88
89                test = (DOMTestCase)constructor.newInstance(new Object[] {
90                    defaultFactory
91                });
92                test.setFramework(this);
93
94            } catch (URISyntaxException e) {
95                failed = true;
96                errorMessage = e.getMessage();
97                if (errorMessage == null) {
98                    errorMessage = "" + e.toString();
99                }
100            } catch (IllegalAccessException e) {
101                failed = true;
102                errorMessage = e.getMessage();
103                if (errorMessage == null) {
104                    errorMessage = "" + e.toString();
105                }
106            } catch (InstantiationException e) {
107                failed = true;
108                errorMessage = e.getMessage();
109                if (errorMessage == null) {
110                    errorMessage = "" + e.toString();
111                }
112            } catch (DOMTestIncompatibleException e) {
113                failed = true;
114                errorMessage = e.getMessage();
115                if (errorMessage == null) {
116                    errorMessage = "" + e.toString();
117                }
118            } catch (SecurityException e) {
119                failed = true;
120                errorMessage = e.getMessage();
121                if (errorMessage == null) {
122                    errorMessage = "" + e.toString();
123                }
124            } catch (NoSuchMethodException e) {
125                failed = true;
126                errorMessage = e.getMessage();
127                if (errorMessage == null) {
128                    errorMessage = "" + e.toString();
129                }
130            } catch (IllegalArgumentException e) {
131                failed = true;
132                errorMessage = e.getMessage();
133                if (errorMessage == null) {
134                    errorMessage = "" + e.toString();
135                }
136            } catch (InvocationTargetException e) {
137                failed = true;
138                Throwable t = e.getCause();
139                if (t != null) {
140                    errorMessage = t.getMessage();
141                    if (errorMessage == null) {
142                        errorMessage = "" + t.toString();
143                    }
144                } else {
145                    errorMessage = e.getMessage();
146                    if (errorMessage == null) {
147                        errorMessage = "" + e.toString();
148                    }
149                }
150            }
151        }
152    }
153//END android-added
154  protected void runTest() throws Throwable {
155      //BEGIN android-added
156      if (failed) {
157          if (errorMessage != null) {
158              fail(errorMessage);
159          } else {
160              fail("init failed");
161          }
162      }
163      //END android-added
164    test.runTest();
165    int mutationCount = test.getMutationCount();
166    if (mutationCount != 0) {
167    	fail("Document loaded with willBeModified='false' was modified in course of test.");
168    }
169  }
170
171  public boolean hasFeature(DocumentBuilder docBuilder,
172        String feature,
173        String version)  {
174     return docBuilder.getDOMImplementation().hasFeature(feature,version);
175  }
176
177  public void wait(int millisecond) {
178  }
179
180  public void fail(DOMTestCase test, String assertID) {
181  	fail(assertID);
182  }
183
184  public void assertTrue(DOMTestCase test, String assertID, boolean actual) {
185    assertTrue(assertID,actual);
186  }
187
188  public void assertFalse(DOMTestCase test, String assertID, boolean actual) {
189    if(actual) {
190      assertEquals(assertID,String.valueOf(false), String.valueOf(actual));
191    }
192  }
193
194  public void assertNull(DOMTestCase test, String assertID, Object actual) {
195    assertNull(assertID,actual);
196  }
197
198  public void assertNotNull(DOMTestCase test, String assertID, Object actual) {
199    assertNotNull(assertID,actual);
200  }
201
202  public void assertSame(DOMTestCase test, String assertID, Object expected, Object actual) {
203    boolean same = (expected == actual);
204    //
205    //   if the not the same but both are not null
206    //      might still really be the same
207    //
208    if(!same) {
209      if(expected == null || actual == null ||
210         !(expected instanceof Node) || !(actual instanceof Node)) {
211        assertEquals(assertID,expected,actual);
212      }
213      else {
214        //
215        //  could do something with isSame
216        //
217        assertEquals(assertID,expected,actual);
218      }
219    }
220  }
221
222  public void assertInstanceOf(DOMTestCase test, String assertID, Object obj, Class cls) {
223    assertTrue(assertID,cls.isInstance(obj));
224  }
225
226  public void assertSize(DOMTestCase test, String assertID, int expectedSize, NodeList collection) {
227    assertEquals(assertID,expectedSize, collection.getLength());
228  }
229
230  public void assertSize(DOMTestCase test, String assertID, int expectedSize, NamedNodeMap collection) {
231    assertEquals(assertID, expectedSize, collection.getLength());
232  }
233
234  public void assertSize(DOMTestCase test, String assertID, int expectedSize, Collection collection) {
235    assertEquals(assertID, expectedSize, collection.size());
236  }
237
238  public void assertEqualsIgnoreCase(DOMTestCase test, String assertID, String expected, String actual) {
239  	if (!expected.equalsIgnoreCase(actual)) {
240  		assertEquals(assertID,expected, actual);
241  	}
242  }
243
244  public void assertEqualsIgnoreCase(DOMTestCase test, String assertID, Collection expected, Collection actual) {
245    int size = expected.size();
246    assertNotNull(assertID,expected);
247    assertNotNull(assertID,actual);
248    assertEquals(assertID,size, actual.size());
249    boolean equals = (expected != null && actual != null && size == actual.size());
250    if(equals) {
251      List expectedArray = new ArrayList(expected);
252      String expectedString;
253      String actualString;
254      Iterator actualIter = actual.iterator();
255      Iterator expectedIter;
256      while(actualIter.hasNext() && equals) {
257        actualString = (String) actualIter.next();
258        expectedIter = expectedArray.iterator();
259        equals = false;
260        while(expectedIter.hasNext() && !equals) {
261          expectedString = (String) expectedIter.next();
262          if(actualString.equalsIgnoreCase(expectedString)) {
263            equals = true;
264            expectedArray.remove(expectedString);
265          }
266        }
267      }
268    }
269    assertTrue(assertID,equals);
270  }
271
272  public void assertEqualsIgnoreCase(DOMTestCase test, String assertID, List expected, List actual) {
273    int size = expected.size();
274    assertNotNull(assertID,expected);
275    assertNotNull(assertID,actual);
276    assertEquals(assertID,size, actual.size());
277    boolean equals = (expected != null && actual != null && size == actual.size());
278    if(equals) {
279      String expectedString;
280      String actualString;
281      for(int i = 0; i < size; i++) {
282        expectedString = (String) expected.get(i);
283        actualString = (String) actual.get(i);
284        if(!expectedString.equalsIgnoreCase(actualString)) {
285          assertEquals(assertID,expectedString,actualString);
286          break;
287        }
288      }
289    }
290  }
291
292  public void assertEquals(DOMTestCase test, String assertID, String expected, String actual) {
293    assertEquals(assertID,expected,actual);
294  }
295
296  public void assertEquals(DOMTestCase test, String assertID, int expected, int actual) {
297    assertEquals(assertID,expected,actual);
298  }
299
300  public void assertEquals(DOMTestCase test, String assertID, boolean expected, boolean actual) {
301    assertEquals(assertID,expected,actual);
302  }
303
304  public void assertEquals(DOMTestCase test, String assertID, double expected, double actual) {
305      assertEquals(assertID, expected, actual, 0.0);
306  }
307
308  public void assertEquals(DOMTestCase test, String assertID, Collection expected, Collection actual) {
309    int size = expected.size();
310    assertNotNull(assertID,expected);
311    assertNotNull(assertID,actual);
312    assertEquals(assertID,size, actual.size());
313    boolean equals = (expected != null && actual != null && size == actual.size());
314    if(equals) {
315      List expectedArray = new ArrayList(expected);
316      Object expectedObj;
317      Object actualObj;
318      Iterator actualIter = actual.iterator();
319      Iterator expectedIter;
320      while(actualIter.hasNext() && equals) {
321        actualObj = actualIter.next();
322        expectedIter = expectedArray.iterator();
323        equals = false;
324        while(expectedIter.hasNext() && !equals) {
325          expectedObj = expectedIter.next();
326          if(expectedObj == actualObj || expectedObj.equals(actualObj)) {
327            equals = true;
328            expectedArray.remove(expectedObj);
329          }
330        }
331      }
332    }
333    assertTrue(assertID,equals);
334  }
335
336  public void assertNotEqualsIgnoreCase(DOMTestCase test, String assertID, String expected, String actual) {
337    if(expected.equalsIgnoreCase(actual)) {
338      assertTrue(assertID, !expected.equalsIgnoreCase(actual));
339    }
340  }
341
342  public void assertNotEquals(DOMTestCase test, String assertID, String expected, String actual) {
343    assertTrue(assertID, !expected.equals(actual));
344  }
345
346  public void assertNotEquals(DOMTestCase test, String assertID, int expected, int actual) {
347    assertTrue(assertID,expected !=actual);
348  }
349
350  public void assertNotEquals(DOMTestCase test, String assertID, boolean expected, boolean actual) {
351    assertTrue(assertID,expected !=actual);
352  }
353
354
355  public void assertNotEquals(DOMTestCase test, String assertID, double expected, double actual) {
356    if(expected == actual) {
357      assertTrue(assertID,expected != actual);
358    }
359  }
360
361
362  public boolean same(Object expected, Object actual) {
363    boolean equals = (expected == actual);
364    if(!equals && expected != null && expected instanceof Node &&
365      actual != null && actual instanceof Node) {
366      //
367      //  can use Node.isSame eventually
368    }
369    return equals;
370  }
371
372  public boolean equalsIgnoreCase(String expected, String actual) {
373    return expected.equalsIgnoreCase(actual);
374  }
375
376  public boolean equalsIgnoreCase(Collection expected, Collection actual) {
377    int size = expected.size();
378    boolean equals = (expected != null && actual != null && size == actual.size());
379    if(equals) {
380      List expectedArray = new ArrayList(expected);
381      String expectedString;
382      String actualString;
383      Iterator actualIter = actual.iterator();
384      Iterator expectedIter;
385      while(actualIter.hasNext() && equals) {
386        actualString = (String) actualIter.next();
387        expectedIter = expectedArray.iterator();
388        equals = false;
389        while(expectedIter.hasNext() && !equals) {
390          expectedString = (String) expectedIter.next();
391          if(actualString.equalsIgnoreCase(expectedString)) {
392            equals = true;
393            expectedArray.remove(expectedString);
394          }
395        }
396      }
397    }
398    return equals;
399  }
400
401  public boolean equalsIgnoreCase(List expected, List actual) {
402    int size = expected.size();
403    boolean equals = (expected != null && actual != null && size == actual.size());
404    if(equals) {
405      String expectedString;
406      String actualString;
407      for(int i = 0; i < size; i++) {
408        expectedString = (String) expected.get(i);
409        actualString = (String) actual.get(i);
410        if(!expectedString.equalsIgnoreCase(actualString)) {
411          equals = false;
412          break;
413        }
414      }
415    }
416    return equals;
417  }
418
419  public boolean equals(String expected, String actual) {
420    return expected.equals(actual);
421  }
422
423  public boolean equals(int expected, int actual) {
424    return expected == actual;
425  }
426
427  public boolean equals(boolean expected, boolean actual) {
428    return expected == actual;
429  }
430
431  public boolean equals(double expected, double actual) {
432    return expected == actual;
433  }
434
435  public boolean equals(Collection expected, Collection actual) {
436    int size = expected.size();
437    boolean equals = (expected != null && actual != null && size == actual.size());
438    if(equals) {
439      List expectedArray = new ArrayList(expected);
440      Object expectedObj;
441      Object actualObj;
442      Iterator actualIter = actual.iterator();
443      Iterator expectedIter;
444      while(actualIter.hasNext() && equals) {
445        actualObj = actualIter.next();
446        expectedIter = expectedArray.iterator();
447        equals = false;
448        while(expectedIter.hasNext() && !equals) {
449          expectedObj = expectedIter.next();
450          if(expectedObj != actualObj && expectedObj.equals(actualObj)) {
451            equals = true;
452            expectedArray.remove(expectedObj);
453          }
454        }
455      }
456    }
457    return equals;
458  }
459
460  public boolean equals(List expected, List actual) {
461    int size = expected.size();
462    boolean equals = (expected != null && actual != null && size == actual.size());
463    if(equals) {
464      Object expectedObj;
465      Object actualObj;
466      for(int i = 0; i < size; i++) {
467        expectedObj = expected.get(i);
468        actualObj = actual.get(i);
469        if(!expectedObj.equals(actualObj)) {
470          equals = false;
471          break;
472        }
473      }
474    }
475    return equals;
476  }
477
478  public int size(Collection collection) {
479    return collection.size();
480  }
481
482  public int size(NamedNodeMap collection) {
483    return collection.getLength();
484  }
485
486  public int size(NodeList collection) {
487    return collection.getLength();
488  }
489
490
491}