Skip to content

Commit

Permalink
[MSHARED-1341] Convert tests to Junit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
slachiewicz committed Dec 23, 2023
1 parent ebefa11 commit dce786d
Show file tree
Hide file tree
Showing 16 changed files with 288 additions and 197 deletions.
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.7.0</version>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-testing</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -140,6 +140,12 @@
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
import org.apache.commons.io.IOUtils;
import org.codehaus.plexus.interpolation.Interpolator;
import org.codehaus.plexus.interpolation.RecursionInterceptor;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.when;
Expand All @@ -40,13 +40,13 @@ public abstract class AbstractInterpolatorFilterReaderLineEndingTest {
@Mock
private Interpolator interpolator;

@Before
@BeforeEach
public void onSetup() {
MockitoAnnotations.initMocks(this);
MockitoAnnotations.openMocks(this);
}

@Test
public void testDefaults() throws Exception {
void defaults() throws Exception {
when(interpolator.interpolate(eq("${a}"), eq(""), isA(RecursionInterceptor.class)))
.thenReturn("DONE_A");

Expand Down Expand Up @@ -89,7 +89,7 @@ public void testDefaults() throws Exception {

// MSHARED-198: custom delimiters doesn't work as expected
@Test
public void testCustomDelimiters() throws Exception {
void customDelimiters() throws Exception {
when(interpolator.interpolate(eq("aaaFILTER.a.MEaaa"), eq(""), isA(RecursionInterceptor.class)))
.thenReturn("DONE");
when(interpolator.interpolate(eq("abcFILTER.a.MEabc"), eq(""), isA(RecursionInterceptor.class)))
Expand All @@ -107,7 +107,7 @@ public void testCustomDelimiters() throws Exception {

// MSHARED-235: reader exceeds readAheadLimit
@Test
public void testMarkInvalid() throws IOException {
void markInvalid() throws IOException {
try (Reader reader = getAtReader(new StringReader("@\").replace(p,\"]\").replace(q,\""), interpolator, "\\")) {
assertEquals("@\").replace(p,\"]\").replace(q,\"", IOUtils.toString(reader));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,39 @@
import java.util.LinkedHashSet;

import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;

/**
* @author Karl Heinz Marbaise <a href="mailto:khmarbaise@apache.org">khmarbaise@apache.org</a>.
*/
public class AbstractMavenFilteringRequestTest {
class AbstractMavenFilteringRequestTest {

private AbstractMavenFilteringRequest request = new AbstractMavenFilteringRequest();
private LinkedHashSet<String> delimiters = new LinkedHashSet<>();

@Test
public void setDelimitersShouldNotChangeAnythingIfUsingNull() {
void setDelimitersShouldNotChangeAnythingIfUsingNull() {
request.setDelimiters(null, false);
assertThat(request.getDelimiters(), Matchers.contains("${*}", "@"));
}

@Test
public void setDelimitersShouldNotChangeAnythingIfUsingEmpty() {
void setDelimitersShouldNotChangeAnythingIfUsingEmpty() {
request.setDelimiters(delimiters, false);
assertThat(request.getDelimiters(), Matchers.contains("${*}", "@"));
}

@Test
public void setDelimitersShouldAddOnlyTheGivenDelimiter() {
void setDelimitersShouldAddOnlyTheGivenDelimiter() {
delimiters.add("test");
request.setDelimiters(delimiters, false);
assertThat(request.getDelimiters(), Matchers.contains("test"));
}

@Test
public void setDelimitersShouldAddDefaultDelimitersForNullElements() {
void setDelimitersShouldAddDefaultDelimitersForNullElements() {
delimiters.add("test");
delimiters.add(null);
delimiters.add("second");
Expand All @@ -62,27 +62,27 @@ public void setDelimitersShouldAddDefaultDelimitersForNullElements() {
}

@Test
public void setDelimitersShouldAddDefaultDelimitersIfUseDefaultDelimitersIfNullGiven() {
void setDelimitersShouldAddDefaultDelimitersIfUseDefaultDelimitersIfNullGiven() {
request.setDelimiters(null, true);
assertThat(request.getDelimiters(), Matchers.contains("${*}", "@"));
}

@Test
public void setDelimitersShouldAddDefaultDelimitersIfUseDefaultDelimitersIfNotNullGiven() {
void setDelimitersShouldAddDefaultDelimitersIfUseDefaultDelimitersIfNotNullGiven() {
LinkedHashSet<String> delimiters = new LinkedHashSet<>();
request.setDelimiters(delimiters, true);
assertThat(request.getDelimiters(), Matchers.contains("${*}", "@"));
}

@Test
public void setDelimitersShouldAddDefaultDelimitersIfUseDefaultDelimitersIfSingleElementIsGiven() {
void setDelimitersShouldAddDefaultDelimitersIfUseDefaultDelimitersIfSingleElementIsGiven() {
delimiters.add("test");
request.setDelimiters(delimiters, true);
assertThat(request.getDelimiters(), Matchers.contains("${*}", "@", "test"));
}

@Test
public void setDelimitersShouldAddDefaultDelimitersForNullElement() {
void setDelimitersShouldAddDefaultDelimitersForNullElement() {
delimiters.add("test");
delimiters.add(null);
delimiters.add("second");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
import java.io.Reader;
import java.io.StringReader;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class BoundedReaderTest {
class BoundedReaderTest {

private final Reader sr = new BufferedReader(new StringReader("01234567890"));

@Test
public void readTillEnd() throws IOException {
void readTillEnd() throws IOException {
try (BoundedReader mr = new BoundedReader(sr, 3)) {
mr.mark(3);
mr.read();
Expand All @@ -43,7 +43,7 @@ public void readTillEnd() throws IOException {
}

@Test
public void readMulti() throws IOException {
void readMulti() throws IOException {
char[] cbuf = new char[4];
for (int i = 0; i < cbuf.length; i++) {
cbuf[i] = 'X';
Expand All @@ -61,7 +61,7 @@ public void readMulti() throws IOException {
}

@Test
public void readMultiWithOffset() throws IOException {
void readMultiWithOffset() throws IOException {

char[] cbuf = new char[4];
for (int i = 0; i < cbuf.length; i++) {
Expand All @@ -80,7 +80,7 @@ public void readMultiWithOffset() throws IOException {
}

@Test
public void resetWorks() throws IOException {
void resetWorks() throws IOException {
try (BoundedReader mr = new BoundedReader(sr, 3)) {
mr.read();
mr.read();
Expand All @@ -94,7 +94,7 @@ public void resetWorks() throws IOException {
}

@Test
public void skipTest() throws IOException {
void skipTest() throws IOException {
try (BoundedReader mr = new BoundedReader(sr, 3)) {
mr.skip(2);
mr.read();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.maven.shared.filtering;

import javax.inject.Inject;

import java.io.File;
import java.io.Reader;
import java.io.StringReader;
Expand All @@ -31,27 +33,34 @@

import org.apache.commons.io.IOUtils;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.testing.PlexusTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.sonatype.plexus.build.incremental.BuildContext;

import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;

/**
* @author Olivier Lamy
*
*/
public class DefaultMavenFileFilterTest extends TestSupport {
@PlexusTest
class DefaultMavenFileFilterTest {

@Inject
MavenFileFilter mavenFileFilter;

File to = new File(getBasedir(), "target/reflection-test.properties");

@Override
protected void setUp() throws Exception {
super.setUp();
@BeforeEach
void setUp() throws Exception {
Files.deleteIfExists(to.toPath());
}

public void testOverwriteFile() throws Exception {
MavenFileFilter mavenFileFilter = lookup(MavenFileFilter.class);

@Test
void overwriteFile() throws Exception {
File from = new File(getBasedir(), "src/test/units-files/reflection-test.properties");

mavenFileFilter.copyFile(from, to, false, null, null);
Expand All @@ -69,15 +78,14 @@ public void testOverwriteFile() throws Exception {
assertEquals("older file", properties.getProperty("version"));
}

public void testNullSafeDefaultFilterWrappers() throws Exception {
MavenFileFilter mavenFileFilter = lookup(MavenFileFilter.class);

@Test
void nullSafeDefaultFilterWrappers() throws Exception {
mavenFileFilter.getDefaultFilterWrappers(null, null, false, null, null);

// shouldn't fail
}

public void testMultiFilterFileInheritance() throws Exception {
@Test
void multiFilterFileInheritance() throws Exception {
DefaultMavenFileFilter mavenFileFilter = new DefaultMavenFileFilter(mock(BuildContext.class));

File testDir = new File(getBasedir(), "src/test/units-files/MSHARED-177");
Expand All @@ -92,14 +100,13 @@ public void testMultiFilterFileInheritance() throws Exception {

mavenFileFilter.loadProperties(filterProperties, new File(getBasedir()), filters, new Properties());

assertTrue(filterProperties.getProperty("third_filter_key").equals("first and second"));
assertEquals("first and second", filterProperties.getProperty("third_filter_key"));
}

// MSHARED-161: DefaultMavenFileFilter.getDefaultFilterWrappers loads
// filters from the current directory instead of using basedir
public void testMavenBasedir() throws Exception {
MavenFileFilter mavenFileFilter = lookup(MavenFileFilter.class);

@Test
void mavenBasedir() throws Exception {
AbstractMavenFilteringRequest req = new AbstractMavenFilteringRequest();
req.setFileFilters(Collections.singletonList("src/main/filters/filefilter.properties"));

Expand All @@ -116,9 +123,8 @@ public void testMavenBasedir() throws Exception {
}

// MSHARED-198: custom delimiters doesn't work as expected
public void testCustomDelimiters() throws Exception {
MavenFileFilter mavenFileFilter = lookup(MavenFileFilter.class);

@Test
void customDelimiters() throws Exception {
AbstractMavenFilteringRequest req = new AbstractMavenFilteringRequest();
Properties additionalProperties = new Properties();
additionalProperties.setProperty("FILTER.a.ME", "DONE");
Expand All @@ -135,9 +141,8 @@ public void testCustomDelimiters() throws Exception {
}

// MSHARED-199: Filtering doesn't work if 2 delimiters are used on the same line, the first one being left open
public void testLineWithSingleAtAndExpression() throws Exception {
MavenFileFilter mavenFileFilter = lookup(MavenFileFilter.class);

@Test
void lineWithSingleAtAndExpression() throws Exception {
AbstractMavenFilteringRequest req = new AbstractMavenFilteringRequest();
Properties additionalProperties = new Properties();
additionalProperties.setProperty("foo", "bar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,28 @@
*/
package org.apache.maven.shared.filtering;

import javax.inject.Inject;

import java.io.Reader;
import java.io.StringReader;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.codehaus.plexus.testing.PlexusTest;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author Kristian Rosenvold
*/
public class DefaultMavenReaderFilterTest extends TestSupport {
public void testJustDoSomeFiltering() throws Exception {
MavenReaderFilter readerFilter = lookup(MavenReaderFilter.class);
@PlexusTest
class DefaultMavenReaderFilterTest {
@Inject
MavenReaderFilter readerFilter;

@Test
void justDoSomeFiltering() throws Exception {
StringReader src = new StringReader("toto@titi.com ${foo}");
MavenReaderFilterRequest req = new MavenReaderFilterRequest();
Properties additionalProperties = new Properties();
Expand Down

0 comments on commit dce786d

Please sign in to comment.