KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > comp > TestBTextRender


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: TestBTextRender.java,v 1.11 2004/02/02 22:05:13 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.comp;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.w3c.dom.*;
26 import junit.framework.*;
27
28 //import org.enhydra.barracuda.plankton.*;
29
//import org.enhydra.barracuda.plankton.data.*;
30
import org.enhydra.barracuda.core.util.dom.*;
31 import org.apache.log4j.*;
32 import org.enhydra.barracuda.core.view.*;
33 import org.enhydra.barracuda.examples.xmlc.*;
34 import org.enhydra.barracuda.testbed.*;
35 import org.enhydra.barracuda.testbed.servlet.*;
36
37
38 /**
39  * This test verifies that BText works correctly.
40  */

41 public class TestBTextRender extends DefaultTestCase {
42     //common vars (customize for every test class)
43
private static String JavaDoc testClass = TestBTextRender.class.getName();
44     private static Logger logger = Logger.getLogger("test."+testClass);
45
46     //variables
47

48     //-------------------- Basics --------------------------------
49
/**
50      * Public Constructor
51      */

52     public TestBTextRender(String JavaDoc name) {
53         super(name);
54     }
55
56     /**
57      * Every test class should have a main method so it can be run
58      * directly (when debugging tests you often will not want to run
59      * the whole suite)
60      *
61      * @param args defined in test.util.TestUtil
62      */

63     public static void main(String JavaDoc args[]) {
64         //check for standard runtime parameters
65
TestUtil.parseParams(args);
66
67         //launch the test
68
if (TestUtil.BATCH_MODE) junit.textui.TestRunner.main(new String JavaDoc[] {testClass});
69         else junit.swingui.TestRunner.main(new String JavaDoc[] {testClass});
70     }
71
72     
73     //-------------------- Actual Tests --------------------------
74
//Note: all the methods herein should follow the testXXXX naming convention
75
//Also keep in mind that local vars set in one test method are NOT retained
76
//when the next method is invoked because JUnit makes a separate instance of
77
//the test class for each testXXXX method!!!
78

79     /**
80      * Simple test to verify that the component renders as expected
81      */

82     public void testBTextRender() throws IOException, RenderException {
83         //render the doc
84
if (logger.isInfoEnabled()) logger.info("testing BText render");
85         CompEx7 cg = new CompEx7();
86         BComponent root = new BComponent();
87         root.initCycle();
88
89         //build a sample view capabilities object
90
MockHttpServletRequest req = new MockHttpServletRequest();
91         req.setHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
92         ViewCapabilities vcap = new ViewCapabilities(req, null);
93 //System.out.println(vcap.toString());
94

95         //create the component amd render
96
ViewContext vc = new DefaultViewContext(vcap, req, null);
97         Document doc = cg.handleDefault(root, vc, req, null);
98         root.render(vc);
99         root.destroyCycle();
100         DefaultDOMWriter ddw = new DefaultDOMWriter(false);
101 // ddw.write(doc, System.out);
102
StringWriter sw = new StringWriter(2000);
103         ddw.write(doc, sw);
104         String JavaDoc html = sw.toString();
105 System.out.println("html:"+html);
106
107         //assert that the text got rendered as we expected
108
int fpos = html.indexOf("Text1: <strong>bold</strong>");
109         assertTrue("BText render err1", fpos>0);
110         fpos = html.indexOf("<em>zing1</em>");
111         assertTrue("BText render err2", fpos>0);
112         fpos = html.indexOf("Text2: <strong>bold</strong>");
113         assertTrue("BText render err3", fpos>0);
114         fpos = html.indexOf("<em>zing2</em>");
115         assertTrue("BText render err4", fpos>0);
116         //...make sure custom attrs are retained
117
fpos = html.indexOf("customattr1");
118         assertTrue("BText render err5.1", fpos>0);
119         fpos = html.indexOf("customattr2");
120         assertTrue("BText render err5.2", fpos>0);
121         fpos = html.indexOf("customattr3");
122         assertTrue("BText render err5.3", fpos>0);
123         fpos = html.indexOf("customattr4");
124         assertTrue("BText render err5.4", fpos>0);
125     }
126 }
127
Popular Tags