View Javadoc
1   /*
2    * Copyright (c) 2011-2025 Yegor Bugayenko
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.maven;
32  
33  import java.io.File;
34  import java.nio.charset.StandardCharsets;
35  import java.util.Collections;
36  import org.apache.commons.io.FileUtils;
37  import org.cactoos.io.ResourceOf;
38  import org.cactoos.text.TextOf;
39  import org.junit.jupiter.api.Test;
40  
41  /**
42   * Test case for {@link PomXpathValidator} class.
43   * @since 0.6
44   */
45  final class PomXpathValidatorTest {
46  
47      /**
48       * PomXpathValidator can validate pom.xml with xpath.
49       * @throws Exception If something wrong happens inside
50       */
51      @Test
52      void canValidatePomWithXpath() throws Exception {
53          final MavenEnvironment env = new MavenEnvironmentMocker()
54              .withAsserts(
55                  Collections.singletonList(
56                  "/pom:project/pom:dependencies/pom:dependency[pom:artifactId='commons-io']/pom:version[.='1.2.5']/text()"
57              )
58          ).mock();
59          final String pom = new TextOf(
60              new ResourceOf("com/qulice/maven/PomXpathValidator/pom.xml")
61          ).asString();
62          FileUtils.write(
63              new File(
64                  String.format(
65                      "%s%spom.xml", env.basedir(), File.separator
66                  )
67              ),
68              pom,
69              StandardCharsets.UTF_8
70          );
71          new PomXpathValidator().validate(env);
72      }
73  }