View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.custom.date;
21  
22  import java.util.Calendar;
23  import java.util.Date;
24  import java.util.GregorianCalendar;
25  import java.util.Locale;
26  
27  import javax.faces.FactoryFinder;
28  import javax.faces.application.Application;
29  import javax.faces.application.ViewHandler;
30  import javax.faces.component.UIViewRoot;
31  import javax.faces.context.FacesContext;
32  import javax.faces.context.FacesContextFactory;
33  import javax.faces.lifecycle.Lifecycle;
34  import javax.faces.lifecycle.LifecycleFactory;
35  
36  import org.apache.cactus.ServletTestCase;
37  import org.apache.cactus.WebRequest;
38  import org.apache.myfaces.custom.date.AbstractHtmlInputDate.UserData;
39  
40  public class HtmlDateRenderCactus extends ServletTestCase {
41    private FacesContext facesContext;
42    private UIViewRoot viewRoot;
43    
44    public void setUp() throws Exception {
45      FacesContextFactory facesContextFactory = (FacesContextFactory) FactoryFinder
46          .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
47      LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
48          .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
49      Lifecycle lifecycle = lifecycleFactory
50          .getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
51      facesContext = facesContextFactory.getFacesContext(this.config
52          .getServletContext(), request, response, lifecycle);
53      assertNotNull(facesContext);
54      Application application = facesContext.getApplication();
55      ViewHandler viewHandler = application.getViewHandler();
56      String viewId = "/index.jsp";
57      viewRoot = viewHandler.createView(facesContext, viewId);
58      viewRoot.setViewId(viewId);
59      facesContext.setViewRoot(viewRoot);
60    }
61  
62    public void beginDecodeDate(WebRequest request) {
63      request.addParameter("test.day", "14");
64      request.addParameter("test.month", "1");
65      request.addParameter("test.year", "2005");
66    }
67    
68    public void testDecodeDate() {
69      HtmlInputDate inputDate = new HtmlInputDate();
70      inputDate.setId("test");
71      inputDate.setType("date");
72      HtmlDateRenderer subject = new HtmlDateRenderer();
73      // decode
74      subject.decode(facesContext, inputDate);
75      UserData data = inputDate.getUserData(Locale.ENGLISH);
76      assertEquals("14", data.getDay());
77      assertEquals("1", data.getMonth());
78      assertEquals("2005", data.getYear());
79    }
80    
81    public void beginDecodeWithSubmittedValue(WebRequest request) {
82      request.addParameter("test.day", "14");
83      request.addParameter("test.month", "1");
84      request.addParameter("test.year", "2005");
85    }
86    
87    public void testDecodeWithSubmittedValue() throws Exception {
88      HtmlInputDate inputDate = new HtmlInputDate();
89      inputDate.setId("test");
90      inputDate.setType("date");
91      Date today = new Date();
92      inputDate.setSubmittedValue(new UserData(today, Locale.ENGLISH, null,false, "date"));
93      HtmlDateRenderer subject = new HtmlDateRenderer();
94      // decode
95      subject.decode(facesContext, inputDate);
96      UserData data = inputDate.getUserData(Locale.ENGLISH);
97      assertEquals("14", data.getDay());
98      assertEquals("1", data.getMonth());
99      assertEquals("2005", data.getYear());
100   }
101 
102   public void beginDecodeTime(WebRequest request) {
103     request.addParameter("test.hours", "12");
104     request.addParameter("test.minutes", "15");
105     request.addParameter("test.seconds", "35");
106   }
107   public void testDecodeTime() throws Exception {
108     HtmlInputDate inputDate = new HtmlInputDate();
109     inputDate.setId("test");
110     inputDate.setType("time");
111     HtmlDateRenderer subject = new HtmlDateRenderer();
112     // decode
113     subject.decode(facesContext, inputDate);
114     UserData data = inputDate.getUserData(Locale.ENGLISH);
115     assertEquals("12", data.getHours());
116     assertEquals("15", data.getMinutes());
117     assertEquals("35", data.getSeconds());
118   }
119 
120   public void beginDecodeFull(WebRequest request) {
121     request.addParameter("test.day", "14");
122     request.addParameter("test.month", "1");
123     request.addParameter("test.year", "2005");
124     request.addParameter("test.hours", "12");
125     request.addParameter("test.minutes", "15");
126     request.addParameter("test.seconds", "35");
127   }
128 
129   public void testDecodeFull() throws Exception {
130     HtmlInputDate inputDate = new HtmlInputDate();
131     inputDate.setId("test");
132     inputDate.setType("full");
133     HtmlDateRenderer subject = new HtmlDateRenderer();
134     // decode
135     subject.decode(facesContext, inputDate);
136     UserData data = inputDate.getUserData(Locale.ENGLISH);
137     assertEquals("14", data.getDay());
138     assertEquals("1", data.getMonth());
139     assertEquals("2005", data.getYear());
140     assertEquals("12", data.getHours());
141     assertEquals("15", data.getMinutes());
142     assertEquals("35", data.getSeconds());
143   }
144 
145   public void beginDecodeFlorp(WebRequest request) {
146     request.addParameter("test.day", "14");
147     request.addParameter("test.month", "1");
148     request.addParameter("test.year", "2005");
149     request.addParameter("test.hours", "12");
150     request.addParameter("test.minutes", "15");
151   }
152 
153   public void testDecodeFlorp() throws Exception {
154     HtmlInputDate inputDate = new HtmlInputDate();
155     inputDate.setId("test");
156     // is this correct? Should it parse correctly if the type is not valid?
157     inputDate.setType("florp");
158     HtmlDateRenderer subject = new HtmlDateRenderer();
159     // decode
160     subject.decode(facesContext, inputDate);
161     UserData data = inputDate.getUserData(Locale.ENGLISH);
162     assertEquals("14", data.getDay());
163     assertEquals("1", data.getMonth());
164     assertEquals("2005", data.getYear());
165     assertEquals("12", data.getHours());
166     assertEquals("15", data.getMinutes());
167   }
168 
169   public void beginDecodeDisabled(WebRequest request) {
170     request.addParameter("test.day", "14");
171     request.addParameter("test.month", "1");
172     request.addParameter("test.year", "2005");
173   }
174 
175   public void testDecodeDisabled() throws Exception {
176     HtmlInputDate inputDate = new HtmlInputDate();
177     inputDate.setId("test");
178     inputDate.setType("date");
179     inputDate.setDisabled(true);
180     HtmlDateRenderer subject = new HtmlDateRenderer();
181     // decode - when disabled currently defaults to today
182     // JIRA Issue #233 requests that the default be null
183     // and the control handle data that is not required
184     subject.decode(facesContext, inputDate);
185     UserData data = inputDate.getUserData(Locale.ENGLISH);
186     Calendar cal = GregorianCalendar.getInstance();
187     assertEquals(cal.get(Calendar.DATE) + "", data.getDay());
188     // different bases - cal starts at 0 in January, UserData starts at 1 in
189     // January
190     assertEquals((cal.get(Calendar.MONTH) + 1) + "", data.getMonth());
191     assertEquals(cal.get(Calendar.YEAR) + "", data.getYear());
192   }
193 
194   public void tearDown() {
195     facesContext = null;
196     viewRoot = null;
197   }
198 }