Skip to content
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

qa: fix typehandlerlibrary spotbugs findings #5154

Merged
merged 5 commits into from
Nov 18, 2023

Conversation

soloturn
Copy link
Contributor

when building with java-11 it ends with:

❯ cd subsystems/TypeHandlerLibrary; gradle clean build
...
> Task :subsystems:TypeHandlerLibrary:spotbugsTest
M B PI: The used identifier ?>?2/2??? as field name in class org.terasology.persistence.typeHandling.InMemorySerializerTest$TypeGetter.STRING in source file ?>?3/2??? shadows the publicly available identifier from the Java Standard Library.  In InMemorySerializerTest.java
M C EC: Using .equals to compare two byte[]'s, (equivalent to ==) in org.terasology.persistence.typeHandling.coreTypes.factories.BytesTypeHandlerTest.byteArraySerializeDeserialize()  At BytesTypeHandlerTest.java:[line 42]
M C EC: Using .equals to compare two byte[]'s, (equivalent to ==) in org.terasology.persistence.typeHandling.coreTypes.factories.BytesTypeHandlerTest.byteArraySerializeDeserialize()  At BytesTypeHandlerTest.java:[line 45]
M C EC: Using .equals to compare two byte[]'s, (equivalent to ==) in org.terasology.persistence.typeHandling.InMemorySerializerTest.serializeBytes()  At InMemorySerializerTest.java:[line 309]
M C EC: Using .equals to compare two byte[]'s, (equivalent to ==) in org.terasology.persistence.typeHandling.InMemorySerializerTest.serializeByteBuffer()  At InMemorySerializerTest.java:[line 338]
H C RV: org.junit.jupiter.api.Assertions.assertThrows(Class, Executable) not thrown in org.terasology.persistence.typeHandling.InMemorySerializerTest.serializeString()  At InMemorySerializerTest.java:[line 82]
H C RV: org.junit.jupiter.api.Assertions.assertThrows(Class, Executable) not thrown in org.terasology.persistence.typeHandling.InMemorySerializerTest.serializeStrings()  At InMemorySerializerTest.java:[line 113]
H C RV: org.junit.jupiter.api.Assertions.assertThrows(Class, Executable) not thrown in org.terasology.persistence.typeHandling.InMemorySerializerTest.template(PersistedData)  At InMemorySerializerTest.java:[line 136]
H C RV: org.junit.jupiter.api.Assertions.assertThrows(Class, Executable) not thrown in org.terasology.persistence.typeHandling.InMemorySerializerTest.serializeBoolean()  At InMemorySerializerTest.java:[line 255]
H C RV: org.junit.jupiter.api.Assertions.assertThrows(Class, Executable) not thrown in org.terasology.persistence.typeHandling.InMemorySerializerTest.serializeBytes()  At InMemorySerializerTest.java:[line 319]
H C RV: org.junit.jupiter.api.Assertions.assertThrows(Class, Executable) not thrown in org.terasology.persistence.typeHandling.InMemorySerializerTest.serializeByteBuffer()  At InMemorySerializerTest.java:[line 348]
H C RV: org.junit.jupiter.api.Assertions.assertThrows(Class, Executable) not thrown in org.terasology.persistence.typeHandling.InMemorySerializerTest.serializeNull()  At InMemorySerializerTest.java:[line 392]
H C RV: org.junit.jupiter.api.Assertions.assertThrows(Class, Executable) not thrown in org.terasology.persistence.typeHandling.InMemorySerializerTest.checkIsArray(PersistedData)  At InMemorySerializerTest.java:[line 434]
H C RV: org.junit.jupiter.api.Assertions.assertThrows(Class, Executable) not thrown in org.terasology.persistence.typeHandling.InMemorySerializerTest.checkIsNumber(PersistedData)  At InMemorySerializerTest.java:[line 458]
H C RV: org.junit.jupiter.api.Assertions.assertThrows(Class, Executable) not thrown in org.terasology.persistence.typeHandling.InMemorySerializerTest.checkValueArray(PersistedData, PersistedData, Set)  At InMemorySerializerTest.java:[line 486]
H C RV: org.junit.jupiter.api.Assertions.assertThrows(Class, Executable) not thrown in org.terasology.persistence.typeHandling.InMemorySerializerTest.lambda$checkValueArray$8(Executable)  At InMemorySerializerTest.java:[line 512]
H C RV: org.junit.jupiter.api.Assertions.assertThrows(Class, Executable) not thrown in org.terasology.persistence.typeHandling.InMemorySerializerTest.lambda$checkValueArray$4(Executable)  At InMemorySerializerTest.java:[line 497]
M P UuF: Unused field: org.terasology.persistence.typeHandling.coreTypes.factories.ObjectFieldMapTypeHandlerFactoryTest$SomeClass.t  In ObjectFieldMapTypeHandlerFactoryTest.java
M P SIC: Should org.terasology.persistence.typeHandling.FutureTypeHandlerTest$ResultCaptor be a _static_ inner class?  At FutureTypeHandlerTest.java:[lines 41-50]
M P UuF: Unused field: org.terasology.persistence.typeHandling.coreTypes.factories.ObjectFieldMapTypeHandlerFactoryTest$SomeClass.list  In ObjectFieldMapTypeHandlerFactoryTest.java
SpotBugs ended with exit code 1

