LicenseHandler implements LicenseMatcher { public DbLicense resolve(final String licenseId) { for (final Entry<String, DbLicense> regexp : licensesRegexp.entrySet()) { try { if (licenseId.matches(regexp.getKey())) { return regexp.getValue(); } } catch (PatternSyntaxException e) { LOG.error("Wrong pattern for the following license " + regexp.getValue().getName(), e); continue; } } if(LOG.isWarnEnabled()) { LOG.warn(String.format("No matching pattern for license %s", licenseId)); } return null; } LicenseHandler(final RepositoryHandler repoHandler); void store(final DbLicense dbLicense); List<String> getLicensesNames(final FiltersHolder filters); DbLicense getLicense(final String name); void deleteLicense(final String licName); void approveLicense(final String name, final Boolean approved); DbLicense resolve(final String licenseId); List<License> getLicenses(); Set<DbLicense> resolveLicenses(List<String> licStrings); @Override Set<DbLicense> getMatchingLicenses(String licenseString);  }
@Test public void resolveLicense(){ final DbLicense license = new DbLicense(); license.setName("Test"); license.setRegexp("\\w*"); final RepositoryHandler repoHandler = mock(RepositoryHandler.class); when(repoHandler.getAllLicenses()).thenReturn(Collections.singletonList(license)); final LicenseHandler licenseHandler = new LicenseHandler(repoHandler); assertEquals(license, licenseHandler.resolve(license.getName())); }
@Test public void ifLicenseDoesNotHaveRegexpItUsesLicenseName(){ final DbLicense license = new DbLicense(); license.setName("Test"); final RepositoryHandler repoHandler = mock(RepositoryHandler.class); when(repoHandler.getAllLicenses()).thenReturn(Collections.singletonList(license)); final LicenseHandler licenseHandler = new LicenseHandler(repoHandler); assertEquals(license, licenseHandler.resolve(license.getName())); assertEquals(null, licenseHandler.resolve("Test2")); }
@Test public void doesNotFailEvenWithWrongPattern(){ final DbLicense license = new DbLicense(); license.setName("Test"); license.setRegexp("x^["); final RepositoryHandler repoHandler = mock(RepositoryHandler.class); when(repoHandler.getAllLicenses()).thenReturn(Collections.singletonList(license)); final LicenseHandler licenseHandler = new LicenseHandler(repoHandler); Exception exception = null; DbLicense resolvedLicense = null; try{ resolvedLicense = licenseHandler.resolve(license.getName()); } catch (Exception e){ exception = e; } assertEquals(null, exception); assertEquals(null, resolvedLicense); }