Commits

Zemian Deng committed 1c36713

Improved Version string loading for Issue#27.
(grafted from f60e212278fbd928cafa5b0756ad3209c0d6df34)

Comments (0)

Files changed (4)

timemachine-scheduler/src/main/java/timemachine/scheduler/support/VersionUtils.java

 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import timemachine.scheduler.SchedulerException;
-
 
 /**
  * A utility class to load and retrieve TimeMachine Scheduler version string.
  * @author Zemian Deng
  */
 public class VersionUtils {
+	private static Logger logger = LoggerFactory.getLogger(VersionUtils.class);
 	public static final String GROUP_ID = "org.bitbucket.timemachine";
 	public static final String ARTIFACT_ID = "timemachine-scheduler";
+	public static final String VERSION_DEV = "DEVELOPMENT";
+	public static final String VERSION_UNKNOWN = "UNKNOWN";
 	public static final String VERSION_FILE = "META-INF/maven/" + GROUP_ID + "/" + ARTIFACT_ID + "/pom.properties";
 	private static String schedulerVersion;
 	
 	synchronized public static String getSchedulerVersion() {
 		if (schedulerVersion == null) {
 			try {
+				//Load version value from pom.properties inside the jar.
 				URL versionResUrl = VersionUtils.class.getClassLoader().getResource(VERSION_FILE);
+				logger.debug("Loading version file {}", versionResUrl);
 				if (versionResUrl == null) {
-					schedulerVersion = "DEVELOPMENT";
+					// If we don't find it, then we must running in a unpack classpath, meaning in DEV mode.
+					schedulerVersion = VERSION_DEV;
 				} else {
+					// Let's read the file
 					Props props = new Props(versionResUrl);
-					schedulerVersion = props.getString("version");
-	
-					// Let's get the published date as well.
-					InputStream inStream = null;
-					try {
-						inStream = versionResUrl.openStream();
-						@SuppressWarnings("unchecked")
-						List<String> lines = IOUtils.readLines(inStream);
-						if (lines.size() >= 2 && lines.get(1).startsWith("#")) {
-							// it should be in this format: #Sun Feb 21 23:38:24 GMT 2010
-							String dateStr = lines.get(1).substring(1);
-							Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(dateStr);
-							schedulerVersion += "." + new SimpleDateFormat("MMddyyyyHHmm").format(date);
+					schedulerVersion = props.getString("version", VERSION_UNKNOWN);
+					
+					// If we have a version value, then try to get the date value as well.
+					if (!schedulerVersion.equals(VERSION_UNKNOWN)) {
+						// Let's get the published date, first try the date comment in file.
+						InputStream inStream = null;
+						try {
+							inStream = versionResUrl.openStream();
+							@SuppressWarnings("unchecked")
+							List<String> lines = IOUtils.readLines(inStream);
+							if (lines.size() >= 2 && lines.get(1).startsWith("#")) {
+								// it should be in this format: #Sun Feb 21 23:38:24 GMT 2010
+								String dateStr = lines.get(1).substring(1).trim();
+								Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(dateStr);
+								schedulerVersion += "." + new SimpleDateFormat("MMddyyyyHHmm").format(date);
+							}
+						} catch (Exception e) {
+							logger.warn("Unable to read published date string.", e);
+							// Something must be not right in pom.properties, but we will ignore the date part in version string.
+							schedulerVersion += "." + VERSION_UNKNOWN;
+						} finally {
+							if (inStream != null)
+								IOUtils.closeQuietly(inStream);
 						}
-					} finally {
-						if (inStream != null)
-							IOUtils.closeQuietly(inStream);
 					}
 				}
 			} catch (Exception e) {
-				throw new SchedulerException("Failed  to read version file to get published date.", e);
+				logger.warn("Unable to read " + VERSION_FILE, e);
+				// We will set version value to UNKOWN and move on.
+				schedulerVersion = VERSION_UNKNOWN;
 			}
 		}
 		return schedulerVersion;

timemachine-scheduler/src/test/java/unit/timemachine/scheduler/support/VersionUtilsTest.java

+package unit.timemachine.scheduler.support;
+
+import java.io.File;
+import java.util.List;
+
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Test;
+
+import timemachine.scheduler.support.ProcessUtils;
+import timemachine.scheduler.support.VersionUtils;
+
+public class VersionUtilsTest {
+
+	@Test
+	public void testNormalLoadingVersionString() throws Exception {
+		String origClasspath = System.getProperty("java.class.path");
+		try {
+			String testClasspath = "src/test/resources/unit/timemachine/scheduler/support/VersionUtilsTestClasspath";
+			String pathSep = File.pathSeparator;
+			System.setProperty("java.class.path", testClasspath + pathSep + origClasspath);
+			List<String> result = ProcessUtils.runJava(-1, new String[]{ Main.class.getName() });
+			Assert.assertThat(result.get(0).matches("0.0.0.\\d{12}"), Matchers.is(true));
+		} finally {
+			System.setProperty("java.class.path", origClasspath);
+		}
+	}
+	
+	@Test
+	public void testBadLoadingVersionString() throws Exception {
+		String origClasspath = System.getProperty("java.class.path");
+		try {
+			String testClasspath = "src/test/resources/unit/timemachine/scheduler/support/VersionUtilsTestClasspath-bad";
+			String pathSep = File.pathSeparator;
+			System.setProperty("java.class.path", testClasspath + pathSep + origClasspath);
+			List<String> result = ProcessUtils.runJava(-1, new String[]{ Main.class.getName() });
+			Assert.assertThat(result.get(result.size() - 1).matches("0.0.0.UNKNOWN"), Matchers.is(true));
+		} finally {
+			System.setProperty("java.class.path", origClasspath);
+		}
+	}
+	
+	public static class Main {
+		public static void main(String[] args) {
+			System.out.println(VersionUtils.getSchedulerVersion());
+		}
+	}
+}

timemachine-scheduler/src/test/resources/unit/timemachine/scheduler/support/VersionUtilsTestClasspath-bad/META-INF/maven/org.bitbucket.timemachine/timemachine-scheduler/pom.properties

+#Generated by Maven Integration for Eclipse
+#BAD_DATE
+version=0.0.0
+groupId=org.bitbucket.timemachine
+m2e.projectName=timemachine-scheduler
+m2e.projectLocation=/Users/zemian/projects/scheduler/timemachine-scheduler
+artifactId=timemachine-scheduler

timemachine-scheduler/src/test/resources/unit/timemachine/scheduler/support/VersionUtilsTestClasspath/META-INF/maven/org.bitbucket.timemachine/timemachine-scheduler/pom.properties

+#Generated by Maven Integration for Eclipse
+#Sun Jan 01 00:00:00 EST 2012
+version=0.0.0
+groupId=org.bitbucket.timemachine
+m2e.projectName=timemachine-scheduler
+m2e.projectLocation=/Users/zemian/projects/scheduler/timemachine-scheduler
+artifactId=timemachine-scheduler