Skip to content

Commit

Permalink
refactoring: util classes instances as final fields
Browse files Browse the repository at this point in the history
  • Loading branch information
giulong committed Aug 17, 2024
1 parent 17bb06f commit ce1e11c
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private FileReporter getTestBookReporterFrom(final Configuration configuration,
.stream()
.filter(reporter -> reporter instanceof FileReporter)
.map(FileReporter.class::cast)
.filter(reporter -> FileUtils.getInstance().getExtensionOf(reporter.getTemplate()).equals(extension))
.filter(reporter -> fileUtils.getExtensionOf(reporter.getTemplate()).equals(extension))
.findFirst()
.orElseThrow();
}
Expand All @@ -235,7 +235,7 @@ private FileReporter getSummaryReporterFrom(final Configuration configuration, f
.stream()
.filter(reporter -> reporter instanceof FileReporter)
.map(FileReporter.class::cast)
.filter(reporter -> FileUtils.getInstance().getExtensionOf(reporter.getTemplate()).equals(extension))
.filter(reporter -> fileUtils.getExtensionOf(reporter.getTemplate()).equals(extension))
.findFirst()
.orElseThrow();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class InterpolatedObjectDeserializer extends JsonDeserializer<Object> {
private static final Pattern NUMBER = Pattern.compile("-?\\d+(.\\d+|,\\d+)?");

private final Vars vars = Vars.getInstance();
private final InterpolatedStringDeserializer interpolatedStringDeserializer = InterpolatedStringDeserializer.getInstance();

public static InterpolatedObjectDeserializer getInstance() {
return INSTANCE;
Expand All @@ -43,7 +44,7 @@ public Object deserialize(final JsonParser jsonParser, final DeserializationCont
final Matcher matcher = INT_PATTERN.matcher(textValue);
yield matcher.matches()
? interpolate(textValue, currentName, matcher)
: InterpolatedStringDeserializer.getInstance().interpolate(textValue, currentName);
: interpolatedStringDeserializer.interpolate(textValue, currentName);
}
case NUMBER -> jsonNode.numberValue();
default -> jsonNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class ExtentReporter implements SessionHook, CanProduceMetadata {
protected final FileUtils fileUtils = FileUtils.getInstance();
protected final Configuration configuration = Configuration.getInstance();

private final MetadataManager metadataManager = MetadataManager.getInstance();

private ExtentReports extentReports;

public static ExtentReporter getInstance() {
Expand Down Expand Up @@ -99,7 +101,6 @@ public Retention getRetention() {

@Override
public void produceMetadata() {
final MetadataManager metadataManager = MetadataManager.getInstance();
final File file = getReportPathFrom(configuration.getExtent()).toFile();
final int maxSize = getRetention().getSuccessful();
final FixedSizeQueue<File> queue = metadataManager.getSuccessfulQueueOf(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import io.github.giulong.spectrum.utils.Reflections;
import io.github.giulong.spectrum.utils.Vars;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -12,7 +13,6 @@
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;

import java.io.IOException;
import java.util.stream.Stream;
Expand All @@ -24,15 +24,12 @@
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.junit.jupiter.params.provider.EnumSource.Mode.EXCLUDE;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

class InterpolatedObjectDeserializerTest {

private static final String VAR_IN_ENV = "456";

private static MockedStatic<InterpolatedStringDeserializer> interpolatedStringDeserializerMockedStatic;

@Mock
private InterpolatedStringDeserializer interpolatedStringDeserializer;

Expand All @@ -55,12 +52,7 @@ public static void beforeAll() {

@BeforeEach
public void beforeEach() {
interpolatedStringDeserializerMockedStatic = mockStatic(InterpolatedStringDeserializer.class);
}

@AfterEach
public void afterEach() {
interpolatedStringDeserializerMockedStatic.close();
Reflections.setField("interpolatedStringDeserializer", interpolatedObjectDeserializer, interpolatedStringDeserializer);
}

@AfterAll
Expand All @@ -86,7 +78,6 @@ public void deserializeStrings() throws IOException {
when(jsonParser.currentName()).thenReturn(currentName);
when(jsonNode.getNodeType()).thenReturn(STRING);
when(jsonNode.textValue()).thenReturn(value);
when(InterpolatedStringDeserializer.getInstance()).thenReturn(interpolatedStringDeserializer);
when(interpolatedStringDeserializer.interpolate(value, currentName)).thenReturn(expected);

assertEquals(expected, interpolatedObjectDeserializer.deserialize(jsonParser, deserializationContext));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class ExtentReporterTest {
public void beforeEach() {
Reflections.setField("fileUtils", extentReporter, fileUtils);
Reflections.setField("configuration", extentReporter, configuration);
Reflections.setField("metadataManager", extentReporter, metadataManager);
testDataMockedStatic = mockStatic(TestData.class);
freeMarkerWrapperMockedStatic = mockStatic(FreeMarkerWrapper.class);
pathMockedStatic = mockStatic(Path.class);
Expand Down Expand Up @@ -382,7 +383,6 @@ public void produceMetadata() {
when(absolutePath.toFile()).thenReturn(file1);
when(retention.getSuccessful()).thenReturn(retentionSuccessful);

when(MetadataManager.getInstance()).thenReturn(metadataManager);
when(metadataManager.getSuccessfulQueueOf(extentReporter)).thenReturn(fileFixedSizeQueue);
when(fileFixedSizeQueue.shrinkTo(retentionSuccessful - 1)).thenReturn(fileFixedSizeQueue);

Expand Down

0 comments on commit ce1e11c

Please sign in to comment.