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.util;
21  
22  import org.apache.myfaces.tobago.component.RendererTypes;
23  import org.apache.myfaces.tobago.component.Tags;
24  import org.apache.myfaces.tobago.component.UIIn;
25  import org.apache.myfaces.tobago.component.UIOut;
26  import org.apache.myfaces.tobago.internal.config.AbstractTobagoTestBase;
27  import org.junit.jupiter.api.Assertions;
28  import org.junit.jupiter.api.Test;
29  
30  import javax.faces.application.FacesMessage;
31  import javax.faces.component.UIComponent;
32  import javax.faces.context.FacesContext;
33  
34  public class ComponentUtilsUnitTest extends AbstractTobagoTestBase {
35  
36    @Test
37    public void testSplitList() {
38      Assertions.assertArrayEquals(new String[]{"ab", "cd"}, ComponentUtils.splitList("ab cd"));
39      Assertions.assertArrayEquals(new String[]{"ab", "cd"}, ComponentUtils.splitList("ab  cd"));
40      Assertions.assertArrayEquals(new String[]{"ab", "cd"}, ComponentUtils.splitList("ab,  cd"));
41      Assertions.assertArrayEquals(new String[]{"ab", "cd"}, ComponentUtils.splitList("ab , cd"));
42      Assertions.assertArrayEquals(new String[]{"ab", "cd"}, ComponentUtils.splitList("ab,,cd"));
43    }
44  
45    @Test
46    public void testFindDescendant() {
47      final FacesContext facesContext = FacesContext.getCurrentInstance();
48      final UIComponent p = ComponentUtils.createComponent(
49          facesContext, Tags.panel.componentType(), RendererTypes.Panel, "p");
50      final UIComponent i = ComponentUtils.createComponent(
51          facesContext, Tags.in.componentType(), RendererTypes.In, "i");
52      p.getChildren().add(i);
53  
54      final UIIn in = ComponentUtils.findDescendant(p, UIIn.class);
55      Assertions.assertEquals(i, in);
56    }
57  
58    @Test
59    public void testGetMaximumSeverity() {
60      final FacesContext facesContext = FacesContext.getCurrentInstance();
61  
62      final UIIn input = new UIIn();
63      final String inputId = "InputID";
64      input.setId(inputId);
65  
66      input.setValid(true);
67      Assertions.assertEquals(null, ComponentUtils.getMaximumSeverity(input));
68      input.setValid(false);
69      Assertions.assertEquals(FacesMessage.SEVERITY_ERROR, ComponentUtils.getMaximumSeverity(input));
70  
71      facesContext.addMessage(inputId, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info Message", null));
72      input.setValid(true);
73      Assertions.assertEquals(FacesMessage.SEVERITY_INFO, ComponentUtils.getMaximumSeverity(input));
74      input.setValid(false);
75      Assertions.assertEquals(FacesMessage.SEVERITY_ERROR, ComponentUtils.getMaximumSeverity(input));
76  
77      facesContext.addMessage(inputId, new FacesMessage(FacesMessage.SEVERITY_FATAL, "FATAL", null));
78      input.setValid(true);
79      Assertions.assertEquals(FacesMessage.SEVERITY_FATAL, ComponentUtils.getMaximumSeverity(input));
80      input.setValid(false);
81      Assertions.assertEquals(FacesMessage.SEVERITY_FATAL, ComponentUtils.getMaximumSeverity(input));
82  
83  
84      final UIOut output = new UIOut();
85      final String outputId = "OutputID";
86      output.setId(outputId);
87  
88      Assertions.assertEquals(null, ComponentUtils.getMaximumSeverity(output));
89  
90      facesContext.addMessage(outputId, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info Message", null));
91      Assertions.assertEquals(FacesMessage.SEVERITY_INFO, ComponentUtils.getMaximumSeverity(output));
92  
93      facesContext.addMessage(outputId, new FacesMessage(FacesMessage.SEVERITY_FATAL, "FATAL", null));
94      Assertions.assertEquals(FacesMessage.SEVERITY_FATAL, ComponentUtils.getMaximumSeverity(output));
95    }
96  }