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.math.BigDecimal;
27  import java.math.BigInteger;
28  import java.util.Calendar;
29  import java.util.Locale;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  import org.apache.commons.beanutils.PropertyUtils;
35  import org.apache.commons.lang.builder.EqualsBuilder;
36  import org.apache.commons.lang.builder.HashCodeBuilder;
37  import org.apache.commons.lang.builder.ToStringBuilder;
38  import org.apache.struts.Globals;
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionForward;
41  import org.apache.struts.action.ActionMapping;
42  import org.apache.struts.validator.DynaValidatorForm;
43  
44  import servletunit.struts.MockStrutsTestCase;
45  
46  /**
47   * @TODO documentation
48   * @author rlogiacco
49   */
50  public class ActionTest extends MockStrutsTestCase {
51  
52  	private Stub stub = new Stub();
53  
54  	private Bean beanEmpty;
55  
56  	private Bean beanFull;
57  
58  	private Form formEmpty;
59  
60  	private Form formFull;
61  	
62  	private NestedBean nestedBeanEmpty;
63  
64  	private NestedBean nestedBeanFull;
65  	
66  
67  	/**
68  	 * @see junit.framework.TestCase#setUp()
69  	 */
70  	protected void setUp() throws Exception {
71  		super.setUp();
72  		super.setContextDirectory(new File("res/test"));
73  		super.setConfigFile("/WEB-INF/struts.xml");
74  		super.setServletConfigFile("/WEB-INF/web.xml");
75  		beanEmpty = new Bean();
76  		beanFull = new Bean();
77  		beanFull.bool = true;
78  		beanFull.b = 5;
79  		beanFull.s = 6;
80  		beanFull.i = 100;
81  		beanFull.l = 5000;
82  		beanFull.f = 300.345f;
83  		beanFull.d = 345.3004;
84  		Calendar calendar = Calendar.getInstance();
85  		calendar.set(2008, Calendar.DECEMBER, 31, 00, 00, 00);
86  		calendar.set(Calendar.MILLISECOND, 00);
87  		beanFull.sd = new java.sql.Date(calendar.getTimeInMillis());
88  		calendar.set(2008, Calendar.DECEMBER, 31, 12, 30, 00);
89  		beanFull.ud = calendar.getTime();
90  		beanFull.st = new java.sql.Time(calendar.getTimeInMillis());
91  		beanFull.ts = new java.sql.Timestamp(calendar.getTimeInMillis());
92  		beanFull.bi = new BigInteger("123456789013456789012345678901234567890");
93  		beanFull.bd = new BigDecimal(".123456789013456789012345678901234567890");
94  		beanFull.en = EnumerationTest.Mock.FIRST;
95  		formEmpty = new Form();
96  		formFull = new Form();
97  		formFull.bool = "true";
98  		formFull.b = "5";
99  		formFull.s = "6";
100 		formFull.i = "100";
101 		formFull.l = "5000";
102 		formFull.f = "300.345";
103 		formFull.d = "345.3004";
104 		formFull.ud = "31/12/2008 12.30.00";
105 		formFull.sd = "31/12/2008";
106 		formFull.st = "12.30.00";
107 		formFull.ts = "31/12/2008 12.30.00";
108 		formFull.bi = "123456789013456789012345678901234567890";
109 		formFull.bd = ".123456789013456789012345678901234567890";
110 		formFull.en = "1";
111 		
112 		nestedBeanEmpty=new NestedBean();
113 		nestedBeanFull=new NestedBean();
114 		Name n=new Name();
115 		n.setFirstname("firstname1");
116 		n.setLastname("lastname1");
117 		nestedBeanFull.setName(n);
118 	}
119 
120 	/**
121 	 * Test method for
122 	 * {@link net.smartlab.web.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}
123 	 * .
124 	 */
125 	public void testExecuteActionMappingActionFormHttpServletRequestHttpServletResponse() {
126 		super.setRequestPathInfo("/action");
127 		super.actionPerform();
128 		super.verifyForward("success");
129 		super.verifyNoActionErrors();
130 		/*
131 		 * FIXME super.setRequestPathInfo("/action");
132 		 * super.addRequestParameter("exec", "error"); super.actionPerform();
133 		 * super.verifyForward("error"); super.verifyNoActionErrors();
134 		 */
135 		super.clearRequestParameters();
136 		super.setRequestPathInfo("/action");
137 		super.getRequest().setAttribute(Globals.CANCEL_KEY, Boolean.TRUE);
138 		super.actionPerform();
139 		super.verifyForwardPath("/input.jsp");
140 		super.verifyNoActionErrors();
141 	}
142 
143 	/**
144 	 * Test method for
145 	 * {@link net.smartlab.web.Action#cancel(org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.apache.struts.action.ActionMapping)}
146 	 * .
147 	 */
148 	public void testCancel() {
149 		fail("Not yet implemented");
150 	}
151 
152 	/**
153 	 * Test method for
154 	 * {@link net.smartlab.web.Action#valorize(org.apache.struts.action.ActionForm, java.lang.Object, java.util.Locale)}
155 	 * .
156 	 */
157 	public void testValorize() throws Exception {
158 		Locale locale = Locale.ITALY;
159 		stub.valorize(formFull, beanEmpty, locale);
160 		super.assertEquals(beanFull, beanEmpty);
161 	}
162 	public void testValorizeNested() throws Exception {
163 		super.setRequestPathInfo("/action-valorize");
164 		super.actionPerform();
165 		
166 		// copy all the properties of my nestedBeanFull into the form
167 		PropertyUtils.copyProperties(super.getActionForm(), nestedBeanFull);
168 		
169 		//try to valorize my empty bean
170 		Locale locale = Locale.ITALY;
171 		stub.valorize(super.getActionForm(), nestedBeanEmpty, locale);
172 		
173 		//verify if the nestedBeanEmpty that has been valorized contains the same values as nestedBeanFull
174 		assertEquals(nestedBeanEmpty, nestedBeanFull);
175 	}
176 	public void testValorizeNestedPropertyUtils() throws Exception {
177 		super.setRequestPathInfo("/action-valorize");
178 		super.actionPerform();
179 		// copy all the properties of my nestedBeanFull into the form
180 		PropertyUtils.copyProperties(super.getActionForm(), nestedBeanFull);
181 		// copy all the properties from the form to my emplty bean nestedBeanEmpty
182 		PropertyUtils.copyProperties(nestedBeanEmpty,super.getActionForm());
183 		//verify if the nestedBeanEmpty that has been valorized contains the same values as nestedBeanFull
184 		assertEquals(nestedBeanEmpty, nestedBeanFull);
185 	}
186 	
187 	/**
188 	 * Test method for
189 	 * {@link net.smartlab.web.Action#populate(org.apache.struts.action.ActionForm, java.lang.Object, java.util.Locale)}
190 	 * .
191 	 */
192 	public void testPopulate() throws Exception {
193 		Locale locale = Locale.ITALY;
194 		stub.populate(formEmpty, beanFull, locale);
195 		super.assertEquals(formFull, formEmpty);
196 	}
197 	public void testPopulateNested() throws Exception {
198 		super.setRequestPathInfo("/action-valorize");
199 		super.actionPerform();
200 		
201 		//try to populate my empty form
202 		Locale locale = Locale.ITALY;
203 		stub.populate(super.getActionForm(), nestedBeanFull, locale);
204 		
205 		//verify if the nested property name in the form has been populated correctly
206 		Name value_name=(Name)((DynaValidatorForm)super.getActionForm()).get("name");
207 		assertEquals(nestedBeanFull.getName().getFirstname(),value_name.getFirstname());
208 		assertEquals(nestedBeanFull.getName().getLastname(),value_name.getLastname());
209 	}
210 	public void testPopulateNestedPropertyUtils() throws Exception {
211 		super.setRequestPathInfo("/action-valorize");
212 		super.actionPerform();
213 		// copy all the properties of my nestedBeanFull into the form
214 		PropertyUtils.copyProperties(super.getActionForm(), nestedBeanFull);
215 		//verify if the nested property name in the form has been populated
216 		Name value_name=(Name)((DynaValidatorForm)super.getActionForm()).get("name");
217 		assertEquals(nestedBeanFull.getName().getFirstname(),value_name.getFirstname());
218 		assertEquals(nestedBeanFull.getName().getLastname(),value_name.getLastname());
219 	}
220 	
221 	/**
222 	 * Test method for
223 	 * {@link net.smartlab.web.Action#reset(org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, org.apache.struts.action.ActionMapping)}
224 	 * .
225 	 */
226 	public void testReset() {
227 		stub.reset(formFull, null, null);
228 		super.assertEquals(formEmpty, formFull);
229 	}
230 
231 	/**
232 	 * Test method for {@link net.smartlab.web.Action#getProperties()}.
233 	 */
234 	public void testGetProperties() {
235 		fail("Not yet implemented");
236 	}
237 	public class NestedBean{
238 		public Name name;
239 		
240 		public Name getName() {
241 			return name;
242 		}
243 		
244 		public void setName(Name name) {
245 			this.name = name;
246 		}
247 		public boolean equals( Object other) {
248 			if (!(other instanceof NestedBean))
249 				return false;
250 			NestedBean castOther = (NestedBean)other;
251 			return new EqualsBuilder().append(name, castOther.name).isEquals();
252 		}
253 	}
254 	public class Name {
255 		public String firstname;
256 		public String lastname;
257 		
258 		public Name() {
259 			firstname="";
260 			lastname="";
261 		}
262 		public String getFirstname() {
263 			return firstname;
264 		}
265 		
266 		public void setFirstname(String firstname) {
267 			this.firstname = firstname;
268 		}
269 		
270 		public String getLastname() {
271 			return lastname;
272 		}
273 		
274 		public void setLastname(String lastname) {
275 			this.lastname = lastname;
276 		}
277 		public boolean equals( Object other) {
278 			if (!(other instanceof Name))
279 				return false;
280 			Name castOther = (Name)other;
281 			return new EqualsBuilder().append(firstname, castOther.firstname).append(lastname, castOther.lastname).isEquals();
282 		}
283 	}
284 	public class Form extends ActionForm {
285 
286 		private static final long serialVersionUID = 1L;
287 
288 		private String bool;
289 
290 		private String b;
291 
292 		private String s;
293 
294 		private String i;
295 
296 		private String l;
297 
298 		private String f;
299 
300 		private String d;
301 
302 		private String ud;
303 
304 		private String sd;
305 
306 		private String st;
307 
308 		private String ts;
309 
310 		private String bi;
311 
312 		private String bd;
313 
314 		private String en;
315 
316 
317 		public String getB() {
318 			return b;
319 		}
320 
321 		public void setB(String b) {
322 			this.b = b;
323 		}
324 
325 		public String getBool() {
326 			return bool;
327 		}
328 
329 		public void setBool(String bool) {
330 			this.bool = bool;
331 		}
332 
333 		public String getD() {
334 			return d;
335 		}
336 
337 		public void setD(String d) {
338 			this.d = d;
339 		}
340 
341 		public String getF() {
342 			return f;
343 		}
344 
345 		public void setF(String f) {
346 			this.f = f;
347 		}
348 
349 		public String getI() {
350 			return i;
351 		}
352 
353 		public void setI(String i) {
354 			this.i = i;
355 		}
356 
357 		public String getL() {
358 			return l;
359 		}
360 
361 		public void setL(String l) {
362 			this.l = l;
363 		}
364 
365 		public String getS() {
366 			return s;
367 		}
368 
369 		public void setS(String s) {
370 			this.s = s;
371 		}
372 
373 		public String getBd() {
374 			return bd;
375 		}
376 
377 		public void setBd(String bd) {
378 			this.bd = bd;
379 		}
380 
381 		public String getBi() {
382 			return bi;
383 		}
384 
385 		public void setBi(String bi) {
386 			this.bi = bi;
387 		}
388 
389 		public String getSd() {
390 			return sd;
391 		}
392 
393 		public void setSd(String sd) {
394 			this.sd = sd;
395 		}
396 
397 		public String getSt() {
398 			return st;
399 		}
400 
401 		public void setSt(String st) {
402 			this.st = st;
403 		}
404 
405 		public String getTs() {
406 			return ts;
407 		}
408 
409 		public void setTs(String ts) {
410 			this.ts = ts;
411 		}
412 
413 		public String getUd() {
414 			return ud;
415 		}
416 
417 		public void setUd(String ud) {
418 			this.ud = ud;
419 		}
420 
421 		public void setEn(String en) {
422 			this.en = en;
423 		}
424 
425 		public String getEn() {
426 			return en;
427 		}
428 
429 		public boolean equals(final Object other) {
430 			if (!(other instanceof Form))
431 				return false;
432 			Form castOther = (Form)other;
433 			return new EqualsBuilder().append(bool, castOther.bool).append(b, castOther.b).append(s, castOther.s)
434 					.append(i, castOther.i).append(l, castOther.l).append(f, castOther.f).append(d, castOther.d)
435 					.append(ud, castOther.ud).append(sd, castOther.sd).append(st, castOther.st)
436 					.append(ts, castOther.ts).append(bi, castOther.bi).append(bd, castOther.bd)
437 					.append(en, castOther.en).isEquals();
438 		}
439 
440 		public int hashCode() {
441 			return new HashCodeBuilder().append(bool).append(b).append(s).append(i).append(l).append(f).append(d)
442 					.append(ud).append(sd).append(st).append(ts).append(bi).append(bd).append(en).toHashCode();
443 		}
444 
445 		public String toString() {
446 			return new ToStringBuilder(this).append(bool).append(b).append(s).append(i).append(i).append(l).append(f)
447 					.append(d).append(ud).append(sd).append(st).append(ts).append(bi).append(bd).append(en).toString();
448 		}
449 	}
450 
451 	public class Bean {
452 
453 		private boolean bool;
454 
455 		private byte b;
456 
457 		private short s;
458 
459 		private int i;
460 
461 		private long l;
462 
463 		private float f;
464 
465 		private double d;
466 
467 		private java.util.Date ud;
468 
469 		private java.sql.Date sd;
470 
471 		private java.sql.Time st;
472 
473 		private java.sql.Timestamp ts;
474 
475 		private BigInteger bi;
476 
477 		private BigDecimal bd;
478 
479 		private EnumerationTest.Mock en;
480 
481 
482 		public byte getB() {
483 			return b;
484 		}
485 
486 		public void setB(byte b) {
487 			this.b = b;
488 		}
489 
490 		public boolean isBool() {
491 			return bool;
492 		}
493 
494 		public void setBool(boolean bool) {
495 			this.bool = bool;
496 		}
497 
498 		public double getD() {
499 			return d;
500 		}
501 
502 		public void setD(double d) {
503 			this.d = d;
504 		}
505 
506 		public float getF() {
507 			return f;
508 		}
509 
510 		public void setF(float f) {
511 			this.f = f;
512 		}
513 
514 		public int getI() {
515 			return i;
516 		}
517 
518 		public void setI(int i) {
519 			this.i = i;
520 		}
521 
522 		public long getL() {
523 			return l;
524 		}
525 
526 		public void setL(long l) {
527 			this.l = l;
528 		}
529 
530 		public short getS() {
531 			return s;
532 		}
533 
534 		public void setS(short s) {
535 			this.s = s;
536 		}
537 
538 		public BigDecimal getBd() {
539 			return bd;
540 		}
541 
542 		public void setBd(BigDecimal bd) {
543 			this.bd = bd;
544 		}
545 
546 		public BigInteger getBi() {
547 			return bi;
548 		}
549 
550 		public void setBi(BigInteger bi) {
551 			this.bi = bi;
552 		}
553 
554 		public java.sql.Date getSd() {
555 			return sd;
556 		}
557 
558 		public void setSd(java.sql.Date sd) {
559 			this.sd = sd;
560 		}
561 
562 		public java.sql.Time getSt() {
563 			return st;
564 		}
565 
566 		public void setSt(java.sql.Time st) {
567 			this.st = st;
568 		}
569 
570 		public java.sql.Timestamp getTs() {
571 			return ts;
572 		}
573 
574 		public void setTs(java.sql.Timestamp ts) {
575 			this.ts = ts;
576 		}
577 
578 		public java.util.Date getUd() {
579 			return ud;
580 		}
581 
582 		public void setUd(java.util.Date ud) {
583 			this.ud = ud;
584 		}
585 
586 		public void setEn(EnumerationTest.Mock en) {
587 			this.en = en;
588 		}
589 
590 		public EnumerationTest.Mock getEn() {
591 			return en;
592 		}
593 
594 		public boolean equals(final Object other) {
595 			if (!(other instanceof Bean))
596 				return false;
597 			Bean castOther = (Bean)other;
598 			return new EqualsBuilder().append(bool, castOther.bool).append(b, castOther.b).append(s, castOther.s)
599 					.append(i, castOther.i).append(l, castOther.l).append(f, castOther.f).append(d, castOther.d)
600 					.append(ud, castOther.ud).append(sd, castOther.sd).append(st, castOther.st)
601 					.append(ts, castOther.ts).append(bi, castOther.bi).append(bd, castOther.bd)
602 					.append(en, castOther.en).isEquals();
603 		}
604 
605 		public int hashCode() {
606 			return new HashCodeBuilder().append(bool).append(b).append(s).append(i).append(l).append(f).append(d)
607 					.append(ud).append(sd).append(st).append(ts).append(bi).append(bd).append(en).toHashCode();
608 		}
609 
610 		public String toString() {
611 			return new ToStringBuilder(this).append(bool).append(b).append(s).append(i).append(i).append(l).append(f)
612 					.append(d).append(ud).append(sd).append(st).append(ts).append(bi).append(bd).append(en).toString();
613 		}
614 	}
615 
616 	public static class Stub extends Action {
617 
618 		protected ActionForward execute(ActionForm form, HttpServletRequest request, HttpServletResponse response,
619 				ActionMapping mapping) throws Exception {
620 			if ("error".equals(request.getParameter("exec"))) {
621 				throw new Exception();
622 			}
623 			return mapping.findForward("success");
624 		}
625 	}
626 }