1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.dhptech.maven.stripbom;
17
18 import org.apache.maven.plugin.MojoExecutionException;
19 import java.io.BufferedInputStream;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
29
30 import org.apache.maven.plugin.logging.Log;
31 import org.apache.maven.shared.model.fileset.FileSet;
32
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.Test;
35
36 import static org.easymock.EasyMock.createMock;
37 import static org.easymock.EasyMock.replay;
38 import static org.easymock.EasyMock.verify;
39 import static org.easymock.EasyMock.expectLastCall;
40 import static org.easymock.EasyMock.expect;
41
42
43
44
45
46 @Test(suiteName = "Strip BOM Plugin Tests", testName = "Strip BOM Mojo Test")
47 public class StripBOMMojoTest {
48
49 public static final String CHECKING_FOR_BOM_MSG = "Checking for BOM in ";
50 public static final String FOUND_BOM_MSG_PREFIX = "Found BOM in ";
51 public static final String FOUND_BOM_MSG_SUFFIX = ", removing.";
52 public static final String FOUND_BOM_WARN_ONLY_MSG_SUFFIX = ", not removing.";
53 public static final String BOM_TEST_FILE_NAME = "./src/test/testFiles/testFileWithBOM.txt";
54 public static final String NO_BOM_TEST_FILE_NAME = "./src/test/testFiles/testFileWithoutBOM.txt";
55
56 private void insertBOM(File file) throws IOException {
57 InputStream in = new BufferedInputStream(new FileInputStream(file));
58 try {
59 byte[] bom = new byte[3];
60 in.mark(10);
61 in.read(bom);
62 if (!Arrays.equals(bom, StripBOMMojo.UTF8_BOM)) {
63 in.reset();
64 File tempFile = File.createTempFile(file.getName(), ".tmp", file.getParentFile());
65 OutputStream out = new FileOutputStream(tempFile);
66 out.write(StripBOMMojo.UTF8_BOM);
67 byte[] buffer = new byte[1024];
68 int cnt = -1;
69 while ((cnt = in.read(buffer)) >= 0) {
70 out.write(buffer, 0, cnt);
71 }
72 out.close();
73 File backupFile = new File(file.getAbsoluteFile().getCanonicalPath() + ".bak");
74 if (!file.renameTo(backupFile)) {
75 assert false : "Could not rename target file, " + file + ", to backup file, " + backupFile;
76 }
77 if (!tempFile.renameTo(file)) {
78 if (!backupFile.renameTo(file)) {
79 assert false : "Could not undo rename of backup file, " + backupFile + ", to target file, " + file;
80 }
81 assert false : "Could not rename temp file, " + tempFile + ", to target file, " + file;
82 }
83 backupFile.delete();
84 }
85 } finally {
86 in.close();
87 }
88 }
89
90 private boolean checkForBOM(File file) throws IOException {
91 InputStream in = new BufferedInputStream(new FileInputStream(file));
92 try {
93 byte[] bom = new byte[3];
94 in.read(bom);
95 return Arrays.equals(bom, StripBOMMojo.UTF8_BOM);
96 } finally {
97 in.close();
98 }
99 }
100
101 @BeforeClass
102 public void prepareTestData() throws Exception {
103 insertBOM(new File(BOM_TEST_FILE_NAME));
104 }
105
106 @Test
107 public void testStripBOMMojoWarnOnlyFailBuild() throws Exception {
108 System.out.println("*** testStripBOMMojoWarnOnlyFailBuild");
109 StripBOMMojo mojo = new StripBOMMojo();
110
111 Log log = createMock(Log.class);
112 Log logDelegate = new StdOutMavenLogger();
113 mojo.setLog(log);
114
115 List<FileSet> fileSets = new ArrayList<FileSet>();
116 FileSet fileSet = new FileSet();
117 fileSet.setDirectory("./src/test/testFiles");
118 fileSet.addInclude("**/*.txt");
119 fileSet.addExclude("**/*-Exclude.txt");
120 fileSets.add(fileSet);
121 mojo.setFiles(fileSets);
122
123 mojo.setWarnOnly(true);
124 mojo.setFailBuild(true);
125
126 log.debug(CHECKING_FOR_BOM_MSG + BOM_TEST_FILE_NAME);
127 expectLastCall().andDelegateTo(logDelegate).once();
128 log.debug(CHECKING_FOR_BOM_MSG + NO_BOM_TEST_FILE_NAME);
129 expectLastCall().andDelegateTo(logDelegate).once();
130 log.warn(FOUND_BOM_MSG_PREFIX + BOM_TEST_FILE_NAME + FOUND_BOM_WARN_ONLY_MSG_SUFFIX);
131 expectLastCall().andDelegateTo(logDelegate).once();
132 expect(log.isDebugEnabled()).andStubDelegateTo(logDelegate);
133 expect(log.isWarnEnabled()).andStubDelegateTo(logDelegate);
134
135 replay(log);
136 try {
137 mojo.execute();
138 assert false : "expected a MojoExecutionException";
139 } catch(MojoExecutionException ex) {
140 assert ex.getMessage().equals("BOM(s) Found in files, see output log for specifics.") : "recieved unknown MojoExecutionException: "+ex;
141 System.out.println("Got expected MojoExecutionException");
142 }
143 verify(log);
144
145 assert checkForBOM(new File(BOM_TEST_FILE_NAME));
146 }
147
148 @Test
149 public void testStripBOMMojoWarnOnly() throws Exception {
150 System.out.println("*** testStripBOMMojoWarnOnly");
151 StripBOMMojo mojo = new StripBOMMojo();
152
153 Log log = createMock(Log.class);
154 Log logDelegate = new StdOutMavenLogger();
155 mojo.setLog(log);
156
157 List<FileSet> fileSets = new ArrayList<FileSet>();
158 FileSet fileSet = new FileSet();
159 fileSet.setDirectory("./src/test/testFiles");
160 fileSet.addInclude("**/*.txt");
161 fileSet.addExclude("**/*-Exclude.txt");
162 fileSets.add(fileSet);
163 mojo.setFiles(fileSets);
164
165 mojo.setWarnOnly(true);
166
167 log.debug(CHECKING_FOR_BOM_MSG + BOM_TEST_FILE_NAME);
168 expectLastCall().andDelegateTo(logDelegate).once();
169 log.debug(CHECKING_FOR_BOM_MSG + NO_BOM_TEST_FILE_NAME);
170 expectLastCall().andDelegateTo(logDelegate).once();
171 log.warn(FOUND_BOM_MSG_PREFIX + BOM_TEST_FILE_NAME + FOUND_BOM_WARN_ONLY_MSG_SUFFIX);
172 expectLastCall().andDelegateTo(logDelegate).once();
173 expect(log.isDebugEnabled()).andStubDelegateTo(logDelegate);
174 expect(log.isWarnEnabled()).andStubDelegateTo(logDelegate);
175
176 replay(log);
177 mojo.execute();
178 verify(log);
179
180 assert checkForBOM(new File(BOM_TEST_FILE_NAME));
181 }
182
183 @Test(dependsOnMethods={"testStripBOMMojoWarnOnly","testStripBOMMojoWarnOnlyFailBuild"})
184 public void testStripBOMMojo() throws Exception {
185 System.out.println("*** testStripBOMMojo");
186 StripBOMMojo mojo = new StripBOMMojo();
187
188 Log log = createMock(Log.class);
189 Log logDelegate = new StdOutMavenLogger();
190 mojo.setLog(log);
191
192 List<FileSet> fileSets = new ArrayList<FileSet>();
193 FileSet fileSet = new FileSet();
194 fileSet.setDirectory("./src/test/testFiles");
195 fileSet.addInclude("**/*.txt");
196 fileSet.addExclude("**/*-Exclude.txt");
197 fileSets.add(fileSet);
198 mojo.setFiles(fileSets);
199
200 log.debug(CHECKING_FOR_BOM_MSG + BOM_TEST_FILE_NAME);
201 expectLastCall().andDelegateTo(logDelegate).once();
202 log.debug(CHECKING_FOR_BOM_MSG + NO_BOM_TEST_FILE_NAME);
203 expectLastCall().andDelegateTo(logDelegate).once();
204 log.warn(FOUND_BOM_MSG_PREFIX + BOM_TEST_FILE_NAME + FOUND_BOM_MSG_SUFFIX);
205 expectLastCall().andDelegateTo(logDelegate).once();
206 expect(log.isDebugEnabled()).andStubDelegateTo(logDelegate);
207 expect(log.isWarnEnabled()).andStubDelegateTo(logDelegate);
208
209 replay(log);
210 mojo.execute();
211 verify(log);
212
213 assert !checkForBOM(new File(BOM_TEST_FILE_NAME));
214 }
215
216 @Test(dependsOnMethods="testStripBOMMojo")
217 public void testStripBOMMojoWarnOnlyFailBuildNoBOMs() throws Exception {
218 System.out.println("*** testStripBOMMojoWarnOnlyFailBuildNoBOMs");
219 StripBOMMojo mojo = new StripBOMMojo();
220
221 Log log = createMock(Log.class);
222 Log logDelegate = new StdOutMavenLogger();
223 mojo.setLog(log);
224
225 List<FileSet> fileSets = new ArrayList<FileSet>();
226 FileSet fileSet = new FileSet();
227 fileSet.setDirectory("./src/test/testFiles");
228 fileSet.addInclude("**/*.txt");
229 fileSet.addExclude("**/*-Exclude.txt");
230 fileSets.add(fileSet);
231 mojo.setFiles(fileSets);
232
233 mojo.setWarnOnly(true);
234 mojo.setFailBuild(true);
235
236 log.debug(CHECKING_FOR_BOM_MSG + BOM_TEST_FILE_NAME);
237 expectLastCall().andDelegateTo(logDelegate).once();
238 log.debug(CHECKING_FOR_BOM_MSG + NO_BOM_TEST_FILE_NAME);
239 expectLastCall().andDelegateTo(logDelegate).once();
240 expect(log.isDebugEnabled()).andStubDelegateTo(logDelegate);
241
242 replay(log);
243 mojo.execute();
244 verify(log);
245 }
246 }