Skip to content

Commit d678ae8

Browse files
committed
remove direct call to System.out.println() in tests
1 parent ba9f1da commit d678ae8

6 files changed

Lines changed: 139 additions & 9 deletions

File tree

pom.xml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,22 @@
360360
</dependency>
361361

362362
<!--to avoid complaints during tests -->
363-
<dependency>
364-
<groupId>org.slf4j</groupId>
365-
<artifactId>slf4j-simple</artifactId>
366-
<version>2.0.17</version>
367-
<scope>test</scope>
368-
</dependency>
369-
</dependencies>
363+
<dependency>
364+
<groupId>org.slf4j</groupId>
365+
<artifactId>slf4j-api</artifactId>
366+
<version>2.0.17</version>
367+
</dependency>
368+
<dependency>
369+
<groupId>org.apache.maven</groupId>
370+
<artifactId>maven-slf4j-provider</artifactId>
371+
<version>3.9.9</version>
372+
</dependency>
373+
<dependency>
374+
<groupId>org.eclipse.sisu</groupId>
375+
<artifactId>org.eclipse.sisu.plexus</artifactId>
376+
<version>0.3.5</version>
377+
</dependency>
378+
</dependencies>
370379

371380
<distributionManagement>
372381
<snapshotRepository>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package pl.project13.log;
2+
3+
import org.apache.maven.plugin.logging.Log;
4+
import org.slf4j.Logger;
5+
import pl.project13.core.log.LogInterface;
6+
7+
public interface CombinedLogger extends Logger, LogInterface, Log {
8+
9+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package pl.project13.log;
2+
3+
import org.apache.maven.monitor.logging.DefaultLog;
4+
import org.apache.maven.plugin.AbstractMojo;
5+
import org.apache.maven.plugin.logging.Log;
6+
import org.slf4j.ILoggerFactory;
7+
import org.slf4j.Logger;
8+
import org.slf4j.helpers.MessageFormatter;
9+
10+
import org.slf4j.impl.MavenSimpleLogger;
11+
import org.slf4j.impl.MavenSimpleLoggerFactory;
12+
import org.slf4j.impl.SimpleLoggerFactory;
13+
14+
import java.lang.reflect.InvocationHandler;
15+
import java.lang.reflect.InvocationTargetException;
16+
import java.lang.reflect.Method;
17+
import java.lang.reflect.Proxy;
18+
19+
/**
20+
* Attempts to route logging to Maven Plugin / Mojo output during Maven builds.
21+
* Falls back to System.out if routing fails.
22+
*/
23+
public class MavenLogger implements ILoggerFactory, InvocationHandler {
24+
private static final MavenLogger INSTANCE = new MavenLogger();
25+
private static final CombinedLogger PROXY = (CombinedLogger) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[] { CombinedLogger.class }, INSTANCE);
26+
27+
private Logger logger;
28+
private AbstractMojo mojo;
29+
30+
private MavenLogger() {
31+
this.mojo = null;
32+
logger = new SimpleLoggerFactory().getLogger(MavenLogger.class.getName());
33+
}
34+
35+
public static CombinedLogger bindToMojo(final AbstractMojo mojo) {
36+
if(mojo == null)
37+
throw new IllegalArgumentException(new NullPointerException("mojo may not be null"));
38+
if(INSTANCE.mojo != mojo) {
39+
mojo.getPluginContext().
40+
mojo.setLog(PROXY);
41+
}
42+
return PROXY;
43+
}
44+
45+
public static CombinedLogger getLogger() {
46+
return PROXY;
47+
}
48+
49+
private boolean isBound() {
50+
return mojo != null;
51+
}
52+
53+
private Object loggingObject() {
54+
return mojo.getLog();
55+
}
56+
57+
@Override
58+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
59+
if(method.getName().equals("getName")) {
60+
return MavenLogger.class.getName();
61+
}
62+
63+
// reroute level checks to mojo logger
64+
if(method.getName().matches("is(Trace|Debug|Info|Warn|Error)Enabled")) {
65+
if(!isBound())
66+
return true;
67+
else {
68+
final String methodName = method.getName().replace("Trace", "Debug");
69+
return Logger.class.getMethod(methodName).invoke(loggingObject());
70+
}
71+
}
72+
73+
if(method.getName().matches("trace|debug|info|warn|error")) {
74+
if(isBound()) {
75+
final String methodName = method.getName().replace("trace", "debug");
76+
Logger.class.getMethod(methodName, String.class).invoke(loggingObject(), formatIntercepted(method, args));
77+
return null;
78+
} else {
79+
System.out.println("unbound " + formatIntercepted(method, args));
80+
return null;
81+
}
82+
}
83+
84+
System.out.println("Unhandled method: " + method.getName());
85+
return null;
86+
}
87+
88+
public String formatIntercepted(Method method, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
89+
if(args.length == 1)
90+
return args[0].toString();
91+
args[0] = args[0].toString();
92+
if(args.length > 1 && args[1] instanceof Object[]) {
93+
return MessageFormatter.class.getMethod("arrayFormat", method.getParameterTypes()).invoke(null, args).toString();
94+
} else {
95+
return MessageFormatter.class.getMethod("format", method.getParameterTypes()).invoke(null, args).toString();
96+
}
97+
}
98+
99+
@Override
100+
public Logger getLogger(String name) {
101+
return PROXY;
102+
}
103+
104+
}

