KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > jsp > AbstractUITagTest


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.jsp;
6
7 import com.opensymphony.webwork.ServletActionContext;
8 import com.opensymphony.xwork.ActionContext;
9
10 import java.io.InputStream JavaDoc;
11 import java.net.URL JavaDoc;
12 import java.util.StringTokenizer JavaDoc;
13
14
15 /**
16  * @author Matt Ho <a HREF="mailto:matt@indigoegg.com">&lt;matt@indigoegg.com&gt;</a>
17  * @version $Id: AbstractUITagTest.java,v 1.13 2005/06/25 14:56:54 plightbo Exp $
18  */

19 public abstract class AbstractUITagTest extends AbstractTagTest {
20     //~ Methods ////////////////////////////////////////////////////////////////
21

22     /**
23      * Attempt to verify the contents of this.writer against the contents of the URL specified. verify() performs a
24      * trim on both ends
25      *
26      * @param url the HTML snippet that we want to validate against
27      * @throws Exception if the validation failed
28      */

29     public void verify(URL JavaDoc url) throws Exception JavaDoc {
30         if (url == null) {
31             fail("unable to verify a null URL");
32         } else if (this.writer == null) {
33             fail("AbstractJspWriter.writer not initialized. Unable to verify");
34         }
35
36         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(128);
37         InputStream JavaDoc in = url.openStream();
38         byte[] buf = new byte[4096];
39         int nbytes;
40
41         while ((nbytes = in.read(buf)) > 0) {
42             buffer.append(new String JavaDoc(buf, 0, nbytes));
43         }
44
45         in.close();
46
47         /**
48          * compare the trimmed values of each buffer and make sure they're equivalent. however, let's make sure to
49          * normalize the strings first to account for line termination differences between platforms.
50          */

51         String JavaDoc writerString = writer.toString();
52         String JavaDoc bufferString = buffer.toString();
53
54         assertEquals(bufferString, writerString);
55     }
56
57     protected void setUp() throws Exception JavaDoc {
58         super.setUp();
59
60         ServletActionContext.setServletContext(pageContext.getServletContext());
61     }
62
63     protected void tearDown() throws Exception JavaDoc {
64         super.tearDown();
65         ActionContext.setContext(null);
66     }
67
68     /**
69      * normalizes a string so that strings generated on different platforms can be compared. any group of one or more
70      * space, tab, \r, and \n characters are converted to a single space character
71      *
72      * @param obj the object to be normalized. normalize will perform its operation on obj.toString().trim() ;
73      * @return the normalized string
74      */

75     private String JavaDoc normalize(Object JavaDoc obj) {
76         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(obj.toString().trim(), " \t\r\n");
77         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(128);
78
79         while (st.hasMoreTokens()) {
80             buffer.append(st.nextToken());
81
82             if (st.hasMoreTokens()) {
83                 buffer.append(" ");
84             }
85         }
86
87         return buffer.toString();
88     }
89 }
90
Popular Tags