VersionsHandler { public String getLastVersion(final Collection<String> versions) { final List<Version> sorted = versions.stream() .filter(Version::isValid) .map(Version::make) .filter(Optional::isPresent) .map(Optional::get) .sorted((v1, v2) -> { try { return v1.compare(v2); } catch (IncomparableException e) { return 0; } }) .collect(Collectors.toList()); if(sorted.isEmpty()) { if(LOG.isWarnEnabled()) { LOG.warn(String.format("Cannot obtain last version from collection %s", versions.toString())); } return null; } return sorted.get(sorted.size() - 1).toString(); } VersionsHandler(final RepositoryHandler repoHandler); boolean isUpToDate(final DbArtifact artifact); String getLastRelease(final Collection<String> versions); String getLastVersion(final Collection<String> versions);  }
@Test public void getTheLastVersionOfRegularVersions() throws IncomparableException { final List<String> versions = new ArrayList<String>(); versions.add("0.1.0-1"); versions.add("0.1.0-SNAPSHOT"); assertEquals("0.1.0-SNAPSHOT", versionsHandler.getLastVersion(versions)); versions.clear(); versions.add("2.1.0-1"); versions.add("0.1.0-SNAPSHOT"); versions.add("1.1.0-SNAPSHOT"); versions.add("2.0.0-SNAPSHOT"); assertEquals("2.1.0-1", versionsHandler.getLastVersion(versions)); }
@Test public void testInvalidVersionDoesNotCauseStackTrace1() throws IncomparableException { final List<String> versions = new ArrayList<String>(); versions.add("1.0.0-aisstudio-4-SNAPSHOT"); versions.add("1.2.0"); assertEquals("1.2.0", versionsHandler.getLastVersion(versions)); }
@Test public void testInvalidVersionDoesNotCauseStackTrace2() throws IncomparableException { final List<String> versions = new ArrayList<String>(); versions.add(null); versions.add("1.2.0"); assertEquals("1.2.0", versionsHandler.getLastVersion(versions)); }
@Test public void lastVersionDoesNotExist() throws UnknownHostException, IncomparableException { String lastVersion = versionsHandler.getLastVersion(new ArrayList<>()); assertNull(lastVersion); }