Skip to content
Permalink
Browse files
Use ArrayList instead of LinkedList
it is considered to be faster in almost all cases.

While we are here
 * Suppress a warning about catching and printing an exception
 * Add override annotations to anonymous classes methods
 * convert to junit5 methods
 * shorten lines to less than 120 chars
 * introduce a private helper to make params (a real builder/DSL would
   be even better)
 * introduce a helper method to assert a range check
  • Loading branch information
FSchumacher committed Apr 18, 2022
1 parent d27bc1f commit cf4ae3281651eabf5d01a55dcd4822793afc74a4
Showing 25 changed files with 365 additions and 361 deletions.
@@ -23,8 +23,8 @@
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
@@ -40,6 +40,7 @@
@Execution(ExecutionMode.SAME_THREAD) // System.setOut must not be run concurrently with other tests
public class DistributedRunnerTest {

@SuppressWarnings("CatchAndPrintStackTrace")
public static void createJmeterEnv() {
File propsFile;
try {
@@ -127,7 +128,7 @@ public void testFailure3() {
}

private static class DistributedRunnerEmul extends DistributedRunner {
public List<EmulatorEngine> engines = new LinkedList<>();
public List<EmulatorEngine> engines = new ArrayList<>();

@Override
protected JMeterEngine createEngine(String address) {
@@ -19,8 +19,8 @@

import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.apache.logging.log4j.Level;
@@ -43,7 +43,7 @@

public class TestGuiLogEventAppender {

private static List<String> log4j2LevelErrorMessages = Collections.synchronizedList(new LinkedList<>());
private static List<String> log4j2LevelErrorMessages = Collections.synchronizedList(new ArrayList<>());

/*
* Configure logging with GuiLogEventAppender for root logger, and override the handler of GuiLogEventAppender
@@ -74,14 +74,17 @@ public static void beforeClass() {

guiLogEventAppender.stop();
guiLogEventAppender.setHandler(new ErrorHandler() {
@Override
public void error(String msg) {
log4j2LevelErrorMessages.add(msg);
}

@Override
public void error(String msg, Throwable t) {
log4j2LevelErrorMessages.add(msg + " " + t);
}

@Override
public void error(String msg, LogEvent event, Throwable t) {
log4j2LevelErrorMessages.add(msg + " " + t);
}
@@ -17,9 +17,9 @@

package org.apache.jmeter.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.slf4j.Logger;
@@ -30,7 +30,7 @@
*/
public class LogRecordingDelegatingLogger implements Logger {

private List<LogRecord> logRecords = Collections.synchronizedList(new LinkedList<>());
private List<LogRecord> logRecords = Collections.synchronizedList(new ArrayList<>());

private Logger delegate;

@@ -30,10 +30,10 @@
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -456,7 +456,7 @@ public static Collection<Object> getObjects(Class<?> extendsClass) throws Except
Object myThis = "";
Iterator<String> classes = ClassFinder
.findClassesThatExtend(JMeterUtils.getSearchPaths(), new Class[] { extendsClass }).iterator();
List<Object> objects = new LinkedList<>();
List<Object> objects = new ArrayList<>();
String n = "";
boolean caughtError = true;
Throwable caught = null;
@@ -21,8 +21,8 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;

import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.junit.JMeterTestCase;
@@ -159,7 +159,7 @@ public void shouldSplitWithPreviousResultOnly() throws Exception {
// Create the SplitFile function and set its parameters.
private static SplitFunction splitParams(String p1, String p2, String p3) throws Exception {
SplitFunction split = new SplitFunction();
Collection<CompoundVariable> parms = new LinkedList<>();
Collection<CompoundVariable> parms = new ArrayList<>();
parms.add(new CompoundVariable(p1));
if (p2 != null) {
parms.add(new CompoundVariable(p2));
@@ -17,34 +17,34 @@

package org.apache.jmeter.functions;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.stream.Collectors;

import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.junit.JMeterTestCase;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class SumFunctionTest extends JMeterTestCase {
class SumFunctionTest extends JMeterTestCase {

private JMeterVariables vars = null;

@BeforeEach
public void setUp() {
void setUp() {
JMeterContext jmctx = JMeterContextService.getContext();
jmctx.setVariables(new JMeterVariables());
vars = jmctx.getVariables();
}

@Test
public void sumTest() throws Exception {
void sumTest() throws Exception {
String maxIntVal = Integer.toString(Integer.MAX_VALUE);
String minIntVal = Integer.toString(Integer.MIN_VALUE);

@@ -78,19 +78,19 @@ public void sumTest() throws Exception {
private void checkSum(AbstractFunction func, String value, String[] addends) throws Exception {
Collection<CompoundVariable> parms = Arrays.stream(addends)
.map(CompoundVariable::new)
.collect(Collectors.toCollection(LinkedList::new));
.collect(Collectors.toCollection(ArrayList::new));
parms.add(new CompoundVariable("Result"));
func.setParameters(parms);
assertEquals(value, func.execute(null,null));
assertEquals(value, vars.getObject("Result"));
Assertions.assertEquals(value, func.execute(null,null));
Assertions.assertEquals(value, vars.getObject("Result"));
}

// Perform a sum and check the results
private void checkSumNoVar(AbstractFunction func, String value, String[] addends) throws Exception {
Collection<CompoundVariable> parms = Arrays.stream(addends)
.map(CompoundVariable::new)
.collect(Collectors.toCollection(LinkedList::new));
.collect(Collectors.toCollection(ArrayList::new));
func.setParameters(parms);
assertEquals(value,func.execute(null,null));
Assertions.assertEquals(value, func.execute(null,null));
}
}
@@ -20,8 +20,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@@ -93,7 +93,7 @@ public void testChangeCaseCapitalize() throws Exception {
public void testChangeCaseError() throws Exception {
assertThrows(
InvalidVariableException.class,
() -> changeCase.setParameters(new LinkedList<>()));
() -> changeCase.setParameters(new ArrayList<>()));
}

@Test
@@ -17,11 +17,10 @@

package org.apache.jmeter.functions;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.TimeZone;

import org.apache.jmeter.engine.util.CompoundVariable;
@@ -31,14 +30,15 @@
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.jorphan.test.JMeterSerialTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Test {@link DateTimeConvertFunction}
* We implement JMeterSerialTest as we change TimeZone
*/
public class TestDateTimeConvertFunction extends JMeterTestCase implements JMeterSerialTest {
class TestDateTimeConvertFunction extends JMeterTestCase implements JMeterSerialTest {

private AbstractFunction dateConvert;
private SampleResult result;
@@ -47,7 +47,7 @@ public class TestDateTimeConvertFunction extends JMeterTestCase implements JMete
private JMeterContext jmctx;

@BeforeEach
public void setUp() {
void setUp() {
dateConvert = new DateTimeConvertFunction();
result = new SampleResult();
jmctx = JMeterContextService.getContext();
@@ -56,7 +56,7 @@ public void setUp() {
vars = new JMeterVariables();
jmctx.setVariables(vars);
jmctx.setPreviousResult(result);
params = new LinkedList<>();
params = new ArrayList<>();
}

@Test
@@ -65,51 +65,51 @@ public void testParameterCount512() throws Exception {
}

@Test
public void testDateTimeConvert() throws Exception {
void testDateTimeConvert() throws Exception {
params.add(new CompoundVariable("2017-01-02 21:00:21"));
params.add(new CompoundVariable("yyyy-MM-dd HH:mm:ss"));
params.add(new CompoundVariable("dd-MM-yyyy hh:mm"));
dateConvert.setParameters(params);
String returnValue = dateConvert.execute(result, null);
assertEquals("02-01-2017 09:00", returnValue);
Assertions.assertEquals("02-01-2017 09:00", returnValue);
}

@Test
public void testDateTimeConvertEpochTime() throws Exception {
void testDateTimeConvertEpochTime() throws Exception {
TimeZone initialTZ = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
params.add(new CompoundVariable("1526574881000"));
params.add(new CompoundVariable(""));
params.add(new CompoundVariable("dd/MM/yyyy HH:mm"));
dateConvert.setParameters(params);
String returnValue = dateConvert.execute(result, null);
assertEquals("17/05/2018 16:34", returnValue);
Assertions.assertEquals("17/05/2018 16:34", returnValue);
TimeZone.setDefault(initialTZ);
}

@Test
public void testDateConvert() throws Exception {
void testDateConvert() throws Exception {
params.add(new CompoundVariable("2017-01-02"));
params.add(new CompoundVariable("yyyy-MM-dd"));
params.add(new CompoundVariable("dd-MM-yyyy"));
dateConvert.setParameters(params);
String returnValue = dateConvert.execute(result, null);
assertEquals("02-01-2017", returnValue);
Assertions.assertEquals("02-01-2017", returnValue);
}

@Test
public void testDateConvertWithVariable() throws Exception {
void testDateConvertWithVariable() throws Exception {
params.add(new CompoundVariable("2017-01-02"));
params.add(new CompoundVariable("yyyy-MM-dd"));
params.add(new CompoundVariable("dd-MM-yyyy"));
params.add(new CompoundVariable("varName"));
dateConvert.setParameters(params);
dateConvert.execute(result, null);
assertEquals("02-01-2017", vars.get("varName"));
Assertions.assertEquals("02-01-2017", vars.get("varName"));
}

@Test
public void testDateConvertError() throws Exception {
void testDateConvertError() throws Exception {
params.add(new CompoundVariable("2017-01-02"));
params.add(new CompoundVariable("yyyy-MM-dd"));
assertThrows(
@@ -118,20 +118,20 @@ public void testDateConvertError() throws Exception {
}

@Test
public void testDateConvertErrorFormat() throws Exception {
void testDateConvertErrorFormat() throws Exception {
params.add(new CompoundVariable("2017-01-02"));
params.add(new CompoundVariable("yyyy-MM-dd"));
params.add(new CompoundVariable("abcd"));
dateConvert.setParameters(params);
assertEquals(dateConvert.execute(result, null), "");
Assertions.assertEquals(dateConvert.execute(result, null), "");
}

@Test
public void testDateConvertDateError() throws Exception {
void testDateConvertDateError() throws Exception {
params.add(new CompoundVariable("a2017-01-02"));
params.add(new CompoundVariable("yyyy-MM-dd"));
params.add(new CompoundVariable("dd-MM-yyyy HH:mm:ss"));
dateConvert.setParameters(params);
assertEquals(dateConvert.execute(result, null), "");
Assertions.assertEquals(dateConvert.execute(result, null), "");
}
}

0 comments on commit cf4ae32

Please sign in to comment.