Apache Lucene open-source search software
SOLR-14316: Fix unchecked type warning in JavaBinCodec (#1344)
Aroop9a8602c96eeb
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 104a0996adc..8afaae2fe2b 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -52,7 +52,8 @@ New FeaturesImprovements----------------------(No changes)+* SOLR-14316: Remove unchecked type conversion warning in JavaBinCodec's readMapEntry's equals() method+ (Aroop Ganguly, Tomás Fernández Löbbe, Noble Paul, Anshum Gupta)Optimizations---------------------diff --git a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.javaindex 2ab81ec175f..e50c7424aa5 100644--- a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java+++ b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java@@ -867,11 +867,11 @@ public class JavaBinCodec implements PushWriter {if(this == obj) {return true;}- if(!(obj instanceof Entry)) {- return false;+ if (obj instanceof Map.Entry<?, ?>) {+ Entry<?, ?> entry = (Entry<?, ?>) obj;+ return (this.getKey().equals(entry.getKey()) && this.getValue().equals(entry.getValue()));}- Map.Entry<Object, Object> entry = (Entry<Object, Object>) obj;- return (this.getKey().equals(entry.getKey()) && this.getValue().equals(entry.getValue()));+ return false;}};}diff --git a/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java b/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.javaindex 0e0ad88453e..7737f0c7fa6 100644--- a/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java+++ b/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java@@ -52,6 +52,9 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {private static final String SOLRJ_JAVABIN_BACKCOMPAT_BIN_CHILD_DOCS = "/solrj/javabin_backcompat_child_docs.bin";private static final String BIN_FILE_LOCATION_CHILD_DOCS = "./solr/solrj/src/test-files/solrj/javabin_backcompat_child_docs.bin";+ private static final String SOLRJ_DOCS_1 = "/solrj/docs1.xml";+ private static final String SOLRJ_DOCS_2 = "/solrj/sampleClusteringResponse.xml";+public void testStrings() throws Exception {for (int i = 0; i < 10000 * RANDOM_MULTIPLIER; i++) {String s = TestUtil.randomUnicodeString(random());@@ -287,6 +290,52 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {);}+ @Test+ public void testReadMapEntryTextStreamSource() throws IOException {+ Map.Entry<Object, Object> entryFromTextDoc1 = getMapFromJavaBinCodec(SOLRJ_DOCS_1);+ Map.Entry<Object, Object> entryFromTextDoc1_clone = getMapFromJavaBinCodec(SOLRJ_DOCS_1);++ Map.Entry<Object, Object> entryFromTextDoc2 = getMapFromJavaBinCodec(SOLRJ_DOCS_2);+ Map.Entry<Object, Object> entryFromTextDoc2_clone = getMapFromJavaBinCodec(SOLRJ_DOCS_2);++ // exactly same document read twice should have same content+ assertEquals ("text-doc1 exactly same document read twice should have same content",entryFromTextDoc1,entryFromTextDoc1_clone);+ // doc1 and doc2 are 2 text files with different content on line 1+ assertNotEquals ("2 text streams with 2 different contents should be unequal",entryFromTextDoc2,entryFromTextDoc1);+ // exactly same document read twice should have same content+ assertEquals ("text-doc2 exactly same document read twice should have same content",entryFromTextDoc2,entryFromTextDoc2_clone);+ }++ @Test+ public void testReadMapEntryBinaryStreamSource() throws IOException {+ // now lets look at binary files+ Map.Entry<Object, Object> entryFromBinFileA = getMapFromJavaBinCodec(SOLRJ_JAVABIN_BACKCOMPAT_BIN);+ Map.Entry<Object, Object> entryFromBinFileA_clone = getMapFromJavaBinCodec(SOLRJ_JAVABIN_BACKCOMPAT_BIN);++ assertEquals("same map entry references should be equal",entryFromBinFileA,entryFromBinFileA);++ // Commenting-out this test as it may have inadvertent effect on someone changing this in future+ // but keeping this in code to make a point, that even the same exact bin file,+ // there could be sub-objects in the key or value of the maps, with types that do not implement equals+ // and in these cases equals would fail as these sub-objects would be equated on their memory-references which is highly probbale to be unique+ // and hence the top-level map's equals will also fail+ // assertNotEquals("2 different references even though from same source are un-equal",entryFromBinFileA,entryFromBinFileA_clone);+++ // read in a different binary file and this should definitely not be equal to the other bi file+ Map.Entry<Object, Object> entryFromBinFileB = getMapFromJavaBinCodec(SOLRJ_JAVABIN_BACKCOMPAT_BIN_CHILD_DOCS);+ assertNotEquals("2 different references from 2 different source bin streams should still be unequal",entryFromBinFileA,entryFromBinFileB);+ }++ private Map.Entry<Object, Object> getMapFromJavaBinCodec(String fileName) throws IOException {+ try (InputStream is = getClass().getResourceAsStream(fileName)) {+ try (DataInputInputStream dis = new FastInputStream(is)) {+ try (JavaBinCodec javabin = new JavaBinCodec()) {+ return javabin.readMapEntry(dis);+ }+ }+ }+ }private static Object serializeAndDeserialize(Object o) throws IOException {return getObject(getBytes(o));