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  package javax.faces.validator;
20  
21  import static org.easymock.EasyMock.expect;
22  import static org.testng.Assert.assertEquals;
23  
24  import java.util.Locale;
25  
26  import javax.el.ELContext;
27  import javax.el.ExpressionFactory;
28  import javax.el.ValueExpression;
29  import javax.faces.application.Application;
30  import javax.faces.application.ViewHandler;
31  import javax.faces.component.UIViewRoot;
32  
33  import org.apache.shale.test.mock.MockFacesContext12;
34  import org.easymock.classextension.EasyMock;
35  import org.easymock.classextension.IMocksControl;
36  import org.testng.annotations.Test;
37  
38  /**
39   * @author Mathias Broekelmann (latest modification by $Author: lu4242 $)
40   * @version $Revision: 600207 $ $Date: 2007-12-01 17:51:41 -0500 (Sat, 01 Dec 2007) $
41   */
42  public class _MessageUtilsTest
43  {
44  
45      /**
46       * Test method for
47       * {@link javax.faces.validator._MessageUtils#getErrorMessage(javax.faces.context.FacesContext, java.lang.String, java.lang.Object[])}.
48       */
49      @Test
50      public void testErrorMessage()
51      {
52          UIViewRoot root = new UIViewRoot();
53          MockFacesContext12 facesContext = new MockFacesContext12();
54          IMocksControl mocksControl = EasyMock.createControl();
55          Application application = mocksControl.createMock(Application.class);
56          ViewHandler viewHandler = mocksControl.createMock(ViewHandler.class);
57          ELContext elContext = mocksControl.createMock(ELContext.class);
58          ExpressionFactory expressionFactory = mocksControl.createMock(ExpressionFactory.class);
59          ValueExpression valueExpression = mocksControl.createMock(ValueExpression.class);
60          facesContext.setApplication(application);
61          facesContext.setViewRoot(root);
62          facesContext.setELContext(elContext);
63          
64          expect(application.getViewHandler()).andReturn(viewHandler);
65          expect(viewHandler.calculateLocale(facesContext)).andReturn(Locale.ENGLISH);
66          expect(application.getMessageBundle()).andReturn("javax.faces.Messages");
67          expect(application.getExpressionFactory()).andReturn(expressionFactory);
68          String s = "xxx: Validation Error: Value is greater than allowable maximum of \"xyz\"";
69          expect(expressionFactory.createValueExpression(elContext,s,String.class)).andReturn(valueExpression);
70          expect(valueExpression.getValue(elContext)).andReturn(s);
71          mocksControl.replay();
72  
73          assertEquals(_MessageUtils.getErrorMessage(facesContext, "javax.faces.validator.DoubleRangeValidator.MAXIMUM",
74                  new Object[] { "xyz", "xxx" }).getDetail(),
75                  "xxx: Validation Error: Value is greater than allowable maximum of \"xyz\"");
76      }
77  
78  }