> Task :subsystems:TypeHandlerLibrary:spotbugsMain
SpotBugs ended with exit code 1

see also #3859

@soloturn soloturn force-pushed the typehandlerlibrary-qa branch 10 times, most recently from 1de72e9 to 655a6f4 Compare October 29, 2023 19:00
Comment on lines 41 to 43
// spotbugs does thinks Assertions.assertThrows does not throw the exception, even if
// test is successful. see bug: https://github.com/spotbugs/spotbugs/issues/2667.
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to be fixed with spotbugs/spotbugs#2648 but not released yet
we don't fail for spotbugs findings at the moment, so I would not suppress this now but wait for the spotbugs patch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree and took it out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new spotbug version now included, warnings are gone.

Comment on lines 23 to 26
// both, pmd and spotbugs complain about not used private fields, suppress in
// the static test class, but fields are checked. suppress.
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings
@SuppressWarnings("PMD.UnusedPrivateField")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure we want to suppress this as it might be a valid indicator of improvement potential. Either the private fields are indeed unused then this begs the question why they're in here in the first place. And if they are not explicitly used but implicitly required for the tests, then I would argue that this asks for refactoring due to nontransparent side effects.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is "just" a type handler test and checks the type? to test nothing needs to be implemented using the fields.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to test nothing needs to be implemented using the fields.

Sorry, you'll have to elaborate on this, I don't follow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved the suppressions to the class which really causes it, when i tried that initially it failed, but cannot reproduce how. its a minimal test case, no point in using these private fields other than by typehandler.

@jdrueckert jdrueckert added this to the 2023 Revive - Milestone 2 milestone Oct 30, 2023
soloturn added a commit to soloturn/teraconfig that referenced this pull request Nov 1, 2023
jdrueckert commented on pr: MovingBlocks/Terasology#5154
that the rule is not really beneficial and better remove the rule than suppress
it.
@soloturn
Copy link
Contributor Author

soloturn commented Nov 5, 2023

@jdrueckert anything else missing?

@soloturn soloturn force-pushed the typehandlerlibrary-qa branch 3 times, most recently from af320a2 to a49261d Compare November 8, 2023 07:18
}
}

// the test verfies that bot, static class (above), and class work. with this typehandler.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spotbugs complains about the inner class ResultCaptor not being static. I'm not convinced, that the statement "the test verfies that bot, static class (above), and class work. with this typehandler." is correct. The test verifies that for recursive types, the expected typehandler (FutureTypeHandler) is fetched. For this, it uses ResultCaptor as a mock to first capture the result and later assert on it.

The reason why it doesn't work making the inner ResultCaptor class static is that this mock is not a static mock - it doesn't return a pre-defined value but executes the "real" method and captures its result.

Furthermore, the reason why Spotbugs complains about this is that the inner class not being static may result in performance issues due to increased instance sizes and object lifetimes. However, as this is a test, we don't care as much about performance.

@soloturn soloturn force-pushed the typehandlerlibrary-qa branch 6 times, most recently from 511dbe2 to 0263c33 Compare November 11, 2023 18:51
@soloturn soloturn force-pushed the typehandlerlibrary-qa branch 3 times, most recently from 842db88 to 4ab80e5 Compare November 17, 2023 08:08
soloturn and others added 4 commits November 17, 2023 21:42
as a workaround, add dependency for spotbugs annotations, and create a
[ticket there so the spotbugs gradle plugin would include the dependency]
(spotbugs/spotbugs-gradle-plugin#1018).

see here a motivation for inner type last rule. if we leave the rule we
should respect it, otherwise disable the rule.
https://stackoverflow.com/questions/25158939/what-motivation-is-behind-checkstyle-inner-type-last-rule

especially in tests just quite the warnings, in most cases the code is
deliberate as it is.

see MovingBlocks#3859 and see bugs:
* pmd/pmd#4731
* spotbugs/spotbugs-gradle-plugin#1018
* spotbugs/spotbugs#2667
now AssertThrows error is fixed:
spotbugs/spotbugs#2667
especially take out pmd guardlog suppresses. fix part of them
in a catch even if not necessary from a performance point of
view. and create a pull request towards teraconfig to not
use the guardlog rule any more:
MovingBlocks/TeraConfig#23

Co-authored-by: jdrueckert <jd.rueckert@googlemail.com>
@jdrueckert jdrueckert changed the title typehandlerlibrary qa, spotbugs qa: fix typehandlerlibrary spotbugs findings Nov 18, 2023
@jdrueckert jdrueckert merged commit 8b4b6b9 into MovingBlocks:develop Nov 18, 2023
9 checks passed
@soloturn soloturn deleted the typehandlerlibrary-qa branch December 3, 2023 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants