-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Main goal of this simplification is to remove 'map name' from resource loader. One place that is used is in Link Localization. Instead of passing a resource loader to the link localizer, we can instead pass a file path which in turn simplifies how link localization can be done. Additional: - removed link localization cache - changed link localizatoin behavior to only localize links that are not already absolute. Previously links in single quotes would be localized and links in double quotes would be left as-is. The localization of fully qualified links would parse the link path, get the file name and localize the file name path. This is removed as it is difficult to understand and is much easier if we localize any relative links but keep absolute links as they are (regardless of quoting).
- Loading branch information
1 parent
16fcfd3
commit 4da2283
Showing
9 changed files
with
99 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 32 additions & 71 deletions
103
game-core/src/test/java/org/triplea/util/LocalizeHtmlTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,46 @@ | ||
package org.triplea.util; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.not; | ||
import static org.mockito.Mockito.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import games.strategy.triplea.ResourceLoader; | ||
import java.net.URL; | ||
import java.io.File; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
class LocalizeHtmlTest { | ||
private final ResourceLoader loader = mock(ResourceLoader.class); | ||
private final String testHtml = | ||
"<audio src='test-audio'> <img src="test">" | ||
+ "<img useless fill src=\"dir/actual-link\" alt='Alternative Text' > " | ||
+ "<p> Placeholder </P> <img\n" | ||
+ " src='another-link.png' />"; | ||
|
||
@Test | ||
void testLocalizeHtml() throws Exception { | ||
|
||
when(loader.getResource("doc/images/actual-link")).thenReturn(new URL("http://local-link-1")); | ||
when(loader.getResource("doc/images/another-link.png")) | ||
.thenReturn(new URL("http://local-link-2")); | ||
|
||
final String result = LocalizeHtml.localizeImgLinksInHtml(testHtml, loader); | ||
|
||
assertThat(result, not(containsString("dir/actual-link"))); | ||
assertThat(result, not(containsString("another-link.png"))); | ||
|
||
assertThat(result, containsString("http://local-link-1")); | ||
assertThat(result, containsString("http://local-link-2")); | ||
|
||
assertThat(result, containsString("test-audio")); | ||
|
||
verify(loader, times(2)).getResource(any()); | ||
} | ||
|
||
@Test | ||
void testFallbackLink() throws Exception { | ||
when(loader.getResource("doc/images/actual-link")).thenReturn(null); | ||
when(loader.getResource("doc/images/another-link.png")).thenReturn(null); | ||
when(loader.getResource("doc/images/notFound.png")) | ||
.thenReturn(null, new URL("http://notFound.png")); | ||
final String result = LocalizeHtml.localizeImgLinksInHtml(testHtml, loader); | ||
assertThat(result, containsString("dir/actual-link")); | ||
assertThat(result, containsString("http://notFound.png")); | ||
assertThat(result, not(containsString("another-link.png"))); | ||
void testLocalizeHtml() { | ||
final String testHtml = | ||
"<audio src='test-audio'> <img src="test">" | ||
+ "<img useless fill src=\"dir/actual-link\" alt='Alternative Text' > " | ||
+ "<p> Placeholder </P> <img\n" | ||
+ " src='another-link.png'/><img src=\"another-link\"/>"; | ||
|
||
final String result = LocalizeHtml.localizeImgLinksInHtml(testHtml, new File("/usr/local")); | ||
|
||
assertThat( | ||
result, | ||
is( | ||
"<audio src='test-audio'> <img src="test">" | ||
+ "<img useless fill src=\"file:/usr/local/doc/images/dir/actual-link\" " | ||
+ "alt='Alternative Text' > <p> Placeholder </P> <img\n" | ||
+ " src='file:/usr/local/doc/images/another-link.png'/>" | ||
+ "<img src=\"file:/usr/local/doc/images/another-link\"/>")); | ||
} | ||
|
||
@Test | ||
void testDoNothingWithoutResources() { | ||
assertThat(LocalizeHtml.localizeImgLinksInHtml(testHtml, loader), is(equalTo(testHtml))); | ||
} | ||
|
||
@Test | ||
void testDoNothingWithoutTag() { | ||
final String testHtml = "<p>Paragraph</p>"; | ||
assertThat(LocalizeHtml.localizeImgLinksInHtml(testHtml, loader), is(equalTo(testHtml))); | ||
verify(loader, never()).getResource(any()); | ||
} | ||
|
||
@Test | ||
void testCaching() throws Exception { | ||
final String testHtml = "<img src='test'><img src='test'>"; | ||
when(loader.getResource("doc/images/test")).thenReturn(new URL("http://test")); | ||
LocalizeHtml.localizeImgLinksInHtml(testHtml, loader); | ||
verify(loader, times(1)).getResource(any()); | ||
} | ||
|
||
@Test | ||
void testStrictHtml() { | ||
final String testHtml = "<img src=\"test\""; | ||
assertThat(LocalizeHtml.localizeImgLinksInHtml(testHtml, loader), is(equalTo(testHtml))); | ||
verify(loader, never()).getResource(any()); | ||
@ParameterizedTest | ||
@ValueSource( | ||
strings = { | ||
"<img src=http://someurl/>", | ||
"<img src=\"http://someurl\"/>", | ||
"<img\nsrc=\"http://someurl\"/>", | ||
"<p>Paragraph</p>" | ||
}) | ||
void testAbsoluteUrl(final String testHtml) { | ||
assertThat( | ||
LocalizeHtml.localizeImgLinksInHtml(testHtml, new File("/usr/local")), | ||
is(equalTo(testHtml))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters