KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > tbutils > httpunit > HttpUnitUtils


1 package com.tonbeller.tbutils.httpunit;
2
3 import java.io.File JavaDoc;
4 import java.io.FileOutputStream JavaDoc;
5 import java.io.FileWriter JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.io.OutputStreamWriter JavaDoc;
9 import java.io.Writer JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Arrays JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import javax.xml.transform.OutputKeys JavaDoc;
17 import javax.xml.transform.Templates JavaDoc;
18 import javax.xml.transform.Transformer JavaDoc;
19 import javax.xml.transform.TransformerConfigurationException JavaDoc;
20 import javax.xml.transform.TransformerException JavaDoc;
21 import javax.xml.transform.TransformerFactory JavaDoc;
22 import javax.xml.transform.dom.DOMSource JavaDoc;
23 import javax.xml.transform.stream.StreamResult JavaDoc;
24 import javax.xml.transform.stream.StreamSource JavaDoc;
25
26 import junit.framework.Assert;
27
28 import org.jaxen.JaxenException;
29 import org.jaxen.dom.DOMXPath;
30 import org.pdfbox.ExtractText;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.Text JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35
36 import com.meterware.httpunit.HTMLElementPredicate;
37 import com.meterware.httpunit.SubmitButton;
38 import com.meterware.httpunit.TableCell;
39 import com.meterware.httpunit.WebConversation;
40 import com.meterware.httpunit.WebForm;
41 import com.meterware.httpunit.WebLink;
42 import com.meterware.httpunit.WebResponse;
43 import com.meterware.httpunit.WebTable;
44 import com.tonbeller.tbutils.httpunit.FileDiff.FileDiffHandler;
45
46 /**
47  * Support for httpunit tests. Allows to address
48  * submit buttons, checkboxes etc via xpath expressions
49  * and table cell coordinates.
50  */

51
52 public class HttpUnitUtils {
53
54   private DOMXPath xpSubmit;
55   private DOMXPath xpAnchor;
56   private DOMXPath xpRadio;
57   private DOMXPath xpCheckbox;
58   private DOMXPath xpSelect;
59   private DOMXPath xpOption;
60   private DOMXPath xpTextInput;
61   private DOMXPath xpTextArea;
62   private DOMXPath xpPassword;
63
64   WebConversation wc;
65   boolean recordMode;
66
67   XmlDiff xmlDiff;
68   FileDiff fileDiff;
69
70   String JavaDoc htmlLogFileEncoding = "utf-8";
71
72   /**
73    * Constructor
74    * @param wc
75    * @throws JaxenException
76    */

77   public HttpUnitUtils(WebConversation wc) throws JaxenException {
78     this.wc = wc;
79     xpSubmit = new DOMXPath(".//input[@type='submit'] | .//input[@type='image']");
80     xpAnchor = new DOMXPath(".//a[@href]");
81     xpCheckbox = new DOMXPath(".//input[@type='checkbox']");
82     xpRadio = new DOMXPath(".//input[@type='radio']");
83     xpSelect = new DOMXPath(".//select");
84     xpOption = new DOMXPath(".//option");
85     xpTextInput = new DOMXPath(".//input[@type='text']");
86     xpTextArea = new DOMXPath(".//textarea");
87     xpPassword = new DOMXPath(".//input[@type='password']");
88
89     recordMode = "true".equals(System.getProperty("httpunit.recordmode"));
90     xmlDiff = new XmlDiff(true);
91   }
92
93   public Node JavaDoc getCellNode(String JavaDoc tableID, int row, int col) throws SAXException JavaDoc {
94     WebTable table = wc.getCurrentPage().getTableWithID(tableID);
95     Assert.assertNotNull(table);
96     TableCell cell = table.getTableCell(row, col);
97     Assert.assertNotNull(cell);
98     // XmlUtils.print(cell.getDOM(), new PrintWriter(System.out));
99
return cell.getDOM();
100   }
101
102   public Element JavaDoc getNth(Node JavaDoc root, DOMXPath xpath, int nth) throws JaxenException, SAXException JavaDoc {
103     if (root == null)
104       root = wc.getCurrentPage().getDOM();
105
106     List JavaDoc list = xpath.selectNodes(root);
107     Assert.assertTrue("getNth: " + list.size() + ">" + nth, list.size() > nth);
108     return (Element JavaDoc) list.get(nth);
109   }
110
111   public WebForm getForm(String JavaDoc formID) throws SAXException JavaDoc {
112     WebResponse wr = wc.getCurrentPage();
113     if (formID == null)
114       return wr.getForms()[0];
115     WebForm[] wfs = wr.getForms();
116     WebForm wf = wr.getFormWithID(formID);
117     Assert.assertNotNull("form " + formID + " not found on page", wf);
118     return wf;
119   }
120
121   void addParameterValue(String JavaDoc formID, String JavaDoc name, String JavaDoc value) throws SAXException JavaDoc {
122     WebForm wf = getForm(formID);
123     String JavaDoc[] values = wf.getParameterValues(name);
124     if (values == null)
125       values = new String JavaDoc[0];
126     List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(values));
127     if (!list.contains(value)) {
128       list.add(value);
129       values = (String JavaDoc[]) list.toArray(values);
130       wf.setParameter(name, values);
131     }
132   }
133
134   void removeParameterValue(String JavaDoc formID, String JavaDoc name, String JavaDoc value) throws SAXException JavaDoc {
135     WebForm wf = getForm(formID);
136     String JavaDoc[] values = wf.getParameterValues(name);
137     if (values == null)
138       values = new String JavaDoc[0];
139     List JavaDoc list = new ArrayList JavaDoc(Arrays.asList(values));
140     if (list.contains(value)) {
141       list.remove(value);
142       if (list.size() == 0)
143         wf.removeParameter(name);
144       else {
145         values = (String JavaDoc[]) list.toArray(values);
146         wf.setParameter(name, values);
147       }
148     }
149   }
150
151   /* -------------------------------------- submit buttons ------------------------------------- */
152
153   /**
154    * klickt auf den <code>nth</code>-ten &lt;input type="submit" ... &gt; oder &lt;input type="image" ... &gt; einer Tabellenzelle
155    */

