KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > bug925693 > Bug925693CactusTest


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.bug925693;
17
18 import javax.faces.FactoryFinder;
19 import javax.faces.application.Application;
20 import javax.faces.context.FacesContext;
21 import javax.faces.context.FacesContextFactory;
22 import javax.faces.el.ValueBinding;
23 import javax.faces.lifecycle.Lifecycle;
24 import javax.faces.lifecycle.LifecycleFactory;
25 import javax.faces.webapp.FacesServlet;
26 import javax.servlet.RequestDispatcher JavaDoc;
27
28 import org.apache.cactus.ServletTestCase;
29 import org.apache.cactus.WebRequest;
30
31 import com.meterware.httpunit.WebResponse;
32 import com.meterware.httpunit.WebTable;
33
34 public class Bug925693CactusTest extends ServletTestCase {
35
36     public static void main(String JavaDoc[] args) {
37         junit.textui.TestRunner.run(Bug925693CactusTest.class);
38     }
39
40     protected void setUp() throws Exception JavaDoc {
41         super.setUp();
42     }
43
44     protected void tearDown() throws Exception JavaDoc {
45         super.tearDown();
46     }
47
48     public Bug925693CactusTest(String JavaDoc name) {
49         super(name);
50     }
51
52     public void testSimpleRender() throws Exception JavaDoc {
53         RequestDispatcher JavaDoc rd = config.getServletContext().getRequestDispatcher(
54                 "/home.jsf");
55         // render the page for the first time
56
rd.forward(request, response);
57     }
58
59     public void endSimpleRender(WebResponse response) throws Exception JavaDoc {
60         WebTable tables[] = response.getTables();
61         assertEquals(1, tables.length);
62         WebTable table = tables[0];
63         System.err.println("table = " + table);
64         // the table has 2 rows because of the way that httpunit parses it
65
assertEquals(2, table.getRowCount());
66     }
67
68     public void beginAddARow(WebRequest request) throws Exception JavaDoc {
69     }
70
71     public void testAddARow() throws Exception JavaDoc {
72         // simulates the config of the faces context
73
FacesContext ctx = performFacesContextConfig();
74         // this block simulates a button click by adding a line item to the invoice
75
addLineItem(ctx);
76         // render the page
77
RequestDispatcher JavaDoc rd = config.getServletContext().getRequestDispatcher(
78                 "/home.jsf");
79         // render the page for the first time
80
rd.forward(request, response);
81     }
82
83     private void addLineItem(FacesContext ctx) {
84         Application app = ctx.getApplication();
85         ValueBinding binding = app.createValueBinding("#{invoice}");
86         Invoice invoice = (Invoice) binding.getValue(ctx);
87         invoice.addLineItem();
88     }
89
90     private FacesContext performFacesContextConfig() {
91         LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
92                 .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
93         Lifecycle lifecycle = lifecycleFactory.getLifecycle(getLifecycleId());
94         FacesContextFactory facesCtxFactory = (FacesContextFactory) FactoryFinder
95                 .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
96         FacesContext ctx = facesCtxFactory.getFacesContext(config
97                 .getServletContext(), request, response, lifecycle);
98         return ctx;
99     }
100
101     private String JavaDoc getLifecycleId() {
102         String JavaDoc lifecycleId = this.config.getServletContext().getInitParameter(
103                 FacesServlet.LIFECYCLE_ID_ATTR);
104         return lifecycleId != null ? lifecycleId
105                 : LifecycleFactory.DEFAULT_LIFECYCLE;
106     }
107
108     public void endAddARow(WebResponse response) throws Exception JavaDoc {
109         WebTable tables[] = response.getTables();
110         assertEquals(1, tables.length);
111         WebTable table = tables[0];
112         System.err.println("table = " + table);
113         // the table has 3 rows because of the way that httpunit parses it
114
assertEquals(3, table.getRowCount());
115     }
116
117 }
Popular Tags