1   /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   *
19   * For further informations on the SmartWeb Framework please visit
20   *
21   *                        http://smartweb.sourceforge.net
22   */
23  package net.smartlab.web;
24  
25  import java.io.File;
26  import java.text.ParseException;
27  import java.util.Arrays;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import net.sf.ezmorph.test.ArrayAssertions;
33  import net.smartlab.web.DataAccessObject.SearchInfo;
34  
35  import org.apache.struts.action.ActionForm;
36  import org.apache.struts.action.ActionMapping;
37  
38  import servletunit.HttpServletRequestSimulator;
39  import servletunit.struts.MockStrutsTestCase;
40  
41  /**
42   * @TODO documentation
43   * @author rlogiacco@smartlab.net
44   */
45  public class AbstractArchiveActionTest extends MockStrutsTestCase {
46  
47  	/**
48  	 * @see junit.framework.TestCase#setUp()
49  	 */
50  	protected void setUp() throws Exception {
51  		super.setUp();
52  		super.setContextDirectory(new File("res/test"));
53  		super.setConfigFile("/WEB-INF/struts.xml");
54  		super.setServletConfigFile("/WEB-INF/web.xml");
55  	}
56  
57  	/**
58  	 * Test method for
59  	 * {@link net.smartlab.web.AbstractArchiveAction#getListSelection(javax.servlet.ServletRequest)}
60  	 * .
61  	 * 
62  	 * @throws ParseException
63  	 */
64  	public void testGetListSelection() throws ParseException {
65  		HttpServletRequestSimulator request = super.getMockRequest();
66  		request.addParameter("selection", new String[] {"1", "2", "3"});
67  		String[] selection = AbstractArchiveAction.getListSelection(request);
68  		ArrayAssertions.assertEquals(new String[] {"1", "2", "3"}, selection);
69  	}
70  
71  	/**
72  	 * Test method for
73  	 * {@link net.smartlab.web.AbstractArchiveAction#getSelection(org.apache.struts.action.ActionForm, java.lang.String)}
74  	 * .
75  	 */
76  	public void testGetSelection() {
77  		super.setRequestPathInfo("/list");
78  		super.addRequestParameter("selection", new String[] {"1", "2", "3"});
79  		super.actionPerform();
80  		super.verifyForward("success");
81  		super.verifyNoActionErrors();
82  	}
83  
84  	/**
85  	 * Test method for
86  	 * {@link net.smartlab.web.AbstractArchiveAction#setSelection(org.apache.struts.action.ActionForm, java.lang.String, java.lang.String[])}
87  	 * .
88  	 */
89  	public void testSetSelection() {
90  		super.setRequestPathInfo("/set");
91  		super.addRequestParameter("selection", new String[] {"1", "2", "3"});
92  		super.actionPerform();
93  		super.verifyForward("success");
94  		super.verifyNoActionErrors();
95  	}
96  
97  	/**
98  	 * Test method for
99  	 * {@link net.smartlab.web.AbstractArchiveAction#resetSelection(org.apache.struts.action.ActionForm, java.lang.String)}
100 	 * .
101 	 */
102 	public void testResetSelection() {
103 		super.setRequestPathInfo("/reset");
104 		super.addRequestParameter("selection", new String[] {"1", "2", "3"});
105 		super.actionPerform();
106 		super.verifyForward("success");
107 		super.verifyNoActionErrors();
108 	}
109 
110 	/**
111 	 * Test method for
112 	 * {@link net.smartlab.web.AbstractArchiveAction#getSearchInfo(javax.servlet.http.HttpServletRequest)}
113 	 * .
114 	 */
115 	public void testGetSearchInfo() {
116 		super.setRequestPathInfo("/reset");
117 		super.addRequestParameter("selection", new String[] {"1", "2", "3"});
118 		super.actionPerform();
119 		super.verifyForward("success");
120 		super.verifyNoActionErrors();
121 	}
122 
123 
124 	public static class Stub extends AbstractArchiveAction {
125 
126 		public String remove(ActionForm form, HttpServletRequest request, HttpServletResponse response,
127 				ActionMapping mapping) throws Exception {
128 			return null;
129 		}
130 
131 		public String removeAll(ActionForm form, HttpServletRequest request, HttpServletResponse response,
132 				ActionMapping mapping) throws Exception {
133 			return null;
134 		}
135 
136 		public String search(ActionForm form, HttpServletRequest request, HttpServletResponse response,
137 				ActionMapping mapping) throws Exception {
138 			SearchInfo info = AbstractArchiveAction.getSearchInfo(request);
139 			if (info.getFilters().size() == 5 && info.getOrder().equals("display") && info.isDescendant()
140 					&& !info.isUnion()) {
141 				return "success";
142 			} else {
143 				throw new Exception(info.toString());
144 			}
145 		}
146 
147 		public String select(ActionForm form, HttpServletRequest request, HttpServletResponse response,
148 				ActionMapping mapping) throws Exception {
149 			return null;
150 		}
151 
152 		public String update(ActionForm form, HttpServletRequest request, HttpServletResponse response,
153 				ActionMapping mapping) throws Exception {
154 			return null;
155 		}
156 
157 		public String listSelection(ActionForm form, HttpServletRequest request, HttpServletResponse response,
158 				ActionMapping mapping) throws Exception {
159 			String[] list = AbstractArchiveAction.getListSelection(request);
160 			if (list.length == 3 && Arrays.equals(new String[] {"1", "2", "3"}, list)) {
161 				return "success";
162 			} else {
163 				throw new Exception(list.toString());
164 			}
165 		}
166 
167 		public String setSelection(ActionForm form, HttpServletRequest request, HttpServletResponse response,
168 				ActionMapping mapping) throws Exception {
169 			String[] list = AbstractArchiveAction.getListSelection(request);
170 			if (list.length == 3 && Arrays.equals(new String[] {"1", "2", "3"}, list)) {
171 				return "success";
172 			} else {
173 				throw new Exception(list.toString());
174 			}
175 		}
176 
177 		public String resetSelection(ActionForm form, HttpServletRequest request, HttpServletResponse response,
178 				ActionMapping mapping) throws Exception {
179 			String[] list = AbstractArchiveAction.getListSelection(request);
180 			if (list.length == 3 && Arrays.equals(new String[] {"1", "2", "3"}, list)) {
181 				return "success";
182 			} else {
183 				throw new Exception(list.toString());
184 			}
185 		}
186 	}
187 }