|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.maven.impl.model; |
| 20 | + |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.nio.file.Paths; |
| 24 | + |
| 25 | +import org.apache.maven.api.model.Model; |
| 26 | +import org.apache.maven.api.services.xml.XmlReaderRequest; |
| 27 | +import org.apache.maven.api.services.xml.XmlWriterRequest; |
| 28 | +import org.apache.maven.impl.DefaultModelXmlFactory; |
| 29 | +import org.junit.jupiter.api.BeforeEach; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.xmlunit.builder.DiffBuilder; |
| 32 | +import org.xmlunit.diff.Diff; |
| 33 | + |
| 34 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 35 | + |
| 36 | +class DefaultInheritanceAssemblerTest { |
| 37 | + |
| 38 | + private DefaultModelXmlFactory xmlFactory; |
| 39 | + |
| 40 | + private DefaultInheritanceAssembler assembler; |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + void setUp() { |
| 44 | + xmlFactory = new DefaultModelXmlFactory(); |
| 45 | + assembler = new DefaultInheritanceAssembler(); |
| 46 | + } |
| 47 | + |
| 48 | + private Path getPom(String name) { |
| 49 | + return Paths.get("../../compat/maven-model-builder/src/test/resources/poms/inheritance/" + name + ".xml"); |
| 50 | + } |
| 51 | + |
| 52 | + private Model getModel(String name) throws Exception { |
| 53 | + return xmlFactory.read(XmlReaderRequest.builder().path(getPom(name)).build()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testPluginConfiguration() throws Exception { |
| 58 | + testInheritance("plugin-configuration"); |
| 59 | + } |
| 60 | + |
| 61 | + public void testInheritance(String baseName) throws Exception { |
| 62 | + testInheritance(baseName, false); |
| 63 | + testInheritance(baseName, true); |
| 64 | + } |
| 65 | + |
| 66 | + public void testInheritance(String baseName, boolean fromRepo) throws Exception { |
| 67 | + Model parent = getModel(baseName + "-parent"); |
| 68 | + Model child = getModel(baseName + "-child"); |
| 69 | + |
| 70 | + if (!fromRepo) { |
| 71 | + // when model is built from disk, pomFile is set |
| 72 | + // (has consequences in inheritance algorithm since getProjectDirectory() returns non-null) |
| 73 | + parent = parent.withPomFile(getPom(baseName + "-parent").toAbsolutePath()); |
| 74 | + child = child.withPomFile(getPom(baseName + "-child").toAbsolutePath()); |
| 75 | + } |
| 76 | + |
| 77 | + Model assembled = assembler.assembleModelInheritance(child, parent, null, null); |
| 78 | + |
| 79 | + // write baseName + "-actual" |
| 80 | + Path actual = Paths.get( |
| 81 | + "target/test-classes/poms/inheritance/" + baseName + (fromRepo ? "-build" : "-repo") + "-actual.xml"); |
| 82 | + Files.createDirectories(actual.getParent()); |
| 83 | + xmlFactory.write(XmlWriterRequest.<Model>builder() |
| 84 | + .content(assembled) |
| 85 | + .path(actual) |
| 86 | + .build()); |
| 87 | + |
| 88 | + // check with getPom( baseName + "-expected" ) |
| 89 | + Path expected = getPom(baseName + "-expected"); |
| 90 | + |
| 91 | + Diff diff = DiffBuilder.compare(expected.toFile()) |
| 92 | + .withTest(actual.toFile()) |
| 93 | + .ignoreComments() |
| 94 | + .ignoreWhitespace() |
| 95 | + .build(); |
| 96 | + assertFalse(diff.hasDifferences(), "XML files should be identical: " + diff.toString()); |
| 97 | + } |
| 98 | +} |
0 commit comments