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.MatcherAssert.assertThat;
24 import static org.junit.jupiter.api.Assertions.fail;
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.Mockito.atLeastOnce;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import java.io.File;
33 import java.io.IOException;
34
35 import org.apache.maven.plugin.MojoExecutionException;
36 import org.apache.maven.plugin.MojoFailureException;
37 import org.apache.maven.plugin.logging.Log;
38 import org.junit.jupiter.api.Assertions;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
41 import org.junit.jupiter.api.io.TempDir;
42
43
44
45
46 public class XmlCheckPluginTest {
47
48 private static final String EMPTY_FILE_NAME = "empty.xml";
49 private static final String EMPTY_TXT = "";
50 private static final String ERR_FILE_NAME = "error.xml";
51 private static final String ERR_TXT = "<xml> <hello> hello not closed! </xml>";
52 private static final String NO_CHG_TXT = "<xml> <leave-me-alone/> </xml>";
53 private static final String TO_CHG_FILE_NAME = "my.xml";
54 private static final String TO_CHG_TXT = "<xml> <hello/> </xml>";
55
56 @TempDir
57 private File tmp;
58
59 @SuppressWarnings("PMD.ProperLogger")
60 private Log log;
61
62 private File proj;
63 private File target;
64
65 @BeforeEach
66 void before() throws IOException {
67 proj = newFolder(tmp, "junit");
68 target = new File(proj, "target");
69 assertThat(target.mkdir(), is(true));
70
71 final File toChange = new File(proj, TO_CHG_FILE_NAME);
72 stringToFile(TO_CHG_TXT, toChange);
73
74 final File noChange = new File(target, "exclude-me.xml");
75 stringToFile(NO_CHG_TXT, noChange);
76
77 final File empty = new File(proj, EMPTY_FILE_NAME);
78 stringToFile(EMPTY_TXT, empty);
79
80 final File error = new File(proj, ERR_FILE_NAME);
81 stringToFile(ERR_TXT, error);
82
83 assertThat(fileToString(toChange), is(TO_CHG_TXT));
84 assertThat(fileToString(noChange), is(NO_CHG_TXT));
85 assertThat(fileToString(empty), is(EMPTY_TXT));
86 assertThat(fileToString(error), is(ERR_TXT));
87
88 log = mock(Log.class);
89 }
90
91 @Test
92 @SuppressWarnings("PMD.JUnitUseExpected")
93 void pluginReportsError() throws MojoExecutionException {
94 final XmlCheckPlugin plugin = new XmlCheckPlugin();
95 plugin.setLog(log);
96 when(log.isDebugEnabled()).thenReturn(true);
97 when(log.isErrorEnabled()).thenReturn(true);
98
99 plugin.setBaseDirectory(proj);
100 plugin.setIncludes("**/*.xml");
101 plugin.setTargetDirectory(target);
102
103 Assertions.assertThrows(MojoFailureException.class, () -> {
104 plugin.execute();
105 fail("Should have raised exception when handling error");
106 });
107
108 verify(log, atLeastOnce()).isErrorEnabled();
109 verify(log, atLeastOnce()).isDebugEnabled();
110 verify(log, atLeastOnce()).debug(anyString());
111 }
112
113 @Test
114 @SuppressWarnings("PMD.JUnitUseExpected")
115 void pluginReportsFormattingNeeded() throws MojoFailureException {
116 final XmlCheckPlugin plugin = new XmlCheckPlugin();
117 plugin.setLog(log);
118 when(log.isDebugEnabled()).thenReturn(true);
119 when(log.isErrorEnabled()).thenReturn(true);
120
121 plugin.setBaseDirectory(proj);
122 plugin.setExcludes("**/" + ERR_FILE_NAME);
123 plugin.setIncludes("**/*.xml");
124 plugin.setTargetDirectory(target);
125
126 Assertions.assertThrows(MojoExecutionException.class, () -> {
127 plugin.execute();
128 fail("Should have raised exception when encountering non-formatted file");
129 });
130
131 verify(log, atLeastOnce()).isErrorEnabled();
132 verify(log, atLeastOnce()).isDebugEnabled();
133 verify(log, atLeastOnce()).debug(anyString());
134 }
135
136 @Test
137 void pluginSucceedsWhenAllFormatted() throws MojoExecutionException, MojoFailureException {
138 final XmlCheckPlugin plugin = new XmlCheckPlugin();
139 plugin.setLog(log);
140 when(log.isDebugEnabled()).thenReturn(true);
141 when(log.isErrorEnabled()).thenReturn(true);
142
143 plugin.setBaseDirectory(proj);
144 plugin.setExcludes("**/" + ERR_FILE_NAME, "**/" + TO_CHG_FILE_NAME);
145 plugin.setIncludes("**/*.xml");
146 plugin.setTargetDirectory(target);
147
148 plugin.execute();
149
150 verify(log, never()).isErrorEnabled();
151 verify(log, atLeastOnce()).isDebugEnabled();
152 verify(log, atLeastOnce()).debug(anyString());
153 }
154
155 private static File newFolder(final File root, final String... subDirs) throws IOException {
156 final String subFolder = String.join("/", subDirs);
157 final File result = new File(root, subFolder);
158 if (!result.mkdirs()) {
159 throw new IOException("Couldn't create folders " + root);
160 }
161 return result;
162 }
163 }