Skip to content

Commit 55e7523

Browse files
authored
Refactor simple Unit Tests (#7571)
1 parent 033c706 commit 55e7523

16 files changed

+436
-195
lines changed

src/test/java/org/jabref/logic/bst/BibtexCaseChangersTest.java

+122-108
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
package org.jabref.logic.bst;
22

3-
import org.junit.jupiter.api.Test;
3+
import java.util.stream.Stream;
4+
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.Arguments;
7+
import org.junit.jupiter.params.provider.MethodSource;
48

59
import static org.junit.jupiter.api.Assertions.assertEquals;
610
import static org.junit.jupiter.api.Assertions.fail;
711

812
public class BibtexPurifyTest {
913

10-
@Test
11-
public void testPurify() {
12-
assertPurify("i", "i");
13-
assertPurify("0I ", "0I~ ");
14-
assertPurify("Hi Hi ", "Hi Hi ");
15-
assertPurify("oe", "{\\oe}");
16-
assertPurify("Hi oeHi ", "Hi {\\oe }Hi ");
17-
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");
18-
assertPurify("e", "{\\'e}");
19-
assertPurify("Edouard Masterly", "{\\'{E}}douard Masterly");
20-
assertPurify("Ulrich Underwood and Ned Net and Paul Pot", "Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot");
14+
@ParameterizedTest
15+
@MethodSource("provideTestStrings")
16+
public void testPurify(String expected, String toBePurified) {
17+
assertEquals(expected, BibtexPurify.purify(toBePurified, s -> fail("Should not Warn (" + s + ")! purify should be " + expected + " for " + toBePurified)));
2118
}
2219

23-
private void assertPurify(final String string, final String string2) {
24-
assertEquals(string, BibtexPurify.purify(string2, s -> fail("Should not Warn (" + s + ")! purify should be " + string + " for " + string2)));
20+
private static Stream<Arguments> provideTestStrings() {
21+
return Stream.of(
22+
Arguments.of("i", "i"),
23+
Arguments.of("0I ", "0I~ "),
24+
Arguments.of("Hi Hi ", "Hi Hi "),
25+
Arguments.of("oe", "{\\oe}"),
26+
Arguments.of("Hi oeHi ", "Hi {\\oe }Hi "),
27+
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"),
28+
Arguments.of("e", "{\\'e}"),
29+
Arguments.of("Edouard Masterly", "{\\'{E}}douard Masterly"),
30+
Arguments.of("Ulrich Underwood and Ned Net and Paul Pot", "Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot")
31+
);
2532
}
2633
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package org.jabref.logic.bst;
22

3-
import org.junit.jupiter.api.Test;
3+
import java.util.stream.Stream;
4+
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.Arguments;
7+
import org.junit.jupiter.params.provider.MethodSource;
48

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

@@ -33,39 +37,39 @@
3337
*/
3438
public class BibtexWidthTest {
3539

36-
private void assertBibtexWidth(final int i, final String string) {
37-
assertEquals(i, BibtexWidth.width(string));
40+
@ParameterizedTest
41+
@MethodSource("provideTestWidth")
42+
public void testWidth(int i, String str) {
43+
assertEquals(i, BibtexWidth.width(str));
3844
}
3945

40-
@Test
41-
public void testWidth() {
42-
43-
assertBibtexWidth(278, "i");
44-
45-
assertBibtexWidth(1639, "0I~ ");
46-
47-
assertBibtexWidth(2612, "Hi Hi ");
48-
49-
assertBibtexWidth(778, "{\\oe}");
50-
51-
assertBibtexWidth(3390, "Hi {\\oe }Hi ");
52-
53-
assertBibtexWidth(444, "{\\'e}");
54-
55-
assertBibtexWidth(19762, "Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot");
56-
57-
assertBibtexWidth(7861, "{\\'{E}}douard Masterly");
58-
59-
assertBibtexWidth(30514, "Jonathan Meyer and Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin");
46+
private static Stream<Arguments> provideTestWidth() {
47+
return Stream.of(
48+
Arguments.of(278, "i"),
49+
Arguments.of(1639, "0I~ "),
50+
Arguments.of(2612, "Hi Hi "),
51+
Arguments.of(778, "{\\oe}"),
52+
Arguments.of(3390, "Hi {\\oe }Hi "),
53+
Arguments.of(444, "{\\'e}"),
54+
Arguments.of(19762, "Ulrich {\\\"{U}}nderwood and Ned {\\~N}et and Paul {\\={P}}ot"),
55+
Arguments.of(7861, "{\\'{E}}douard Masterly"),
56+
Arguments.of(30514, "Jonathan Meyer and Charles Louis Xavier Joseph de la Vall{\\'e}e Poussin")
57+
);
58+
}
6059

60+
@ParameterizedTest
61+
@MethodSource("provideTestGetCharWidth")
62+
public void testGetCharWidth(int i, Character c) {
63+
assertEquals(i, BibtexWidth.getCharWidth(c));
6164
}
6265

63-
@Test
64-
public void testGetCharWidth() {
65-
assertEquals(500, BibtexWidth.getCharWidth('0'));
66-
assertEquals(361, BibtexWidth.getCharWidth('I'));
67-
assertEquals(500, BibtexWidth.getCharWidth('~'));
68-
assertEquals(500, BibtexWidth.getCharWidth('}'));
69-
assertEquals(278, BibtexWidth.getCharWidth(' '));
66+
private static Stream<Arguments> provideTestGetCharWidth() {
67+
return Stream.of(
68+
Arguments.of(500, '0'),
69+
Arguments.of(361, 'I'),
70+
Arguments.of(500, '~'),
71+
Arguments.of(500, '}'),
72+
Arguments.of(278, ' ')
73+
);
7074
}
7175
}

src/test/java/org/jabref/logic/formatter/bibtexfields/NormalizePagesFormatterTest.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.stream.Stream;
44

5+
import org.junit.jupiter.api.BeforeEach;
56
import org.junit.jupiter.params.ParameterizedTest;
67
import org.junit.jupiter.params.provider.Arguments;
78
import org.junit.jupiter.params.provider.MethodSource;
@@ -13,7 +14,12 @@
1314
*/
1415
public class NormalizePagesFormatterTest {
1516

16-
private final NormalizePagesFormatter formatter = new NormalizePagesFormatter();
17+
private NormalizePagesFormatter formatter;
18+
19+
@BeforeEach
20+
public void setUp() {
21+
formatter = new NormalizePagesFormatter();
22+
}
1723

1824
private static Stream<Arguments> tests() {
1925
return Stream.of(

src/test/java/org/jabref/logic/formatter/bibtexfields/RemoveHyphenatedNewlinesFormatterTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static org.junit.jupiter.api.Assertions.assertEquals;
77

88
public class RemoveHyphenatedNewlinesFormatterTest {
9+
910
private RemoveHyphenatedNewlinesFormatter formatter;
1011

1112
@BeforeEach

src/test/java/org/jabref/logic/formatter/casechanger/SentenceCaseFormatterTest.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.stream.Stream;
44

5+
import org.junit.jupiter.api.BeforeEach;
56
import org.junit.jupiter.params.ParameterizedTest;
67
import org.junit.jupiter.params.provider.Arguments;
78
import org.junit.jupiter.params.provider.MethodSource;
@@ -13,7 +14,12 @@
1314
*/
1415
public class SentenceCaseFormatterTest {
1516

16-
private final SentenceCaseFormatter formatter = new SentenceCaseFormatter();
17+
private SentenceCaseFormatter formatter;
18+
19+
@BeforeEach
20+
public void setUp() {
21+
formatter = new SentenceCaseFormatter();
22+
}
1723

1824
private static Stream<Arguments> testData() {
1925
return Stream.of(
@@ -22,15 +28,15 @@ private static Stream<Arguments> testData() {
2228
Arguments.of("Upper {NOT} first", "upper {NOT} FIRST"),
2329
Arguments.of("Upper {N}ot first", "upper {N}OT FIRST"),
2430
Arguments.of("Whose music? A sociology of musical language",
25-
"Whose music? a sociology of musical language"),
31+
"Whose music? a sociology of musical language"),
2632
Arguments.of("Bibliographic software. A comparison.",
27-
"bibliographic software. a comparison."),
33+
"bibliographic software. a comparison."),
2834
Arguments.of("England’s monitor; The history of the separation",
29-
"England’s Monitor; the History of the Separation"),
35+
"England’s Monitor; the History of the Separation"),
3036
Arguments.of("Dr. schultz: a dentist turned bounty hunter.",
31-
"Dr. schultz: a dentist turned bounty hunter."),
37+
"Dr. schultz: a dentist turned bounty hunter."),
3238
Arguments.of("Example case. {EXCLUDED SENTENCE.}",
33-
"Example case. {EXCLUDED SENTENCE.}"),
39+
"Example case. {EXCLUDED SENTENCE.}"),
3440
Arguments.of("I have {Aa} dream", new SentenceCaseFormatter().getExampleInput()));
3541
}
3642

src/test/java/org/jabref/logic/formatter/casechanger/TitleCaseFormatterTest.java

+17-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.stream.Stream;
44

5+
import org.junit.jupiter.api.BeforeEach;
56
import org.junit.jupiter.params.ParameterizedTest;
67
import org.junit.jupiter.params.provider.Arguments;
78
import org.junit.jupiter.params.provider.MethodSource;
@@ -13,7 +14,12 @@
1314
*/
1415
public class TitleCaseFormatterTest {
1516

16-
private final TitleCaseFormatter formatter = new TitleCaseFormatter();
17+
private TitleCaseFormatter formatter;
18+
19+
@BeforeEach
20+
public void setUp() {
21+
formatter = new TitleCaseFormatter();
22+
}
1723

1824
private static Stream<Arguments> testData() {
1925
return Stream.of(
@@ -22,25 +28,25 @@ private static Stream<Arguments> testData() {
2228
Arguments.of("An Upper Each First And", "an upper each first and"),
2329
Arguments.of("An Upper Each First And", "an upper each first AND"),
2430
Arguments.of("An Upper Each of the and First And",
25-
"an upper each of the and first and"),
31+
"an upper each of the and first and"),
2632
Arguments.of("An Upper Each of the and First And",
27-
"an upper each of the AND first and"),
33+
"an upper each of the AND first and"),
2834
Arguments.of("An Upper Each of: The and First And",
29-
"an upper each of: the and first and"),
35+
"an upper each of: the and first and"),
3036
Arguments.of("An Upper First with and without {CURLY} {brackets}",
31-
"AN UPPER FIRST WITH AND WITHOUT {CURLY} {brackets}"),
37+
"AN UPPER FIRST WITH AND WITHOUT {CURLY} {brackets}"),
3238
Arguments.of("An Upper First with {A}nd without {C}urly {b}rackets",
33-
"AN UPPER FIRST WITH {A}ND WITHOUT {C}URLY {b}rackets"),
39+
"AN UPPER FIRST WITH {A}ND WITHOUT {C}URLY {b}rackets"),
3440
Arguments.of("{b}rackets {b}rac{K}ets Brack{E}ts",
35-
"{b}RaCKeTS {b}RaC{K}eTS bRaCK{E}ts"),
41+
"{b}RaCKeTS {b}RaC{K}eTS bRaCK{E}ts"),
3642
Arguments.of("Two Experiences Designing for Effective Security",
37-
"Two experiences designing for effective security"),
43+
"Two experiences designing for effective security"),
3844
Arguments.of("Bibliographic Software. A Comparison.",
39-
"bibliographic software. a comparison."),
45+
"bibliographic software. a comparison."),
4046
Arguments.of("Bibliographic Software. {A COMPARISON.}",
41-
"bibliographic software. {A COMPARISON.}"),
47+
"bibliographic software. {A COMPARISON.}"),
4248
Arguments.of("{BPMN} Conformance in Open Source Engines",
43-
new TitleCaseFormatter().getExampleInput()));
49+
new TitleCaseFormatter().getExampleInput()));
4450
}
4551

