### Question:
Version { public static String getVersion(Class<?> baseClass) { Manifest manifest; try { manifest = getManifest(baseClass); } catch (IOException e) { logger.error(e.getMessage(), e); return UNKNOWN_VERSION; } return getVersion(manifest); } private  Version(); static String getVersion(Class<?> baseClass); static final String UNKNOWN_VERSION; }### Answer:
@Test public void testWithNullManifest() { String version = Version.getVersion((Manifest) null); assertThat(version).isEqualTo("unknown"); }
@Test public void testWithNoImplementationVersion() { Manifest manifest = mock(Manifest.class); Attributes attributes = mock(Attributes.class); when(manifest.getMainAttributes()).thenReturn(attributes); String version = Version.getVersion(manifest); assertThat(version).isEqualTo("unknown"); }
@Test public void testWithNonSnapshotVersion() { Manifest manifest = mock(Manifest.class); Attributes attributes = mock(Attributes.class); when(manifest.getMainAttributes()).thenReturn(attributes); when(attributes.getValue("Implementation-Version")).thenReturn("0.1.2"); String version = Version.getVersion(manifest); assertThat(version).isEqualTo("0.1.2"); }
@Test public void testWithSnapshotVersion() { Manifest manifest = mock(Manifest.class); Attributes attributes = mock(Attributes.class); when(manifest.getMainAttributes()).thenReturn(attributes); when(attributes.getValue("Implementation-Version")).thenReturn("0.1.2-SNAPSHOT"); when(attributes.getValue("Build-Commit")) .thenReturn("0123456789abcdeabcde0123456789abcdeabcde"); when(attributes.getValue("Build-Time")).thenReturn("xyz"); String version = Version.getVersion(manifest); assertThat(version).isEqualTo("0.1.2-SNAPSHOT, commit 0123456789, built xyz"); }
@Test public void testWithNoneBuildCommit() { Manifest manifest = mock(Manifest.class); Attributes attributes = mock(Attributes.class); when(manifest.getMainAttributes()).thenReturn(attributes); when(attributes.getValue("Implementation-Version")).thenReturn("0.1.2-SNAPSHOT"); when(attributes.getValue("Build-Commit")).thenReturn("[none]"); when(attributes.getValue("Build-Time")).thenReturn("xyz"); String version = Version.getVersion(manifest); assertThat(version).isEqualTo("0.1.2-SNAPSHOT, built xyz"); }
@Test public void testWithInvalidBuildCommit() { Manifest manifest = mock(Manifest.class); Attributes attributes = mock(Attributes.class); when(manifest.getMainAttributes()).thenReturn(attributes); when(attributes.getValue("Implementation-Version")).thenReturn("0.1.2-SNAPSHOT"); when(attributes.getValue("Build-Commit")).thenReturn("tooshort"); when(attributes.getValue("Build-Time")).thenReturn("xyz"); String version = Version.getVersion(manifest); assertThat(version).isEqualTo("0.1.2-SNAPSHOT, built xyz"); }
@Test public void testWithMissingBuildTime() { Manifest manifest = mock(Manifest.class); Attributes attributes = mock(Attributes.class); when(manifest.getMainAttributes()).thenReturn(attributes); when(attributes.getValue("Implementation-Version")).thenReturn("0.1.2-SNAPSHOT"); when(attributes.getValue("Build-Commit")).thenReturn("tooshort"); String version = Version.getVersion(manifest); assertThat(version).isEqualTo("0.1.2-SNAPSHOT"); }
@Test public void testWithInvalidBuildCommitAndMissingBuildTime() { Manifest manifest = mock(Manifest.class); Attributes attributes = mock(Attributes.class); when(manifest.getMainAttributes()).thenReturn(attributes); when(attributes.getValue("Implementation-Version")).thenReturn("0.1.2-SNAPSHOT"); when(attributes.getValue("Build-Commit")).thenReturn("tooshort"); String version = Version.getVersion(manifest); assertThat(version).isEqualTo("0.1.2-SNAPSHOT"); }