You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2023/02/22 19:26:39 UTC

[tomcat] 04/06: Refactor to make testing BeanSupport implementations easier

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 8588fa9c34b4b2307f69b9796797675c6d0183d1
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed Feb 22 17:12:43 2023 +0000

    Refactor to make testing BeanSupport implementations easier
---
 java/jakarta/el/BeanSupport.java                   | 18 +++++-
 test/jakarta/el/ELBaseTest.java                    | 69 ++++++++++++++++++++++
 ...anSupportBaseTest.java => TestBeanSupport.java} |  2 +-
 test/jakarta/el/TestBeanSupportFull.java           | 29 ---------
 test/jakarta/el/TestBeanSupportStandalone.java     | 34 -----------
 5 files changed, 86 insertions(+), 66 deletions(-)

diff --git a/java/jakarta/el/BeanSupport.java b/java/jakarta/el/BeanSupport.java
index fdd60e596e..ee6dc7001d 100644
--- a/java/jakarta/el/BeanSupport.java
+++ b/java/jakarta/el/BeanSupport.java
@@ -27,6 +27,17 @@ abstract class BeanSupport {
     private static final BeanSupport beanSupport;
 
     static {
+        // Only intended for unit tests. Not intended to be part of public API.
+        boolean doNotCacheInstance = Boolean.getBoolean("jakarta.el.BeanSupport.doNotCacheInstance");
+        if (doNotCacheInstance) {
+            beanSupport = null;
+        } else {
+            beanSupport = createInstance();
+        }
+    }
+
+    private static BeanSupport createInstance() {
+        // Only intended for unit tests. Not intended to be part of public API.
         boolean useFull = !Boolean.getBoolean("jakarta.el.BeanSupport.useStandalone");
 
         if (useFull) {
@@ -40,14 +51,17 @@ abstract class BeanSupport {
         }
         if (useFull) {
             // The full implementation provided by the java.beans package
-            beanSupport = new BeanSupportFull();
+            return new BeanSupportFull();
         } else {
             // The cut-down local implementation that does not depend on the java.beans package
-            beanSupport = new BeanSupportStandalone();
+            return new BeanSupportStandalone();
         }
     }
 
     static BeanSupport getInstance() {
+        if (beanSupport == null) {
+            return createInstance();
+        }
         return beanSupport;
     }
 
diff --git a/test/jakarta/el/ELBaseTest.java b/test/jakarta/el/ELBaseTest.java
new file mode 100644
index 0000000000..79e262a220
--- /dev/null
+++ b/test/jakarta/el/ELBaseTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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 jakarta.el;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+/**
+ * Base class for tests that (indirectly) use BeanSupport and want to test both implementations.
+ */
+@RunWith(Parameterized.class)
+public abstract class ELBaseTest {
+
+    @Parameters(name = "{index}: useStandalone[{0}]")
+    public static Collection<Object[]> data() {
+        List<Object[]> parameterSets = new ArrayList<>();
+
+        parameterSets.add(new Object[] { Boolean.FALSE });
+        parameterSets.add(new Object[] { Boolean.TRUE });
+
+        return parameterSets;
+    }
+
+    @Parameter(0)
+    public boolean useStandalone;
+
+    @Before
+    public void setup() {
+        // Disable caching so we can switch implementations within a JVM instance.
+        System.setProperty("jakarta.el.BeanSupport.doNotCacheInstance", "true");
+        // Set up the implementation for this test run
+        System.setProperty("jakarta.el.BeanSupport.useStandalone", Boolean.toString(useStandalone));
+    }
+
+    /*
+     * Double check test has been configured as expected
+     */
+    @Test
+    public void testImplementation() {
+        if (useStandalone) {
+            Assert.assertEquals(BeanSupportStandalone.class, BeanSupport.getInstance().getClass());
+        } else {
+            Assert.assertEquals(BeanSupportFull.class, BeanSupport.getInstance().getClass());
+        }
+    }
+}
diff --git a/test/jakarta/el/BeanSupportBaseTest.java b/test/jakarta/el/TestBeanSupport.java
similarity index 99%
rename from test/jakarta/el/BeanSupportBaseTest.java
rename to test/jakarta/el/TestBeanSupport.java
index e594811052..0807a90dce 100644
--- a/test/jakarta/el/BeanSupportBaseTest.java
+++ b/test/jakarta/el/TestBeanSupport.java
@@ -22,7 +22,7 @@ import jakarta.el.BeanELResolver.BeanProperty;
 import org.junit.Assert;
 import org.junit.Test;
 
-public class BeanSupportBaseTest {
+public class TestBeanSupport extends ELBaseTest {
 
     @Test
     public void testSimpleBean() {
diff --git a/test/jakarta/el/TestBeanSupportFull.java b/test/jakarta/el/TestBeanSupportFull.java
deleted file mode 100644
index e8a5d1323c..0000000000
--- a/test/jakarta/el/TestBeanSupportFull.java
+++ /dev/null
@@ -1,29 +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 jakarta.el;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-public class TestBeanSupportFull extends BeanSupportBaseTest {
-
-    @Test
-    public void testImplementationClass() {
-        Assert.assertTrue(BeanSupport.getInstance() instanceof BeanSupportFull);
-    }
-
-}
diff --git a/test/jakarta/el/TestBeanSupportStandalone.java b/test/jakarta/el/TestBeanSupportStandalone.java
deleted file mode 100644
index ebb97a9158..0000000000
--- a/test/jakarta/el/TestBeanSupportStandalone.java
+++ /dev/null
@@ -1,34 +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 jakarta.el;
-
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-
-public class TestBeanSupportStandalone extends BeanSupportBaseTest {
-
-    @Before
-    public void setup() {
-        System.setProperty("jakarta.el.BeanSupport.useStandalone", "true");
-    }
-
-    @Test
-    public void testImplementationClass() {
-        Assert.assertTrue(BeanSupport.getInstance() instanceof BeanSupportStandalone);
-    }
-}


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org