Skip to content

Add unit tests for execution package to achieve 100% coverage #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Scanner Core - A Modular Framework for Probe Definition, Execution, and Result Analysis.
*
* Copyright 2017-2023 Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH
*
* Licensed under Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0.txt
*/
package de.rub.nds.scanner.core.execution;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;

public class NamedThreadFactoryTest {

@Test
public void testNewThreadWithPrefix() {
String prefix = "TestThread";
NamedThreadFactory factory = new NamedThreadFactory(prefix);

Runnable runnable = () -> {};
Thread thread = factory.newThread(runnable);

assertNotNull(thread);
assertEquals(prefix + "-1", thread.getName());
}

@Test
public void testMultipleThreadsWithIncrementingNumbers() {
String prefix = "Worker";
NamedThreadFactory factory = new NamedThreadFactory(prefix);

Thread thread1 = factory.newThread(() -> {});
Thread thread2 = factory.newThread(() -> {});
Thread thread3 = factory.newThread(() -> {});

assertEquals(prefix + "-1", thread1.getName());
assertEquals(prefix + "-2", thread2.getName());
assertEquals(prefix + "-3", thread3.getName());
}

@Test
public void testThreadsAreNotDaemon() {
NamedThreadFactory factory = new NamedThreadFactory("Test");
Thread thread = factory.newThread(() -> {});

// Default thread factory creates non-daemon threads
assertTrue(!thread.isDaemon());
}

@Test
public void testThreadsWithDifferentPrefixes() {
NamedThreadFactory factory1 = new NamedThreadFactory("Factory1");
NamedThreadFactory factory2 = new NamedThreadFactory("Factory2");

Thread thread1 = factory1.newThread(() -> {});
Thread thread2 = factory2.newThread(() -> {});

assertEquals("Factory1-1", thread1.getName());
assertEquals("Factory2-1", thread2.getName());
}

@Test
public void testThreadExecutesRunnable() throws InterruptedException {
NamedThreadFactory factory = new NamedThreadFactory("Executor");
AtomicInteger counter = new AtomicInteger(0);

Runnable runnable =
() -> {
counter.incrementAndGet();
};

Thread thread = factory.newThread(runnable);
thread.start();
thread.join();

assertEquals(1, counter.get());
}

@Test
public void testEmptyPrefix() {
NamedThreadFactory factory = new NamedThreadFactory("");
Thread thread = factory.newThread(() -> {});

assertEquals("-1", thread.getName());
}

@Test
public void testSpecialCharactersInPrefix() {
String prefix = "Test-Thread_123@#$";
NamedThreadFactory factory = new NamedThreadFactory(prefix);
Thread thread = factory.newThread(() -> {});

assertEquals(prefix + "-1", thread.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Scanner Core - A Modular Framework for Probe Definition, Execution, and Result Analysis.
*
* Copyright 2017-2023 Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH
*
* Licensed under Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0.txt
*/
package de.rub.nds.scanner.core.execution;

import static org.junit.jupiter.api.Assertions.*;

import de.rub.nds.scanner.core.report.ScanReport;
import org.junit.jupiter.api.Test;

public class ScanJobExecutorTest {

// Mock ScanReport for testing
static class TestReport extends ScanReport {
private boolean executed = false;

@Override
public String getRemoteName() {
return "TestHost";
}

@Override
public void serializeToJson(java.io.OutputStream outputStream) {
// Simple implementation for testing
}

public boolean isExecuted() {
return executed;
}

public void setExecuted(boolean executed) {
this.executed = executed;
}
}

// Test implementation of ScanJobExecutor
static class TestScanJobExecutor extends ScanJobExecutor<TestReport> {
private boolean shutdownCalled = false;
private int executeCallCount = 0;

@Override
public void execute(TestReport report) throws InterruptedException {
executeCallCount++;
report.setExecuted(true);
}

@Override
public void shutdown() {
shutdownCalled = true;
}

public boolean isShutdownCalled() {
return shutdownCalled;
}

public int getExecuteCallCount() {
return executeCallCount;
}
}

// Test implementation that throws InterruptedException
static class InterruptingExecutor extends ScanJobExecutor<TestReport> {
@Override
public void execute(TestReport report) throws InterruptedException {
throw new InterruptedException("Test interruption");
}

@Override
public void shutdown() {}
}

@Test
public void testExecuteMethod() throws InterruptedException {
TestScanJobExecutor executor = new TestScanJobExecutor();
TestReport report = new TestReport();

assertFalse(report.isExecuted());
executor.execute(report);

assertTrue(report.isExecuted());
assertEquals(1, executor.getExecuteCallCount());
}

@Test
public void testShutdownMethod() {
TestScanJobExecutor executor = new TestScanJobExecutor();

assertFalse(executor.isShutdownCalled());
executor.shutdown();
assertTrue(executor.isShutdownCalled());
}

@Test
public void testMultipleExecuteCalls() throws InterruptedException {
TestScanJobExecutor executor = new TestScanJobExecutor();
TestReport report1 = new TestReport();
TestReport report2 = new TestReport();
TestReport report3 = new TestReport();

executor.execute(report1);
executor.execute(report2);
executor.execute(report3);

assertEquals(3, executor.getExecuteCallCount());
assertTrue(report1.isExecuted());
assertTrue(report2.isExecuted());
assertTrue(report3.isExecuted());
}

@Test
public void testExecuteThrowsInterruptedException() {
InterruptingExecutor executor = new InterruptingExecutor();
TestReport report = new TestReport();

assertThrows(InterruptedException.class, () -> executor.execute(report));
}

@Test
public void testAbstractClass() {
// Verify that ScanJobExecutor is abstract and cannot be instantiated directly
assertTrue(java.lang.reflect.Modifier.isAbstract(ScanJobExecutor.class.getModifiers()));
}

@Test
public void testExecuteMethodSignature() throws NoSuchMethodException {
// Verify the execute method signature
var method = ScanJobExecutor.class.getDeclaredMethod("execute", ScanReport.class);
assertTrue(java.lang.reflect.Modifier.isAbstract(method.getModifiers()));
assertEquals(void.class, method.getReturnType());

// Check that it declares InterruptedException
Class<?>[] exceptionTypes = method.getExceptionTypes();
assertEquals(1, exceptionTypes.length);
assertEquals(InterruptedException.class, exceptionTypes[0]);
}

@Test
public void testShutdownMethodSignature() throws NoSuchMethodException {
// Verify the shutdown method signature
var method = ScanJobExecutor.class.getDeclaredMethod("shutdown");
assertTrue(java.lang.reflect.Modifier.isAbstract(method.getModifiers()));
assertEquals(void.class, method.getReturnType());
assertEquals(0, method.getExceptionTypes().length);
}
}
Loading