@Test(expected = NumberFormatException.class) public void parseTooBigNumberHours() throws Exception { Element connectionLeasingElement = getXMLConfigurationElement("2562048", "hours"); LeaseConfigurationParser parser = new LeaseConfigurationParser(); parser.parse(connectionLeasingElement, "source"); }
@Override public LeaseConfiguration parse(Element element, String source) { LeaseElement leaseElement = parser().apply(element); String leaseLengthString = leaseElement.getLeaseValue(); if (leaseLengthString.compareToIgnoreCase(MAX) == 0) { return new LeaseConfiguration(MAX_LEASE_LENGTH); } String timeUnitString = leaseElement.getTimeUnit(); TimeUnit timeUnit; try { timeUnit = TimeUnit.valueOf(timeUnitString.toUpperCase()); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Unknown time unit: " + timeUnitString + ". Must be one of " + VALID_TIME_UNITS_STRING + ".", e); } if (!VALID_TIME_UNITS.contains(timeUnit)) { throw new IllegalArgumentException("Invalid lease time unit: " + timeUnitString + ". Must be one of " + VALID_TIME_UNITS_STRING + "."); } long leaseLength = Long.parseLong(leaseLengthString); long maxLeaseLengthInMatchingUnits = timeUnit.convert(MAX_LEASE_LENGTH, MILLISECONDS); if (leaseLength > maxLeaseLengthInMatchingUnits) { throw new NumberFormatException("Lease length in " + timeUnitString + " must be less than or equal to: " + maxLeaseLengthInMatchingUnits); } return new LeaseConfiguration(TimeUnit.MILLISECONDS.convert(leaseLength, timeUnit)); }
LeaseConfigurationParser implements ServiceConfigParser { @Override public LeaseConfiguration parse(Element element, String source) { LeaseElement leaseElement = parser().apply(element); String leaseLengthString = leaseElement.getLeaseValue(); if (leaseLengthString.compareToIgnoreCase(MAX) == 0) { return new LeaseConfiguration(MAX_LEASE_LENGTH); } String timeUnitString = leaseElement.getTimeUnit(); TimeUnit timeUnit; try { timeUnit = TimeUnit.valueOf(timeUnitString.toUpperCase()); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Unknown time unit: " + timeUnitString + ". Must be one of " + VALID_TIME_UNITS_STRING + ".", e); } if (!VALID_TIME_UNITS.contains(timeUnit)) { throw new IllegalArgumentException("Invalid lease time unit: " + timeUnitString + ". Must be one of " + VALID_TIME_UNITS_STRING + "."); } long leaseLength = Long.parseLong(leaseLengthString); long maxLeaseLengthInMatchingUnits = timeUnit.convert(MAX_LEASE_LENGTH, MILLISECONDS); if (leaseLength > maxLeaseLengthInMatchingUnits) { throw new NumberFormatException("Lease length in " + timeUnitString + " must be less than or equal to: " + maxLeaseLengthInMatchingUnits); } return new LeaseConfiguration(TimeUnit.MILLISECONDS.convert(leaseLength, timeUnit)); } }
LeaseConfigurationParser implements ServiceConfigParser { @Override public LeaseConfiguration parse(Element element, String source) { LeaseElement leaseElement = parser().apply(element); String leaseLengthString = leaseElement.getLeaseValue(); if (leaseLengthString.compareToIgnoreCase(MAX) == 0) { return new LeaseConfiguration(MAX_LEASE_LENGTH); } String timeUnitString = leaseElement.getTimeUnit(); TimeUnit timeUnit; try { timeUnit = TimeUnit.valueOf(timeUnitString.toUpperCase()); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Unknown time unit: " + timeUnitString + ". Must be one of " + VALID_TIME_UNITS_STRING + ".", e); } if (!VALID_TIME_UNITS.contains(timeUnit)) { throw new IllegalArgumentException("Invalid lease time unit: " + timeUnitString + ". Must be one of " + VALID_TIME_UNITS_STRING + "."); } long leaseLength = Long.parseLong(leaseLengthString); long maxLeaseLengthInMatchingUnits = timeUnit.convert(MAX_LEASE_LENGTH, MILLISECONDS); if (leaseLength > maxLeaseLengthInMatchingUnits) { throw new NumberFormatException("Lease length in " + timeUnitString + " must be less than or equal to: " + maxLeaseLengthInMatchingUnits); } return new LeaseConfiguration(TimeUnit.MILLISECONDS.convert(leaseLength, timeUnit)); }  }
LeaseConfigurationParser implements ServiceConfigParser { @Override public LeaseConfiguration parse(Element element, String source) { LeaseElement leaseElement = parser().apply(element); String leaseLengthString = leaseElement.getLeaseValue(); if (leaseLengthString.compareToIgnoreCase(MAX) == 0) { return new LeaseConfiguration(MAX_LEASE_LENGTH); } String timeUnitString = leaseElement.getTimeUnit(); TimeUnit timeUnit; try { timeUnit = TimeUnit.valueOf(timeUnitString.toUpperCase()); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Unknown time unit: " + timeUnitString + ". Must be one of " + VALID_TIME_UNITS_STRING + ".", e); } if (!VALID_TIME_UNITS.contains(timeUnit)) { throw new IllegalArgumentException("Invalid lease time unit: " + timeUnitString + ". Must be one of " + VALID_TIME_UNITS_STRING + "."); } long leaseLength = Long.parseLong(leaseLengthString); long maxLeaseLengthInMatchingUnits = timeUnit.convert(MAX_LEASE_LENGTH, MILLISECONDS); if (leaseLength > maxLeaseLengthInMatchingUnits) { throw new NumberFormatException("Lease length in " + timeUnitString + " must be less than or equal to: " + maxLeaseLengthInMatchingUnits); } return new LeaseConfiguration(TimeUnit.MILLISECONDS.convert(leaseLength, timeUnit)); }  @Override Source getXmlSchema(); @Override URI getNamespace(); @Override LeaseConfiguration parse(Element element, String source); Function<Element, LeaseElement> parser(); ConfigValidator getConfigValidator(); }
LeaseConfigurationParser implements ServiceConfigParser { @Override public LeaseConfiguration parse(Element element, String source) { LeaseElement leaseElement = parser().apply(element); String leaseLengthString = leaseElement.getLeaseValue(); if (leaseLengthString.compareToIgnoreCase(MAX) == 0) { return new LeaseConfiguration(MAX_LEASE_LENGTH); } String timeUnitString = leaseElement.getTimeUnit(); TimeUnit timeUnit; try { timeUnit = TimeUnit.valueOf(timeUnitString.toUpperCase()); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Unknown time unit: " + timeUnitString + ". Must be one of " + VALID_TIME_UNITS_STRING + ".", e); } if (!VALID_TIME_UNITS.contains(timeUnit)) { throw new IllegalArgumentException("Invalid lease time unit: " + timeUnitString + ". Must be one of " + VALID_TIME_UNITS_STRING + "."); } long leaseLength = Long.parseLong(leaseLengthString); long maxLeaseLengthInMatchingUnits = timeUnit.convert(MAX_LEASE_LENGTH, MILLISECONDS); if (leaseLength > maxLeaseLengthInMatchingUnits) { throw new NumberFormatException("Lease length in " + timeUnitString + " must be less than or equal to: " + maxLeaseLengthInMatchingUnits); } return new LeaseConfiguration(TimeUnit.MILLISECONDS.convert(leaseLength, timeUnit)); }  @Override Source getXmlSchema(); @Override URI getNamespace(); @Override LeaseConfiguration parse(Element element, String source); Function<Element, LeaseElement> parser(); ConfigValidator getConfigValidator();  }