This is an automated email from the ASF dual-hosted git repository. gk pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/turbine-core.git
commit f88d9c1a787982f586998e8e67a8f951ace36d63 Author: Georg Kallidis <[email protected]> AuthorDate: Thu Jul 6 14:01:05 2023 +0200 Add anotation TurbineTool and exampels in tests to allow advanced testing and custom initializing in actions --- conf/test/TorqueTest.properties | 15 +++++--- pom.xml | 19 ++++++---- .../turbine/annotation/AnnotationProcessor.java | 44 ++++++++++++++++++++++ .../org/apache/turbine/annotation/TurbineTool.java | 43 +++++++++++++++++++++ .../annotation/AnnotationProcessorTest.java | 18 +++++++++ .../localization/LocalizationToolTest.java | 25 +++++++----- 6 files changed, 142 insertions(+), 22 deletions(-) diff --git a/conf/test/TorqueTest.properties b/conf/test/TorqueTest.properties index 1e443d86..35884b7a 100644 --- a/conf/test/TorqueTest.properties +++ b/conf/test/TorqueTest.properties @@ -22,19 +22,22 @@ torque.defaults.pool.defaultTestOnBorrow=true torque.defaults.pool.validationQuery=SELECT 1 torque.idbroker.cleverquantity=true -torque.idbroker.prefetch=true +torque.idbroker.prefetch=false torque.idbroker.usenewconnection=true torque.database.default=default torque.database.schema = # supported auto, hsqldb, mysql.. -torque.database.default.adapter=hsqldb +#torque.database.default.adapter=hsqldb +torque.database.default.adapter=mymysql +torque.database.default.adapter.mymysql.className=org.apache.torque.adapter.MysqlAdapter + # dbcp2 torque.dsfactory.default.factory= org.apache.torque.dsfactory.SharedPool2DataSourceFactory torque.dsfactory.default.pool.defaultTestOnBorrow=true torque.dsfactory.default.pool.validationQuery=SELECT 1 from INFORMATION_SCHEMA.SYSTEM_USERS -torque.dsfactory.default.connection.driver = org.hsqldb.jdbcDriver -torque.dsfactory.default.connection.url = jdbc:hsqldb:mem: -torque.dsfactory.default.connection.user = sa -torque.dsfactory.default.connection.password = +#torque.dsfactory.default.connection.driver = org.hsqldb.jdbcDriver +#torque.dsfactory.default.connection.url = jdbc:hsqldb:mem: +#torque.dsfactory.default.connection.user = sa +#torque.dsfactory.default.connection.password = diff --git a/pom.xml b/pom.xml index f749daad..b4d660a2 100644 --- a/pom.xml +++ b/pom.xml @@ -535,7 +535,7 @@ <plugins> <plugin> - <!-- hint: mvn verify --> + <!-- hint: mvn verify or skip with -Ddependency.check.skip=true --> <groupId>org.owasp</groupId> <artifactId>dependency-check-maven</artifactId> <configuration> @@ -913,7 +913,7 @@ <groupId>nl.basjes.parse.useragent</groupId> <artifactId>yauaa</artifactId> <!-- java 11 required for v6.1+ --> - <version>7.20.1</version> + <version>7.20.0</version> </dependency> <dependency> <groupId>org.apache.fulcrum</groupId> @@ -1070,6 +1070,12 @@ <version>${turbine.log4j2.version}</version> <scope>runtime</scope> </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <version>1.7.36</version> + <scope>runtime</scope> + </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> @@ -1105,11 +1111,10 @@ <version>4.0.0</version> </dependency> <dependency> - <groupId>com.sun.xml.bind</groupId> - <artifactId>jaxb-impl</artifactId> - <version>4.0.2</version> - <scope>runtime</scope> - </dependency> + <groupId>org.glassfish.jaxb</groupId> + <artifactId>jaxb-runtime</artifactId> + <version>4.0.3</version> + </dependency> <!-- testcontainer minimal shared resources --> <dependency> <groupId>org.apache.fulcrum</groupId> diff --git a/src/java/org/apache/turbine/annotation/AnnotationProcessor.java b/src/java/org/apache/turbine/annotation/AnnotationProcessor.java index 1abf3bb3..e25e8b37 100644 --- a/src/java/org/apache/turbine/annotation/AnnotationProcessor.java +++ b/src/java/org/apache/turbine/annotation/AnnotationProcessor.java @@ -30,6 +30,8 @@ import java.util.concurrent.ConcurrentMap; import org.apache.commons.configuration2.Configuration; import org.apache.commons.lang3.StringUtils; +import org.apache.fulcrum.pool.PoolException; +import org.apache.fulcrum.pool.PoolService; import org.apache.fulcrum.security.model.turbine.TurbineAccessControlList; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -237,6 +239,7 @@ public class AnnotationProcessor * objects * * @param object the object + * @param hasTurbineServicesInMethodFields set <code>true </code>, if methods should be parsed * @throws TurbineException if the objects could not be injected */ public static void process(Object object, Boolean hasTurbineServicesInMethodFields) throws TurbineException @@ -244,6 +247,7 @@ public class AnnotationProcessor ServiceManager manager = null; Configuration config = null; AssemblerBrokerService assembler = null; + PoolService pool= null; Class<?> clazz = object.getClass(); while (clazz != null) @@ -281,6 +285,15 @@ public class AnnotationProcessor } injectTurbineLoader(object, assembler, field, (TurbineLoader) a); } + else if (a instanceof TurbineTool) + { + if (pool == null) + { + pool = (PoolService)TurbineServices.getInstance() + .getService(PoolService.ROLE); + } + injectTurbineTool(object, pool, field, (TurbineTool) a); + } } } @@ -341,6 +354,37 @@ public class AnnotationProcessor + loader + " into object " + object, e); } } + + /** + * Inject Turbine configuration into field of object + * + * @param object the object to process + * @param assembler AssemblerBrokerService, provides the loader + * @param field the field + * @param annotation the value of the annotation + * + * @throws TurbineException if loader cannot be set + */ + private static void injectTurbineTool(Object object, PoolService pool, Field field, TurbineTool annotation) throws TurbineException + { + Object tool = null; + try + { + tool = pool.getInstance(annotation.value()); + // inject annotations in tool + process(tool); + + field.setAccessible(true); + log.debug("Injection of {} into object {}", tool, object); + + field.set(object, tool); + } + catch (PoolException | IllegalArgumentException | IllegalAccessException e) + { + throw new TurbineException("Could not inject tool " + + tool + " into object " + object, e); + } + } /** * Inject Turbine configuration into field of object diff --git a/src/java/org/apache/turbine/annotation/TurbineTool.java b/src/java/org/apache/turbine/annotation/TurbineTool.java new file mode 100644 index 00000000..ad783f2e --- /dev/null +++ b/src/java/org/apache/turbine/annotation/TurbineTool.java @@ -0,0 +1,43 @@ +package org.apache.turbine.annotation; + +/* + * 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. + */ + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.apache.turbine.services.pull.ApplicationTool; + + +/** + * Annotation to mark fields in modules that require a loader to be injected + */ +@Retention( RetentionPolicy.RUNTIME ) +@Target( ElementType.FIELD ) +public @interface TurbineTool +{ + /** + * Get the class of the tool target to inject + * + * @return the class the tool is responsible for + */ + Class<? extends ApplicationTool> value(); +} diff --git a/src/test/org/apache/turbine/annotation/AnnotationProcessorTest.java b/src/test/org/apache/turbine/annotation/AnnotationProcessorTest.java index 2242c479..a30806b8 100644 --- a/src/test/org/apache/turbine/annotation/AnnotationProcessorTest.java +++ b/src/test/org/apache/turbine/annotation/AnnotationProcessorTest.java @@ -48,12 +48,15 @@ import java.util.List; import org.apache.commons.configuration2.Configuration; import org.apache.fulcrum.factory.FactoryService; +import org.apache.fulcrum.security.UserManager; import org.apache.fulcrum.security.entity.Role; import org.apache.fulcrum.security.model.turbine.TurbineAccessControlList; import org.apache.turbine.annotation.AnnotationProcessor.ConditionType; import org.apache.turbine.modules.Screen; import org.apache.turbine.modules.ScreenLoader; import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService; +import org.apache.turbine.services.localization.LocalizationTool; +import org.apache.turbine.services.security.SecurityService; import org.apache.turbine.util.RunData; import org.apache.turbine.util.TurbineConfig; import org.apache.turbine.util.TurbineException; @@ -98,9 +101,19 @@ public class AnnotationProcessorTest @TurbineService private AssemblerBrokerService asb; + + /** a Fulcrum userManager */ + @TurbineService + private UserManager userManager; + + @TurbineService + private SecurityService security; @TurbineService private FactoryService factory; + + @TurbineTool(LocalizationTool.class) + private LocalizationTool lt; @BeforeAll public static void init() throws Exception @@ -144,6 +157,11 @@ public class AnnotationProcessorTest assertNotNull(screenLoader); assertNotNull(asb); assertNotNull(factory); + + assertNotNull(userManager); + assertNotNull(security); + assertNotNull(lt); + // although you need to initialize with context in lt.init(getRunData()); } diff --git a/src/test/org/apache/turbine/services/localization/LocalizationToolTest.java b/src/test/org/apache/turbine/services/localization/LocalizationToolTest.java index 150bba86..e555f5ae 100644 --- a/src/test/org/apache/turbine/services/localization/LocalizationToolTest.java +++ b/src/test/org/apache/turbine/services/localization/LocalizationToolTest.java @@ -28,8 +28,12 @@ import javax.servlet.ServletConfig; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.fulcrum.pool.PoolService; import org.apache.turbine.annotation.AnnotationProcessor; +import org.apache.turbine.annotation.TurbineService; +import org.apache.turbine.annotation.TurbineTool; import org.apache.turbine.services.TurbineServices; +import org.apache.turbine.services.pull.PullService; import org.apache.turbine.services.rundata.RunDataService; import org.apache.turbine.test.BaseTestCase; import org.apache.turbine.util.RunData; @@ -50,13 +54,23 @@ import org.junit.Test; public class LocalizationToolTest extends BaseTestCase { private static TurbineConfig tc = null; + + @TurbineTool(LocalizationTool.class) private LocalizationTool lt; + + @BeforeClass + public static void setUp() throws Exception + { + tc = new TurbineConfig(".", "/conf/test/TemplateService.properties"); + tc.initialize(); + } @Before public void initTool() throws Exception { - lt = new LocalizationTool(); - AnnotationProcessor.process(lt); +// lt = new LocalizationTool(); +// AnnotationProcessor.process(lt); + AnnotationProcessor.process(this); lt.init(getRunData()); } @@ -99,13 +113,6 @@ public class LocalizationToolTest extends BaseTestCase return runData; } - @BeforeClass - public static void setUp() throws Exception - { - tc = new TurbineConfig(".", "/conf/test/TestFulcrumComponents.properties"); - tc.initialize(); - } - @AfterClass public static void tearDown() throws Exception {
