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

Refactor simple Unit Tests #7571

Merged
merged 12 commits into from
May 6, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,13 @@
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;

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

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

public class CrossRefEntryComparatorTest {
private CrossRefEntryComparator comparator;

@BeforeEach
public void setUp() {
comparator = new CrossRefEntryComparator();
}

@AfterEach
public void tearDown() {
comparator = null;
}
private static final CrossRefEntryComparator comparator = new CrossRefEntryComparator();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this? I think it's good practice to initialize the object under test in a setup method, so that e.g. changes to it in one test doesn't leak to other tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I see it correctly it is not possible to change the CrossRefEntryComparator Object after creation. In other words there are no setter methods and the constructor does not take any arguments.

That is why I think the beforeeach is superfluous. Why create a new object, if the one created can be used?

On the other hand, I get your point. If changes to this class are done, like adding arguments to the constructor or adding setter methods, then the BeforeEach makes sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creation of objects I java is extremely efficient. This shouldn't be a concern.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In tests, one just does not use static.

  • Possiblity one: class variable (you can go for that, because the setup just does that) --> move the initialization code up.
  • Possibility two: use setup (as in the code)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reason for no static: Each test has to be independed of the last test. Thus, everything under test has to be in the initial state.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! I'll revert these changes then and use the setup method for those formatter tests.


@Test
public void isEqualForEntriesWithoutCrossRef() {
Expand Down
240 changes: 131 additions & 109 deletions src/test/java/org/jabref/logic/bst/BibtexCaseChangersTest.java

Large diffs are not rendered by default.

35 changes: 23 additions & 12 deletions src/test/java/org/jabref/logic/bst/BibtexPurifyTest.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
package org.jabref.logic.bst;

import org.junit.jupiter.api.Test;
import java.util.stream.Stream;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

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

public class BibtexPurifyTest {

@Test
public void testPurify() {
assertPurify("i", "i");
assertPurify("0I ", "0I~ ");
assertPurify("Hi Hi ", "Hi Hi ");
assertPurify("oe", "{\\oe}");
assertPurify("Hi oeHi ", "Hi {\\oe }Hi ");
assertPurify("Jonathan Meyer and Charles Louis Xavier Joseph de la Vallee Poussin", "Jonathan Meyer and Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin");
assertPurify("e", "{\\'e}");
assertPurify("Edouard Masterly", "{\\'{E}}douard Masterly");
assertPurify("Ulrich Underwood and Ned Net and Paul Pot", "Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot");
@ParameterizedTest
@MethodSource("provideTestStrings")
public void testPurify(String expected, String toBePurified) {
assertPurify(expected, toBePurified);
}

private static Stream<Arguments> provideTestStrings() {
return Stream.of(
Arguments.of("i", "i"),
Arguments.of("0I ", "0I~ "),
Arguments.of("Hi Hi ", "Hi Hi "),
Arguments.of("oe", "{\\oe}"),
Arguments.of("Hi oeHi ", "Hi {\\oe }Hi "),
Arguments.of("Jonathan Meyer and Charles Louis Xavier Joseph de la Vallee Poussin", "Jonathan Meyer and Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin"),
Arguments.of("e", "{\\'e}"),
Arguments.of("Edouard Masterly", "{\\'{E}}douard Masterly"),
Arguments.of("Ulrich Underwood and Ned Net and Paul Pot", "Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot")
);
}

private void assertPurify(final String string, final String string2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please integrate in testPurify. We don't see why this private (!) method is there.

Expand Down
64 changes: 34 additions & 30 deletions src/test/java/org/jabref/logic/bst/BibtexWidthTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.jabref.logic.bst;

import org.junit.jupiter.api.Test;
import java.util.stream.Stream;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

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

Expand Down Expand Up @@ -33,39 +37,39 @@
*/
public class BibtexWidthTest {

private void assertBibtexWidth(final int i, final String string) {
assertEquals(i, BibtexWidth.width(string));
@ParameterizedTest
@MethodSource("provideTestWidth")
public void testWidth(int i, String str) {
assertEquals(i, BibtexWidth.width(str));
}

@Test
public void testWidth() {

assertBibtexWidth(278, "i");

assertBibtexWidth(1639, "0I~ ");

assertBibtexWidth(2612, "Hi Hi ");

assertBibtexWidth(778, "{\\oe}");

assertBibtexWidth(3390, "Hi {\\oe }Hi ");

assertBibtexWidth(444, "{\\'e}");

assertBibtexWidth(19762, "Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot");

assertBibtexWidth(7861, "{\\'{E}}douard Masterly");

assertBibtexWidth(30514, "Jonathan Meyer and Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin");
private static Stream<Arguments> provideTestWidth() {
return Stream.of(
Arguments.of(278, "i"),
Arguments.of(1639, "0I~ "),
Arguments.of(2612, "Hi Hi "),
Arguments.of(778, "{\\oe}"),
Arguments.of(3390, "Hi {\\oe }Hi "),
Arguments.of(444, "{\\'e}"),
Arguments.of(19762, "Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot"),
Arguments.of(7861, "{\\'{E}}douard Masterly"),
Arguments.of(30514, "Jonathan Meyer and Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin")
);
}

@ParameterizedTest
@MethodSource("provideTestGetCharWidth")
public void testGetCharWidth(int i, Character c) {
assertEquals(i, BibtexWidth.getCharWidth(c));
}

@Test
public void testGetCharWidth() {
assertEquals(500, BibtexWidth.getCharWidth('0'));
assertEquals(361, BibtexWidth.getCharWidth('I'));
assertEquals(500, BibtexWidth.getCharWidth('~'));
assertEquals(500, BibtexWidth.getCharWidth('}'));
assertEquals(278, BibtexWidth.getCharWidth(' '));
private static Stream<Arguments> provideTestGetCharWidth() {
return Stream.of(
Arguments.of(500, '0'),
Arguments.of(361, 'I'),
Arguments.of(500, '~'),
Arguments.of(500, '}'),
Arguments.of(278, ' ')
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jabref.logic.formatter.bibtexfields;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -10,12 +9,7 @@
*/
class AddBracesFormatterTest {

private AddBracesFormatter formatter;

@BeforeEach
public void setUp() {
formatter = new AddBracesFormatter();
}
private static final AddBracesFormatter formatter = new AddBracesFormatter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nononono static

Suggested change
private static final AddBracesFormatter formatter = new AddBracesFormatter();
private AddBracesFormatter formatter = new AddBracesFormatter();


@Test
public void formatAddsSingleEnclosingBraces() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jabref.logic.formatter.bibtexfields;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -10,12 +9,7 @@
*/
class CleanupUrlFormatterTest {

private CleanupUrlFormatter formatter;

@BeforeEach
void setUp() {
formatter = new CleanupUrlFormatter();
}
private static final CleanupUrlFormatter formatter = new CleanupUrlFormatter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static final CleanupUrlFormatter formatter = new CleanupUrlFormatter();
private CleanupUrlFormatter formatter = new CleanupUrlFormatter();


@Test
void removeSpecialSymbolsFromURLLink() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jabref.logic.formatter.bibtexfields;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -10,12 +9,7 @@
*/
public class ClearFormatterTest {

private ClearFormatter formatter;

@BeforeEach
public void setUp() {
formatter = new ClearFormatter();
}
private static final ClearFormatter formatter = new ClearFormatter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static final ClearFormatter formatter = new ClearFormatter();
private ClearFormatter formatter = new ClearFormatter();


/**
* Check whether the clear formatter really returns the empty string for the empty string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package org.jabref.logic.formatter.bibtexfields;

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

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

class EscapeAmpersandsFormatterTest {

private EscapeAmpersandsFormatter formatter;

@BeforeEach
void setUp() {
formatter = new EscapeAmpersandsFormatter();
}
private static final EscapeAmpersandsFormatter formatter = new EscapeAmpersandsFormatter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static final EscapeAmpersandsFormatter formatter = new EscapeAmpersandsFormatter();
private EscapeAmpersandsFormatter formatter = new EscapeAmpersandsFormatter();


@Test
void formatReturnsSameTextIfNoAmpersandsPresent() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package org.jabref.logic.formatter.bibtexfields;

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

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

class EscapeUnderscoresFormatterTest {

private EscapeUnderscoresFormatter formatter;

@BeforeEach
void setUp() {
formatter = new EscapeUnderscoresFormatter();
}
private static final EscapeUnderscoresFormatter formatter = new EscapeUnderscoresFormatter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static final EscapeUnderscoresFormatter formatter = new EscapeUnderscoresFormatter();
private EscapeUnderscoresFormatter formatter = new EscapeUnderscoresFormatter();


@Test
void formatReturnsSameTextIfNoUnderscoresPresent() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jabref.logic.formatter.bibtexfields;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -10,12 +9,7 @@
*/
public class HtmlToLatexFormatterTest {

private HtmlToLatexFormatter formatter;

@BeforeEach
public void setUp() {
formatter = new HtmlToLatexFormatter();
}
private static final HtmlToLatexFormatter formatter = new HtmlToLatexFormatter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static final HtmlToLatexFormatter formatter = new HtmlToLatexFormatter();
private HtmlToLatexFormatter formatter = new HtmlToLatexFormatter();


@Test
public void formatWithoutHtmlCharactersReturnsSameString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package org.jabref.logic.formatter.bibtexfields;

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

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

public class HtmlToUnicodeFormatterTest {

private HtmlToUnicodeFormatter formatter;

@BeforeEach
public void setUp() {
formatter = new HtmlToUnicodeFormatter();
}
private static final HtmlToUnicodeFormatter formatter = new HtmlToUnicodeFormatter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static final HtmlToUnicodeFormatter formatter = new HtmlToUnicodeFormatter();
private HtmlToUnicodeFormatter formatter = new HtmlToUnicodeFormatter();


@Test
public void formatWithoutHtmlCharactersReturnsSameString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package org.jabref.logic.formatter.bibtexfields;

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

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

class LatexCleanupFormatterTest {

private LatexCleanupFormatter formatter;

@BeforeEach
void setUp() {
formatter = new LatexCleanupFormatter();
}
private static final LatexCleanupFormatter formatter = new LatexCleanupFormatter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static final LatexCleanupFormatter formatter = new LatexCleanupFormatter();
private LatexCleanupFormatter formatter = new LatexCleanupFormatter();


@Test
void test() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jabref.logic.formatter.bibtexfields;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -10,12 +9,7 @@
*/
public class NormalizeDateFormatterTest {

private NormalizeDateFormatter formatter;

@BeforeEach
public void setUp() {
formatter = new NormalizeDateFormatter();
}
private static final NormalizeDateFormatter formatter = new NormalizeDateFormatter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static final NormalizeDateFormatter formatter = new NormalizeDateFormatter();
private NormalizeDateFormatter formatter = new NormalizeDateFormatter();


@Test
public void formatDateYYYYMM0D() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jabref.logic.formatter.bibtexfields;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -10,12 +9,7 @@
*/
public class NormalizeEnDashesFormatterTest {

private NormalizeEnDashesFormatter formatter;

@BeforeEach
public void setUp() {
formatter = new NormalizeEnDashesFormatter();
}
private static final NormalizeEnDashesFormatter formatter = new NormalizeEnDashesFormatter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static final NormalizeEnDashesFormatter formatter = new NormalizeEnDashesFormatter();
private NormalizeEnDashesFormatter formatter = new NormalizeEnDashesFormatter();


@Test
public void formatExample() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jabref.logic.formatter.bibtexfields;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -10,12 +9,7 @@
*/
public class NormalizeMonthFormatterTest {

private NormalizeMonthFormatter formatter;

@BeforeEach
public void setUp() {
formatter = new NormalizeMonthFormatter();
}
private static final NormalizeMonthFormatter formatter = new NormalizeMonthFormatter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static final NormalizeMonthFormatter formatter = new NormalizeMonthFormatter();
private NormalizeMonthFormatter formatter = new NormalizeMonthFormatter();


@Test
public void formatExample() {
Expand Down
Loading