156   public WebResponse submitCell(String JavaDoc formID, String JavaDoc tableID, int row, int col, int nth)
157       throws JaxenException, IOException JavaDoc, SAXException JavaDoc {
158     Node JavaDoc root = getCellNode(tableID, row, col);
159     return submitXPath(formID, root, xpSubmit, nth);
160   }
161
162   /**
163    * klickt auf den durch <code>xpath</code> selektierten Submit Button auf der Seite
164    */

165   public WebResponse submitXPath(String JavaDoc formID, DOMXPath xpath, int nth) throws JaxenException,
166       IOException JavaDoc, SAXException JavaDoc {
167     return submitXPath(formID, wc.getCurrentPage().getDOM(), xpath, nth);
168   }
169
170   /**
171    * klickt auf den durch <code>xpath</code> selektierten Submit Button auf der Seite
172    */

173   public WebResponse submitXPath(String JavaDoc formID, String JavaDoc xpath, int nth) throws JaxenException,
174       IOException JavaDoc, SAXException JavaDoc {
175     DOMXPath dx = new DOMXPath(xpath);
176     return submitXPath(formID, wc.getCurrentPage().getDOM(), dx, nth);
177   }
178
179   /**
180    * klickt auf den durch <code>xpath</code> selektierten Submit Button in root
181    */

182   public WebResponse submitXPath(String JavaDoc formID, Node JavaDoc root, DOMXPath xpath, int nth)
183       throws JaxenException, IOException JavaDoc, SAXException JavaDoc {
184     Element JavaDoc button = getNth(root, xpath, nth);
185     String JavaDoc name = button.getAttribute("name");
186     WebForm wf = getForm(formID);
187     SubmitButton sb = wf.getSubmitButton(name);
188     return wf.submit(sb);
189   }
190
191   public WebResponse submitButton(String JavaDoc formID, String JavaDoc buttonName) throws IOException JavaDoc,
192       SAXException JavaDoc {
193     WebForm wf = getForm(formID);
194     SubmitButton sb = wf.getSubmitButton(buttonName);
195     return wf.submit(sb);
196   }
197
198   /**
199    * presses the first button in the form whose name attribute contains <code>namePart</code>
200    */

