LicenseUtils { public static String getLicenseText(Locale locale, Collection collection, Item item, EPerson eperson, Map<String, Object> additionalInfo) { Formatter formatter = new Formatter(locale); int numArgs = 7 + (additionalInfo != null ? additionalInfo.size() : 0); Object[] args = new Object[numArgs]; args[0] = eperson.getFirstName(); args[1] = eperson.getLastName(); args[2] = eperson.getEmail(); args[3] = new java.util.Date(); args[4] = new FormattableArgument("collection", collection); args[5] = new FormattableArgument("item", item); args[6] = new FormattableArgument("eperson", eperson); if (additionalInfo != null) { int i = 7; for (Map.Entry<String, Object> info : additionalInfo.entrySet()) { args[i] = new FormattableArgument(info.getKey(), info.getValue()); i++; } } String licenseTemplate = collectionService.getLicense(collection); return formatter.format(licenseTemplate, args).toString(); } private  LicenseUtils(); static String getLicenseText(Locale locale, Collection collection,
                                        Item item, EPerson eperson, Map<String, Object> additionalInfo); static String getLicenseText(Locale locale, Collection collection,
                                        Item item, EPerson eperson); static void grantLicense(Context context, Item item,
                                    String licenseText, String acceptanceDate);  }
@Test public void testGetLicenseText_5args() throws SQLException, AuthorizeException, IOException { Locale locale; Collection collection; Item item; EPerson person; Map<String, Object> additionalInfo; String template = "Template license: %1$s %2$s %3$s %5$s %6$s"; String templateLong = "Template license: %1$s %2$s %3$s %5$s %6$s %8$s %9$s %10$s %11$s"; String templateResult = "Template license: first name last name testgetlicensetext_5args@email.com "; String templateLongResult = "Template license: first name last name testgetlicensetext_5args@email.com arg1" + " arg2 arg3 arg4"; String defaultLicense = licenseService.getDefaultSubmissionLicense(); context.turnOffAuthorisationSystem(); person = ePersonService.create(context); person.setFirstName(context, "first name"); person.setLastName(context, "last name"); person.setEmail("testGetLicenseText_5args@email.com"); locale = Locale.ENGLISH; collection = collectionService.create(context, owningCommunity); item = installItemService.installItem(context, workspaceItemService.create(context, collection, false)); additionalInfo = null; assertThat("testGetLicenseText_5args 0", LicenseUtils.getLicenseText(locale, collection, item, person, additionalInfo), equalTo(defaultLicense)); locale = Locale.GERMAN; collection = collectionService.create(context, owningCommunity); item = installItemService.installItem(context, workspaceItemService.create(context, collection, false)); additionalInfo = null; assertThat("testGetLicenseText_5args 1", LicenseUtils.getLicenseText(locale, collection, item, person, additionalInfo), equalTo(defaultLicense)); locale = Locale.ENGLISH; collection = collectionService.create(context, owningCommunity); item = installItemService.installItem(context, workspaceItemService.create(context, collection, false)); additionalInfo = new HashMap<String, Object>(); additionalInfo.put("arg1", "arg1"); additionalInfo.put("arg2", "arg2"); additionalInfo.put("arg3", "arg3"); assertThat("testGetLicenseText_5args 2", LicenseUtils.getLicenseText(locale, collection, item, person, additionalInfo), equalTo(defaultLicense)); locale = Locale.ENGLISH; collection = collectionService.create(context, owningCommunity); collection.setLicense(context, template); item = installItemService.installItem(context, workspaceItemService.create(context, collection, false)); additionalInfo = null; assertThat("testGetLicenseText_5args 3", LicenseUtils.getLicenseText(locale, collection, item, person, additionalInfo), equalTo(templateResult)); locale = Locale.GERMAN; collection = collectionService.create(context, owningCommunity); collection.setLicense(context, template); item = installItemService.installItem(context, workspaceItemService.create(context, collection, false)); additionalInfo = null; assertThat("testGetLicenseText_5args 4", LicenseUtils.getLicenseText(locale, collection, item, person, additionalInfo), equalTo(templateResult)); locale = Locale.ENGLISH; collection = collectionService.create(context, owningCommunity); collection.setLicense(context, templateLong); item = installItemService.installItem(context, workspaceItemService.create(context, collection, false)); additionalInfo = new LinkedHashMap<String, Object>(); additionalInfo.put("arg1", "arg1"); additionalInfo.put("arg2", "arg2"); additionalInfo.put("arg3", "arg3"); additionalInfo.put("arg4", "arg4"); assertThat("testGetLicenseText_5args 5", LicenseUtils.getLicenseText(locale, collection, item, person, additionalInfo), equalTo(templateLongResult)); context.restoreAuthSystemState(); }
@Test public void testGetLicenseText_4args() throws SQLException, AuthorizeException, IOException { Locale locale; Collection collection; Item item; EPerson person; String template = "Template license: %1$s %2$s %3$s %5$s %6$s"; String templateResult = "Template license: first name last name testgetlicensetext_4args@email.com "; context.turnOffAuthorisationSystem(); person = ePersonService.create(context); person.setFirstName(context, "first name"); person.setLastName(context, "last name"); person.setEmail("testGetLicenseText_4args@email.com"); ePersonService.update(context, person); String defaultLicense = licenseService.getDefaultSubmissionLicense(); context.turnOffAuthorisationSystem(); locale = Locale.ENGLISH; collection = collectionService.create(context, owningCommunity); item = installItemService.installItem(context, workspaceItemService.create(context, collection, false)); assertThat("testGetLicenseText_5args 0", LicenseUtils.getLicenseText(locale, collection, item, person), equalTo(defaultLicense)); locale = Locale.GERMAN; collection = collectionService.create(context, owningCommunity); item = installItemService.installItem(context, workspaceItemService.create(context, collection, false)); assertThat("testGetLicenseText_5args 1", LicenseUtils.getLicenseText(locale, collection, item, person), equalTo(defaultLicense)); locale = Locale.ENGLISH; collection = collectionService.create(context, owningCommunity); collection.setLicense(context, template); item = installItemService.installItem(context, workspaceItemService.create(context, collection, false)); assertThat("testGetLicenseText_5args 3", LicenseUtils.getLicenseText(locale, collection, item, person), equalTo(templateResult)); locale = Locale.GERMAN; collection = collectionService.create(context, owningCommunity); collection.setLicense(context, template); item = installItemService.installItem(context, workspaceItemService.create(context, collection, false)); assertThat("testGetLicenseText_5args 4", LicenseUtils.getLicenseText(locale, collection, item, person), equalTo(templateResult)); context.restoreAuthSystemState(); }