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