201   public void submitByNamePart(String JavaDoc formId, String JavaDoc namePart) throws JaxenException, IOException JavaDoc,
202       SAXException JavaDoc {
203     submitXPath(formId, "//input[contains(@name,'" + namePart + "')]", 0);
204   }
205
206   /**
207    * presses the first button in the form whose value attribute contains <code>valuePart</code>
208    */

209   public void submitByValuePart(String JavaDoc formId, String JavaDoc valuePart) throws JaxenException,
210       IOException JavaDoc, SAXException JavaDoc {
211     submitXPath(formId, "//input[contains(@value,'" + valuePart + "')]", 0);
212   }
213
214   /* -------------------------------------- anchors ------------------------------------- */
215
216   private HTMLElementPredicate matchLink = new HTMLElementPredicate() {
217     public boolean matchesCriteria(Object JavaDoc a, Object JavaDoc href) {
218       Node JavaDoc node = ((WebLink) a).getDOMSubtree();
219       Element JavaDoc e = (Element JavaDoc) node;
220       return e.getAttribute("href").equals(href);
221     }
222   };
223
224   /**
225    * folgt dem <code>nth</code> Anchor einer Tabellenzelle
226    */

227   public WebResponse followCell(String JavaDoc tableID, int row, int col, int nth) throws JaxenException,
228       IOException JavaDoc, SAXException JavaDoc {
229     return followXPath(getCellNode(tableID, row, col), xpAnchor, nth);
230   }
231
232   /**
233    * folgt dem <code>nth</code>-ten Anchor auf der Seite, der von <code>xpath</code> selektiert wird.
234    */

235   public WebResponse followXPath(DOMXPath xpath, int nth) throws JaxenException, IOException JavaDoc,
236       SAXException JavaDoc {
237     return followXPath(wc.getCurrentPage().getDOM(), xpath, nth);
238   }
239
240   /**
241    * folgt dem <code>nth</code>-ten Anchor unterhalb von root, der von <code>xpath</code> selektiert wird.
242    */

243   public WebResponse followXPath(Node JavaDoc root, DOMXPath xpath, int nth) throws JaxenException,
244       IOException JavaDoc, SAXException JavaDoc {
245     Element JavaDoc anchor = (Element JavaDoc) getNth(root, xpath, nth);
246     String JavaDoc href = anchor.getAttribute("href");
247     WebResponse wr = wc.getCurrentPage();
248     WebLink link = wr.getFirstMatchingLink(matchLink, href);
249     return link.click();
250   }
251
252   public WebResponse followXPath(String JavaDoc xpath, int i) throws JaxenException, IOException JavaDoc,
253       SAXException JavaDoc {
254     return followXPath(new DOMXPath(xpath), i);
255   }
256
257   /* -------------------------------------- checkbox / radiobutton ------------------------------------- */
258
259   /**
260    * setzt die <code>nth</code>-te checkbox in der durch <code>tableID</code>
261    * <code>row</code>, <code>col</code> identifizierten Tabellenzelle. Die Checkbox muss
262    * zu dem Formular mit <code>formID</code> gehoeren (d.h. die Tabelle muss in diesem Formular liegen).
263    * @param formID id Attribut des HTML form Elements
264    * @param tableID id Attribut des HTML table Elements
265    * @param row 0 basierter Index der Tabellenzeile
266    * @param col 0 basierter Index der Tabellenspalte
267    * @param nth 0 basierter Index der Checkboxen innerhalb der Zelle
268    * @param checked
269    * @throws JaxenException
270    * @throws SAXException
271    */

