This is an automated email from the ASF dual-hosted git repository.
tv pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/turbine-core.git
The following commit(s) were added to refs/heads/trunk by this push:
new a1530162 Some modernization, improved tests
a1530162 is described below
commit a15301629cee0d9d78eaea00fa4445a02881ee22
Author: Thomas Vandahl <[email protected]>
AuthorDate: Mon Jan 19 22:06:35 2026 +0100
Some modernization, improved tests
---
conf/test/CompleteTurbineResources.properties | 6 +-
.../apache/turbine/services/intake/IntakeTool.java | 101 ++++++++-------------
.../turbine/services/intake/IntakeToolTest.java | 43 +++++++--
3 files changed, 73 insertions(+), 77 deletions(-)
diff --git a/conf/test/CompleteTurbineResources.properties
b/conf/test/CompleteTurbineResources.properties
index 3e0bb02c..74766725 100644
--- a/conf/test/CompleteTurbineResources.properties
+++ b/conf/test/CompleteTurbineResources.properties
@@ -319,11 +319,7 @@ use.ssl=true
# that depend on other services during initialization.
# -------------------------------------------------------------------
-# Choose between the two available implementations of an Avalon container -
ECM or YAAFI
-
-#
services.AvalonComponentService.classname=org.apache.turbine.services.avaloncomponent.TurbineAvalonComponentService
services.AvalonComponentService.classname=org.apache.turbine.services.avaloncomponent.TurbineYaafiComponentService
-
services.RunDataService.classname=org.apache.turbine.services.rundata.TurbineRunDataService
services.ServletService.classname=org.apache.turbine.services.servlet.TurbineServletService
services.AssemblerBrokerService.classname=org.apache.turbine.services.assemblerbroker.TurbineAssemblerBrokerService
@@ -494,6 +490,7 @@
tool.request.link=org.apache.turbine.services.pull.tools.TemplateLink
tool.request.page=org.apache.turbine.util.template.HtmlPageAttributes
tool.request.content=org.apache.turbine.services.pull.tools.ContentTool
tool.request.l10n=org.apache.turbine.services.localization.LocalizationTool
+tool.request.intake=org.apache.turbine.services.intake.IntakeTool
# This pull tool is to allow for easy formatting of Date object into Strings
tool.request.dateFormatter=org.apache.turbine.services.pull.util.DateFormatter
@@ -553,7 +550,6 @@ tool.om.factory=org.apache.turbine.om.MockRetrieverFactory
#
# -------------------------------------------------------------------
-
services.VelocityService.earlyInit = true
services.VelocityService.template.extension = vm
diff --git a/src/java/org/apache/turbine/services/intake/IntakeTool.java
b/src/java/org/apache/turbine/services/intake/IntakeTool.java
index a493ebbb..63443f07 100644
--- a/src/java/org/apache/turbine/services/intake/IntakeTool.java
+++ b/src/java/org/apache/turbine/services/intake/IntakeTool.java
@@ -19,10 +19,11 @@ package org.apache.turbine.services.intake;
* under the License.
*/
-import java.util.Arrays;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.stream.Stream;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.fulcrum.intake.IntakeException;
@@ -66,7 +67,7 @@ public class IntakeTool
/** ValueParser instance */
protected ValueParser pp;
- private final HashMap<String, Group> declaredGroups = new HashMap<>();
+ private final HashSet<String> declaredGroups = new HashSet<>();
private final StringBuilder allGroupsSB = new StringBuilder(256);
private final StringBuilder groupSB = new StringBuilder(128);
@@ -95,17 +96,16 @@ public class IntakeTool
if (groups == null) // Initialize only once
{
String[] groupNames = intakeService.getGroupNames();
- int groupCount = 0;
- if (groupNames != null)
- {
- groupCount = groupNames.length;
- }
- groups = new HashMap<>((int) (1.25 * groupCount + 1));
- pullMap = new HashMap<>((int) (1.25 * groupCount + 1));
+ ArrayUtils.reverse(groupNames);
+ groups = new HashMap<>();
+ pullMap = new HashMap<>();
- for (int i = groupCount - 1; i >= 0; i--)
+ if (groupNames != null)
{
- pullMap.put(groupNames[i], new PullHelper(groupNames[i]));
+ for (String groupName : groupNames)
+ {
+ pullMap.put(groupName, new PullHelper(groupName));
+ }
}
}
@@ -119,20 +119,19 @@ public class IntakeTool
}
else
{
- groupNames = new String[groupKeys.length];
- for (int i = groupKeys.length - 1; i >= 0; i--)
- {
- groupNames[i] = intakeService.getGroupName(groupKeys[i]);
- }
+ groupNames = Stream.of(groupKeys)
+ .map(key -> intakeService.getGroupName(key))
+ .toArray(String[]::new);
}
- for (int i = groupNames.length - 1; i >= 0; i--)
+ ArrayUtils.reverse(groupNames);
+ for (String groupName : groupNames)
{
Group foundGroup = null;
try
{
- foundGroup = intakeService.getGroup(groupNames[i]);
+ foundGroup = intakeService.getGroup(groupName);
List<Group> foundGroups = foundGroup.getObjects(pp);
if (foundGroups != null)
@@ -153,9 +152,9 @@ public class IntakeTool
{
intakeService.releaseGroup(foundGroup);
}
- catch (IntakeException intakeException)
+ catch (IntakeException ie)
{
- log.error(intakeException, intakeException);
+ log.error("Tried to release unknown group {}",
foundGroup.getIntakeGroupName(), ie);
}
}
}
@@ -171,9 +170,9 @@ public class IntakeTool
{
for (Group group : groups.values())
{
- if (!declaredGroups.containsKey(group.getIntakeGroupName()))
+ if (!declaredGroups.contains(group.getIntakeGroupName()))
{
- declaredGroups.put(group.getIntakeGroupName(), null);
+ declaredGroups.add(group.getIntakeGroupName());
vp.add("intake-grp", group.getGID());
}
vp.add(group.getGID(), group.getOID());
@@ -224,9 +223,9 @@ public class IntakeTool
*/
public void declareGroup(Group group, StringBuilder sb)
{
- if (!declaredGroups.containsKey(group.getIntakeGroupName()))
+ if (!declaredGroups.contains(group.getIntakeGroupName()))
{
- declaredGroups.put(group.getIntakeGroupName(), null);
+ declaredGroups.add(group.getIntakeGroupName());
sb.append("<input type=\"hidden\" name=\"")
.append(INTAKE_GRP)
.append("\" value=\"")
@@ -302,24 +301,6 @@ public class IntakeTool
Group g = null;
String inputKey = intakeService.getGroupKey(groupName) + key;
-// g = groups.computeIfAbsent(inputKey, k -> {
-// if (create)
-// {
-// try
-// {
-// Group gg = intakeService.getGroup(groupName);
-// gg.init(key, pp);
-// return gg;
-// }
-// catch (IntakeException e)
-// {
-// // TODO Auto-generated catch block
-// e.printStackTrace();
-// }
-// }
-//
-// return null;
-// });
if (groups.containsKey(inputKey))
{
g = groups.get(inputKey);
@@ -342,30 +323,23 @@ public class IntakeTool
*/
public Group mapTo(Retrievable obj)
{
- Group g = null;
-
- try
- {
- String inputKey = intakeService.getGroupKey(groupName)
- + obj.getQueryKey();
- if (groups.containsKey(inputKey))
+ String inputKey = intakeService.getGroupKey(groupName)
+ + obj.getQueryKey();
+ Group g = groups.computeIfAbsent(inputKey, k -> {
+ try
{
- g = groups.get(inputKey);
+ Group gg = intakeService.getGroup(groupName);
+ return gg.init(obj);
}
- else
+ catch (IntakeException e)
{
- g = intakeService.getGroup(groupName);
- groups.put(inputKey, g);
+ log.error(e);
}
- return g.init(obj);
- }
- catch (IntakeException e)
- {
- log.error(e);
- }
+ return null;
+ });
- return null;
+ return g;
}
}
@@ -401,10 +375,13 @@ public class IntakeTool
public boolean isAllValid()
{
boolean allValid = true;
- // TODO: fail fast?
for (Group group : groups.values())
{
allValid &= group.isAllValid();
+ if (!allValid)
+ {
+ break;
+ }
}
return allValid;
}
@@ -466,7 +443,7 @@ public class IntakeTool
if (groupKeys != null)
{
- Arrays.stream(groupKeys)
+ Stream.of(groupKeys)
.filter(groupKey ->
!groupKey.equals(group.getGID()))
.forEach(groupKey -> pp.add(INTAKE_GRP,
groupKey));
}
diff --git a/src/test/org/apache/turbine/services/intake/IntakeToolTest.java
b/src/test/org/apache/turbine/services/intake/IntakeToolTest.java
index 8e8192b9..27efa0fd 100644
--- a/src/test/org/apache/turbine/services/intake/IntakeToolTest.java
+++ b/src/test/org/apache/turbine/services/intake/IntakeToolTest.java
@@ -21,21 +21,26 @@ package org.apache.turbine.services.intake;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.mockito.Mockito.mock;
import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
-import org.apache.fulcrum.intake.IntakeService;
import org.apache.fulcrum.intake.model.Group;
import org.apache.fulcrum.parser.DefaultParameterParser;
-import org.apache.turbine.annotation.AnnotationProcessor;
+import org.apache.turbine.TurbineConstants;
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;
import org.apache.turbine.util.TurbineConfig;
+import org.apache.velocity.context.Context;
+import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -49,19 +54,31 @@ import jakarta.servlet.http.HttpServletResponse;
* Unit test for Intake Tool, wrapping the Fulcrum Intake service.
*
* @author <a href="mailto:[email protected]">Eric Pugh</a>
- * @version $Id$
*/
public class IntakeToolTest extends BaseTestCase
{
private static TurbineConfig tc = null;
+ private static PullService pullService = null;
+ private Context context;
private IntakeTool intakeTool;
@Before
public void initTool() throws Exception
{
- intakeTool = new IntakeTool();
- AnnotationProcessor.process(intakeTool);
- intakeTool.init(getRunData());
+ context = pullService.getGlobalContext();
+ assertNotNull(context);
+
+ pullService.populateContext(context, getRunData());
+ intakeTool = (IntakeTool) context.get("intake");
+ }
+
+ @After
+ public void recycle() throws Exception
+ {
+ pullService.releaseTools(context);
+ assertTrue(intakeTool.isDisposed());
+ assertTrue(intakeTool.getGroups().isEmpty());
+ assertNull(intakeTool.pp);
}
@Test
@@ -82,9 +99,9 @@ public class IntakeToolTest extends BaseTestCase
assertTrue("Make sure serialized data file exists:" + file,
file.exists());
Group group = intakeTool.get("LoginGroup", "loginGroupKey");
assertNotNull(group);
- assertEquals(1, intakeTool.groups.size());
+ assertEquals(1, intakeTool.getGroups().size());
intakeTool.remove(group);
- assertTrue(intakeTool.groups.isEmpty());
+ assertTrue(intakeTool.getGroups().isEmpty());
}
/**
@@ -115,9 +132,15 @@ public class IntakeToolTest extends BaseTestCase
@BeforeClass
public static void setUp() throws Exception
{
- tc = new TurbineConfig(".",
"/conf/test/TestFulcrumComponents.properties");
+ Map<String, String> initParams = new HashMap<>();
+ initParams.put(TurbineConfig.PROPERTIES_PATH_KEY,
"/conf/test/CompleteTurbineResources.properties"); //
"conf/test/TurbineResources.properties"
+ initParams.put(TurbineConstants.LOGGING_ROOT_KEY, "target/test-logs");
+
+ tc = new TurbineConfig(".", initParams);
tc.initialize();
-
TurbineServices.getInstance().getService(IntakeService.class.getName());
+
+ pullService =
(PullService)TurbineServices.getInstance().getService(PullService.SERVICE_NAME);
+ assertNotNull(pullService);
}
@AfterClass