Skip to content

Commit af88d62

Browse files
author
Mike Reiche
committed
Merge branch 'bugfix/thread-safe-localization' into testerra2
# Conflicts: # integration-tests/src/test/java/eu/tsystems/mms/tic/testframework/test/reporting/ScreenshotsTest.java # integration-tests/src/test/resources/Integration.xml
2 parents a9527ba + fea986c commit af88d62

File tree

13 files changed

+93
-52
lines changed

13 files changed

+93
-52
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ allprojects {
2929
apply plugin: 'java-library'
3030
apply plugin: 'project-report'
3131

32+
compileJava.options.encoding = 'UTF-8'
33+
compileTestJava.options.encoding = "UTF-8"
3234
sourceCompatibility = JavaVersion.VERSION_1_8
3335
targetCompatibility = JavaVersion.VERSION_1_8
3436
}

core/src/main/java/eu/tsystems/mms/tic/testframework/l10n/LocalizedBundle.java

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,18 @@
2626
import java.util.ResourceBundle;
2727

2828
public class LocalizedBundle {
29-
private ResourceBundle resourceBundle;
29+
private final ResourceBundle resourceBundle;
3030
private static final ResourceBundle.Control resourceBundleController = new UTF8ResourceBundleControl();
31-
/**
32-
* When the bundle name is NULL, the {@link #resourceBundle} is fixed
33-
*/
34-
private final String bundleName;
3531

3632
public LocalizedBundle(String bundleName, Locale locale) {
37-
this.bundleName = null;
3833
this.resourceBundle = ResourceBundle.getBundle(bundleName, locale, resourceBundleController);
3934
}
4035

4136
public LocalizedBundle(String bundleName) {
42-
this.bundleName = bundleName;
43-
recreateLocalizedResourceBundle();
44-
}
45-
46-
private ResourceBundle getResourceBundle() {
47-
/**
48-
* When the locale is not fixed and differs from the default locale
49-
* than recreate the bundle.
50-
*/
51-
if (this.bundleName != null && Locale.getDefault() != this.resourceBundle.getLocale()) {
52-
recreateLocalizedResourceBundle();
53-
}
54-
return resourceBundle;
55-
}
56-
57-
private void recreateLocalizedResourceBundle() {
58-
this.resourceBundle = ResourceBundle.getBundle(this.bundleName, resourceBundleController);
37+
this(bundleName, Locale.getDefault());
5938
}
6039

6140
public String getString(String label) {
62-
ResourceBundle resourceBundle = getResourceBundle();
6341
if (resourceBundle.containsKey(label)) {
6442
return resourceBundle.getString(label);
6543
} else {

core/src/main/java/eu/tsystems/mms/tic/testframework/l10n/SimpleLocalization.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,29 @@
2121
*/
2222
package eu.tsystems.mms.tic.testframework.l10n;
2323

24+
import java.util.Locale;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
2428
final public class SimpleLocalization {
25-
public static final String BUNDLE_NAME="lang";
26-
private static final LocalizedBundle bundle = new LocalizedBundle(BUNDLE_NAME);
29+
public static final String BUNDLE_NAME = "lang";
30+
private static LocalizedBundle bundle;
31+
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleLocalization.class);
2732

2833
public static String getText(final String label) {
29-
return bundle.getString(label);
34+
return getDefaultBundle().getString(label);
35+
}
36+
37+
public static LocalizedBundle getDefaultBundle() {
38+
if (bundle == null) {
39+
setDefault(Locale.getDefault());
40+
}
41+
return bundle;
3042
}
3143

32-
public LocalizedBundle getBundle() {
44+
public static LocalizedBundle setDefault(Locale locale) {
45+
LOGGER.info("Change default locale to: " + locale);
46+
bundle = new LocalizedBundle(BUNDLE_NAME, locale);
3347
return bundle;
3448
}
3549
}

core/src/main/java/eu/tsystems/mms/tic/testframework/report/DefaultReport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private File addFile(File sourceFile, File directory, FileMode fileMode) {
6161
break;
6262
}
6363
} catch (IOException e) {
64-
log().error(e.getMessage());
64+
log().error("Could not add file", e);
6565
}
6666
return new File(directory, sourceFile.getName());
6767
}

docs/src/docs/modules/localization.adoc

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,14 @@ import eu.tsystems.mms.tic.testframework.l10n.SimpleLocalization;
6060
UiElement loginBtn = find(By.linkText(SimpleLocalization.getText("BTN_LOGIN")));
6161
----
6262

63-
But you can use your own localized bundles.
64-
63+
`SimpleLocalization` uses `Locale.getDefault()` by default, but
64+
you can switch the default locale the following way.
6565
[source, java]
6666
----
67-
import eu.tsystems.mms.tic.testframework.l10n.LocalizedBundle;
68-
69-
LocalizedBundle bundle = new LocalizedBundle("testdata");
70-
bundle.getString("TEST_KEY");
67+
LocalizedBundle bundle = SimpleLocalization.setDefault(Locale.GERMAN);
7168
----
7269

73-
Or bundles with a fixed locale.
70+
But you can use your own localized bundles:
7471

7572
[source, java]
7673
----

integration-tests/src/main/java/eu/tsystems/mms/tic/testframework/core/pageobjects/testdata/WebTestPage.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public WebTestPage reloadPage() {
8787
return new WebTestPage(this.getWebDriver());
8888
}
8989

90+
public GuiElement getOpenAgain() {
91+
return this.openAgainLink;
92+
}
93+
9094
/**
9195
* Click on not existing element
9296
*

integration-tests/src/test/java/eu/tsystems/mms/tic/testframework/test/l10n/LocalizationTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ public class LocalizationTest extends TesterraTest {
3434

3535
@Test(dataProvider = "locales")
3636
public void test_readUtf8FromResourceBundle(String locale, String expected) {
37-
Locale.setDefault(Locale.forLanguageTag(locale));
38-
Assert.assertEquals(SimpleLocalization.getText("TEST"), expected);
37+
LocalizedBundle defaultBundle = SimpleLocalization.setDefault(Locale.forLanguageTag(locale));
38+
Assert.assertEquals(defaultBundle.getString("TEST"), expected);
3939
}
4040

4141
@Test
4242
public void test_inexistentLocalizedProperty() {
43-
Assert.assertEquals("NOT_EXISTENT", SimpleLocalization.getText("NOT_EXISTENT"));
43+
Assert.assertEquals(SimpleLocalization.getText("NOT_EXISTENT"), "NOT_EXISTENT");
4444
}
4545

4646
@DataProvider
@@ -53,12 +53,11 @@ public Object[][] locales() {
5353

5454
@Test
5555
public void test_fixedLocale() {
56-
Locale.setDefault(Locale.ENGLISH);
56+
LocalizedBundle defaultBundle = SimpleLocalization.setDefault(Locale.ENGLISH);
5757
LocalizedBundle germanBundle = new LocalizedBundle(SimpleLocalization.BUNDLE_NAME, Locale.GERMAN);
58-
LocalizedBundle defaultBundle = new LocalizedBundle(SimpleLocalization.BUNDLE_NAME);
5958
Assert.assertNotEquals(germanBundle.getString("TEST"), defaultBundle.getString("TEST"));
6059

61-
Locale.setDefault(Locale.GERMAN);
60+
defaultBundle = SimpleLocalization.setDefault(Locale.GERMAN);
6261
Assert.assertEquals(germanBundle.getString("TEST"), defaultBundle.getString("TEST"));
6362
}
6463

integration-tests/src/test/java/eu/tsystems/mms/tic/testframework/test/reporting/ScreenshotsTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,24 @@
2525
import eu.tsystems.mms.tic.testframework.annotations.Fails;
2626
import eu.tsystems.mms.tic.testframework.common.Testerra;
2727
import eu.tsystems.mms.tic.testframework.core.pageobjects.testdata.BasePage;
28+
import eu.tsystems.mms.tic.testframework.core.pageobjects.testdata.WebTestPage;
2829
import eu.tsystems.mms.tic.testframework.execution.testng.AssertCollector;
2930
import eu.tsystems.mms.tic.testframework.pageobjects.UiElement;
3031
import eu.tsystems.mms.tic.testframework.report.Report;
3132
import eu.tsystems.mms.tic.testframework.report.model.context.MethodContext;
3233
import eu.tsystems.mms.tic.testframework.report.model.context.Screenshot;
3334
import eu.tsystems.mms.tic.testframework.report.utils.ExecutionContextController;
3435
import eu.tsystems.mms.tic.testframework.test.PageFactoryTest;
36+
import eu.tsystems.mms.tic.testframework.utils.AssertUtils;
37+
import eu.tsystems.mms.tic.testframework.utils.UITestUtils;
3538
import eu.tsystems.mms.tic.testframework.testing.AssertProvider;
39+
import java.io.IOException;
3640
import java.util.Optional;
3741
import org.openqa.selenium.By;
3842
import org.testng.Assert;
3943
import org.testng.annotations.AfterMethod;
4044
import org.testng.annotations.Test;
45+
import org.testng.reporters.Files;
4146

4247
/**
4348
* Tests if screenshots are added to the MethodContext when a test fails.
@@ -109,4 +114,18 @@ public void test_take_screenshot_via_collected_assertion() {
109114
public void test_Screenshot_is_present_in_MethodContext_on_collected_assertion() {
110115
this.screenshot_is_present_in_MethodContext("test_take_screenshot_via_collected_assertion");
111116
}
117+
118+
@Test
119+
public void test_DOMSource() throws IOException {
120+
WebTestPage page = new WebTestPage(WebDriverManager.getWebDriver());
121+
122+
for (int s = 0; s < 3; ++s) {
123+
page.getOpenAgain().click();
124+
}
125+
Screenshot screenshot = UITestUtils.takeScreenshot(page.getWebDriver(), false);
126+
String screenshotSource = Files.readFile(screenshot.getPageSourceFile());
127+
128+
String expected = "<p id=\"99\">Open again clicked<br>Open again clicked<br>Open again clicked<br>";
129+
AssertUtils.assertContains(screenshotSource, expected);
130+
}
112131
}

integration-tests/src/test/resources/Integration.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<suite-file path="PageObjects.xml"></suite-file>
88
<suite-file path="UiElement.xml"></suite-file>
99
<suite-file path="MethodParallel.xml"></suite-file>
10+
<suite-file path="Localization.xml"></suite-file>
1011
</suite-files>
1112

1213
<test name="Sequential tests" parallel="false">
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
2+
<suite name="Localization " verbose="10" configfailurepolicy="continue" thread-count="10" parallel="false">
3+
4+
<test name="Core" parallel="methods">
5+
<groups>
6+
<run>
7+
<exclude name="SEQUENTIAL"/>
8+
</run>
9+
</groups>
10+
<packages>
11+
<package name="eu.tsystems.mms.tic.testframework.test.l10n"/>
12+
</packages>
13+
</test>
14+
</suite>

0 commit comments

Comments
 (0)