// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.config.application; import com.yahoo.config.application.api.ApplicationPackage; import org.junit.Test; import org.w3c.dom.Document; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; import java.io.File; import java.io.IOException; import java.nio.file.NoSuchFileException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; /** * @author Ulf Lilleengen */ public class IncludeProcessorTest { @Test public void testInclude() throws IOException, SAXException, ParserConfigurationException, TransformerException { File app = new File("src/test/resources/multienvapp"); DocumentBuilder docBuilder = Xml.getPreprocessDocumentBuilder(); String expected = """ 4099 5000 5001 5002 1 """; Document doc = new IncludeProcessor(app).process(docBuilder.parse(getServices(app))); // System.out.println(Xml.documentAsString(doc)); TestBase.assertDocument(expected, doc); } @Test public void testIllegalParent() throws ParserConfigurationException, IOException, SAXException, TransformerException { try { File app = new File("src/test/resources/multienvapp_fail_parent"); DocumentBuilder docBuilder = Xml.getPreprocessDocumentBuilder(); new IncludeProcessor(app).process(docBuilder.parse(getServices(app))); fail("sibling to package should not be allowed"); } catch (IllegalArgumentException e) { assertEquals("src/test/resources/multienvapp_fail_parent/../multienvapp/services.xml is not a descendant of src/test/resources/multienvapp_fail_parent", e.getMessage()); } } @Test(expected = IllegalArgumentException.class) public void testIllegalParent2() throws ParserConfigurationException, IOException, SAXException, TransformerException { File app = new File("src/test/resources/multienvapp_fail_parent2"); DocumentBuilder docBuilder = Xml.getPreprocessDocumentBuilder(); new IncludeProcessor(app).process(docBuilder.parse(getServices(app))); fail("absolute include path should not be allowed"); } @Test(expected = NoSuchFileException.class) public void testRequiredIncludeIsDefault() throws ParserConfigurationException, IOException, SAXException, TransformerException { File app = new File("src/test/resources/multienvapp_failrequired"); DocumentBuilder docBuilder = Xml.getPreprocessDocumentBuilder(); new IncludeProcessor(app).process(docBuilder.parse(getServices(app))); fail("should fail by default to include a non-existent file"); } static File getServices(File app) { return new File(app, ApplicationPackage.SERVICES); } }