Skip to content

Commit 40efe6c

Browse files
author
Developer
committed
Fix serialization issues in RatingInfluencers and Recommendations
- Made RatingInfluencer and Recommendation classes implement Serializable - Made PropertyResultRatingInfluencer and PropertyResultRecommendation implement Serializable - Made AnalyzedProperty and AnalyzedPropertyCategory interfaces extend Serializable - Added SerializationTest to verify serialization works correctly This fixes SpotBugs warnings about non-transient non-serializable fields in serializable classes.
1 parent d4a3032 commit 40efe6c

File tree

7 files changed

+137
-6
lines changed

7 files changed

+137
-6
lines changed

src/main/java/de/rub/nds/scanner/core/probe/AnalyzedProperty.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
import jakarta.xml.bind.annotation.XmlAccessType;
1414
import jakarta.xml.bind.annotation.XmlAccessorType;
1515
import jakarta.xml.bind.annotation.XmlRootElement;
16+
import java.io.Serializable;
1617

1718
@XmlRootElement
1819
@XmlAccessorType(XmlAccessType.FIELD)
1920
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
20-
public interface AnalyzedProperty {
21+
public interface AnalyzedProperty extends Serializable {
2122

2223
AnalyzedPropertyCategory getCategory();
2324

src/main/java/de/rub/nds/scanner/core/probe/AnalyzedPropertyCategory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import com.fasterxml.jackson.annotation.JsonTypeInfo;
1212
import jakarta.xml.bind.annotation.XmlAccessType;
1313
import jakarta.xml.bind.annotation.XmlAccessorType;
14+
import java.io.Serializable;
1415

1516
@XmlAccessorType(XmlAccessType.FIELD)
1617
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
17-
public interface AnalyzedPropertyCategory {}
18+
public interface AnalyzedPropertyCategory extends Serializable {}

src/main/java/de/rub/nds/scanner/core/report/rating/PropertyResultRatingInfluencer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import jakarta.xml.bind.annotation.XmlElement;
1919
import jakarta.xml.bind.annotation.XmlRootElement;
2020
import jakarta.xml.bind.annotation.XmlType;
21+
import java.io.Serializable;
2122
import java.util.Objects;
2223

2324
@XmlRootElement
@@ -30,7 +31,8 @@
3031
"referencedProperty",
3132
"referencedPropertyResult"
3233
})
33-
public class PropertyResultRatingInfluencer implements Comparable<PropertyResultRatingInfluencer> {
34+
public class PropertyResultRatingInfluencer
35+
implements Comparable<PropertyResultRatingInfluencer>, Serializable {
3436

3537
@XmlElement(type = TestResults.class, name = "result")
3638
@JsonIgnore

src/main/java/de/rub/nds/scanner/core/report/rating/PropertyResultRecommendation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
import jakarta.xml.bind.annotation.XmlAnyElement;
1616
import jakarta.xml.bind.annotation.XmlRootElement;
1717
import jakarta.xml.bind.annotation.XmlSeeAlso;
18+
import java.io.Serializable;
1819

1920
@XmlRootElement
2021
@XmlSeeAlso({TestResults.class})
2122
@XmlAccessorType(XmlAccessType.FIELD)
22-
public class PropertyResultRecommendation {
23+
public class PropertyResultRecommendation implements Serializable {
2324

2425
@XmlAnyElement(lax = true)
2526
private TestResult result;

src/main/java/de/rub/nds/scanner/core/report/rating/RatingInfluencer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
import de.rub.nds.scanner.core.probe.AnalyzedProperty;
1212
import de.rub.nds.scanner.core.probe.result.TestResult;
1313
import jakarta.xml.bind.annotation.*;
14+
import java.io.Serializable;
1415
import java.util.Arrays;
1516
import java.util.LinkedList;
1617
import java.util.List;
1718

1819
@XmlRootElement
1920
@XmlAccessorType(XmlAccessType.FIELD)
20-
public class RatingInfluencer {
21+
public class RatingInfluencer implements Serializable {
2122

2223
@XmlAnyElement(lax = true)
2324
private AnalyzedProperty analyzedProperty;

src/main/java/de/rub/nds/scanner/core/report/rating/Recommendation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import jakarta.xml.bind.annotation.XmlAnyElement;
1616
import jakarta.xml.bind.annotation.XmlRootElement;
1717
import jakarta.xml.bind.annotation.XmlType;
18+
import java.io.Serializable;
1819
import java.util.Arrays;
1920
import java.util.LinkedList;
2021
import java.util.List;
@@ -31,7 +32,7 @@
3132
"propertyRecommendations"
3233
})
3334
@XmlAccessorType(XmlAccessType.FIELD)
34-
public class Recommendation {
35+
public class Recommendation implements Serializable {
3536

3637
static final String NO_INFORMATION_FOUND = "No detailed information available";
3738

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Scanner Core - A Modular Framework for Probe Definition, Execution, and Result Analysis.
3+
*
4+
* Copyright 2017-2023 Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH
5+
*
6+
* Licensed under Apache License, Version 2.0
7+
* http://www.apache.org/licenses/LICENSE-2.0.txt
8+
*/
9+
package de.rub.nds.scanner.core.report.rating;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertNotNull;
13+
14+
import de.rub.nds.scanner.core.probe.AnalyzedProperty;
15+
import de.rub.nds.scanner.core.probe.AnalyzedPropertyCategory;
16+
import de.rub.nds.scanner.core.probe.result.TestResults;
17+
import java.io.*;
18+
import java.util.Arrays;
19+
import java.util.LinkedList;
20+
import java.util.List;
21+
import org.junit.jupiter.api.Test;
22+
23+
public class SerializationTest {
24+
25+
private static class TestPropertyCategory implements AnalyzedPropertyCategory {
26+
private static final long serialVersionUID = 1L;
27+
}
28+
29+
private static class TestProperty implements AnalyzedProperty {
30+
private static final long serialVersionUID = 1L;
31+
private final String name;
32+
33+
public TestProperty(String name) {
34+
this.name = name;
35+
}
36+
37+
@Override
38+
public AnalyzedPropertyCategory getCategory() {
39+
return new TestPropertyCategory();
40+
}
41+
42+
@Override
43+
public String getName() {
44+
return name;
45+
}
46+
}
47+
48+
@Test
49+
public void testRatingInfluencersSerialization() throws IOException, ClassNotFoundException {
50+
// Create test data
51+
TestProperty property = new TestProperty("TEST_PROPERTY");
52+
PropertyResultRatingInfluencer influencer1 =
53+
new PropertyResultRatingInfluencer(TestResults.TRUE, 10);
54+
PropertyResultRatingInfluencer influencer2 =
55+
new PropertyResultRatingInfluencer(TestResults.FALSE, -5);
56+
List<PropertyResultRatingInfluencer> influencerList =
57+
Arrays.asList(influencer1, influencer2);
58+
59+
RatingInfluencer ratingInfluencer = new RatingInfluencer(property, influencerList);
60+
LinkedList<RatingInfluencer> ratingInfluencersList = new LinkedList<>();
61+
ratingInfluencersList.add(ratingInfluencer);
62+
63+
RatingInfluencers ratingInfluencers = new RatingInfluencers(ratingInfluencersList);
64+
65+
// Serialize
66+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
67+
ObjectOutputStream oos = new ObjectOutputStream(baos);
68+
oos.writeObject(ratingInfluencers);
69+
oos.close();
70+
71+
// Deserialize
72+
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
73+
ObjectInputStream ois = new ObjectInputStream(bais);
74+
RatingInfluencers deserialized = (RatingInfluencers) ois.readObject();
75+
ois.close();
76+
77+
// Verify
78+
assertNotNull(deserialized);
79+
assertNotNull(deserialized.getRatingInfluencers());
80+
assertEquals(1, deserialized.getRatingInfluencers().size());
81+
}
82+
83+
@Test
84+
public void testRecommendationsSerialization() throws IOException, ClassNotFoundException {
85+
// Create test data
86+
TestProperty property = new TestProperty("TEST_PROPERTY");
87+
PropertyResultRecommendation recommendation1 =
88+
new PropertyResultRecommendation(
89+
TestResults.TRUE, "Short desc", "Handling recommendation");
90+
PropertyResultRecommendation recommendation2 =
91+
new PropertyResultRecommendation(
92+
TestResults.FALSE, "Another desc", "Another recommendation");
93+
94+
Recommendation recommendation =
95+
new Recommendation(
96+
property,
97+
"Test Recommendation",
98+
"Short description",
99+
"Detailed description",
100+
recommendation1,
101+
"https://example.com");
102+
recommendation.getPropertyRecommendations().add(recommendation2);
103+
104+
List<Recommendation> recommendationList = Arrays.asList(recommendation);
105+
Recommendations recommendations = new Recommendations(recommendationList);
106+
107+
// Serialize
108+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
109+
ObjectOutputStream oos = new ObjectOutputStream(baos);
110+
oos.writeObject(recommendations);
111+
oos.close();
112+
113+
// Deserialize
114+
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
115+
ObjectInputStream ois = new ObjectInputStream(bais);
116+
Recommendations deserialized = (Recommendations) ois.readObject();
117+
ois.close();
118+
119+
// Verify
120+
assertNotNull(deserialized);
121+
assertNotNull(deserialized.getRecommendations());
122+
assertEquals(1, deserialized.getRecommendations().size());
123+
}
124+
}

0 commit comments

Comments
 (0)