[java-identity-provider] 01/07: IDP-1595 Initial work

Rod Widdowson rdw at steadingsoftware.com
Tue Jun 9 09:52:27 UTC 2020


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch master
in repository java-identity-provider.

View the commit online:
http://git.shibboleth.net/view/?p=java-identity-provider.git;a=commit;h=dc8f4edef753480eeaa459b76a8cc750cda5169a

commit dc8f4edef753480eeaa459b76a8cc750cda5169a
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Sun May 10 16:10:56 2020 +0100

    IDP-1595 Initial work
    
    https://issues.shibboleth.net/jira/browse/IDP-1595
    
    Part one, start to introduce code structure the concept of a comparable Version.
---
 .../idp/installer/plugin/PluginVersion.java        | 136 +++++++++++++++++++++
 .../idp/installer/plugin/package-info.java         |  21 ++++
 .../idp/installer/plugin/PluginVersionTest.java    |  81 ++++++++++++
 3 files changed, 238 insertions(+)

diff --git a/idp-installer/src/main/java/net/shibboleth/idp/installer/plugin/PluginVersion.java b/idp-installer/src/main/java/net/shibboleth/idp/installer/plugin/PluginVersion.java
new file mode 100644
index 000000000..054443cff
--- /dev/null
+++ b/idp-installer/src/main/java/net/shibboleth/idp/installer/plugin/PluginVersion.java
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.idp.installer.plugin;
+
+import net.shibboleth.utilities.java.support.primitive.StringSupport;
+
+/**
+ * A version string (Major.minor.patch) as a handy class.
+ */
+public final class PluginVersion implements Comparable<PluginVersion>{
+
+    /** Major version. */
+    private int major;
+    
+    /** Minor version. */
+    private int minor;
+    
+    /** Patch version. */
+    private int patch;
+    
+    /**
+     * Constructor.
+     *
+     * @param version what to build from 
+     * @throws NumberFormatException if it doesn't fit a 1.2.3 format.
+     */
+    public PluginVersion(final String version) throws NumberFormatException {
+        
+        final String versionStr = StringSupport.trimOrNull(version);
+        if (null == versionStr) {
+            throw new NumberFormatException("Empty Version not allowed");
+        }
+        
+        final String[] components  = versionStr.split("\\.|\\+|-");
+        if (components.length >= 1) {
+            major = Integer.parseInt(components[0]);
+        }
+        if (components.length >= 2) {
+            minor = Integer.parseInt(components[1]);
+        }
+        if (components.length >= 3) {
+            patch = Integer.parseInt(components[2]);
+        }
+    }
+    
+    /**
+     * Constructor.
+     *
+     * @param maj Major Version 
+     * @param min Minor Version 
+     * @param pat Patch Version 
+     */
+    public PluginVersion(final int maj, final int min, final int pat) {
+        major = maj;
+        minor = min;
+        patch = pat;
+    }
+
+    /** Get the major version.
+     * @return Returns the major version.
+     */
+    public int getMajor() {
+        return major;
+    }
+
+    /** Get the minor version.
+     * @return Returns the minor version.
+     */
+    public int getMinor() {
+        return minor;
+    }
+
+    /** Get the patch version.
+     * @return Returns the patch version.
+     */
+    public int getPatch() {
+        return patch;
+    }
+    
+    /** Is this version all zeros (usually as a result of a parsing issue.
+     * @return if all nulls */
+    public boolean isNull() {
+        return major == 0 && minor ==0 && patch == 0;
+    }
+    
+
+
+    /** {@inheritDoc} */
+    public boolean equals(final Object obj) {
+        if (obj instanceof PluginVersion) {
+            final PluginVersion other= (PluginVersion)obj;
+            return other.major == major && other.minor == minor && other.patch == other.patch;
+        }
+        return false;
+    }
+    
+    /** {@inheritDoc} */
+    public int hashCode() {
+        return Integer.valueOf(major*100000 + minor*1000 +patch).hashCode();
+    }
+
+    /** {@inheritDoc} */
+    public int compareTo(final PluginVersion other) {
+        if (major == other.major) {
+            if (minor == other.minor) {
+                return patch - other.patch;
+            }
+            return minor - other.minor;
+        }
+        return major - other.major;
+    }
+    
+    /** {@inheritDoc} */
+    public String toString() {
+        return new StringBuffer(8).append(Integer.toString(major))
+                .append('.')
+                .append(Integer.toString(minor))
+                .append('.')
+                .append(Integer.toString(patch)).toString();
+    }
+}
diff --git a/idp-installer/src/main/java/net/shibboleth/idp/installer/plugin/package-info.java b/idp-installer/src/main/java/net/shibboleth/idp/installer/plugin/package-info.java
new file mode 100644
index 000000000..a5a053ca1
--- /dev/null
+++ b/idp-installer/src/main/java/net/shibboleth/idp/installer/plugin/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ * Publuc information for handling plugins.
+ */
+
+package net.shibboleth.idp.installer.plugin;
\ No newline at end of file
diff --git a/idp-installer/src/test/java/net/shibboleth/idp/installer/plugin/PluginVersionTest.java b/idp-installer/src/test/java/net/shibboleth/idp/installer/plugin/PluginVersionTest.java
new file mode 100644
index 000000000..2d9217a71
--- /dev/null
+++ b/idp-installer/src/test/java/net/shibboleth/idp/installer/plugin/PluginVersionTest.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.idp.installer.plugin;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
+import org.testng.annotations.Test;
+
+/**
+ * Tests for {@link PluginVersion}
+ */
+ at SuppressWarnings("javadoc")
+public final class PluginVersionTest {
+
+    private void failParse(final String what) {
+        try {
+            new PluginVersion(what);
+            fail("Invalid version parsed OK");
+        } catch (final NumberFormatException e) {
+            return;
+        }
+    }
+    
+    @Test public void parseTest() {
+        PluginVersion ver = new PluginVersion("4.2.1");
+        assertEquals(ver.getMajor(), 4);
+        assertEquals(ver.getMinor(), 2);
+        assertEquals(ver.getPatch(), 1);
+
+        ver = new PluginVersion("3.4");
+        assertEquals(ver.getMajor(), 3);
+        assertEquals(ver.getMinor(), 4);
+        assertEquals(ver.getPatch(), 0);
+        
+        ver = new PluginVersion("2");
+        assertEquals(ver.getMajor(), 2);
+        assertEquals(ver.getMinor(), 0);
+        assertEquals(ver.getPatch(), 0);
+
+        // Edge cases
+        ver = new PluginVersion("2.-.");
+        assertEquals(ver.getMajor(), 2);
+        assertEquals(ver.getMinor(), 0);
+        assertEquals(ver.getPatch(), 0);
+
+        failParse("1.2.X");
+        failParse("1.Y.");
+        failParse(null);
+        failParse("1.jo");
+        failParse("");
+        failParse("XXXX");
+    }
+
+    @Test public void compareTest() {
+        // check direction
+        assertTrue(Integer.valueOf(-1).compareTo(Integer.valueOf(0)) < 0);
+        
+        assertTrue(new PluginVersion("4.5.6").compareTo(new PluginVersion(4,5,6)) == 0); 
+        assertTrue(new PluginVersion(4,0,0).compareTo(new PluginVersion(3,9,9)) > 0); 
+        assertTrue(new PluginVersion(4,1,0).compareTo(new PluginVersion(4,2,1)) < 0);
+        assertTrue(new PluginVersion(4,1,0).compareTo(new PluginVersion(4,1,1)) < 0);
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list