diff --git a/src/main/java/org/apache/xmlbeans/impl/values/JavaNotationHolderEx.java b/src/main/java/org/apache/xmlbeans/impl/values/JavaNotationHolderEx.java
index a6eddc0bb..6dbbd4871 100644
--- a/src/main/java/org/apache/xmlbeans/impl/values/JavaNotationHolderEx.java
+++ b/src/main/java/org/apache/xmlbeans/impl/values/JavaNotationHolderEx.java
@@ -101,7 +101,7 @@ private static boolean check(String v, SchemaType sType)
if (len != null)
{
int m = ((XmlObjectBase)len).getBigIntegerValue().intValue();
- if (!(v.length() != m))
+ if (v.length() != m)
return false;
}
diff --git a/src/main/java/org/apache/xmlbeans/impl/values/JavaUriHolderEx.java b/src/main/java/org/apache/xmlbeans/impl/values/JavaUriHolderEx.java
index 74b155532..41817bc3d 100644
--- a/src/main/java/org/apache/xmlbeans/impl/values/JavaUriHolderEx.java
+++ b/src/main/java/org/apache/xmlbeans/impl/values/JavaUriHolderEx.java
@@ -123,7 +123,7 @@ private static boolean check(String v, SchemaType sType) {
XmlObject len = sType.getFacet(SchemaType.FACET_LENGTH);
if (len != null) {
int m = ((SimpleValue) len).getBigIntegerValue().intValue();
- if (length == m) {
+ if (length != m) {
return false;
}
}
diff --git a/src/test/java/misc/checkin/LengthFacetValidateOnSetTest.java b/src/test/java/misc/checkin/LengthFacetValidateOnSetTest.java
new file mode 100644
index 000000000..f2bfa85fe
--- /dev/null
+++ b/src/test/java/misc/checkin/LengthFacetValidateOnSetTest.java
@@ -0,0 +1,63 @@
+/* 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.SchemaTypeLoader;
+import org.apache.xmlbeans.SimpleValue;
+import org.apache.xmlbeans.XmlBeans;
+import org.apache.xmlbeans.XmlCursor;
+import org.apache.xmlbeans.XmlObject;
+import org.apache.xmlbeans.XmlOptions;
+import org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException;
+import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+public class LengthFacetValidateOnSetTest {
+
+ private static SimpleValue rootWithValidateOnSet(String base) throws Exception {
+ String xsd =
+ "" +
+ " " +
+ " " +
+ " " +
+ " " +
+ " " +
+ " " +
+ " " +
+ "";
+
+ SchemaTypeLoader loader = XmlBeans.loadXsd(new XmlObject[]{SchemaDocument.Factory.parse(xsd)});
+ XmlOptions options = new XmlOptions().setValidateOnSet();
+ XmlObject doc = loader.parse("abcde", null, options);
+ try (XmlCursor c = doc.newCursor()) {
+ c.toFirstChild();
+ return (SimpleValue) c.getObject();
+ }
+ }
+
+ @Test
+ void anyUriLengthFacetOnSet() throws Exception {
+ SimpleValue root = rootWithValidateOnSet("xs:anyURI");
+ // a value whose length matches the length facet must be accepted
+ assertDoesNotThrow(() -> root.setStringValue("xyzab"));
+ // a value whose length does not match must be rejected
+ assertThrows(XmlValueOutOfRangeException.class, () -> root.setStringValue("xyz"));
+ }
+}