1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package au.com.acegi.xmlformat;
19
20 import static au.com.acegi.xmlformat.TestUtil.fileToString;
21 import static au.com.acegi.xmlformat.TestUtil.stringToFile;
22 import static org.hamcrest.CoreMatchers.is;
23 import static org.hamcrest.CoreMatchers.not;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.junit.jupiter.api.Assertions.fail;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.Mockito.atLeastOnce;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.never;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.io.File;
35 import java.io.IOException;
36
37 import org.apache.maven.plugin.MojoExecutionException;
38 import org.apache.maven.plugin.MojoFailureException;
39 import org.apache.maven.plugin.logging.Log;
40 import org.junit.jupiter.api.Assertions;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.io.TempDir;
44
45
46
47
48 public class XmlFormatPluginTest {
49
50 private static final String EMPTY_FILE_NAME = "empty.xml";
51 private static final String EMPTY_TXT = "";
52 private static final String ERR_FILE_NAME = "error.xml";
53 private static final String ERR_TXT = "<xml> <hello> hello not closed! </xml>";
54 private static final String NO_CHG_TXT = "<xml> <leave-me-alone/> </xml>";
55 private static final String TO_CHG_TXT = "<xml> <hello/> </xml>";
56 private static final String INCLUDE_ALL_XML = "**/*.xml";
57
58 @TempDir
59 private File tmp;
60 private File error;
61 @SuppressWarnings("PMD.ProperLogger")
62 private Log log;
63 private File noChange;
64 private File proj;
65 private File target;
66 private File toChange;
67
68 @BeforeEach
69 void before() throws IOException {
70 proj = newFolder(tmp, "junit");
71 target = new File(proj, "target");
72 assertThat(target.mkdir(), is(true));
73
74 toChange = new File(proj, "my.xml");
75 stringToFile(TO_CHG_TXT, toChange);
76
77 noChange = new File(target, "exclude-me.xml");
78 stringToFile(NO_CHG_TXT, noChange);
79
80 final File empty = new File(proj, EMPTY_FILE_NAME);
81 stringToFile(EMPTY_TXT, empty);
82
83 error = new File(proj, ERR_FILE_NAME);
84 stringToFile(ERR_TXT, error);
85
86 assertThat(fileToString(toChange), is(TO_CHG_TXT));
87 assertThat(fileToString(noChange), is(NO_CHG_TXT));
88 assertThat(fileToString(empty), is(EMPTY_TXT));
89 assertThat(fileToString(error), is(ERR_TXT));
90
91 log = mock(Log.class);
92 }
93
94 @Test
95 void pluginExcludesError() throws MojoExecutionException, MojoFailureException {
96 final XmlFormatPlugin plugin = new XmlFormatPlugin();
97 plugin.setLog(log);
98 when(log.isDebugEnabled()).thenReturn(true);
99 when(log.isErrorEnabled()).thenReturn(true);
100
101 plugin.setBaseDirectory(proj);
102 plugin.setExcludes("**/" + ERR_FILE_NAME);
103 plugin.setIncludes(INCLUDE_ALL_XML);
104 plugin.setTargetDirectory(target);
105
106 plugin.execute();
107
108 verify(log, never()).isErrorEnabled();
109 verify(log, atLeastOnce()).isDebugEnabled();
110 verify(log, atLeastOnce()).debug(anyString());
111
112 assertThat(fileToString(toChange), not(TO_CHG_TXT));
113 assertThat(fileToString(noChange), is(NO_CHG_TXT));
114 assertThat(fileToString(error), is(ERR_TXT));
115 }
116
117 @Test
118 @SuppressWarnings("PMD.JUnitUseExpected")
119 void pluginReportsError() throws MojoExecutionException {
120 final XmlFormatPlugin plugin = new XmlFormatPlugin();
121 plugin.setLog(log);
122 when(log.isDebugEnabled()).thenReturn(false);
123 when(log.isErrorEnabled()).thenReturn(true);
124
125 plugin.setBaseDirectory(proj);
126 plugin.setExcludes("");
127 plugin.setIncludes(INCLUDE_ALL_XML);
128 plugin.setTargetDirectory(target);
129
130 Assertions.assertThrows(MojoFailureException.class, () -> {
131 plugin.execute();
132 fail("Should have raised exception when handling error");
133 });
134
135 verify(log, atLeastOnce()).error(anyString(), any(Throwable.class));
136 verify(log, atLeastOnce()).isDebugEnabled();
137 verify(log, never()).debug(anyString());
138
139 assertThat(fileToString(toChange), not(TO_CHG_TXT));
140 assertThat(fileToString(noChange), is(NO_CHG_TXT));
141 assertThat(fileToString(error), is(ERR_TXT));
142 }
143
144 @Test
145 @SuppressWarnings("PMD.JUnitUseExpected")
146 void pluginSkipTargetFolder() throws MojoExecutionException, MojoFailureException {
147 final XmlFormatPlugin plugin = new XmlFormatPlugin();
148 plugin.setLog(log);
149
150 plugin.setSkipTargetFolder(false);
151 when(log.isDebugEnabled()).thenReturn(true);
152 when(log.isErrorEnabled()).thenReturn(true);
153
154 plugin.setBaseDirectory(proj);
155 plugin.setExcludes("**/" + ERR_FILE_NAME);
156 plugin.setIncludes(INCLUDE_ALL_XML);
157 plugin.setTargetDirectory(target);
158
159 plugin.execute();
160
161 assertThat(fileToString(toChange), not(TO_CHG_TXT));
162 assertThat(fileToString(noChange), not(NO_CHG_TXT));
163 assertThat(fileToString(error), is(ERR_TXT));
164 }
165
166 @Test
167 void pluginSkip() throws MojoExecutionException, MojoFailureException {
168 final XmlFormatPlugin plugin = new XmlFormatPlugin();
169 plugin.setLog(log);
170 plugin.setSkip(true);
171 when(log.isDebugEnabled()).thenReturn(true);
172 when(log.isInfoEnabled()).thenReturn(true);
173 when(log.isErrorEnabled()).thenReturn(true);
174
175 plugin.setBaseDirectory(proj);
176 plugin.setExcludes("**/" + ERR_FILE_NAME);
177 plugin.setIncludes(INCLUDE_ALL_XML);
178 plugin.setTargetDirectory(target);
179
180 plugin.execute();
181
182 verify(log, atLeastOnce()).info("[xml-format] Skipped");
183
184 assertThat(fileToString(toChange), is(TO_CHG_TXT));
185 }
186
187 private static File newFolder(final File root, final String... subDirs) throws IOException {
188 final String subFolder = String.join("/", subDirs);
189 final File result = new File(root, subFolder);
190 if (!result.mkdirs()) {
191 throw new IOException("Couldn't create folders " + root);
192 }
193 return result;
194 }
195 }