1
1
package org .jabref .logic .util ;
2
2
3
3
import java .net .MalformedURLException ;
4
+ import java .util .Collections ;
4
5
import java .util .Optional ;
5
6
7
+ import org .jabref .logic .importer .ImporterPreferences ;
6
8
import org .jabref .model .entry .BibEntry ;
7
9
import org .jabref .model .entry .field .StandardField ;
8
10
9
11
import org .junit .jupiter .api .Test ;
10
12
11
- import static org .jabref .logic .util .ExternalLinkCreator .getShortScienceSearchURL ;
12
13
import static org .junit .jupiter .api .Assertions .assertEquals ;
13
14
import static org .junit .jupiter .api .Assertions .assertTrue ;
14
15
15
16
class ExternalLinkCreatorTest {
16
17
18
+ // Create stub for ImporterPreferences to test
19
+ private static class StubImporterPreferences extends ImporterPreferences {
20
+ public StubImporterPreferences () {
21
+ super (
22
+ true , // importerEnabled
23
+ true , // generateNewKeyOnImport
24
+ null , // importWorkingDirectory
25
+ true , // warnAboutDuplicatesOnImport
26
+ Collections .emptySet (), // customImporters
27
+ Collections .emptySet (), // apiKeys
28
+ Collections .emptyMap (), // defaultApiKeys
29
+ true , // persistCustomKeys
30
+ Collections .emptyList (), // catalogs
31
+ null , // defaultPlainCitationParser
32
+ Collections .emptyMap () // searchEngineUrlTemplates
33
+ );
34
+ }
35
+ }
36
+
17
37
/**
18
38
* Validates URL conformance to RFC2396. Does not perform complex checks such as opening connections.
19
39
*/
@@ -29,25 +49,34 @@ private boolean urlIsValid(String url) {
29
49
30
50
@ Test
31
51
void getShortScienceSearchURLEncodesSpecialCharacters () {
52
+ ImporterPreferences stubPreferences = new StubImporterPreferences ();
53
+ ExternalLinkCreator linkCreator = new ExternalLinkCreator (stubPreferences );
54
+
32
55
BibEntry entry = new BibEntry ();
33
56
String rfc3986ReservedCharacters = "!*'();:@&=+$,/?#[]" ;
34
57
entry .setField (StandardField .TITLE , rfc3986ReservedCharacters );
35
- Optional <String > url = getShortScienceSearchURL (entry );
58
+ Optional <String > url = linkCreator . getShortScienceSearchURL (entry );
36
59
assertTrue (url .isPresent ());
37
60
assertTrue (urlIsValid (url .get ()));
38
61
}
39
62
40
63
@ Test
41
64
void getShortScienceSearchURLReturnsEmptyOnMissingTitle () {
65
+ ImporterPreferences stubPreferences = new StubImporterPreferences ();
66
+ ExternalLinkCreator linkCreator = new ExternalLinkCreator (stubPreferences );
67
+
42
68
BibEntry entry = new BibEntry ();
43
- assertEquals (Optional .empty (), getShortScienceSearchURL (entry ));
69
+ assertEquals (Optional .empty (), linkCreator . getShortScienceSearchURL (entry ));
44
70
}
45
71
46
72
@ Test
47
73
void getShortScienceSearchURLLinksToSearchResults () {
74
+ ImporterPreferences stubPreferences = new StubImporterPreferences ();
75
+ ExternalLinkCreator linkCreator = new ExternalLinkCreator (stubPreferences );
76
+
48
77
// Take an arbitrary article name
49
78
BibEntry entry = new BibEntry ().withField (StandardField .TITLE , "JabRef bibliography management" );
50
- Optional <String > url = getShortScienceSearchURL (entry );
79
+ Optional <String > url = linkCreator . getShortScienceSearchURL (entry );
51
80
// Expected behaviour is to link to the search results page, /internalsearch
52
81
assertEquals (Optional .of ("https://www.shortscience.org/internalsearch?q=JabRef%20bibliography%20management" ), url );
53
82
}
0 commit comments