-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
modules/core/src/main/java/com/tsurugidb/console/core/util/TanzawaVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package com.tsurugidb.console.core.util; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.text.MessageFormat; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.tsurugidb.tsubakuro.util.TsubakuroVersion; | ||
|
||
/** | ||
* Class to get the version of Tanzawa. | ||
*/ | ||
public final class TanzawaVersion { | ||
static final Logger LOG = LoggerFactory.getLogger(TanzawaVersion.class); | ||
|
||
/** core */ | ||
public static final String MODULE_CORE = "core"; //$NON-NLS-1$ | ||
|
||
private static final String PATH_VERSION_DIR = "/META-INF/tsurugidb/"; //$NON-NLS-1$ | ||
private static final String MODULE_PREFIX = "tanzawa-"; //$NON-NLS-1$ | ||
private static final String VERSION_FILE_SUFFIX = ".properties"; //$NON-NLS-1$ | ||
|
||
private static final String ATTRIBUTE_BUILD_TIMESTAMP = "Build-Timestamp"; //$NON-NLS-1$ | ||
private static final String ATTRIBUTE_BUILD_REVISION = "Build-Revision"; //$NON-NLS-1$ | ||
private static final String ATTRIBUTE_BUILD_VERSION = "Build-Version"; //$NON-NLS-1$ | ||
|
||
private static final Map<String, Properties> PROPERTIES_MAP = new ConcurrentHashMap<>(); | ||
|
||
private TanzawaVersion() { | ||
} | ||
|
||
/** | ||
* Get Build-Timestamp. | ||
* | ||
* @param moduleName module name | ||
* @return Build-Timestamp | ||
* @throws IOException if an I/O error occurs loading from the version file | ||
*/ | ||
public static String getBuildTimestamp(String moduleName) throws IOException { | ||
var properties = getProperties(moduleName); | ||
return properties.getProperty(ATTRIBUTE_BUILD_TIMESTAMP); | ||
} | ||
|
||
/** | ||
* Get Build-Revision. | ||
* | ||
* @param moduleName module name | ||
* @return Build-Revision | ||
* @throws IOException if an I/O error occurs loading from the version file | ||
*/ | ||
public static String getBuildRevision(String moduleName) throws IOException { | ||
var properties = getProperties(moduleName); | ||
return properties.getProperty(ATTRIBUTE_BUILD_REVISION); | ||
} | ||
|
||
/** | ||
* Get Build-Version. | ||
* | ||
* @param moduleName module name | ||
* @return Build-Version | ||
* @throws IOException if an I/O error occurs loading from the version file | ||
*/ | ||
public static String getBuildVersion(String moduleName) throws IOException { | ||
var properties = getProperties(moduleName); | ||
return properties.getProperty(ATTRIBUTE_BUILD_VERSION); | ||
} | ||
|
||
/** | ||
* Get version properties. | ||
* | ||
* @param moduleName module name | ||
* @return version properties | ||
* @throws IOException if an I/O error occurs loading from the version file | ||
*/ | ||
public static Properties getProperties(String moduleName) throws IOException { | ||
try { | ||
return PROPERTIES_MAP.computeIfAbsent(moduleName, key -> { | ||
String module = MODULE_PREFIX + key; | ||
try { | ||
return loadProperties(module); | ||
} catch (Exception e) { | ||
LOG.debug("version file load error. module={}", module, e); // $NON-NLS-1$ | ||
throw new UncheckedIOException(new IOException(MessageFormat.format("version file load error. module={0}", module), e)); | ||
} | ||
}); | ||
} catch (UncheckedIOException e) { | ||
throw e.getCause(); | ||
} | ||
} | ||
|
||
private static Properties loadProperties(String module) throws IOException { | ||
String path = PATH_VERSION_DIR + module + VERSION_FILE_SUFFIX; | ||
LOG.trace("searching for version file: {}", path); // $NON-NLS-1$ | ||
var versionFile = TsubakuroVersion.class.getResource(path); | ||
if (versionFile == null) { | ||
throw new FileNotFoundException(MessageFormat.format("missing version file. path={0}", path)); | ||
} | ||
LOG.debug("loading version file: {}", versionFile); // $NON-NLS-1$ | ||
try (var input = versionFile.openStream()) { | ||
var properties = new Properties(); | ||
properties.load(input); | ||
return properties; | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
modules/core/src/main/java/com/tsurugidb/console/core/util/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Utility classes for Tsurugi SQL console. | ||
*/ | ||
package com.tsurugidb.console.core.util; |
40 changes: 40 additions & 0 deletions
40
modules/core/src/test/java/com/tsurugidb/console/core/util/TanzawaVersionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.tsurugidb.console.core.util; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class TanzawaVersionTest { | ||
|
||
@Test | ||
void getBuildTimestamp() throws IOException { | ||
String version = TanzawaVersion.getBuildTimestamp("version-test"); | ||
assertEquals("2023-10-24T12:52:36.203+0900", version); | ||
} | ||
|
||
@Test | ||
void getBuildRevision() throws IOException { | ||
String version = TanzawaVersion.getBuildRevision("version-test"); | ||
assertEquals("abcdefg", version); | ||
} | ||
|
||
@Test | ||
void getBuildVersion() throws IOException { | ||
String version = TanzawaVersion.getBuildVersion("version-test"); | ||
assertEquals("1.0.0-TEST", version); | ||
} | ||
|
||
@Test | ||
void notFoundVersionFile() { | ||
var e = assertThrows(IOException.class, () -> { | ||
TanzawaVersion.getProperties("not-found"); | ||
}); | ||
assertEquals("version file load error. module=tanzawa-not-found", e.getMessage()); | ||
var c = (FileNotFoundException) e.getCause(); | ||
assertEquals("missing version file. path=/META-INF/tsurugidb/tanzawa-not-found.properties", c.getMessage()); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
modules/core/src/test/resources/META-INF/tsurugidb/tanzawa-version-test.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Build-Jdk=11.0.14.1+1 Eclipse Temurin 11.0.14.1+1 | ||
Build-OS=Windows 10 amd64 10.0 | ||
Build-Revision=abcdefg | ||
Build-Timestamp=2023-10-24T12\:52\:36.203+0900 | ||
Build-Version=1.0.0-TEST | ||
Created-By=Gradle 8.3 |