You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2026/10/02 13:20:47 UTC
(poi-xmlbeans) branch trunk updated: align JavaIntHolder.value_hash_code with the other decimal holders (#124)
This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi-xmlbeans.git
The following commit(s) were added to refs/heads/trunk by this push:
new c7e40d59 align JavaIntHolder.value_hash_code with the other decimal holders (#124)
c7e40d59 is described below
commit c7e40d59c4940e0f7965336d70b75575447f07d9
Author: aizu-m <ai...@gmail.com>
AuthorDate: Fri Oct 2 18:50:41 2026 +0530
align JavaIntHolder.value_hash_code with the other decimal holders (#124)
---
.../apache/xmlbeans/impl/values/JavaIntHolder.java | 3 +-
.../misc/checkin/DecimalValueHashCodeTest.java | 88 ++++++++++++++++++++++
2 files changed, 90 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/xmlbeans/impl/values/JavaIntHolder.java b/src/main/java/org/apache/xmlbeans/impl/values/JavaIntHolder.java
index 53fb40c5..8ffb9329 100644
--- a/src/main/java/org/apache/xmlbeans/impl/values/JavaIntHolder.java
+++ b/src/main/java/org/apache/xmlbeans/impl/values/JavaIntHolder.java
@@ -123,6 +123,7 @@ public abstract class JavaIntHolder extends XmlObjectBase {
* primitives.
*/
protected int value_hash_code() {
- return _value;
+ long longval = _value;
+ return (int) ((longval >> 32) * 19 + longval);
}
}
diff --git a/src/test/java/misc/checkin/DecimalValueHashCodeTest.java b/src/test/java/misc/checkin/DecimalValueHashCodeTest.java
new file mode 100644
index 00000000..25905f98
--- /dev/null
+++ b/src/test/java/misc/checkin/DecimalValueHashCodeTest.java
@@ -0,0 +1,88 @@
+/* Copyright 2004 The Apache Software Foundation
+ *
+ * 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
+ *
+ * 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 misc.checkin;
+
+import org.apache.xmlbeans.SchemaType;
+import org.apache.xmlbeans.SchemaTypeLoader;
+import org.apache.xmlbeans.XmlBeans;
+import org.apache.xmlbeans.XmlObject;
+import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
+import org.junit.jupiter.api.Test;
+
+import javax.xml.namespace.QName;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class DecimalValueHashCodeTest {
+
+ private static final String XSD = "http://www.w3.org/2001/XMLSchema";
+
+ private static XmlObject value(String localName, String lexical) {
+ SchemaType type = XmlBeans.getBuiltinTypeSystem().findType(new QName(XSD, localName));
+ return type.newValue(lexical);
+ }
+
+ @Test
+ void equalDecimalValuesHashTheSame() {
+ String[] intTypes = {"int", "short", "byte", "unsignedShort", "unsignedByte"};
+ String[] wideTypes = {"long", "integer", "decimal"};
+ String[] lexicals = {"-1", "-5", "-128", "0", "5", "127"};
+
+ for (String intType : intTypes) {
+ for (String wideType : wideTypes) {
+ for (String lexical : lexicals) {
+ XmlObject narrow = value(intType, lexical);
+ XmlObject wide = value(wideType, lexical);
+
+ assertTrue(narrow.valueEquals(wide),
+ "xs:" + intType + " and xs:" + wideType + " '" + lexical + "' should be equal");
+ assertEquals(narrow.valueHashCode(), wide.valueHashCode(),
+ "xs:" + intType + " and xs:" + wideType + " '" + lexical + "' hash differently");
+ }
+ }
+ }
+ }
+
+ @Test
+ void duplicateNegativeUniqueValueIsReported() throws Exception {
+ String xsd =
+ "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' " +
+ "xmlns:t='urn:t' targetNamespace='urn:t' elementFormDefault='qualified'>" +
+ " <xs:element name='root'>" +
+ " <xs:complexType>" +
+ " <xs:sequence>" +
+ " <xs:element name='i' type='xs:int'/>" +
+ " <xs:element name='l' type='xs:long'/>" +
+ " </xs:sequence>" +
+ " </xs:complexType>" +
+ " <xs:unique name='u'>" +
+ " <xs:selector xpath='t:i|t:l'/>" +
+ " <xs:field xpath='.'/>" +
+ " </xs:unique>" +
+ " </xs:element>" +
+ "</xs:schema>";
+
+ SchemaTypeLoader loader = XmlBeans.loadXsd(new XmlObject[]{SchemaDocument.Factory.parse(xsd)});
+
+ // the same value spelled as xs:int and as xs:long is one value, so it breaks the
+ // unique constraint whatever its sign
+ assertFalse(loader.parse("<t:root xmlns:t='urn:t'><t:i>-5</t:i><t:l>-5</t:l></t:root>", null, null).validate());
+ assertFalse(loader.parse("<t:root xmlns:t='urn:t'><t:i>5</t:i><t:l>5</t:l></t:root>", null, null).validate());
+ assertTrue(loader.parse("<t:root xmlns:t='urn:t'><t:i>-5</t:i><t:l>-6</t:l></t:root>", null, null).validate());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org