4652
@ParameterizedTest

src/test/java/org/jabref/logic/formatter/casechanger/UnprotectTermsFormatterTest.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.util.stream.Stream;
55

6+
import org.junit.jupiter.api.BeforeEach;
67
import org.junit.jupiter.params.ParameterizedTest;
78
import org.junit.jupiter.params.provider.Arguments;
89
import org.junit.jupiter.params.provider.MethodSource;
@@ -14,7 +15,12 @@
1415
*/
1516
public class UnprotectTermsFormatterTest {
1617

17-
private UnprotectTermsFormatter formatter = new UnprotectTermsFormatter();
18+
private UnprotectTermsFormatter formatter;
19+
20+
@BeforeEach
21+
public void setUp() {
22+
formatter = new UnprotectTermsFormatter();
23+
}
1824

1925
private static Stream<Arguments> terms() throws IOException {
2026
return Stream.of(

src/test/java/org/jabref/logic/integrity/BibStringCheckerTest.java

+16-14
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
import java.util.Collections;
44
import java.util.List;
5+
import java.util.stream.Stream;
56

67
import org.jabref.model.entry.BibEntry;
8+
import org.jabref.model.entry.field.Field;
79
import org.jabref.model.entry.field.StandardField;
810

911
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.params.ParameterizedTest;
13+
import org.junit.jupiter.params.provider.Arguments;
14+
import org.junit.jupiter.params.provider.MethodSource;
1015

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

@@ -15,22 +20,19 @@ public class BibStringCheckerTest {
1520
private final BibStringChecker checker = new BibStringChecker();
1621
private final BibEntry entry = new BibEntry();
1722

18-
@Test
19-
void fieldAcceptsNoHashMarks() {
20-
entry.setField(StandardField.TITLE, "Not a single hash mark");
21-
assertEquals(Collections.emptyList(), checker.check(entry));
23+
@ParameterizedTest
24+
@MethodSource("provideAcceptedInputs")
25+
void acceptsAllowedInputs(List<IntegrityMessage> expected, Field field, String value) {
26+
entry.setField(field, value);
27+
assertEquals(expected, checker.check(entry));
2228
}
2329

24-
@Test
25-
void monthAcceptsEvenNumberOfHashMarks() {
26-
entry.setField(StandardField.MONTH, "#jan#");
27-
assertEquals(Collections.emptyList(), checker.check(entry));
28-
}
29-
30-
@Test
31-
void authorAcceptsEvenNumberOfHashMarks() {
32-
entry.setField(StandardField.AUTHOR, "#einstein# and #newton#");
33-
assertEquals(Collections.emptyList(), checker.check(entry));
30+
private static Stream<Arguments> provideAcceptedInputs() {
31+
return Stream.of(
32+
Arguments.of(Collections.emptyList(), StandardField.TITLE, "Not a single hash mark"),
33+
Arguments.of(Collections.emptyList(), StandardField.MONTH, "#jan#"),
34+
Arguments.of(Collections.emptyList(), StandardField.AUTHOR, "#einstein# and #newton#")
35+
);
3436
}
3537

3638
@Test

src/test/java/org/jabref/logic/integrity/HowPublishedCheckerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void bibTexAcceptsStringWithCapitalFirstLetter() {
3232
}
3333

3434
@Test
35-
void bibTexDoesNotCareAboutSpecialChracters() {
35+
void bibTexDoesNotCareAboutSpecialCharacters() {
3636
assertEquals(Optional.empty(), checker.checkValue("Lorem ipsum? 10"));
3737
}
3838

src/test/java/org/jabref/logic/integrity/NoBibTexFieldCheckerTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,4 @@ void biblatexOnlyField(StandardField field) {
5656
List<IntegrityMessage> messages = checker.check(entry);
5757
assertEquals(Collections.singletonList(message), messages);
5858
}
59-
6059
}

src/test/java/org/jabref/logic/integrity/TypeCheckerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class TypeCheckerTest {
1717
private BibEntry entry;
1818

1919
@Test
20-
void inProceedingshasPagesNumbers() {
20+
void inProceedingsHasPagesNumbers() {
2121
entry = new BibEntry(StandardEntryType.InProceedings);
2222
entry.setField(StandardField.PAGES, "11--15");
2323
assertEquals(Collections.emptyList(), checker.check(entry));

src/test/java/org/jabref/logic/l10n/LocalizationKeyParamsTest.java

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
package org.jabref.logic.l10n;
22

3+
import java.util.stream.Stream;
4+
35
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
49

510
import static org.junit.jupiter.api.Assertions.assertEquals;
611
import static org.junit.jupiter.api.Assertions.assertThrows;
712

813
public class LocalizationKeyParamsTest {
914

10-
@Test
11-
public void testReplacePlaceholders() {
12-
assertEquals("biblatex mode", new LocalizationKeyParams("biblatex mode").replacePlaceholders());
13-
assertEquals("biblatex mode", new LocalizationKeyParams("%0 mode", "biblatex").replacePlaceholders());
14-
assertEquals("C:\\bla mode", new LocalizationKeyParams("%0 mode", "C:\\bla").replacePlaceholders());
15-
assertEquals("What \n : %e %c a b", new LocalizationKeyParams("What \n : %e %c %0 %1", "a", "b").replacePlaceholders());
16-
assertEquals("What \n : %e %c_a b", new LocalizationKeyParams("What \n : %e %c_%0 %1", "a", "b").replacePlaceholders());
15+
@ParameterizedTest
16+
@MethodSource("provideTestData")
17+
public void testReplacePlaceholders(String expected, LocalizationKeyParams input) {
18+
assertEquals(expected, input.replacePlaceholders());
19+
}
20+
21+
private static Stream<Arguments> provideTestData() {
22+
return Stream.of(
23+
Arguments.of("biblatex mode", new LocalizationKeyParams("biblatex mode")),
24+
Arguments.of("biblatex mode", new LocalizationKeyParams("%0 mode", "biblatex")),
25+
Arguments.of("C:\\bla mode", new LocalizationKeyParams("%0 mode", "C:\\bla")),
26+
Arguments.of("What \n : %e %c a b", new LocalizationKeyParams("What \n : %e %c %0 %1", "a", "b")),
27+
Arguments.of("What \n : %e %c_a b", new LocalizationKeyParams("What \n : %e %c_%0 %1", "a", "b"))
28+
);
1729
}
1830

1931
@Test

0 commit comments

Comments
 (0)