Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove PowerMock #134

Merged
merged 2 commits into from
Sep 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.25</version>
<version>4.26</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -160,17 +160,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
32 changes: 19 additions & 13 deletions src/test/java/hudson/plugins/timestamper/TimestampNoteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import hudson.MarkupText;
import hudson.model.Run;
import hudson.plugins.timestamper.format.TimestampFormat;
import hudson.plugins.timestamper.format.TimestampFormatProvider;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.function.Supplier;
import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.junit.After;
Expand All @@ -45,8 +46,8 @@
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.MockitoAnnotations;
import org.powermock.reflect.Whitebox;

/**
* Unit test for the {@link TimestampNote} class.
Expand All @@ -62,8 +63,6 @@ public class TimestampNoteTest {

private static final long TIME = 3;

private Supplier<TimestampFormat> originalSupplier;

/** @return the test cases */
@Parameters(name = "{0}, {1}")
public static Iterable<Object[]> data() {
Expand All @@ -82,7 +81,7 @@ public static Iterable<Object[]> data() {

private static Run<?, ?> build() {
Run<?, ?> build = mock(Run.class);
Whitebox.setInternalState(build, "timestamp", BUILD_START);
when(build.getStartTimeInMillis()).thenReturn(BUILD_START);
when(build.toString())
.thenReturn(new ToStringBuilder("Run").append("startTime", BUILD_START).toString());
return build;
Expand All @@ -92,11 +91,21 @@ private static TimestampNote note(Long elapsedMillis, long millisSinceEpoch) {
TimestampNote note =
new TimestampNote(elapsedMillis == null ? 0L : elapsedMillis, millisSinceEpoch);
if (elapsedMillis == null) {
Whitebox.setInternalState(note, "elapsedMillis", (Object) null);
setInternalState(note, "elapsedMillis", null);
}
return note;
}

private static void setInternalState(Object obj, String fieldName, Object newValue) {
try {
Field field = obj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, newValue);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new AssertionError(e);
}
}

@Parameter(0)
public Object context;

Expand All @@ -113,16 +122,10 @@ private static TimestampNote note(Long elapsedMillis, long millisSinceEpoch) {
@Before
public void setUp() {
closeable = MockitoAnnotations.openMocks(this);

originalSupplier = Whitebox.getInternalState(TimestampFormatProvider.class, Supplier.class);
Whitebox.setInternalState(
TimestampFormatProvider.class, (Supplier<TimestampFormat>) () -> format);
}

@After
public void tearDown() throws Exception {
Whitebox.setInternalState(TimestampFormatProvider.class, Supplier.class, originalSupplier);

closeable.close();
}

Expand All @@ -140,7 +143,10 @@ public void testGetTimestamp_afterSerialization() {
@Test
public void testAnnotate() {
MarkupText text = new MarkupText("");
note.annotate(context, text, 0);
try (MockedStatic<TimestampFormatProvider> mocked = mockStatic(TimestampFormatProvider.class)) {
mocked.when(TimestampFormatProvider::get).thenReturn(format);
note.annotate(context, text, 0);
}
verify(format).markup(text, expectedTimestamp);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

import hudson.MarkupText;
Expand All @@ -43,7 +44,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
import org.apache.commons.lang3.SerializationUtils;
import org.junit.After;
import org.junit.Before;
Expand All @@ -54,8 +54,8 @@
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.mockito.MockedStatic;
import org.mockito.stubbing.Answer;
import org.powermock.reflect.Whitebox;

/**
* Unit test for the {@link TimestampAnnotator} class.
Expand Down Expand Up @@ -166,11 +166,9 @@ private List<Timestamp> writeTimestamps(int count) throws IOException {
private List<Timestamp> annotate() {
ConsoleLogParser logParser = new MockConsoleLogParser();
ConsoleAnnotator annotator = new TimestampAnnotator(logParser);
Supplier<TimestampFormat> originalSupplier =
Whitebox.getInternalState(TimestampFormatProvider.class, Supplier.class);

captureFormattedTimestamps();
try {
try (MockedStatic<TimestampFormatProvider> mocked = mockStatic(TimestampFormatProvider.class)) {
captureFormattedTimestamps(mocked);
int iterations = 0;
while (annotator != null) {
if (serialize) {
Expand All @@ -182,13 +180,11 @@ private List<Timestamp> annotate() {
throw new AssertionError("annotator is not terminating");
}
}
} finally {
Whitebox.setInternalState(TimestampFormatProvider.class, Supplier.class, originalSupplier);
}
return capturedTimestamps;
}

private void captureFormattedTimestamps() {
private static void captureFormattedTimestamps(MockedStatic<TimestampFormatProvider> mocked) {
final TimestampFormat format = mock(TimestampFormat.class);
doAnswer(
(Answer<Void>)
Expand All @@ -199,8 +195,7 @@ private void captureFormattedTimestamps() {
})
.when(format)
.markup(any(MarkupText.class), any(Timestamp.class));
Whitebox.setInternalState(
TimestampFormatProvider.class, Supplier.class, (Supplier<TimestampFormat>) () -> format);
mocked.when(TimestampFormatProvider::get).thenReturn(format);
}

private static class MockConsoleLogParser extends ConsoleLogParser {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

/**
* Unit test for the {@link SystemTimestampFormat} class.
Expand All @@ -45,8 +44,8 @@
*/
public class SystemTimestampFormatTest {

private static final String TIME_ZONE_PROPERTY =
Whitebox.getInternalState(SystemTimestampFormat.class, "TIME_ZONE_PROPERTY");
// cf. hudson.plugins.timestamper.format.SystemTimestampFormat.TIME_ZONE_PROPERTY
private static final String TIME_ZONE_PROPERTY = "org.apache.commons.jelly.tags.fmt.timeZone";

private TimeZone systemDefaultTimeZone;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.powermock.reflect.Whitebox;

/**
* Test for integration between the {@link TimestampsReader} and {@link TimestampsWriter} classes.
Expand All @@ -57,7 +56,7 @@ public class TimestampsIOTest {
public void setUp() throws Exception {
build = mock(Run.class);
when(build.getRootDir()).thenReturn(folder.getRoot());
Whitebox.setInternalState(build, "timestamp", 1L);
when(build.getStartTimeInMillis()).thenReturn(1L);

reader = new TimestampsReader(build);
writer = new TimestampsWriter(build);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.powermock.reflect.Whitebox;

/**
* Unit test for the {@link TimestampsWriter} class.
Expand Down Expand Up @@ -122,7 +121,7 @@ public void testWriteSeveralTimes() throws Exception {

@Test
public void testWriteSameTimestampManyTimes() throws Exception {
int bufferSize = Whitebox.getField(TimestampsWriter.class, "BUFFER_SIZE").getInt(null);
int bufferSize = 1024; // cf. hudson.plugins.timestamper.io.TimestampsWriter.BUFFER_SIZE
int times = bufferSize + 1000; // larger than the buffer
timestampsWriter = new TimestampsWriter(build);
timestampsWriter.write(10000, times);
Expand Down