|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * Utility functions to introspect metadata of SciJava libraries. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2022 - 2024 SciJava developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | + |
| 30 | +package org.scijava.meta; |
| 31 | + |
| 32 | +import java.io.File; |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.ArrayList; |
| 35 | + |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | +import org.w3c.dom.Element; |
| 38 | + |
| 39 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 40 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 41 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 42 | + |
| 43 | +/** |
| 44 | + * Tests {@link POM}. |
| 45 | + * |
| 46 | + * @author Curtis Rueden |
| 47 | + */ |
| 48 | +public class POMTest { |
| 49 | + |
| 50 | + /** Tests {@link POM#compareVersions(String, String)}. */ |
| 51 | + @Test |
| 52 | + public void testCompareVersions() { |
| 53 | + // basic checks |
| 54 | + assertTrue(POM.compareVersions("1", "2") < 0); |
| 55 | + assertTrue(POM.compareVersions("1.0", "2.0") < 0); |
| 56 | + assertTrue(POM.compareVersions("1.0", "1.1") < 0); |
| 57 | + assertTrue(POM.compareVersions("1.0.0", "2.0.0") < 0); |
| 58 | + |
| 59 | + // sanity checks for argument order |
| 60 | + assertTrue(POM.compareVersions("2", "1") > 0); |
| 61 | + assertTrue(POM.compareVersions("2.0", "1.0") > 0); |
| 62 | + assertTrue(POM.compareVersions("1.1", "1.0") > 0); |
| 63 | + assertTrue(POM.compareVersions("2.0.0", "1.0.0") > 0); |
| 64 | + |
| 65 | + // more complex/unusual checks |
| 66 | + assertTrue(POM.compareVersions("1.0-RC1", "1.0-RC2") < 0); |
| 67 | + assertTrue(POM.compareVersions("1.0-RC-1", "1.0-RC-2") < 0); |
| 68 | + assertTrue(POM.compareVersions("1.0-RC-2", "1.0-RC-10") < 0); |
| 69 | + assertTrue(POM.compareVersions("0.4-alpha", "0.4-beta") < 0); |
| 70 | + assertTrue(POM.compareVersions("foo", "bar") > 0); |
| 71 | + |
| 72 | + // checks which expose bugs/limitations |
| 73 | + assertTrue(POM.compareVersions("1.0-RC2", "1.0-RC10") > 0); |
| 74 | + assertTrue(POM.compareVersions("1.0-rc1", "1.0-RC2") > 0); |
| 75 | + |
| 76 | + // check that varying numbers of digits are handled properly |
| 77 | + assertTrue(POM.compareVersions("2.0.0", "2.0.0.1") < 0); |
| 78 | + // check that SemVer pre-release versions are handled properly |
| 79 | + assertTrue(POM.compareVersions("2.0.0", "2.0.0-beta-1") > 0); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testAccessors() throws IOException { |
| 84 | + final POM pom = new POM(new File("pom.xml")); |
| 85 | + assertEquals("org.scijava", pom.parentGroupId()); |
| 86 | + //assertEquals("pom-scijava", pom.parentArtifactId()); |
| 87 | + assertNotNull(pom.parentVersion()); |
| 88 | + assertEquals("org.scijava", pom.groupId()); |
| 89 | + assertEquals("scijava-meta", pom.artifactId()); |
| 90 | + assertNotNull(pom.version()); |
| 91 | + assertEquals("GitHub Actions", pom.ciManagementSystem()); |
| 92 | + final String ciManagementURL = pom.ciManagementURL(); |
| 93 | + assertEquals("https://github.com/scijava/scijava/actions", |
| 94 | + ciManagementURL); |
| 95 | + assertEquals("GitHub Issues", pom.issueManagementSystem()); |
| 96 | + final String issueManagementURL = pom.issueManagementURL(); |
| 97 | + assertEquals("https://github.com/scijava/scijava/issues", |
| 98 | + issueManagementURL); |
| 99 | + assertEquals("SciJava", pom.organizationName()); |
| 100 | + assertEquals("https://scijava.org/", pom.organizationURL()); |
| 101 | + assertTrue(pom.path().endsWith("pom.xml")); |
| 102 | + assertTrue(pom.projectDescription().startsWith( |
| 103 | + "Utility functions to introspect metadata of SciJava libraries.")); |
| 104 | + assertEquals("2022", pom.projectInceptionYear()); |
| 105 | + assertEquals("SciJava Meta", pom.projectName()); |
| 106 | + assertEquals("https://github.com/scijava/scijava", // |
| 107 | + pom.projectURL()); |
| 108 | + final String scmConnection = pom.scmConnection(); |
| 109 | + assertEquals("scm:git:https://github.com/scijava/scijava", |
| 110 | + scmConnection); |
| 111 | + final String scmDeveloperConnection = pom.scmDeveloperConnection(); |
| 112 | + assertEquals("scm:git:git@github.com:scijava/scijava", |
| 113 | + scmDeveloperConnection); |
| 114 | + assertNotNull(pom.scmTag()); // won't be HEAD for release tags |
| 115 | + assertEquals("https://github.com/scijava/scijava", pom.scmURL()); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testCdata() throws IOException { |
| 120 | + final POM pom = new POM(new File("pom.xml")); |
| 121 | + assertEquals("repo", pom.cdata("//project/licenses/license/distribution")); |
| 122 | + assertEquals("https://github.com/scijava/scijava", // |
| 123 | + pom.cdata("//project/url")); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testElements() throws IOException { |
| 128 | + final POM pom = new POM(new File("pom.xml")); |
| 129 | + final ArrayList<Element> developers = |
| 130 | + pom.elements("//project/developers/developer"); |
| 131 | + assertEquals(2, developers.size()); |
| 132 | + assertEquals("ctrueden", XML.cdata(developers.get(0), "id")); |
| 133 | + assertEquals("gselzer", XML.cdata(developers.get(1), "id")); |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments