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.tobago.component;
21  
22  import org.apache.myfaces.tobago.internal.mock.faces.AbstractTobagoTestBase;
23  import org.junit.Assert;
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import javax.faces.application.FacesMessage;
28  import javax.faces.context.FacesContext;
29  import java.util.List;
30  
31  public class UIMessagesUnitTest extends AbstractTobagoTestBase {
32  
33    @Before
34    public void setUp() throws Exception {
35      super.setUp();
36      FacesContext facesContext = getFacesContext();
37      facesContext.addMessage("id0", new FacesMessage(FacesMessage.SEVERITY_INFO, "test", "a test"));
38      facesContext.addMessage("id0", new FacesMessage(FacesMessage.SEVERITY_WARN, "test", "a test"));
39      facesContext.addMessage("id1", new FacesMessage(FacesMessage.SEVERITY_ERROR, "test", "a test"));
40      facesContext.addMessage("id1", new FacesMessage(FacesMessage.SEVERITY_FATAL, "test", "a test"));
41      facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "test", "a test"));
42    }
43  
44    @Test
45    public void testCreateMessageListAll() {
46  
47      UIMessages component = new UIMessages();
48      List<UIMessages.Item> messages = component.createMessageList(getFacesContext());
49  
50      Assert.assertEquals(5, messages.size());
51    }
52  
53    @Test
54    public void testCreateMessageListGlobalOnly() {
55  
56      UIMessages component = new UIMessages();
57      component.setGlobalOnly(true);
58      List<UIMessages.Item> messages = component.createMessageList(getFacesContext());
59  
60      Assert.assertEquals(1, messages.size());
61    }
62  
63    @Test
64    public void testCreateMessageListForId0() {
65  
66      UIMessages component = new UIMessages();
67      component.setFor("id0");
68      List<UIMessages.Item> messages = component.createMessageList(getFacesContext());
69  
70      Assert.assertEquals(2, messages.size());
71    }
72  
73    @Test
74    public void testCreateMessageListInfoToWarn() {
75  
76      UIMessages component = new UIMessages();
77      component.setMaxSeverity(FacesMessage.SEVERITY_WARN);
78      List<UIMessages.Item> messages = component.createMessageList(getFacesContext());
79  
80      Assert.assertEquals(2, messages.size());
81    }
82  
83    @Test
84    public void testCreateMessageListWarnToError() {
85  
86      UIMessages component = new UIMessages();
87      component.setMinSeverity(FacesMessage.SEVERITY_WARN);
88      component.setMaxSeverity(FacesMessage.SEVERITY_ERROR);
89      List<UIMessages.Item> messages = component.createMessageList(getFacesContext());
90  
91      Assert.assertEquals(2, messages.size());
92    }
93  
94    @Test
95    public void testCreateMessageListErrorToFatal() {
96  
97      UIMessages component = new UIMessages();
98      component.setMinSeverity(FacesMessage.SEVERITY_ERROR);
99      List<UIMessages.Item> messages = component.createMessageList(getFacesContext());
100 
101     Assert.assertEquals(3, messages.size());
102   }
103 
104   @Test
105   public void testCreateMessageListMaxNumber() {
106 
107     UIMessages component = new UIMessages();
108 
109     component.setMaxNumber(3);
110     List<UIMessages.Item> messages = component.createMessageList(getFacesContext());
111     Assert.assertEquals(3, messages.size());
112 
113     component.setMaxNumber(30000);
114     messages = component.createMessageList(getFacesContext());
115     Assert.assertEquals(5, messages.size());
116   }
117 
118   @Test
119   public void testCreateMessageListOrderBySeverity() {
120 
121     UIMessages component = new UIMessages();
122     component.setOrderBy(UIMessages.OrderBy.SEVERITY);
123     List<UIMessages.Item> messages = component.createMessageList(getFacesContext());
124 
125     int mustShrink = FacesMessage.SEVERITY_FATAL.getOrdinal();
126     for (UIMessages.Item message : messages) {
127       int newValue = message.getFacesMessage().getSeverity().getOrdinal();
128       Assert.assertTrue(mustShrink >= newValue);
129       mustShrink = newValue;
130     }
131   }
132 
133   @Test
134   public void testOrderByEnum() {
135     Assert.assertEquals(2, UIMessages.OrderBy.values().length);
136     Assert.assertEquals(UIMessages.OrderBy.OCCURRENCE, UIMessages.OrderBy.parse(UIMessages.OrderBy.OCCURRENCE_STRING));
137     Assert.assertEquals(UIMessages.OrderBy.SEVERITY, UIMessages.OrderBy.parse(UIMessages.OrderBy.SEVERITY_STRING));
138   }
139 
140 }