CRYPTO-110 Provide component version and name Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/c4db965e Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/c4db965e Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/c4db965e
Branch: refs/heads/CRYPTO-1.0.0 Commit: c4db965e38b7a101b2747d845d757ac5dba17aba Parents: d58e6f3 Author: Sebb <s...@apache.org> Authored: Sat Jul 9 13:42:02 2016 +0100 Committer: Sebb <s...@apache.org> Committed: Sat Jul 9 13:42:02 2016 +0100 ---------------------------------------------------------------------- .../java/org/apache/commons/crypto/Crypto.java | 51 ++++++++++++++++++++ .../apache/commons/crypto/component.properties | 20 ++++++++ .../org/apache/commons/crypto/CryptoTest.java | 43 +++++++++++++++++ 3 files changed, 114 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/c4db965e/src/main/java/org/apache/commons/crypto/Crypto.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/Crypto.java b/src/main/java/org/apache/commons/crypto/Crypto.java index e7aa101..d913878 100644 --- a/src/main/java/org/apache/commons/crypto/Crypto.java +++ b/src/main/java/org/apache/commons/crypto/Crypto.java @@ -17,6 +17,11 @@ */ package org.apache.commons.crypto; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Properties; + /** * Provides diagnostic information about Commons Crypto and keys for native class loading */ @@ -45,6 +50,23 @@ public final class Crypto { public static final String LIB_TEMPDIR_KEY = Crypto.CONF_PREFIX + "lib.tempdir"; + private static class ComponentPropertiesHolder { + static final Properties PROPERTIES = getComponentProperties(); + + private static Properties getComponentProperties() { + URL url = Crypto.class.getResource("/org/apache/commons/crypto/component.properties"); + if (url != null) { + Properties versionData = new Properties(); + try (InputStream openStream = url.openStream()) { + versionData.load(openStream); + return versionData; + } catch (IOException e) { + } + } + return new Properties(); // make sure field is not null + } + } + /** * Checks whether the native code has been successfully loaded for the platform. * @@ -62,4 +84,33 @@ public final class Crypto { public static Throwable getLoadingError() { return NativeCodeLoader.getLoadingError(); } + + /** + * Gets the component version of Apache Commons Crypto. + * <p> + * This implementation relies on the VERSION properties file which + * must be set up with the correct contents by the build process. + * This is done automatically by Maven. + * + * @return the version; may be null if not found + */ + public static String getComponentVersion() { + // Note: the component properties file allows the method to work without needing the jar + return ComponentPropertiesHolder.PROPERTIES.getProperty("VERSION"); + } + + /** + * Gets the component version of Apache Commons Crypto. + * <p> + * This implementation relies on the VERSION properties file which + * must be set up with the correct contents by the build process. + * This is done automatically by Maven. + * + * @return the version; may be null if not found + */ + public static String getComponentName() { + // Note: the component properties file allows the method to work without needing the jar + return ComponentPropertiesHolder.PROPERTIES.getProperty("NAME"); + } + } http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/c4db965e/src/main/resources/org/apache/commons/crypto/component.properties ---------------------------------------------------------------------- diff --git a/src/main/resources/org/apache/commons/crypto/component.properties b/src/main/resources/org/apache/commons/crypto/component.properties new file mode 100644 index 0000000..3889e45 --- /dev/null +++ b/src/main/resources/org/apache/commons/crypto/component.properties @@ -0,0 +1,20 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF 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. +# + +# This file gives access to the component string from Java + +VERSION=${project.version} +NAME=${project.name} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/c4db965e/src/test/java/org/apache/commons/crypto/CryptoTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/crypto/CryptoTest.java b/src/test/java/org/apache/commons/crypto/CryptoTest.java new file mode 100644 index 0000000..065ea77 --- /dev/null +++ b/src/test/java/org/apache/commons/crypto/CryptoTest.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 org.apache.commons.crypto; + +import org.junit.Assert; +import org.junit.Test; + +public class CryptoTest { + + @Test + // This test may fail unless the code was built by Maven, as + // it relies on the VERSION file being set up correctly + public void testGetComponentVersion() { + String version = Crypto.getComponentVersion(); + Assert.assertNotNull("Should not be null",version); + Assert.assertTrue(version,version.matches("^\\d+\\.\\d+.*")); + } + + @Test + // This test may fail unless the code was built by Maven, as + // it relies on the VERSION file being set up correctly + public void testGetComponentName() { + String version = Crypto.getComponentName(); + Assert.assertNotNull("Should not be null",version); + Assert.assertTrue(version,version.matches("^Apache Commons Crypto.*")); + } + +}