github-code-scanning[bot] commented on code in PR #2090:
URL: https://github.com/apache/avro/pull/2090#discussion_r1108526785


##########
lang/java/avro/src/test/java/org/apache/avro/util/springframework/ConcurrentReferenceHashMapTests.java:
##########
@@ -0,0 +1,688 @@
+/*
+ * Copyright 2002-2021 the original author or authors.
+ *
+ * Licensed 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
+ *
+ *      https://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.avro.util.springframework;
+
+import org.apache.avro.reflect.Nullable;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+import org.apache.avro.util.springframework.ConcurrentReferenceHashMap.Entry;
+import 
org.apache.avro.util.springframework.ConcurrentReferenceHashMap.Reference;
+import 
org.apache.avro.util.springframework.ConcurrentReferenceHashMap.Restructure;
+
+import java.lang.ref.WeakReference;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.WeakHashMap;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests for {@link ConcurrentReferenceHashMap}.
+ *
+ * @author Phillip Webb
+ * @author Juergen Hoeller
+ */
+class ConcurrentReferenceHashMapTests {
+
+  private static final Comparator<? super String> NULL_SAFE_STRING_SORT = new 
NullSafeComparator<>(
+      new ComparableComparator<String>(), true);
+
+  private TestWeakConcurrentCache<Integer, String> map = new 
TestWeakConcurrentCache<>();
+
+  @Test
+  void shouldCreateWithDefaults() {
+    ConcurrentReferenceHashMap<Integer, String> map = new 
ConcurrentReferenceHashMap<>();
+    assertThat(map.getSegmentsSize(), equalTo(16));
+    assertThat(map.getSegment(0).getSize(), equalTo(1));
+    assertThat(map.getLoadFactor(), equalTo(0.75f));
+  }
+
+  @Test
+  void shouldCreateWithInitialCapacity() {
+    ConcurrentReferenceHashMap<Integer, String> map = new 
ConcurrentReferenceHashMap<>(32);
+    assertThat(map.getSegmentsSize(), equalTo(16));
+    assertThat(map.getSegment(0).getSize(), equalTo(2));
+    assertThat(map.getLoadFactor(), equalTo(0.75f));
+  }
+
+  @Test
+  void shouldCreateWithInitialCapacityAndLoadFactor() {
+    ConcurrentReferenceHashMap<Integer, String> map = new 
ConcurrentReferenceHashMap<>(32, 0.5f);
+    assertThat(map.getSegmentsSize(), equalTo(16));
+    assertThat(map.getSegment(0).getSize(), equalTo(2));
+    assertThat(map.getLoadFactor(), equalTo(0.5f));
+  }
+
+  @Test
+  void shouldCreateWithInitialCapacityAndConcurrentLevel() {
+    ConcurrentReferenceHashMap<Integer, String> map = new 
ConcurrentReferenceHashMap<>(16, 2);
+    assertThat(map.getSegmentsSize(), equalTo(2));
+    assertThat(map.getSegment(0).getSize(), equalTo(8));
+    assertThat(map.getLoadFactor(), equalTo(0.75f));
+  }
+
+  @Test
+  void shouldCreateFullyCustom() {
+    ConcurrentReferenceHashMap<Integer, String> map = new 
ConcurrentReferenceHashMap<>(5, 0.5f, 3);
+    // concurrencyLevel of 3 ends up as 4 (nearest power of 2)
+    assertThat(map.getSegmentsSize(), equalTo(4));
+    // initialCapacity is 5/4 (rounded up, to nearest power of 2)
+    assertThat(map.getSegment(0).getSize(), equalTo(2));
+    assertThat(map.getLoadFactor(), equalTo(0.5f));
+  }
+
+  @Test
+  void shouldNeedNonNegativeInitialCapacity() {
+    new ConcurrentReferenceHashMap<Integer, String>(0, 1);
+    IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
+        () -> new TestWeakConcurrentCache<Integer, String>(-1, 1));
+    assertTrue(e.getMessage().contains("Initial capacity must not be 
negative"));
+  }
+
+  @Test
+  void shouldNeedPositiveLoadFactor() {
+    new ConcurrentReferenceHashMap<Integer, String>(0, 0.1f, 1);
+    IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
+        () -> new TestWeakConcurrentCache<Integer, String>(0, 0.0f, 1));
+    assertTrue(e.getMessage().contains("Load factor must be positive"));
+  }
+
+  @Test
+  void shouldNeedPositiveConcurrencyLevel() {
+    new ConcurrentReferenceHashMap<Integer, String>(1, 1);
+    IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
+        () -> new TestWeakConcurrentCache<Integer, String>(1, 0));
+    assertTrue(e.getMessage().contains("Concurrency level must be positive"));
+  }
+
+  @Test
+  void shouldPutAndGet() {
+    // NOTE we are using mock references so we don't need to worry about GC
+    assertEquals(0, this.map.size());
+    this.map.put(123, "123");
+    assertThat(this.map.get(123), equalTo("123"));
+    assertEquals(1, this.map.size());
+    this.map.put(123, "123b");
+    assertEquals(1, this.map.size());
+    this.map.put(123, null);
+    assertEquals(1, this.map.size());
+  }
+
+  @Test
+  void shouldReplaceOnDoublePut() {
+    this.map.put(123, "321");
+    this.map.put(123, "123");
+    assertThat(this.map.get(123), equalTo("123"));
+  }
+
+  @Test
+  void shouldPutNullKey() {
+    assertNull(this.map.get(null));
+    assertThat(this.map.getOrDefault(null, "456"), equalTo("456"));
+    this.map.put(null, "123");
+    assertThat(this.map.get(null), equalTo("123"));
+    assertThat(this.map.getOrDefault(null, "456"), equalTo("123"));
+  }
+
+  @Test
+  void shouldPutNullValue() {
+    assertNull(this.map.get(123));
+    assertThat(this.map.getOrDefault(123, "456"), equalTo("456"));
+    this.map.put(123, "321");
+    assertThat(this.map.get(123), equalTo("321"));
+    assertThat(this.map.getOrDefault(123, "456"), equalTo("321"));
+    this.map.put(123, null);
+    assertNull(this.map.get(123));
+    assertNull(this.map.getOrDefault(123, "456"));
+  }
+
+  @Test
+  void shouldGetWithNoItems() {
+    assertNull(this.map.get(123));
+  }
+
+  @Test
+  void shouldApplySupplementalHash() {
+    Integer key = 123;

Review Comment:
   ## Boxed variable is never null
   
   The variable 'key' is only assigned values of primitive type and is never 
'null', but it is declared with the boxed type 'Integer'.
   
   [Show more 
details](https://github.com/apache/avro/security/code-scanning/2975)



-- 
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]

Reply via email to