paulrutter commented on code in PR #564:
URL: https://github.com/apache/felix-dev/pull/564#discussion_r4181505129
##########
utils/src/main/java/org/apache/felix/utils/resource/ResourceImpl.java:
##########
@@ -129,7 +141,16 @@ public boolean equals(Object o) {
@Override
public int hashCode() {
- return Objects.hash(caps, reqs);
+ int h = hash;
Review Comment:
The cache is only invalidated in the four add* methods, but `caps` and
`reqs` are protected and `getCapabilities(null)` / `getRequirements(null)`
return the live internal lists. If a subclass or caller mutates them directly
after `hashCode()` has been called, the cached hash no longer matches `equals`
(which still compares the lists), and the resource can get lost in a
HashSet/HashMap. Before this change the hash was always recomputed, so this is
new.
Suggestion: return an unmodifiable view from the no-namespace case, e.g.
`Collections.unmodifiableList(caps)` / `Collections.unmodifiableList(reqs)`, so
the add* methods are the only way to mutate and the invalidation is reliable.
(Subclasses can still reach the protected fields, so those would ideally be
private or documented as not to be mutated directly.)
##########
utils/src/test/java/org/apache/felix/utils/resource/ResourceImplTest.java:
##########
@@ -0,0 +1,199 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.utils.resource;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.*;
+import org.junit.Test;
+import org.osgi.framework.Version;
+import org.osgi.framework.namespace.IdentityNamespace;
+import org.osgi.resource.Capability;
+import org.osgi.resource.Requirement;
+
+public class ResourceImplTest {
+
+ private static CapabilityImpl newCapability(ResourceImpl res, String ns) {
+ return new CapabilityImpl(res, ns, new HashMap<>(), new HashMap<>());
+ }
+
+ private static RequirementImpl newRequirement(ResourceImpl res, String ns)
{
+ return new RequirementImpl(res, ns, new HashMap<>(), new HashMap<>());
+ }
+
+ /**
+ * Wrapper method to avoid direct field reference
+ */
+ private static List<Capability> getCaps(ResourceImpl res) {
+ return res.getCapabilities(null);
+ }
+
+ /**
+ * Wrapper method to avoid direct field reference
+ */
+ private static List<Requirement> getReqs(ResourceImpl res) {
+ return res.getRequirements(null);
+ }
+
+ private static int expectedHash(ResourceImpl res) {
Review Comment:
None of the tests reach the `h == 0` / `hashIsZero` branch, which is the
only non-trivial logic added. An empty resource hashes to 993, so it is never
hit. It would be worth covering, e.g. with a Capability/Requirement stub whose
`hashCode()` is chosen so `Objects.hash(caps, reqs)` is 0, asserting repeated
calls return 0.
Also, the `assertNotEquals` checks on hash values assume no collisions.
Capability/Requirement hashes are identity-based here, so there is a tiny
chance of a flaky failure.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]