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.internal.util;
21  
22  import org.apache.myfaces.tobago.internal.config.AbstractTobagoTestBase;
23  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
24  import org.junit.jupiter.api.Assertions;
25  import org.junit.jupiter.api.Test;
26  
27  import javax.faces.context.FacesContext;
28  import java.io.IOException;
29  
30  public class StyleRenderUtilsUnitTest extends AbstractTobagoTestBase {
31  
32    @Test
33    public void testEncodeIdSelector() {
34      Assertions.assertEquals("#", StyleRenderUtils.encodeIdSelector(""));
35  
36      Assertions.assertEquals("#tag", StyleRenderUtils.encodeIdSelector("tag"));
37  
38      Assertions.assertEquals("#id\\:sub", StyleRenderUtils.encodeIdSelector("id:sub"));
39  
40      Assertions.assertEquals("#id\\:sub\\:sub2", StyleRenderUtils.encodeIdSelector("id:sub:sub2"));
41  
42      Assertions.assertEquals("#id\\:sub\\:sub2\\:sub3", StyleRenderUtils.encodeIdSelector("id:sub:sub2:sub3"));
43    }
44  
45    @Test
46    public void writeIdSelector() throws IOException {
47  
48      final FacesContext facesContext = FacesContext.getCurrentInstance();
49      final TobagoResponseWriter writer = (TobagoResponseWriter) facesContext.getResponseWriter();
50  
51      StyleRenderUtils.writeIdSelector(writer, "id");
52      Assertions.assertEquals("#id", getLastWritten());
53  
54      StyleRenderUtils.writeIdSelector(writer, "id:sub");
55      Assertions.assertEquals("#id\\:sub", getLastWritten());
56  
57      StyleRenderUtils.writeIdSelector(writer, "id:sub:sub2");
58      Assertions.assertEquals("#id\\:sub\\:sub2", getLastWritten());
59  
60      StyleRenderUtils.writeIdSelector(writer, "id:sub:sub2:sub3");
61      Assertions.assertEquals("#id\\:sub\\:sub2\\:sub3", getLastWritten());
62  
63      StyleRenderUtils.writeIdSelector(writer, "id::sub");
64      Assertions.assertEquals("#id\\:\\:sub", getLastWritten());
65    }
66  
67    @Test
68    public void writeSelector() throws IOException {
69  
70      final FacesContext facesContext = FacesContext.getCurrentInstance();
71      final TobagoResponseWriter writer = (TobagoResponseWriter) facesContext.getResponseWriter();
72  
73      StyleRenderUtils.writeSelector(writer, "parent>child");
74      Assertions.assertEquals("parent>child", getLastWritten());
75  
76      StyleRenderUtils.writeSelector(writer, "parent<child");
77      Assertions.assertEquals("parent&lt;child", getLastWritten());
78  
79      StyleRenderUtils.writeSelector(writer, "#id");
80      Assertions.assertEquals("#id", getLastWritten());
81  
82      StyleRenderUtils.writeSelector(writer, "#id\\:sub");
83      Assertions.assertEquals("#id\\:sub", getLastWritten());
84    }
85  }