Skip to content

Fix questionable cast to abstract collection in ListResult and SetResult #138

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 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
*/
public class ListResult<T> extends CollectionResult<T> {

private final List<T> list;

@SuppressWarnings("unused")
// Default constructor for deserialization
private ListResult() {
// Default constructor for deserialization
super(null, null);
this.list = null;
}

/**
Expand All @@ -33,12 +36,13 @@
*/
public ListResult(AnalyzedProperty property, List<T> list) {
super(property, list);
this.list = list;
}

/**
* @return the list of the listResult object.

Check warning on line 43 in src/main/java/de/rub/nds/scanner/core/probe/result/ListResult.java

View check run for this annotation

Jenkins CI - TLS-Attacker / JavaDoc

-

NORMAL: no main description
*/
public List<T> getList() {
return (List<T>) collection;
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
*/
public class SetResult<T> extends CollectionResult<T> {

private final Set<T> set;

@SuppressWarnings("unused")
private SetResult() {
// Default constructor for deserialization
super(null, null);
this.set = null;
}

/**
Expand All @@ -32,12 +35,13 @@ private SetResult() {
*/
public SetResult(AnalyzedProperty property, Set<T> set) {
super(property, set);
this.set = set;
}

/**
* @return The set of the SetResult.
*/
public Set<T> getSet() {
return (Set<T>) collection;
return set;
}
}
102 changes: 102 additions & 0 deletions src/test/java/de/rub/nds/scanner/core/probe/result/ListResultTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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.probe.result;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;

import de.rub.nds.scanner.core.probe.AnalyzedProperty;
import de.rub.nds.scanner.core.probe.AnalyzedPropertyCategory;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;

class ListResultTest {

@Test
void testConstructor() {
AnalyzedProperty property = new TestAnalyzedProperty();
List<String> testList = new ArrayList<>();
testList.add("test1");
testList.add("test2");

ListResult<String> result = new ListResult<>(property, testList);

assertNotNull(result);
assertEquals(property, result.getProperty());
assertNotNull(result.getList());
assertEquals(2, result.getList().size());
assertEquals("test1", result.getList().get(0));
assertEquals("test2", result.getList().get(1));
}

@Test
void testGetListReturnsSameInstance() {
AnalyzedProperty property = new TestAnalyzedProperty();
List<Integer> testList = new ArrayList<>();
testList.add(1);
testList.add(2);

ListResult<Integer> result = new ListResult<>(property, testList);

List<Integer> returnedList = result.getList();
assertSame(testList, returnedList);
}

@Test
void testGetCollectionReturnsCorrectCollection() {
AnalyzedProperty property = new TestAnalyzedProperty();
List<String> testList = new ArrayList<>();
testList.add("a");
testList.add("b");

ListResult<String> result = new ListResult<>(property, testList);

assertEquals(testList, result.getCollection());
}

@Test
void testEmptyList() {
AnalyzedProperty property = new TestAnalyzedProperty();
List<String> emptyList = new ArrayList<>();

ListResult<String> result = new ListResult<>(property, emptyList);

assertNotNull(result.getList());
assertEquals(0, result.getList().size());
}

@Test
void testNullList() {
AnalyzedProperty property = new TestAnalyzedProperty();

ListResult<String> result = new ListResult<>(property, null);

assertNull(result.getList());
assertNull(result.getCollection());
}

// Test implementation of AnalyzedProperty for testing
private static class TestAnalyzedProperty implements AnalyzedProperty {
@Override
public String getName() {
return "TestProperty";
}

@Override
public AnalyzedPropertyCategory getCategory() {
return new TestAnalyzedPropertyCategory();
}
}

// Test implementation of AnalyzedPropertyCategory for testing
private static class TestAnalyzedPropertyCategory implements AnalyzedPropertyCategory {}
}
118 changes: 118 additions & 0 deletions src/test/java/de/rub/nds/scanner/core/probe/result/SetResultTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* 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.probe.result;

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

import de.rub.nds.scanner.core.probe.AnalyzedProperty;
import de.rub.nds.scanner.core.probe.AnalyzedPropertyCategory;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.Test;

class SetResultTest {

@Test
void testConstructor() {
AnalyzedProperty property = new TestAnalyzedProperty();
Set<String> testSet = new HashSet<>();
testSet.add("test1");
testSet.add("test2");

SetResult<String> result = new SetResult<>(property, testSet);

assertNotNull(result);
assertEquals(property, result.getProperty());
assertNotNull(result.getSet());
assertEquals(2, result.getSet().size());
assertTrue(result.getSet().contains("test1"));
assertTrue(result.getSet().contains("test2"));
}

@Test
void testGetSetReturnsSameInstance() {
AnalyzedProperty property = new TestAnalyzedProperty();
Set<Integer> testSet = new HashSet<>();
testSet.add(1);
testSet.add(2);

SetResult<Integer> result = new SetResult<>(property, testSet);

Set<Integer> returnedSet = result.getSet();
assertSame(testSet, returnedSet);
}

@Test
void testGetCollectionReturnsCorrectCollection() {
AnalyzedProperty property = new TestAnalyzedProperty();
Set<String> testSet = new HashSet<>();
testSet.add("a");
testSet.add("b");

SetResult<String> result = new SetResult<>(property, testSet);

assertEquals(testSet, result.getCollection());
}

@Test
void testEmptySet() {
AnalyzedProperty property = new TestAnalyzedProperty();
Set<String> emptySet = new HashSet<>();

SetResult<String> result = new SetResult<>(property, emptySet);

assertNotNull(result.getSet());
assertEquals(0, result.getSet().size());
}

@Test
void testNullSet() {
AnalyzedProperty property = new TestAnalyzedProperty();

SetResult<String> result = new SetResult<>(property, null);

assertNull(result.getSet());
assertNull(result.getCollection());
}

@Test
void testSetUniqueness() {
AnalyzedProperty property = new TestAnalyzedProperty();
Set<String> testSet = new HashSet<>();
testSet.add("duplicate");
testSet.add("duplicate");
testSet.add("unique");

SetResult<String> result = new SetResult<>(property, testSet);

assertEquals(2, result.getSet().size());
assertTrue(result.getSet().contains("duplicate"));
assertTrue(result.getSet().contains("unique"));
}

// Test implementation of AnalyzedProperty for testing
private static class TestAnalyzedProperty implements AnalyzedProperty {
@Override
public String getName() {
return "TestProperty";
}

@Override
public AnalyzedPropertyCategory getCategory() {
return new TestAnalyzedPropertyCategory();
}
}

// Test implementation of AnalyzedPropertyCategory for testing
private static class TestAnalyzedPropertyCategory implements AnalyzedPropertyCategory {}
}
Loading