Skip to content

Commit

Permalink
make AssertJ tests work on non-English locales, closes #180
Browse files Browse the repository at this point in the history
  • Loading branch information
bodewig committed Apr 28, 2020
1 parent 4866621 commit c5f1899
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Expand Up @@ -2,6 +2,9 @@

## XMLUnit for Java 2.6.5 - /not released, yet/

* the AssertJ tests now pass on non-English locales as well
Issue [#180](https://github.com/xmlunit/xmlunit/pull/180)

## XMLUnit for Java 2.6.4 - /Released 2020-03-08/

* the dependencies on JAXB implementation and its transitive
Expand Down
Expand Up @@ -13,11 +13,13 @@
*/
package org.xmlunit.assertj;

import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mockito;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xmlunit.assertj.util.SetEnglishLocaleRule;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
Expand All @@ -34,6 +36,9 @@ public class ValueAssertTest {
@Rule
public ExpectedException thrown = none();

@ClassRule
public static SetEnglishLocaleRule locale = new SetEnglishLocaleRule();

@Test
public void testAsInt_shouldPass() {

Expand Down
Expand Up @@ -13,9 +13,11 @@
*/
package org.xmlunit.assertj;

import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.xmlunit.validation.Languages;
import org.xmlunit.assertj.util.SetEnglishLocaleRule;

import java.io.File;

Expand All @@ -31,6 +33,9 @@ public class XmlAssertValidationTest {
@Rule
public ExpectedException thrown = none();

@ClassRule
public static SetEnglishLocaleRule locale = new SetEnglishLocaleRule();

@Test
public void testIsValidAgainst_shouldPass() {
StreamSource xml = new StreamSource(new File("../test-resources/BookXsdGenerated.xml"));
Expand Down
Expand Up @@ -13,8 +13,10 @@
*/
package org.xmlunit.assertj;

import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.xmlunit.assertj.util.SetEnglishLocaleRule;

import static java.lang.String.format;
import static org.xmlunit.assertj.ExpectedException.none;
Expand All @@ -25,6 +27,9 @@ public class XmlAssertValueByXPathTest {
@Rule
public ExpectedException thrown = none();

@ClassRule
public static SetEnglishLocaleRule locale = new SetEnglishLocaleRule();

@Test
public void testValueByXPath_withNull_shouldFailed() {

Expand Down
@@ -0,0 +1,41 @@
/*
This file is licensed to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.xmlunit.assertj.util;

import org.junit.rules.ExternalResource;

import java.util.Locale;

public class SetEnglishLocaleRule extends ExternalResource {

private Locale locale;

@Override
protected void before() {
locale = Locale.getDefault();
maybeSetDefault(Locale.ENGLISH);
}

@Override
protected void after() {
maybeSetDefault(locale);
}

private void maybeSetDefault(final Locale l) {
if (l != null && !l.equals(Locale.getDefault())) {
Locale.setDefault(l);
}
}
}

0 comments on commit c5f1899

Please sign in to comment.