272   public void setCheckBox(String JavaDoc formID, String JavaDoc tableID, int row, int col, int nth, boolean checked)
273       throws JaxenException, SAXException JavaDoc {
274     Element JavaDoc cb = (Element JavaDoc) getNth(getCellNode(tableID, row, col), xpCheckbox, nth);
275     String JavaDoc name = cb.getAttribute("name");
276     String JavaDoc value = cb.getAttribute("value");
277     if (checked)
278       addParameterValue(formID, name, value);
279     else
280       this.removeParameterValue(formID, name, value);
281   }
282
283   public void assertChecked(String JavaDoc formID, String JavaDoc tableID, int row, int col, int nth,
284       boolean checked) throws JaxenException, SAXException JavaDoc {
285     Element JavaDoc cb = (Element JavaDoc) getNth(getCellNode(tableID, row, col), xpCheckbox, nth);
286     String JavaDoc name = cb.getAttribute("name");
287     String JavaDoc value = cb.getAttribute("value");
288     WebForm wf = getForm(formID);
289     String JavaDoc[] values = wf.getParameterValues(name);
290     List JavaDoc valueList = Arrays.asList(values);
291     if (checked)
292       Assert.assertTrue(valueList.contains(value));
293     else
294       Assert.assertFalse(valueList.contains(value));
295   }
296
297   /**
298    * setzt den <code>nth</code>-te radiobutton in der durch <code>tableID</code>
299    * <code>row</code>, <code>col</code> identifizierten Tabellenzelle. Der Radio Button muss
300    * zu dem Formular mit <code>formID</code> gehoeren (d.h. die Tabelle muss in diesem Formular liegen).
301    * dem
302    * @param formID id Attribut des HTML form Elements
303    * @param tableID id Attribut des HTML table Elements
304    * @param row 0 basierter Index der Tabellenzeile
305    * @param col 0 basierter Index der Tabellenspalte
306    * @param nth 0 basierter Index der Radio Buttons innerhalb der Zelle
307    * @throws JaxenException
308    * @throws SAXException
309    */

310   public void setRadioButton(String JavaDoc formID, String JavaDoc tableID, int row, int col, int nth)
311       throws JaxenException, SAXException JavaDoc {
312     Element JavaDoc cb = (Element JavaDoc) getNth(getCellNode(tableID, row, col), xpRadio, nth);
313     String JavaDoc name = cb.getAttribute("name");
314     String JavaDoc value = cb.getAttribute("value");
315     WebForm wf = getForm(formID);
316     // overwrite previous value
317
wf.setParameter(name, value);
318   }
319
320   /* ----------------------------- select / option --------------------------- */
321
322   /**
323    * Selectiert eine Option eines Select Elements.
324    * Ermittelt das <code>nth</code>-te &lt;select ...&gt; Eingabefeld in der
325    * der durch <code>tableID</code>, <code>row</code>, <code>col</code> identifizierten Tabellenzelle.
326    * Das Select Element muss zu dem Formular mit <code>formID</code> gehoeren
327    * (d.h. die Tabelle muss in diesem Formular liegen). Selektiert dann die
328    * <code>optindex</code> option.
329    *
330    * @param formID id Attribut des HTML form Elements
331    * @param tableID id Attribut des HTML table Elements
332    * @param row 0 basierter Index der Tabellenzeile
333    * @param col 0 basierter Index der Tabellenspalte
334    * @param nth 0 basierter Index der select elemente innerhalb der Zelle
335    * @param optind 0 basierter index er zu selektierenden Option
336    * @throws JaxenException
337    * @throws SAXException
338    */

339   public void setSelect1(String JavaDoc formID, String JavaDoc tableID, int row, int col, int nth, int optind)
340       throws JaxenException, SAXException JavaDoc {
341     Element JavaDoc select = (Element JavaDoc) getNth(getCellNode(tableID, row, col), xpSelect, nth);
342     String JavaDoc name = select.getAttribute("name");
343     List JavaDoc options = xpOption.selectNodes(select);
344     Element JavaDoc option = (Element JavaDoc) options.get(optind);
345     String JavaDoc value = option.getAttribute("value");
346     WebForm wf = getForm(formID);
347     // overwrite previous value
348
wf.setParameter(name, value);
349   }
350
351   public void setSelect1(String JavaDoc formID, String JavaDoc tableID, int row, int col, int nth, String JavaDoc optText)
352       throws JaxenException, SAXException JavaDoc {
353     Element JavaDoc select = (Element JavaDoc) getNth(getCellNode(tableID, row, col), xpSelect, nth);
354     String JavaDoc name = select.getAttribute("name");
355     List JavaDoc options = xpOption.selectNodes(select);
356
357     for (Iterator JavaDoc it = options.iterator(); it.hasNext();) {
358       Element JavaDoc option = (Element JavaDoc) it.next();
359       Text text = (Text) option.getChildNodes().item(0);
360       if (optText.equals(text.getData())) {
361         WebForm wf = getForm(formID);
362         // overwrite previous value
363
wf.setParameter(name, option.getAttribute("value"));
364         return;
365       }
366     }
367     throw new IllegalArgumentException JavaDoc("Option \"" + optText + "\" not found");
368   }
369
370   /**
371    * Setzt Eingabe in ein Textfeld.
372    * Ermittelt das <code>nth</code>-te &lt;input type="text" ...&gt; Eingabefeld in der
373    * der durch <code>tableID</code>, <code>row</code>, <code>col</code> identifizierten Tabellenzelle.
374    * Das input Element muss zu dem Formular mit <code>formID</code> gehoeren
375    * (d.h. die Tabelle muss in diesem Formular liegen). Traegt dann
376    * <code>value</code> dort ein.
377    *
378    * @param formID id Attribut des HTML form Elements
379    * @param tableID id Attribut des HTML table Elements
380    * @param row 0 basierter Index der Tabellenzeile
381    * @param col 0 basierter Index der Tabellenspalte
382    * @param nth 0 basierter Index der select elemente innerhalb der Zelle
383    * @param value der simulierte user input
384    * @throws JaxenException
385    * @throws SAXException
386    */

