This is an automated email from the ASF dual-hosted git repository. tandraschko pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/bval.git
The following commit(s) were added to refs/heads/master by this push: new b89a633 fixed tests the right way b89a633 is described below commit b89a6331baffce920c87269f1b4e4b1f10a6a55a Author: Thomas Andraschko <tandrasc...@apache.org> AuthorDate: Wed Feb 8 15:59:55 2023 +0100 fixed tests the right way --- .../bval/jsr/DefaultMessageInterpolatorTest.java | 9 ++- .../apache/bval/jsr/DelegateExpressionFactory.java | 93 ++++++++++++++++++++++ .../META-INF/services/jakarta.el.ExpressionFactory | 17 ++++ 3 files changed, 115 insertions(+), 4 deletions(-) diff --git a/bval-jsr/src/test/java/org/apache/bval/jsr/DefaultMessageInterpolatorTest.java b/bval-jsr/src/test/java/org/apache/bval/jsr/DefaultMessageInterpolatorTest.java index 5ad3929..bd8e744 100644 --- a/bval-jsr/src/test/java/org/apache/bval/jsr/DefaultMessageInterpolatorTest.java +++ b/bval-jsr/src/test/java/org/apache/bval/jsr/DefaultMessageInterpolatorTest.java @@ -93,15 +93,16 @@ public class DefaultMessageInterpolatorTest { Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[] {}, originalClassLoader)); try { - Class<?> elFactoryClass; if (elFactory == null) { - elFactoryClass = ExpressionFactory.class; System.clearProperty(ExpressionFactory.class.getName()); } else { - elFactoryClass = Class.forName(elFactory); + Class<?> elFactoryClass = Class.forName(elFactory); System.setProperty(ExpressionFactory.class.getName(), elFactory); + + Class<? extends ExpressionFactory> usedImpl = + ((DelegateExpressionFactory) ExpressionFactory.newInstance()).getWrapped().getClass(); + assertTrue(elFactoryClass == usedImpl); } - assertTrue(elFactoryClass.isInstance(ExpressionFactory.newInstance())); elAvailable = true; } catch (Exception e) { elAvailable = false; diff --git a/bval-jsr/src/test/java/org/apache/bval/jsr/DelegateExpressionFactory.java b/bval-jsr/src/test/java/org/apache/bval/jsr/DelegateExpressionFactory.java new file mode 100644 index 0000000..24742c3 --- /dev/null +++ b/bval-jsr/src/test/java/org/apache/bval/jsr/DelegateExpressionFactory.java @@ -0,0 +1,93 @@ +/* + * 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.bval.jsr; + +import jakarta.el.*; + +import java.lang.reflect.Method; +import java.util.Iterator; +import java.util.Map; +import java.util.ServiceLoader; + +/** + * EL5.0 ExpressionFactory lookups the ServiceLoader before the system property + * In our tests we have at least 2 EL impls and DefaultMessageInterpolatorTest needs a replaceable ExpressionFactory + * So this a wrapper, which checks the system property first and then asking the ServiceLoader + */ +public class DelegateExpressionFactory extends ExpressionFactory { + + public DelegateExpressionFactory() { + + } + + @Override + public ValueExpression createValueExpression(ELContext context, String expression, Class<?> expectedType) { + return getWrapped().createValueExpression(context, expression, expectedType); + } + + @Override + public ValueExpression createValueExpression(Object instance, Class<?> expectedType) { + return getWrapped().createValueExpression(instance, expectedType); + } + + @Override + public MethodExpression createMethodExpression(ELContext context, String expression, Class<?> expectedReturnType, Class<?>[] expectedParamTypes) { + return getWrapped().createMethodExpression(context, expression, expectedReturnType, expectedParamTypes); + } + + @Override + public <T> T coerceToType(Object obj, Class<T> targetType) { + return getWrapped().coerceToType(obj, targetType); + } + + @Override + public ELResolver getStreamELResolver() { + return getWrapped().getStreamELResolver(); + } + + @Override + public Map<String, Method> getInitFunctionMap() { + return getWrapped().getInitFunctionMap(); + } + + public ExpressionFactory getWrapped() + { + String systemProperty = System.getProperty(ExpressionFactory.class.getName()); + if (systemProperty != null) { + try { + return (ExpressionFactory) Class.forName(systemProperty).getConstructor().newInstance(); + } catch (Exception e) { + return null; + } + } + + try { + ServiceLoader<ExpressionFactory> serviceLoader = ServiceLoader.load(ExpressionFactory.class, + Thread.currentThread().getContextClassLoader()); + Iterator<ExpressionFactory> iter = serviceLoader.iterator(); + while (iter.hasNext()) { + ExpressionFactory service = iter.next(); + if (service != null && service.getClass() != this.getClass()) { + return service; + } + } + } catch (Exception ex) { + } + + return null; + } +} diff --git a/bval-jsr/src/test/resources/META-INF/services/jakarta.el.ExpressionFactory b/bval-jsr/src/test/resources/META-INF/services/jakarta.el.ExpressionFactory new file mode 100644 index 0000000..8b1ad5e --- /dev/null +++ b/bval-jsr/src/test/resources/META-INF/services/jakarta.el.ExpressionFactory @@ -0,0 +1,17 @@ +# 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. +org.apache.bval.jsr.DelegateExpressionFactory