Repository: commons-testing Updated Branches: refs/heads/master e22cc4fe8 -> 2062a231b
Add tests. Project: http://git-wip-us.apache.org/repos/asf/commons-testing/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-testing/commit/2062a231 Tree: http://git-wip-us.apache.org/repos/asf/commons-testing/tree/2062a231 Diff: http://git-wip-us.apache.org/repos/asf/commons-testing/diff/2062a231 Branch: refs/heads/master Commit: 2062a231b3de86217866a9b5bd05163b3e33a88f Parents: e22cc4f Author: Gary Gregory <garydgreg...@gmail.com> Authored: Sun Feb 4 14:17:12 2018 -0700 Committer: Gary Gregory <garydgreg...@gmail.com> Committed: Sun Feb 4 14:17:12 2018 -0700 ---------------------------------------------------------------------- .gitignore | 7 ++- .../junit4/AbstractAvailableLocalesTest.java | 4 +- .../testing/junit4/DefaultLocaleTestRule.java | 60 ++++++++++++++++++++ .../junit4/ObjectToStringComparator.java | 5 ++ .../junit4/SetDefaultLocaleTestRule.java | 58 ------------------- .../testing/junit4/AvailableLocalesTest.java | 58 +++++++++++++++++++ .../junit4/DefaultLocaleTestRuleTest.java | 41 +++++++++++++ .../junit4/ObjectToStringComparatorTest.java | 39 +++++++++++++ 8 files changed, 209 insertions(+), 63 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-testing/blob/2062a231/.gitignore ---------------------------------------------------------------------- diff --git a/.gitignore b/.gitignore index fbdf7eb..721dfeb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -/.classpath -/.project -/.settings/ +**/.classpath +**/.project +**/.settings/ +**/target/ http://git-wip-us.apache.org/repos/asf/commons-testing/blob/2062a231/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/AbstractAvailableLocalesTest.java ---------------------------------------------------------------------- diff --git a/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/AbstractAvailableLocalesTest.java b/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/AbstractAvailableLocalesTest.java index 23459a3..160c384 100644 --- a/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/AbstractAvailableLocalesTest.java +++ b/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/AbstractAvailableLocalesTest.java @@ -81,12 +81,12 @@ public abstract class AbstractAvailableLocalesTest { private final Locale locale; @Rule - public final SetDefaultLocaleTestRule rule; + public final DefaultLocaleTestRule rule; public AbstractAvailableLocalesTest(final Locale locale) { super(); this.locale = locale; - this.rule = new SetDefaultLocaleTestRule(locale); + this.rule = new DefaultLocaleTestRule(locale); } public Locale getLocale() { http://git-wip-us.apache.org/repos/asf/commons-testing/blob/2062a231/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/DefaultLocaleTestRule.java ---------------------------------------------------------------------- diff --git a/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/DefaultLocaleTestRule.java b/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/DefaultLocaleTestRule.java new file mode 100644 index 0000000..31e3f38 --- /dev/null +++ b/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/DefaultLocaleTestRule.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file 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.apache.commons.testing.junit4; + +import java.util.Locale; + +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +/** + * Sets the default {@code Locale} to the given locale for the duration of the test. + * + * @since 1.0.0 + */ +public class DefaultLocaleTestRule implements TestRule { + + private final Locale locale; + + public DefaultLocaleTestRule(final Locale locale) { + super(); + this.locale = locale; + } + + @Override + public Statement apply(final Statement base, final Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + final Locale savedLocale = Locale.getDefault(); + Locale.setDefault(getLocale()); + try { + base.evaluate(); + } finally { + Locale.setDefault(savedLocale); + } + } + }; + } + + public Locale getLocale() { + return locale; + } + +} http://git-wip-us.apache.org/repos/asf/commons-testing/blob/2062a231/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/ObjectToStringComparator.java ---------------------------------------------------------------------- diff --git a/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/ObjectToStringComparator.java b/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/ObjectToStringComparator.java index c04bacf..3e2861b 100644 --- a/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/ObjectToStringComparator.java +++ b/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/ObjectToStringComparator.java @@ -20,6 +20,11 @@ package org.apache.commons.testing.junit4; import java.io.Serializable; import java.util.Comparator; +/** + * Compares objects based on their {@code toString()} values. + * + * @since 1.0.0 + */ public final class ObjectToStringComparator implements Comparator<Object>, Serializable { private static final long serialVersionUID = 1L; http://git-wip-us.apache.org/repos/asf/commons-testing/blob/2062a231/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/SetDefaultLocaleTestRule.java ---------------------------------------------------------------------- diff --git a/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/SetDefaultLocaleTestRule.java b/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/SetDefaultLocaleTestRule.java deleted file mode 100644 index 4b7dbd9..0000000 --- a/commons-testing-junit4/src/main/java/org/apache/commons/testing/junit4/SetDefaultLocaleTestRule.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file 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.apache.commons.testing.junit4; - -import java.util.Locale; - -import org.junit.rules.TestRule; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; - -/** - * Sets the default {@code Locale} to the given locale for the duration of the test. - */ -public class SetDefaultLocaleTestRule implements TestRule { - - private final Locale locale; - - public SetDefaultLocaleTestRule(final Locale locale) { - super(); - this.locale = locale; - } - - @Override - public Statement apply(final Statement base, final Description description) { - return new Statement() { - @Override - public void evaluate() throws Throwable { - final Locale savedLocale = Locale.getDefault(); - Locale.setDefault(getLocale()); - try { - base.evaluate(); - } finally { - Locale.setDefault(savedLocale); - } - } - }; - } - - public Locale getLocale() { - return locale; - } - -} http://git-wip-us.apache.org/repos/asf/commons-testing/blob/2062a231/commons-testing-junit4/src/test/java/org/apache/commons/testing/junit4/AvailableLocalesTest.java ---------------------------------------------------------------------- diff --git a/commons-testing-junit4/src/test/java/org/apache/commons/testing/junit4/AvailableLocalesTest.java b/commons-testing-junit4/src/test/java/org/apache/commons/testing/junit4/AvailableLocalesTest.java new file mode 100644 index 0000000..17eea73 --- /dev/null +++ b/commons-testing-junit4/src/test/java/org/apache/commons/testing/junit4/AvailableLocalesTest.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file 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.apache.commons.testing.junit4; + +import java.util.Collections; +import java.util.Locale; +import java.util.Set; +import java.util.concurrent.ConcurrentSkipListSet; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +public class AvailableLocalesTest extends AbstractAvailableLocalesTest { + + private static Set<Locale> found; + + @AfterClass + public static void afterClass() { + final ConcurrentSkipListSet<Locale> expected = createSet(); + Collections.addAll(expected, Locale.getAvailableLocales()); + Assert.assertEquals(expected, found); + } + + @BeforeClass + public static void beforeClass() { + found = createSet(); + } + + private static ConcurrentSkipListSet<Locale> createSet() { + return new ConcurrentSkipListSet<>(new ObjectToStringComparator()); + } + + public AvailableLocalesTest(final Locale locale) { + super(locale); + } + + @Test + public void test() { + found.add(getLocale()); + } +} http://git-wip-us.apache.org/repos/asf/commons-testing/blob/2062a231/commons-testing-junit4/src/test/java/org/apache/commons/testing/junit4/DefaultLocaleTestRuleTest.java ---------------------------------------------------------------------- diff --git a/commons-testing-junit4/src/test/java/org/apache/commons/testing/junit4/DefaultLocaleTestRuleTest.java b/commons-testing-junit4/src/test/java/org/apache/commons/testing/junit4/DefaultLocaleTestRuleTest.java new file mode 100644 index 0000000..514cc80 --- /dev/null +++ b/commons-testing-junit4/src/test/java/org/apache/commons/testing/junit4/DefaultLocaleTestRuleTest.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file 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.apache.commons.testing.junit4; + +import java.util.Locale; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; + +/** + * Sets the default {@code Locale} to the given Locale for the duration of the test. + * + * @since 1.0.0 + */ +public class DefaultLocaleTestRuleTest { + + @Rule + public DefaultLocaleTestRule testRule = new DefaultLocaleTestRule(Locale.CANADA); + + @Test + public void test() { + Assert.assertEquals(Locale.CANADA, Locale.getDefault()); + } + +} http://git-wip-us.apache.org/repos/asf/commons-testing/blob/2062a231/commons-testing-junit4/src/test/java/org/apache/commons/testing/junit4/ObjectToStringComparatorTest.java ---------------------------------------------------------------------- diff --git a/commons-testing-junit4/src/test/java/org/apache/commons/testing/junit4/ObjectToStringComparatorTest.java b/commons-testing-junit4/src/test/java/org/apache/commons/testing/junit4/ObjectToStringComparatorTest.java new file mode 100644 index 0000000..a3b720e --- /dev/null +++ b/commons-testing-junit4/src/test/java/org/apache/commons/testing/junit4/ObjectToStringComparatorTest.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file 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.apache.commons.testing.junit4; + +import java.util.Collections; +import java.util.Locale; +import java.util.concurrent.ConcurrentSkipListSet; + +import org.junit.Assert; + +public class ObjectToStringComparatorTest { + + public static void afterClass() { + final ConcurrentSkipListSet<Locale> expected = createSet(); + final Locale[] availableLocales = Locale.getAvailableLocales(); + Collections.addAll(expected, availableLocales); + Assert.assertEquals(expected.size(), availableLocales.length); + } + + private static ConcurrentSkipListSet<Locale> createSet() { + return new ConcurrentSkipListSet<>(new ObjectToStringComparator()); + } + +}