View Javadoc
1   /*
2    * SPDX-License-Identifier: Apache-2.0
3    * See LICENSE file for details.
4    *
5    * Copyright 2012-2026 Hazendaz
6    */
7   package com.github.hazendaz.maven.smartsprites_maven_plugin;
8   
9   import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
10  import static org.junit.jupiter.api.Assertions.assertEquals;
11  import static org.junit.jupiter.api.Assertions.assertNull;
12  import static org.junit.jupiter.api.Assertions.assertThrows;
13  import static org.junit.jupiter.api.Assertions.assertTrue;
14  
15  import java.lang.reflect.Field;
16  import java.lang.reflect.Method;
17  import java.util.List;
18  
19  import javax.xml.parsers.DocumentBuilderFactory;
20  
21  import org.apache.maven.plugin.MojoExecutionException;
22  import org.junit.jupiter.api.Test;
23  import org.w3c.dom.Document;
24  import org.w3c.dom.Element;
25  import org.w3c.dom.Node;
26  
27  /**
28   * The Class HelpMojoTest.
29   */
30  public class HelpMojoTest {
31  
32      /**
33       * Test execute with default parameters.
34       */
35      @Test
36      void testExecuteWithDefaultParameters() {
37          HelpMojo mojo = new HelpMojo();
38          assertDoesNotThrow(mojo::execute);
39      }
40  
41      /**
42       * Test execute with detail and goal.
43       *
44       * @throws ReflectiveOperationException
45       *             the reflective operation exception
46       */
47      @Test
48      void testExecuteWithDetailAndGoal() throws ReflectiveOperationException {
49          HelpMojo mojo = new HelpMojo();
50          setField(mojo, "detail", true);
51          setField(mojo, "goal", "smartsprites");
52          setField(mojo, "lineLength", 64);
53          setField(mojo, "indentSize", 2);
54  
55          assertDoesNotThrow(mojo::execute);
56      }
57  
58      /**
59       * Test get single child throws for missing and multiple.
60       *
61       * @throws Exception
62       *             the exception
63       */
64      @Test
65      void testGetSingleChildThrowsForMissingAndMultiple() throws Exception {
66          Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
67          Element root = document.createElement("root");
68          document.appendChild(root);
69          root.appendChild(document.createElement("a"));
70          root.appendChild(document.createElement("a"));
71  
72          Method getSingleChild = method("getSingleChild", Node.class, String.class);
73          assertThrows(MojoExecutionException.class, () -> invoke(getSingleChild, null, root, "missing"));
74          assertThrows(MojoExecutionException.class, () -> invoke(getSingleChild, null, root, "a"));
75      }
76  
77      /**
78       * Test find single child returns null when missing.
79       *
80       * @throws Exception
81       *             the exception
82       */
83      @Test
84      void testFindSingleChildReturnsNullWhenMissing() throws Exception {
85          Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
86          Element root = document.createElement("root");
87          document.appendChild(root);
88  
89          Method findSingleChild = method("findSingleChild", Node.class, String.class);
90          Object child = invoke(findSingleChild, null, root, "absent");
91  
92          assertNull(child);
93      }
94  
95      /**
96       * Test get property from expression variants.
97       *
98       * @throws Exception
99       *             the exception
100      */
101     @Test
102     void testGetPropertyFromExpressionVariants() throws Exception {
103         Method method = method("getPropertyFromExpression", String.class);
104         assertEquals("demo.property", invoke(method, null, "${demo.property}"));
105         assertNull(invoke(method, null, "${a${b}}"));
106         assertNull(invoke(method, null, "plain-value"));
107     }
108 
109     /**
110      * Test to lines wraps and normalizes whitespace.
111      *
112      * @throws Exception
113      *             the exception
114      */
115     @Test
116     @SuppressWarnings("unchecked")
117     void testToLinesWrapsAndNormalizesWhitespace() throws Exception {
118         Method toLines = method("toLines", String.class, int.class, int.class, int.class);
119         List<String> lines = (List<String>) invoke(toLines, null, "\tword\u00A0word longlongtoken", 1, 2, 12);
120 
121         assertTrue(lines.size() > 1);
122         assertTrue(lines.stream().noneMatch(line -> line.contains("\u00A0")));
123     }
124 
125     /**
126      * Method.
127      *
128      * @param name
129      *            the name
130      * @param parameterTypes
131      *            the parameter types
132      *
133      * @return the method
134      *
135      * @throws ReflectiveOperationException
136      *             the reflective operation exception
137      */
138     private static Method method(String name, Class<?>... parameterTypes) throws ReflectiveOperationException {
139         Method method = HelpMojo.class.getDeclaredMethod(name, parameterTypes);
140         method.setAccessible(true);
141         return method;
142     }
143 
144     /**
145      * Invoke.
146      *
147      * @param method
148      *            the method
149      * @param target
150      *            the target
151      * @param args
152      *            the args
153      *
154      * @return the object
155      *
156      * @throws Exception
157      *             the exception
158      */
159     private static Object invoke(Method method, Object target, Object... args) throws Exception {
160         try {
161             return method.invoke(target, args);
162         } catch (ReflectiveOperationException e) {
163             if (e.getCause() instanceof Exception) {
164                 throw (Exception) e.getCause();
165             }
166             throw e;
167         }
168     }
169 
170     /**
171      * Set field.
172      *
173      * @param target
174      *            the target
175      * @param fieldName
176      *            the field name
177      * @param value
178      *            the value
179      *
180      * @throws ReflectiveOperationException
181      *             the reflective operation exception
182      */
183     private static void setField(Object target, String fieldName, Object value) throws ReflectiveOperationException {
184         Field field = target.getClass().getDeclaredField(fieldName);
185         field.setAccessible(true);
186         field.set(target, value);
187     }
188 }