Skip to content

Commit

Permalink
fix: classpath mode logging in MavenLauncher (#3750)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbwogi authored Jan 12, 2021
1 parent 515daee commit efebce8
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 54 deletions.
18 changes: 17 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,19 @@
<version>2.0.1.Final</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/uk.org.lidalia/slf4j-test -->
<dependency>
<groupId>uk.org.lidalia</groupId>
<artifactId>slf4j-test</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down Expand Up @@ -269,6 +275,16 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<classpathDependencyExcludes>
<classpathDependencyExclude>ch.qos.logback:logback-classic</classpathDependencyExclude>
</classpathDependencyExcludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/spoon/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ protected void processArguments() {
}

String cpmode = jsapActualArgs.getString("cpmode").toUpperCase();
Launcher.LOGGER.info("Running in " + cpmode + " mode (doc: http://spoon.gforge.inria.fr/launcher.html).");
reportClassPathMode();
CLASSPATH_MODE classpath_mode = CLASSPATH_MODE.valueOf(cpmode);
switch (classpath_mode) {
case NOCLASSPATH:
Expand Down Expand Up @@ -538,6 +538,11 @@ protected void processArguments() {

}

protected void reportClassPathMode() {
String cpmode = jsapActualArgs.getString("cpmode").toUpperCase();
Launcher.LOGGER.info("Running in " + cpmode + " mode (doc: http://spoon.gforge.inria.fr/launcher.html).");
}

/**
* Gets the list of processor types to be initially applied during the
* processing (-p option).
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/spoon/MavenLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
package spoon;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import spoon.support.compiler.SpoonPom;

import java.io.File;
Expand All @@ -21,6 +23,7 @@ public class MavenLauncher extends Launcher {
private SOURCE_TYPE sourceType;
private SpoonPom model;
private boolean forceRefresh = false;
private static final Logger LOGGER = LogManager.getLogger();

/**
* @return SpoonPom corresponding to the pom file used by the launcher
Expand Down Expand Up @@ -192,15 +195,24 @@ private void init(String mavenProject, String[] classpath, Pattern profileFilter

if (classpath == null) {
classpath = model.buildClassPath(mvnHome, sourceType, LOGGER, forceRefresh);
LOGGER.info("Running in FULLCLASSPATH mode. Source folders and dependencies are inferred from the pom.xml file (doc: http://spoon.gforge.inria.fr/launcher.html).");
} else {
LOGGER.info("Running in FULLCLASSPATH mode. Classpath is manually set (doc: http://spoon.gforge.inria.fr/launcher.html).");
}

// dependencies
factory.getEnvironment().setNoClasspath(false);
this.getModelBuilder().setSourceClasspath(classpath);

// compliance level
this.getEnvironment().setComplianceLevel(model.getSourceVersion());
}

@Override
protected void reportClassPathMode() {
// skip classpath mode logs from Launcher
}

/**
* Triggers regeneration of the classpath that is used for building the model, based on pom.xml
*/
Expand Down
121 changes: 69 additions & 52 deletions src/test/java/spoon/test/logging/LogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,70 +20,87 @@
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.mockito.ArgumentCaptor;
import spoon.Launcher;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import spoon.MavenLauncher;
import uk.org.lidalia.slf4jtest.TestLogger;
import uk.org.lidalia.slf4jtest.TestLoggerFactory;

import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;

@RunWith(Parameterized.class)
@RunWith(Enclosed.class)
public class LogTest {

@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{Level.ALL, 5 },
{Level.DEBUG, 5 },
{Level.INFO, 1 },
{Level.WARN, 0 },
{Level.ERROR, 0 },
{Level.OFF, 0 }
});
@RunWith(Parameterized.class)
public static class ParameterizedTest {
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{Level.ALL, 5},
{Level.DEBUG, 5},
{Level.INFO, 1},
{Level.WARN, 0},
{Level.ERROR, 0},
{Level.OFF, 0}
});
}

@Parameterized.Parameter(0)
public Level level;

@Parameterized.Parameter(1)
public int nbLogMessagesMinimum;

@Test
public void testAllLevelsForLogs() throws Exception {
final Launcher launcher = new Launcher();

Logger logger = mock(Logger.class);
FieldUtils.writeField(launcher.getEnvironment(), "logger", logger, true);

ArgumentCaptor<String> valueCaptureMessage = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Level> valueCaptureLevel = ArgumentCaptor.forClass(Level.class);

doNothing().when(logger).log(valueCaptureLevel.capture(), valueCaptureMessage.capture());

launcher.setArgs(new String[]{
"-i", "./src/test/java/spoon/test/logging",
"--level", level.toString()
});

// launcher provides two logging methods
launcher.getEnvironment().debugMessage("debugMessage");
launcher.getEnvironment().reportProgressMessage("reportProgressMessage");

// contract: the --level arguments sets the level
assertEquals(level, launcher.getFactory().getEnvironment().getLevel());

// contract: the number of messages increases with the log level
// System.out.println(level+ " " + valueCaptureMessage.getAllValues().size());
assertTrue(valueCaptureMessage.getAllValues().size() >= nbLogMessagesMinimum);
}
}

@Parameterized.Parameter(0)
public Level level;

@Parameterized.Parameter(1)
public int nbLogMessagesMinimum;

@Test
public void testAllLevelsForLogs() throws Exception {
final Launcher launcher = new Launcher();

Logger logger = mock(Logger.class);
FieldUtils.writeField(launcher.getEnvironment(), "logger", logger, true);

ArgumentCaptor<String> valueCaptureMessage = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Level> valueCaptureLevel = ArgumentCaptor.forClass(Level.class);

doNothing().when(logger).log(valueCaptureLevel.capture(), valueCaptureMessage.capture());

launcher.setArgs(new String[] {
"-i", "./src/test/java/spoon/test/logging",
"--level", level.toString()
});

// launcher provides two logging methods
launcher.getEnvironment().debugMessage("debugMessage");
launcher.getEnvironment().reportProgressMessage("reportProgressMessage");

// contract: the --level arguments sets the level
assertEquals(level, launcher.getFactory().getEnvironment().getLevel());

// contract: the number of messages increases with the log level
// System.out.println(level+ " " + valueCaptureMessage.getAllValues().size());
assertTrue(valueCaptureMessage.getAllValues().size() >= nbLogMessagesMinimum);



public static class NonParameterizedTest {

@Test
public void testMavenLauncherLogs() {
// contract: MavenLauncher should output different logs depending on whether the classpath is inferred or manually set
final TestLogger logger = TestLoggerFactory.getTestLogger(MavenLauncher.class);
MavenLauncher mavenLauncher = new MavenLauncher("./pom.xml", MavenLauncher.SOURCE_TYPE.APP_SOURCE);
assertEquals("Running in FULLCLASSPATH mode. Source folders and dependencies are inferred from the pom.xml file (doc: http://spoon.gforge.inria.fr/launcher.html).",logger.getLoggingEvents().get(0).getMessage());
logger.clear();
mavenLauncher = new MavenLauncher("./pom.xml", MavenLauncher.SOURCE_TYPE.APP_SOURCE, new String[]{"./"});
assertEquals("Running in FULLCLASSPATH mode. Classpath is manually set (doc: http://spoon.gforge.inria.fr/launcher.html).",logger.getLoggingEvents().get(0).getMessage());
}
}
}
}

0 comments on commit efebce8

Please sign in to comment.