387   public void setTextInput(String JavaDoc formID, String JavaDoc tableID, int row, int col, int nth, String JavaDoc value)
388       throws JaxenException, SAXException JavaDoc {
389     Element JavaDoc input = (Element JavaDoc) getNth(getCellNode(tableID, row, col), xpTextInput, nth);
390     String JavaDoc name = input.getAttribute("name");
391     WebForm wf = getForm(formID);
392     wf.setParameter(name, value);
393   }
394
395   /**
396    * Setzt Eingabe in ein Passwortfeld.
397    * Ermittelt das <code>nth</code>-te &lt;input type="password" ...&gt; Eingabefeld in der
398    * der durch <code>tableID</code>, <code>row</code>, <code>col</code> identifizierten Tabellenzelle.
399    * Das input Element muss zu dem Formular mit <code>formID</code> gehoeren
400    * (d.h. die Tabelle muss in diesem Formular liegen). Traegt dann
401    * <code>value</code> dort ein.
402    *
403    * @param formID id Attribut des HTML form Elements
404    * @param tableID id Attribut des HTML table Elements
405    * @param row 0 basierter Index der Tabellenzeile
406    * @param col 0 basierter Index der Tabellenspalte
407    * @param nth 0 basierter Index der select elemente innerhalb der Zelle
408    * @param value der simulierte user input
409    * @throws JaxenException
410    * @throws SAXException
411    */

412   public void setPassword(String JavaDoc formID, String JavaDoc tableID, int row, int col, int nth, String JavaDoc value)
413       throws JaxenException, SAXException JavaDoc {
414     Element JavaDoc input = (Element JavaDoc) getNth(getCellNode(tableID, row, col), xpPassword, nth);
415     String JavaDoc name = input.getAttribute("name");
416     WebForm wf = getForm(formID);
417     wf.setParameter(name, value);
418   }
419
420   /**
421    * Setzt Eingabe in ein TextArea.
422    * @see #setTextInput
423    */

424   public void setTextArea(String JavaDoc formID, String JavaDoc tableID, int row, int col, int nth, String JavaDoc value)
425       throws JaxenException, SAXException JavaDoc {
426     Element JavaDoc input = (Element JavaDoc) getNth(getCellNode(tableID, row, col), xpTextArea, nth);
427     String JavaDoc name = input.getAttribute("name");
428     WebForm wf = getForm(formID);
429     wf.setParameter(name, value);
430   }
431
432   /* ------------------------------------------- logging ------------------------------------------ */
433
434   private File JavaDoc getDir(String JavaDoc name) {
435     String JavaDoc path = System.getProperty("httpunit.dir") + name;
436     File JavaDoc dir = new File JavaDoc(path);
437     dir.mkdirs();
438     return dir;
439   }
440
441   private void createParentDir(File JavaDoc f) {
442     File JavaDoc dir = f.getParentFile();
443     dir.mkdirs();
444   }
445
446   /**
447    * liefert Log-Verzeichnis fuer den aktuelle Test
448    */

449   public File JavaDoc getLogDir() {
450     return getDir("/log");
451   }
452
453   /**
454    * liefert Verzeichnis mit Referenzdateien fuer Vergleich
455    */

