|
| 1 | +package org.codehaus.modello.plugin.xsd; |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2004, Codehaus.org |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | + * this software and associated documentation files (the "Software"), to deal in |
| 8 | + * the Software without restriction, including without limitation the rights to |
| 9 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 10 | + * of the Software, and to permit persons to whom the Software is furnished to do |
| 11 | + * so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +import javax.inject.Inject; |
| 26 | +import javax.xml.XMLConstants; |
| 27 | +import javax.xml.transform.stream.StreamSource; |
| 28 | +import javax.xml.validation.Schema; |
| 29 | +import javax.xml.validation.SchemaFactory; |
| 30 | +import javax.xml.validation.Validator; |
| 31 | + |
| 32 | +import java.io.File; |
| 33 | +import java.io.StringReader; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +import org.codehaus.modello.AbstractModelloGeneratorTest; |
| 37 | +import org.codehaus.modello.core.ModelloCore; |
| 38 | +import org.codehaus.modello.model.Model; |
| 39 | +import org.codehaus.plexus.testing.PlexusTest; |
| 40 | +import org.junit.jupiter.api.Test; |
| 41 | +import org.xml.sax.SAXParseException; |
| 42 | + |
| 43 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 44 | +import static org.junit.jupiter.api.Assertions.fail; |
| 45 | + |
| 46 | +/** |
| 47 | + * Test that verifies required fields are properly enforced in generated XSD schemas |
| 48 | + * without needing the XSD_ENFORCE_MANDATORY_ELEMENTS parameter. |
| 49 | + * |
| 50 | + * @author Modello Team |
| 51 | + */ |
| 52 | +@PlexusTest |
| 53 | +public class RequiredFieldXsdGeneratorTest extends AbstractModelloGeneratorTest { |
| 54 | + @Inject |
| 55 | + private ModelloCore modello; |
| 56 | + |
| 57 | + public RequiredFieldXsdGeneratorTest() { |
| 58 | + super("required-field"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testRequiredFieldsEnforcedWithoutParameter() throws Throwable { |
| 63 | + Model model = modello.loadModel(getXmlResourceReader("/required-field.mdo")); |
| 64 | + |
| 65 | + // Generate XSD WITHOUT the XSD_ENFORCE_MANDATORY_ELEMENTS parameter |
| 66 | + Map<String, Object> parameters = getModelloParameters("1.0.0"); |
| 67 | + // Note: NOT setting XSD_ENFORCE_MANDATORY_ELEMENTS parameter |
| 68 | + |
| 69 | + modello.generate(model, "xsd", parameters); |
| 70 | + |
| 71 | + SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); |
| 72 | + Schema schema = sf.newSchema(new StreamSource(new File(getOutputDirectory(), "test-1.0.0.xsd"))); |
| 73 | + Validator validator = schema.newValidator(); |
| 74 | + |
| 75 | + // Test 1: Valid XML with all required fields should pass |
| 76 | + String validXml = "<?xml version=\"1.0\"?>\n" |
| 77 | + + "<testModel xmlns=\"http://codehaus-plexus.github.io/TEST/1.0.0\">\n" |
| 78 | + + " <requiredField>value</requiredField>\n" |
| 79 | + + "</testModel>"; |
| 80 | + |
| 81 | + validator.validate(new StreamSource(new StringReader(validXml))); |
| 82 | + |
| 83 | + // Test 2: XML missing required field should fail |
| 84 | + String missingRequiredXml = "<?xml version=\"1.0\"?>\n" |
| 85 | + + "<testModel xmlns=\"http://codehaus-plexus.github.io/TEST/1.0.0\">\n" |
| 86 | + + " <optionalField>value</optionalField>\n" |
| 87 | + + "</testModel>"; |
| 88 | + |
| 89 | + try { |
| 90 | + validator.validate(new StreamSource(new StringReader(missingRequiredXml))); |
| 91 | + fail("Validation should have failed for XML missing required field"); |
| 92 | + } catch (SAXParseException e) { |
| 93 | + // Expected - the error should mention the required field |
| 94 | + assertTrue( |
| 95 | + e.getMessage().contains("requiredField") || e.getMessage().contains("expected"), |
| 96 | + "Error message should indicate missing required field: " + e.getMessage()); |
| 97 | + } |
| 98 | + |
| 99 | + // Test 3: XML with optional field omitted should pass |
| 100 | + String withoutOptionalXml = "<?xml version=\"1.0\"?>\n" |
| 101 | + + "<testModel xmlns=\"http://codehaus-plexus.github.io/TEST/1.0.0\">\n" |
| 102 | + + " <requiredField>value</requiredField>\n" |
| 103 | + + "</testModel>"; |
| 104 | + |
| 105 | + validator.validate(new StreamSource(new StringReader(withoutOptionalXml))); |
| 106 | + } |
| 107 | +} |
0 commit comments