src/test/java/pl/project13/maven/git/FileSystemMavenSandbox.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.annotation.Nullable;
2626
import org.apache.commons.io.FileUtils;
2727
import org.apache.maven.project.MavenProject;
28+
import pl.project13.log.MavenLogger;
2829

2930
/**
3031
* Quick and dirty maven projects tree structure to create on disk during integration tests. Can
@@ -73,7 +74,9 @@ public FileSystemMavenSandbox withChildProject(String childProjectDirName, Strin
7374

7475
@Nonnull
7576
public FileSystemMavenSandbox withGitRepoInParent(@Nonnull AvailableGitTestRepo repo) {
76-
gitRepoSourceDir = repo.getDir();
77+
MavenLogger.getLogger().info("Will prepare sandbox repository based on: [" + repo.getDir() + "]");
78+
MavenLogger.getLogger().info(MavenLogger.getLogger().getClass().getName());
79+
gitRepoSourceDir = repo.getDir();
7780
gitRepoTargetDir = parentProject.getBasedir();
7881
return this;
7982
}

src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class GitCommitIdMojoIntegrationTest extends GitIntegrationTest {
5050
@ParameterizedTest
5151
@MethodSource("useNativeGit")
5252
public void shouldIncludeExpectedProperties(boolean useNativeGit) throws Exception {
53-
// given
53+
// given
5454
mavenSandbox
5555
.withParentProject("my-jar-project", "jar")
5656
.withNoChildProject()

src/test/java/pl/project13/maven/git/GitIntegrationTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,17 @@
3636
import javax.annotation.Nonnull;
3737
import org.apache.commons.io.FileUtils;
3838
import org.apache.maven.execution.MavenSession;
39+
import org.apache.maven.monitor.logging.DefaultLog;
3940
import org.apache.maven.project.MavenProject;
4041
import org.apache.maven.settings.Settings;
4142
import org.eclipse.jgit.api.Git;
4243
import org.junit.jupiter.api.AfterEach;
4344
import org.junit.jupiter.api.BeforeEach;
4445
import org.junit.jupiter.params.provider.Arguments;
46+
import org.slf4j.Logger;
47+
import org.slf4j.spi.SLF4JServiceProvider;
4548
import pl.project13.core.CommitIdPropertiesOutputFormat;
49+
import pl.project13.log.MavenLogger;
4650

4751
/**
4852
* Base class for various Testcases to verify that the git-commit-id-plugin works properly.
@@ -83,6 +87,7 @@ public void setUp() throws Exception {
8387
mavenSandbox = new FileSystemMavenSandbox(currSandbox);
8488
mojo = spy(GitCommitIdMojo.class);
8589
initializeMojoWithDefaults(mojo);
90+
MavenLogger.bindToMojo(mojo);
8691
}
8792

8893
@AfterEach

0 commit comments

Comments
 (0)