456   public File JavaDoc getRefDir() {
457     return getDir("/ref");
458   }
459
460   /**
461    * Schreibt die aktuelle Seite als HTML Datei ins Log Verzeichnis,
462    * verwendet dabei htmlLogFileEncoding
463    *
464    * @param fileName Dateiname ohne .html Extension
465    * @throws IOException
466    */

467   public void saveHTML(String JavaDoc fileName) throws IOException JavaDoc {
468     saveTextFile(fileName + ".html", htmlLogFileEncoding);
469   }
470
471   private void saveTextFile(String JavaDoc fileName, String JavaDoc encoding) throws IOException JavaDoc {
472     File JavaDoc f = new File JavaDoc(getLogDir(), fileName);
473     createParentDir(f);
474     FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(f);
475     OutputStreamWriter JavaDoc osw = new OutputStreamWriter JavaDoc(fos, encoding);
476     try {
477       osw.write(wc.getCurrentPage().getText());
478     } finally {
479       osw.close();
480     }
481   }
482
483   /**
484    * Schreibt die aktuelle Seite als Textdatei ins Log Verzeichnis
485    * @param fileName Dateiname
486    * @throws IOException
487    */

488   public void saveTextFile(String JavaDoc fileName) throws IOException JavaDoc {
489     File JavaDoc f = new File JavaDoc(getLogDir(), fileName);
490     createParentDir(f);
491     Writer JavaDoc w = new FileWriter JavaDoc(f);
492     w.write(wc.getCurrentPage().getText());
493     w.close();
494   }
495
496   /**
497    * Schreibt die aktuelle Seite binär ins Log Verzeichnis
498    * @param fileName Dateiname
499    * @throws IOException
500    */

501   public void saveBinFile(String JavaDoc fileName) throws IOException JavaDoc {
502     File JavaDoc f = new File JavaDoc(getLogDir(), fileName);
503     createParentDir(f);
504     FileOutputStream JavaDoc out = null;
505     try {
506       out = new FileOutputStream JavaDoc(f);
507
508       InputStream JavaDoc in = null;
509       try {
510         in = wc.getCurrentPage().getInputStream();
511         int c;
512         while ((c = in.read()) >= 0)
513           out.write(c);
514
515       } finally {
516         if (in != null)
517           in.close();
518       }
519     } finally {
520       if (out != null)
521         out.close();
522     }
523   }
524
525   /**
526    * Transformiert die aktuelle Seite mit einem XSL Stylesheet und schreibt
527    * das Ergebnis als XML Datei ins Log Verzeichnis. Das XSL dient dazu,
528    * aus der Seite die wesentlichen Elemente zu extrahieren.
529    *
530    * @param fileName Dateiname ohne .xml Extension
531    * @param xslName Name des Stylesheets fuer Class.getResource()
532    * @param id parameter an das Stylesheet
533    * @param root
534    * @throws TransformerException
535    */

536   public void saveXML(String JavaDoc fileName, String JavaDoc xslName, String JavaDoc id, Node JavaDoc root)
537       throws TransformerException JavaDoc {
538     Transformer JavaDoc trans = getTransformer(xslName);
539     trans.setParameter("id", id);
540     File JavaDoc f = new File JavaDoc(getLogDir(), fileName + ".xml");
541     createParentDir(f);
542     trans.transform(new DOMSource JavaDoc(root), new StreamResult JavaDoc(f));
543   }
544
545   /**
546    * Schreibt Node in eine XML Datei
547    */

548   public void saveXML(String JavaDoc fileName, Node JavaDoc root) throws TransformerException JavaDoc {
549     TransformerFactory JavaDoc tf = TransformerFactory.newInstance();
550     Transformer JavaDoc trans = tf.newTransformer();
551     trans.setOutputProperty(OutputKeys.ENCODING, "iso-8859-1");
552     trans.setOutputProperty(OutputKeys.INDENT, "yes");
553     trans.setOutputProperty(OutputKeys.METHOD, "xml");
554     File JavaDoc f = new File JavaDoc(getLogDir(), fileName + ".xml");
555     createParentDir(f);
556     trans.transform(new DOMSource JavaDoc(root), new StreamResult JavaDoc(f));
557   }
558
559   public void check(String JavaDoc fileName, String JavaDoc xslName, String JavaDoc nodeId) throws IOException JavaDoc,
560       JaxenException, SAXException JavaDoc, TransformerException JavaDoc {
561     saveHTML(fileName);
562     saveXML(fileName, xslName, nodeId, wc.getCurrentPage().getDOM());
563     Assert.assertTrue(fileName, equalsXML(fileName));
564   }
565
566   /**
567    * vergleicht 2 Versionen der Datei in LogDir und RefDir.
568    * @param fileName
569    * @return
570    */

