1
1
package org .javalite .lessc .maven ;
2
2
3
+ import org .apache .maven .shared .invoker .*;
3
4
import org .junit .Test ;
4
5
5
6
import java .io .*;
7
+ import java .util .Arrays ;
6
8
7
9
import static org .javalite .test .jspec .JSpec .a ;
8
10
import static org .javalite .test .jspec .JSpec .the ;
12
14
*/
13
15
public class IntegrationTest {
14
16
15
- String maven = System .getProperty ("os.name" ).contains ("Windows" ) ? "cmd.exe /c mvn" : "mvn" ;
17
+ protected String execute (String root , String ... args ) throws IOException , InterruptedException , MavenInvocationException {
18
+ InvocationRequest request = new DefaultInvocationRequest ();
19
+ request .setPomFile ( new File ( root + "/pom.xml" ) );
20
+ request .setGoals (Arrays .asList (args ));
21
+ Invoker invoker = new DefaultInvoker ();
22
+ invoker .setWorkingDirectory (new File (root ));
16
23
17
- protected String execute (String dir , String ... args ) throws IOException , InterruptedException {
18
- InputStream stdErr , stdOut ;
19
- Process process = Runtime .getRuntime ().exec (args , new String []{"JAVA_HOME=" + System .getProperty ("java.home" )}, new File (dir ));
20
- stdErr = process .getErrorStream ();
21
- stdOut = process .getInputStream ();
22
- BufferedReader reader = new BufferedReader (new InputStreamReader (stdOut ));
23
- String content = getContent (reader );
24
- reader .close ();
25
- reader = new BufferedReader (new InputStreamReader (stdErr ));
26
- content += getContent (reader );
27
- reader .close ();
28
- return content ;
24
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
25
+ ByteArrayOutputStream errorStream = new ByteArrayOutputStream ();
26
+ invoker .setErrorHandler (new PrintStreamHandler (new PrintStream (outputStream ), true ));
27
+ invoker .setOutputHandler (new PrintStreamHandler (new PrintStream (errorStream ), true ));
28
+ invoker .execute (request );
29
+ String output = outputStream .toString ();
30
+ output += errorStream .toString ();
31
+ return output ;
29
32
}
30
33
31
34
private String getContent (BufferedReader reader ) throws IOException {
@@ -39,25 +42,26 @@ private String getContent(BufferedReader reader) throws IOException {
39
42
}
40
43
41
44
@ Test
42
- public void shouldCompileProjectWithSingleLessFile () throws IOException , InterruptedException {
45
+ public void shouldCompileProjectWithSingleLessFile () throws IOException , InterruptedException , MavenInvocationException {
43
46
44
47
String root = "target/test-project" ;
45
48
46
- String output = execute (root , maven , "clean" );
49
+ String output = execute (root , "clean" );
47
50
the (output ).shouldContain ("BUILD SUCCESS" );
48
- output = execute (root , "mvn" , "install" , "-o" );
51
+
52
+ output = execute (root , "install" , "-o" );
49
53
the (output ).shouldContain ("BUILD SUCCESS" );
50
54
51
55
File f = new File (root + "/target/web/bootstrap.css" );
52
56
a (f .exists ()).shouldBeTrue ();
53
57
}
54
58
55
59
@ Test
56
- public void shouldCompileProjectWithMultipleLessFile () throws IOException , InterruptedException {
60
+ public void shouldCompileProjectWithMultipleLessFile () throws IOException , InterruptedException , MavenInvocationException {
57
61
String root = "target/test-project-list" ;
58
- String output = execute (root , maven , "clean" );
62
+ String output = execute (root , "clean" );
59
63
the (output ).shouldContain ("BUILD SUCCESS" );
60
- output = execute (root , maven , "install" , "-o" );
64
+ output = execute (root , "install" , "-o" );
61
65
the (output ).shouldContain ("BUILD SUCCESS" );
62
66
63
67
File f = new File (root + "/target/web1/bootstrap.css" );
0 commit comments