This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new 0309d6252 Modernize tests
0309d6252 is described below
commit 0309d6252f6a790d33faf1f37f731c9415ea182e
Author: James Bognar <[email protected]>
AuthorDate: Fri Aug 22 23:43:54 2025 -0400
Modernize tests
---
.../juneau/transforms/FunctionalSwapTest.java | 44 ++------
.../transforms/InputStreamBase64SwapTest.java | 47 ++------
.../transforms/OneWayStringSwapTest_Base.java | 29 +++++
.../juneau/transforms/OneWayStringSwapTester.java | 105 ++++++++++++++++++
.../apache/juneau/transforms/ReaderSwapTest.java | 47 ++------
.../transforms/RoundTripObjectSwapTest_Base.java | 35 ++++++
.../transforms/RoundTripObjectSwapTester.java | 119 +++++++++++++++++++++
7 files changed, 318 insertions(+), 108 deletions(-)
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/transforms/FunctionalSwapTest.java
b/juneau-utest/src/test/java/org/apache/juneau/transforms/FunctionalSwapTest.java
index ace04517c..fe59b56fb 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/transforms/FunctionalSwapTest.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/transforms/FunctionalSwapTest.java
@@ -17,11 +17,8 @@ import java.util.*;
import org.apache.juneau.*;
import org.apache.juneau.swap.*;
import org.apache.juneau.swaps.*;
-import org.junit.runner.*;
-import org.junit.runners.*;
-@RunWith(Parameterized.class)
-public class FunctionalSwapTest extends RoundTripObjectSwapTest<Locale,String>
{
+class FunctionalSwapTest extends RoundTripObjectSwapTest_Base {
//------------------------------------------------------------------------------------------------------------------
// Setup
@@ -31,42 +28,21 @@ public class FunctionalSwapTest extends
RoundTripObjectSwapTest<Locale,String> {
private static final LocaleSwap localeSwap = new LocaleSwap();
private static FunctionalSwap<Locale,String> SWAP = new
FunctionalSwap<>(Locale.class, String.class, x->localeSwap.swap(BS, x),
x->localeSwap.unswap(BS, x, null));
- public FunctionalSwapTest(String label, Locale o,
FunctionalSwap<Locale,String> s, String r, BeanSession bs) {
- super(label, o, s, r, bs);
+ private static <T,S> RoundTripObjectSwapTester<T,S> tester(int index,
String label, T object, ObjectSwap<T,S> swap, S expected, BeanSession bs) {
+ return RoundTripObjectSwapTester.create(index, label, object,
swap, expected, bs).build();
}
//------------------------------------------------------------------------------------------------------------------
// Parameters
//------------------------------------------------------------------------------------------------------------------
- @Parameterized.Parameters
- public static Collection<Object[]> getPairs() {
- return Arrays.asList(new Object[][] {
+ private static final RoundTripObjectSwapTester<?,?>[] TESTERS = {
+ tester(1, "Language only", Locale.ENGLISH, SWAP, "en", BS),
+ tester(2, "Language and country", Locale.JAPAN, SWAP, "ja-JP",
BS),
+ tester(3, "null", null, SWAP, null, BS)
+ };
-
//----------------------------------------------------------------------------------------------------------
- // Basic tests
-
//----------------------------------------------------------------------------------------------------------
- {
- "[0] Language only ",
- Locale.ENGLISH,
- SWAP,
- "en",
- BS
- },
- {
- "[1] Language and country",
- Locale.JAPAN,
- SWAP,
- "ja-JP",
- BS
- },
- {
- "[2] null",
- null,
- SWAP,
- null,
- BS
- },
- });
+ static RoundTripObjectSwapTester<?,?>[] testers() {
+ return TESTERS;
}
}
\ No newline at end of file
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/transforms/InputStreamBase64SwapTest.java
b/juneau-utest/src/test/java/org/apache/juneau/transforms/InputStreamBase64SwapTest.java
index b12e5d549..29a1919b9 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/transforms/InputStreamBase64SwapTest.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/transforms/InputStreamBase64SwapTest.java
@@ -14,17 +14,11 @@ package org.apache.juneau.transforms;
import static org.apache.juneau.utest.utils.Utils2.*;
-import java.io.*;
-import java.util.*;
-
import org.apache.juneau.*;
import org.apache.juneau.swap.*;
import org.apache.juneau.swaps.*;
-import org.junit.runner.*;
-import org.junit.runners.*;
-@RunWith(Parameterized.class)
-public class InputStreamBase64SwapTest extends
OneWayStringSwapTest<InputStream> {
+class InputStreamBase64SwapTest extends OneWayStringSwapTest_Base {
//------------------------------------------------------------------------------------------------------------------
// Setup
@@ -33,42 +27,21 @@ public class InputStreamBase64SwapTest extends
OneWayStringSwapTest<InputStream>
private static BeanSession BS = BeanContext.DEFAULT_SESSION;
private static InputStreamSwap SWAP = new InputStreamSwap.Base64();
- public InputStreamBase64SwapTest(String label, InputStream o,
StringSwap<InputStream> s, String r, BeanSession bs) throws Exception {
- super(label, o, s, r, bs);
+ private static <T> OneWayStringSwapTester<T> tester(int index, String
label, T object, StringSwap<T> swap, String expected, BeanSession bs) {
+ return OneWayStringSwapTester.create(index, label, object,
swap, expected, bs).build();
}
//------------------------------------------------------------------------------------------------------------------
// Parameters
//------------------------------------------------------------------------------------------------------------------
- @Parameterized.Parameters
- public static Collection<Object[]> getPairs() {
- return Arrays.asList(new Object[][] {
+ private static final OneWayStringSwapTester<?>[] TESTERS = {
+ tester(1, "Basic string", inputStream("foo"), SWAP, "Zm9v", BS),
+ tester(2, "Blank string", inputStream(""), SWAP, "", BS),
+ tester(3, "null", null, SWAP, "", BS)
+ };
-
//----------------------------------------------------------------------------------------------------------
- // Basic tests
-
//----------------------------------------------------------------------------------------------------------
- {
- "[0] Basic string",
- inputStream("foo"),
- SWAP,
- "Zm9v",
- BS
- },
- {
- "[1] Blank string",
- inputStream(""),
- SWAP,
- "",
- BS
- },
- {
- "[2] null",
- null,
- SWAP,
- "",
- BS
- },
- });
+ static OneWayStringSwapTester<?>[] testers() {
+ return TESTERS;
}
}
\ No newline at end of file
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/transforms/OneWayStringSwapTest_Base.java
b/juneau-utest/src/test/java/org/apache/juneau/transforms/OneWayStringSwapTest_Base.java
new file mode 100644
index 000000000..2855f6324
--- /dev/null
+++
b/juneau-utest/src/test/java/org/apache/juneau/transforms/OneWayStringSwapTest_Base.java
@@ -0,0 +1,29 @@
+//
***************************************************************************************************************************
+// * 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.juneau.transforms;
+
+import org.apache.juneau.*;
+import org.junit.jupiter.params.*;
+import org.junit.jupiter.params.provider.*;
+
+/**
+ * Base class for JUnit 5 one-way string swap tests.
+ */
+public abstract class OneWayStringSwapTest_Base extends SimpleTestBase {
+
+ @ParameterizedTest
+ @MethodSource("testers")
+ void testSwap(OneWayStringSwapTester<?> tester) throws Exception {
+ tester.testSwap();
+ }
+}
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/transforms/OneWayStringSwapTester.java
b/juneau-utest/src/test/java/org/apache/juneau/transforms/OneWayStringSwapTester.java
new file mode 100644
index 000000000..21535abb7
--- /dev/null
+++
b/juneau-utest/src/test/java/org/apache/juneau/transforms/OneWayStringSwapTester.java
@@ -0,0 +1,105 @@
+//
***************************************************************************************************************************
+// * 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.juneau.transforms;
+
+import static org.apache.juneau.common.internal.StringUtils.*;
+import static org.apache.juneau.common.internal.Utils.*;
+import static org.junit.jupiter.api.Assertions.*;
+import static org.apache.juneau.AssertionHelpers.*;
+
+import java.util.function.*;
+
+import org.apache.juneau.*;
+import org.apache.juneau.swap.*;
+
+/**
+ * Tester class for one-way string swap testing designed for JUnit 5
parameterized tests.
+ * Only performs swap operation (no unswap).
+ */
+public class OneWayStringSwapTester<T> {
+
+ public static <T> Builder<T> create(int index, String label, T object,
StringSwap<T> swap, String expected, BeanSession beanSession) {
+ return new Builder<>("["+index+"] " + label, ()->object, swap,
expected, beanSession);
+ }
+
+ public static class Builder<T> {
+ private String label;
+ private Supplier<T> objectSupplier;
+ private StringSwap<T> swap;
+ private String expected;
+ private BeanSession beanSession;
+ private String exceptionMsg;
+
+ public Builder(String label, Supplier<T> objectSupplier,
StringSwap<T> swap, String expected, BeanSession beanSession) {
+ this.label = label;
+ this.objectSupplier = objectSupplier;
+ this.swap = swap;
+ this.expected = expected;
+ this.beanSession = beanSession;
+ }
+
+ public Builder<T> exceptionMsg(String v) {
+ exceptionMsg = v;
+ return this;
+ }
+
+ public OneWayStringSwapTester<T> build() {
+ return new OneWayStringSwapTester<>(this);
+ }
+ }
+
+ private final String label;
+ private final Supplier<T> objectSupplier;
+ private final StringSwap<T> swap;
+ private final String expected;
+ private final BeanSession beanSession;
+ private final String exceptionMsg;
+
+ private OneWayStringSwapTester(Builder<T> b) {
+ label = b.label;
+ objectSupplier = b.objectSupplier;
+ swap = b.swap;
+ expected = b.expected;
+ beanSession = b.beanSession;
+ exceptionMsg = b.exceptionMsg;
+ }
+
+ public void testSwap() throws Exception {
+ try {
+ var o = objectSupplier.get();
+ var s = swap.swap(beanSession, o);
+ if (ne(expected, s)) {
+ if (expected.isEmpty()) {
+ if (! label.startsWith("[]"))
+
System.err.println(label.substring(0, label.indexOf(']')+1) + " "+s); // NOT
DEBUG
+ fail("Test [" + label + " swap] failed
- expected was empty but got: " + s);
+ } else {
+ fail("Test [" + label + " swap] failed.
Expected=[" + expected + "], Actual=[" + s + "]");
+ }
+ }
+ } catch (AssertionError e) {
+ if (exceptionMsg == null)
+ throw e;
+ assertTrue(e.getMessage().contains(exceptionMsg),
ss("Expected exception message to contain: {0}, but was {1}.", exceptionMsg,
e.getMessage()));
+ } catch (Exception e) {
+ if (exceptionMsg == null)
+ throw new AssertionError("Test [" + label + "
swap] failed with exception: " + e.getLocalizedMessage(), e);
+ assertTrue(e.getMessage().contains(exceptionMsg),
ss("Expected exception message to contain: {0}, but was {1}.", exceptionMsg,
e.getMessage()));
+ }
+ }
+
+ @Override
+ public String toString() {
+ return "OneWayStringSwapTester: " + label;
+ }
+}
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/transforms/ReaderSwapTest.java
b/juneau-utest/src/test/java/org/apache/juneau/transforms/ReaderSwapTest.java
index c33f271d1..f68757891 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/transforms/ReaderSwapTest.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/transforms/ReaderSwapTest.java
@@ -14,17 +14,11 @@ package org.apache.juneau.transforms;
import static org.apache.juneau.utest.utils.Utils2.*;
-import java.io.*;
-import java.util.*;
-
import org.apache.juneau.*;
import org.apache.juneau.swap.*;
import org.apache.juneau.swaps.*;
-import org.junit.runner.*;
-import org.junit.runners.*;
-@RunWith(Parameterized.class)
-public class ReaderSwapTest extends OneWayStringSwapTest<Reader> {
+class ReaderSwapTest extends OneWayStringSwapTest_Base {
//------------------------------------------------------------------------------------------------------------------
// Setup
@@ -33,42 +27,21 @@ public class ReaderSwapTest extends
OneWayStringSwapTest<Reader> {
private static BeanSession BS = BeanContext.DEFAULT_SESSION;
private static ReaderSwap SWAP = new ReaderSwap();
- public ReaderSwapTest(String label, Reader o, StringSwap<Reader> s,
String r, BeanSession bs) throws Exception {
- super(label, o, s, r, bs);
+ private static <T> OneWayStringSwapTester<T> tester(int index, String
label, T object, StringSwap<T> swap, String expected, BeanSession bs) {
+ return OneWayStringSwapTester.create(index, label, object,
swap, expected, bs).build();
}
//------------------------------------------------------------------------------------------------------------------
// Parameters
//------------------------------------------------------------------------------------------------------------------
- @Parameterized.Parameters
- public static Collection<Object[]> getPairs() {
- return Arrays.asList(new Object[][] {
+ private static final OneWayStringSwapTester<?>[] TESTERS = {
+ tester(1, "Basic string", reader("foo"), SWAP, "foo", BS),
+ tester(2, "Blank string", reader(""), SWAP, "", BS),
+ tester(3, "null", null, SWAP, null, BS)
+ };
-
//----------------------------------------------------------------------------------------------------------
- // Basic tests
-
//----------------------------------------------------------------------------------------------------------
- {
- "[0] Basic string",
- reader("foo"),
- SWAP,
- "foo",
- BS
- },
- {
- "[1] Blank string",
- reader(""),
- SWAP,
- "",
- BS
- },
- {
- "[2] null",
- null,
- SWAP,
- null,
- BS
- },
- });
+ static OneWayStringSwapTester<?>[] testers() {
+ return TESTERS;
}
}
\ No newline at end of file
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/transforms/RoundTripObjectSwapTest_Base.java
b/juneau-utest/src/test/java/org/apache/juneau/transforms/RoundTripObjectSwapTest_Base.java
new file mode 100644
index 000000000..999adce98
--- /dev/null
+++
b/juneau-utest/src/test/java/org/apache/juneau/transforms/RoundTripObjectSwapTest_Base.java
@@ -0,0 +1,35 @@
+//
***************************************************************************************************************************
+// * 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.juneau.transforms;
+
+import org.apache.juneau.*;
+import org.junit.jupiter.params.*;
+import org.junit.jupiter.params.provider.*;
+
+/**
+ * Base class for JUnit 5 round-trip object swap tests.
+ */
+public abstract class RoundTripObjectSwapTest_Base extends SimpleTestBase {
+
+ @ParameterizedTest
+ @MethodSource("testers")
+ void testSwap(RoundTripObjectSwapTester<?,?> tester) throws Exception {
+ tester.testSwap();
+ }
+
+ @ParameterizedTest
+ @MethodSource("testers")
+ void testUnswap(RoundTripObjectSwapTester<?,?> tester) throws Exception
{
+ tester.testUnswap();
+ }
+}
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/transforms/RoundTripObjectSwapTester.java
b/juneau-utest/src/test/java/org/apache/juneau/transforms/RoundTripObjectSwapTester.java
new file mode 100644
index 000000000..1aa86e0be
--- /dev/null
+++
b/juneau-utest/src/test/java/org/apache/juneau/transforms/RoundTripObjectSwapTester.java
@@ -0,0 +1,119 @@
+//
***************************************************************************************************************************
+// * 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.juneau.transforms;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.apache.juneau.AssertionHelpers.*;
+
+import java.util.*;
+import java.util.function.*;
+
+import org.apache.juneau.*;
+import org.apache.juneau.swap.*;
+
+/**
+ * Tester class for round-trip object swap testing designed for JUnit 5
parameterized tests.
+ * Performs both swap and unswap operations.
+ */
+public class RoundTripObjectSwapTester<T,S> {
+
+ public static <T,S> Builder<T,S> create(int index, String label, T
object, ObjectSwap<T,S> swap, S expected, BeanSession beanSession) {
+ return new Builder<>("["+index+"] " + label, ()->object, swap,
expected, beanSession);
+ }
+
+ public static class Builder<T,S> {
+ private String label;
+ private Supplier<T> objectSupplier;
+ private ObjectSwap<T,S> swap;
+ private S expected;
+ private BeanSession beanSession;
+ private String exceptionMsg;
+
+ public Builder(String label, Supplier<T> objectSupplier,
ObjectSwap<T,S> swap, S expected, BeanSession beanSession) {
+ this.label = label;
+ this.objectSupplier = objectSupplier;
+ this.swap = swap;
+ this.expected = expected;
+ this.beanSession = beanSession;
+ }
+
+ public Builder<T,S> exceptionMsg(String v) {
+ exceptionMsg = v;
+ return this;
+ }
+
+ public RoundTripObjectSwapTester<T,S> build() {
+ return new RoundTripObjectSwapTester<>(this);
+ }
+ }
+
+ private final String label;
+ private final Supplier<T> objectSupplier;
+ private final ObjectSwap<T,S> swap;
+ private final S expected;
+ private final BeanSession beanSession;
+ private final String exceptionMsg;
+
+ private RoundTripObjectSwapTester(Builder<T,S> b) {
+ label = b.label;
+ objectSupplier = b.objectSupplier;
+ swap = b.swap;
+ expected = b.expected;
+ beanSession = b.beanSession;
+ exceptionMsg = b.exceptionMsg;
+ }
+
+ public void testSwap() throws Exception {
+ try {
+ var o = objectSupplier.get();
+ var s = swap.swap(beanSession, o);
+ if (!Objects.equals(expected, s)) {
+ fail("Test [" + label + " swap] failed.
Expected=[" + expected + "], Actual=[" + s + "]");
+ }
+ } catch (AssertionError e) {
+ if (exceptionMsg == null)
+ throw e;
+ assertTrue(e.getMessage().contains(exceptionMsg),
ss("Expected exception message to contain: {0}, but was {1}.", exceptionMsg,
e.getMessage()));
+ } catch (Exception e) {
+ if (exceptionMsg == null)
+ throw new AssertionError("Test [" + label + "
swap] failed with exception: " + e.getLocalizedMessage(), e);
+ assertTrue(e.getMessage().contains(exceptionMsg),
ss("Expected exception message to contain: {0}, but was {1}.", exceptionMsg,
e.getMessage()));
+ }
+ }
+
+ public void testUnswap() throws Exception {
+ try {
+ T o = objectSupplier.get();
+ S s = swap.swap(beanSession, o);
+ T o2 = swap.unswap(beanSession, s,
beanSession.getClassMetaForObject(o));
+ S s2 = swap.swap(beanSession, o2);
+ if (!Objects.equals(s, s2)) {
+ System.err.println("s=["+s+"], o=["+o+"],
o.type=["+o.getClass().getName()+"], o2=["+o2+"],
o2.type=["+o2.getClass().getName()+"]"); // NOT DEBUG
+ fail("Test [" + label + " unswap] failed.
Expected=[" + s + "], Actual=[" + s2 + "]");
+ }
+ } catch (AssertionError e) {
+ if (exceptionMsg == null)
+ throw e;
+ assertTrue(e.getMessage().contains(exceptionMsg),
ss("Expected exception message to contain: {0}, but was {1}.", exceptionMsg,
e.getMessage()));
+ } catch (Exception e) {
+ if (exceptionMsg == null)
+ throw new AssertionError("Test [" + label + "
unswap] failed with exception: " + e.getLocalizedMessage(), e);
+ assertTrue(e.getMessage().contains(exceptionMsg),
ss("Expected exception message to contain: {0}, but was {1}.", exceptionMsg,
e.getMessage()));
+ }
+ }
+
+ @Override
+ public String toString() {
+ return "RoundTripObjectSwapTester: " + label;
+ }
+}