KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > shim > SiteContextActionTest


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.shim;
22
23 import java.util.*;
24 import java.sql.*;
25 import junit.framework.*;
26 import org.apache.log4j.*;
27 import com.methodhead.persistable.*;
28 import com.methodhead.test.*;
29 import servletunit.struts.*;
30 import org.apache.struts.action.*;
31 import org.apache.cactus.*;
32 import com.methodhead.sitecontext.*;
33 import java.io.*;
34 import com.methodhead.auth.*;
35 import com.methodhead.util.*;
36 import com.methodhead.*;
37
38 public class SiteContextActionTest extends CactusStrutsTestCase {
39
40   SiteContext siteContext1_ = null;
41   SiteContext siteContext2_ = null;
42   HtmlFragment fragment1_ = null;
43
44   Panel panel = null;
45   Page page1_ = null;
46   Page page = null;
47   List list = null;
48
49   SiteContext siteContext = null;
50   File file = null;
51
52   private void createData() {
53     
54     //
55
// create two site contexts (resource directories already exist for ids 0,
56
// 1, and 2).
57
//
58
siteContext1_ = new SiteContext();
59     siteContext1_.getDomains().add( "test.com" );
60     siteContext1_.saveNew();
61
62     siteContext2_ = new SiteContext();
63     siteContext2_.getDomains().add( "test2.com" );
64     siteContext2_.saveNew();
65
66     panel = new Panel();
67     panel.setName( "body" );
68     panel.setModuleClass( "com.methodhead.shim.MockModule" );
69
70     page1_ = new Page();
71     page1_.set( "title", "Page1" );
72     page1_.set( "template", "template.jsp" );
73     page1_.addPanel( panel );
74     page1_.setSiteContext( siteContext1_ );
75     page1_.saveNew();
76
77     fragment1_ = new HtmlFragment();
78     fragment1_.setSiteContext( siteContext2_ );
79     fragment1_.saveNew();
80   }
81
82   static {
83     TestUtils.initLogger();
84     TestUtils.initDb();
85   }
86
87   public SiteContextActionTest( String JavaDoc name ) {
88     super( name );
89   }
90
91   public void setUp() {
92     try {
93       super.setUp();
94
95       ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) );
96
97       TestData.createUsers();
98       AuthUtil.setUser( request, TestData.user1 );
99     }
100     catch ( Exception JavaDoc e ) {
101       fail( e.getMessage() );
102     }
103   }
104
105   public void tearDown()
106   throws
107     Exception JavaDoc {
108     super.tearDown();
109   }
110
111   public void testDoSaveNew() {
112     try {
113       createData();
114
115       setRequestPathInfo( "/siteContext" );
116       addRequestParameter( "action", "saveNew" );
117       addRequestParameter( "domainsText", "danhensgen.com" );
118       addRequestParameter( "path", "" );
119       actionPerform();
120
121       verifyForwardPath( "/siteContext.do?action=list" );
122
123       siteContext = new SiteContext();
124       assertTrue( siteContext.loadForDomainAndPath( "danhensgen.com", "" ) );
125
126       file = ServletUtils.getRealFile( request, "/" + siteContext.get( "id" ) );
127       assertTrue( file.exists() );
128       assertTrue( file.isDirectory() );
129
130       file = ServletUtils.getRealFile( request, "/WEB-INF/resources/" + siteContext.get( "id" ) );
131       assertTrue( file.exists() );
132       assertTrue( file.isDirectory() );
133
134       file = ServletUtils.getRealFile( request, "/WEB-INF/resources/" + siteContext.get( "id" ) + "/templates" );
135       assertTrue( file.exists() );
136       assertTrue( file.isDirectory() );
137     }
138     catch ( Exception JavaDoc e ) {
139       e.printStackTrace();
140       fail();
141     }
142   }
143
144   public void testDoDelete() {
145     try {
146       createData();
147
148       setRequestPathInfo( "/siteContext" );
149       addRequestParameter( "action", "delete" );
150       addRequestParameter( "id", "" + siteContext2_.get( "id" ) );
151       actionPerform();
152
153       verifyForwardPath( "/siteContext.do?action=list" );
154
155       siteContext = new SiteContext();
156       assertTrue( !siteContext.loadForDomainAndPath( "test2.com", "" ) );
157
158       page = new Page();
159       page.setSiteContext( siteContext2_ );
160       list = page.loadAll();
161       assertEquals( 0, list.size() );
162
163       file = ServletUtils.getRealFile( request, "/" + siteContext2_.get( "id" ) );
164       assertTrue( !file.exists() );
165
166       file = ServletUtils.getRealFile( request, "/WEB-INF/resources/" + siteContext2_.get( "id" ) );
167       assertTrue( !file.exists() );
168     }
169     catch ( Exception JavaDoc e ) {
170       e.printStackTrace();
171       fail();
172     }
173   }
174 }
175
Popular Tags