571   public boolean equalsXML(String JavaDoc fileName) {
572     if (isRecordMode())
573       return true;
574     File JavaDoc f1 = new File JavaDoc(getRefDir(), fileName + ".xml");
575     File JavaDoc f2 = new File JavaDoc(getLogDir(), fileName + ".xml");
576     return xmlDiff.equals(f1, f2);
577   }
578
579   /**
580    * vergleicht 2 Versionen der Datei in LogDir und RefDir.
581    * @param fileName
582    * @return
583    */

584   public boolean equalsFile(String JavaDoc fileName) throws IOException JavaDoc {
585     return equalsFile(fileName, null);
586   }
587
588   /**
589    * Erzeugt aus einer PDF-Datei eine Textdatei und vergleicht diese mit der Referenz.
590    */

591   public boolean equalsPdfFile(String JavaDoc fileName, FileDiffHandler handler) throws Exception JavaDoc {
592     if (isRecordMode())
593       return true;
594     if (!fileName.endsWith(".pdf"))
595       throw new RuntimeException JavaDoc("Error: pdf file name expected " + fileName);
596
597     String JavaDoc textName = fileName.substring(0, fileName.length() - 4) + ".txt";
598     File JavaDoc pdfFile = new File JavaDoc(getLogDir(), fileName);
599     File JavaDoc textFile = new File JavaDoc(getLogDir(), textName);
600
601     ExtractText.main(new String JavaDoc[] { pdfFile.getAbsolutePath(), textFile.getAbsolutePath()});
602
603     return equalsFile(textName, handler);
604   }
605
606   /**
607    * vergleicht 2 Versionen der Datei in LogDir und RefDir.
608    * @param fileName
609    * @return
610    */

611   public boolean equalsFile(String JavaDoc fileName, FileDiffHandler handler) throws IOException JavaDoc {
612     if (isRecordMode())
613       return true;
614     File JavaDoc f1 = new File JavaDoc(getRefDir(), fileName);
615     File JavaDoc f2 = new File JavaDoc(getLogDir(), fileName);
616     FileDiff fileDiff = new FileDiff();
617     return fileDiff.equalsFiles(f1, f2, handler);
618   }
619
620   private Transformer JavaDoc getTransformer(String JavaDoc name) throws TransformerConfigurationException JavaDoc {
621     TransformerFactory JavaDoc tf = TransformerFactory.newInstance();
622     URL JavaDoc url = this.getClass().getResource(name);
623     Templates JavaDoc templates = tf.newTemplates(new StreamSource JavaDoc(url.toExternalForm()));
624     return templates.newTransformer();
625   }
626
627   /**
628    * true = Dateivergleich liefert immer true
629    */

630   public boolean isRecordMode() {
631     return recordMode;
632   }
633
634   /**
635    * true = Dateivergleich liefert immer true
636    */

637   public void setRecordMode(boolean b) {
638     recordMode = b;
639   }
640
641   public WebConversation setWc(WebConversation wc) {
642     WebConversation oldWc = wc;
643     this.wc = wc;
644     return oldWc;
645   }
646
647   /**
648    * @return den Fragement Identifier der aktuellen URL oder null
649    */

650   public String JavaDoc getFragmentIdentifier() {
651     String JavaDoc url = wc.getCurrentPage().getURL().toExternalForm();
652     int pos = url.indexOf('#');
653     if (pos < 0)
654       return null;
655     return url.substring(pos + 1);
656   }
657
658   public XmlDiff getXmlDiff() {
659     return xmlDiff;
660   }
661
662   public String JavaDoc getHtmlLogFileEncoding() {
663     return htmlLogFileEncoding;
664   }
665
666   public void setHtmlLogFileEncoding(String JavaDoc htmlLogFileEncoding) {
667     this.htmlLogFileEncoding = htmlLogFileEncoding;
668   }
669 }
Popular Tags