forked from ifedorenko/incrementalbuild-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CopyFilesMojoTest.java
99 lines (82 loc) · 3.51 KB
/
CopyFilesMojoTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*******************************************************************************
* Copyright (c) 2015-2016 Igor Fedorenko
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Igor Fedorenko - initial API and implementation
*******************************************************************************/
package com.ifedorenko.incrementalbuild.tutorial;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.maven.plugin.MojoExecutionException;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import io.takari.incrementalbuild.maven.testing.IncrementalBuildRule;
import io.takari.maven.testing.TestResources;
public class CopyFilesMojoTest {
@Rule
public final TemporaryFolder temp = new TemporaryFolder();
@Rule
public final TestResources resources = new TestResources();
@Rule
public final IncrementalBuildRule maven = new IncrementalBuildRule();
@Test
public void testBasicIncremental() throws Exception {
File basedir = resources.getBasedir("copy-resources");
// initial build
maven.executeMojo(basedir, "copy-resources");
maven.assertBuildOutputs(basedir, "target/r1.txt", "target/r2.txt");
// no-change rebuild
maven.executeMojo(basedir, "copy-resources");
maven.assertCarriedOverOutputs(basedir, "target/r1.txt", "target/r2.txt");
// introduce new input
Assert.assertTrue(new File(basedir, "src/r3.txt").createNewFile());
maven.executeMojo(basedir, "copy-resources");
maven.assertBuildOutputs(basedir, "target/r3.txt");
maven.assertCarriedOverOutputs(basedir, "target/r1.txt", "target/r2.txt");
// delete an input
Assert.assertTrue(new File(basedir, "src/r1.txt").delete());
maven.executeMojo(basedir, "copy-resources");
maven.assertDeletedOutputs(basedir, "target/r1.txt");
maven.assertCarriedOverOutputs(basedir, "target/r2.txt", "target/r3.txt");
}
@Test
public void testMessages() throws Exception {
File basedir = resources.getBasedir();
File src = new File(basedir, "src");
src.mkdirs();
// generate an invalid resource
try (OutputStream out = new FileOutputStream(new File(src, "r1.txt"))) {
out.write(new byte[13]);
}
// initial build
try {
maven.executeMojo(basedir, "copy-resources");
} catch (MojoExecutionException e) {
Assert.assertTrue(e.getMessage().contains("invalid file length"));
}
maven.assertBuildOutputs(basedir, new String[0]);
maven.assertMessages(basedir, "src/r1.txt", "ERROR r1.txt [0:0] invalid file length");
// no-change rebuild, expect the same failure and the same error message
try {
maven.executeMojo(basedir, "copy-resources");
} catch (MojoExecutionException e) {
Assert.assertTrue(e.getMessage().contains("invalid file length"));
}
maven.assertBuildOutputs(basedir, new String[0]);
maven.assertMessages(basedir, "src/r1.txt", "ERROR r1.txt [0:0] invalid file length");
// fix the build by change input file length
try (OutputStream out = new FileOutputStream(new File(src, "r1.txt"))) {
out.write(new byte[12]);
}
maven.executeMojo(basedir, "copy-resources");
maven.assertMessages(basedir, "src/r1.txt", new String[0]);
maven.assertBuildOutputs(basedir, "target/r1.txt");
}
}