Length { public Length(double value, String unit) { this.value = value; this.unit = unit.toLowerCase(); } Length(double value, String unit);  Length(String text, String defaultUnit);  Length(String text); String getUnit(); double getValue(); double getValueAsMillimeter(); static Set<String> getSupportedUnits(); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString(); static final String INCH; static final String POINT; static final String CM; static final String MM; static final String MW; }
@Test public void testLength() throws Exception { Length l = new Length(1.77, "cm"); assertNotNull(l); assertEquals(1.77, l.getValue(), 0.001); assertEquals("cm", l.getUnit()); l = new Length("1.77cm"); assertNotNull(l); assertEquals(1.77, l.getValue(), 0.001); assertEquals("cm", l.getUnit()); l = new Length("1.77 cm"); assertNotNull(l); assertEquals(1.77, l.getValue(), 0.001); assertEquals("cm", l.getUnit()); l = new Length("1.77 cm gugus"); assertNotNull(l); assertEquals(1.77, l.getValue(), 0.001); assertEquals("cm", l.getUnit()); l = new Length("2,33", "mm"); assertNotNull(l); assertEquals(2.33, l.getValue(), 0.001); assertEquals("mm", l.getUnit()); l = new Length("2,33pt", "mm"); assertNotNull(l); assertEquals(2.33, l.getValue(), 0.001); assertEquals("pt", l.getUnit()); try { l = new Length(null); fail("Expected NPE on null parameter"); } catch (NullPointerException npe) { } try { l = new Length("garbage"); fail("Expected IllegalArgumentException on garbage parameter"); } catch (IllegalArgumentException iae) { } try { l = new Length("2.33"); fail("Expected IllegalArgumentException on incomplete parameter"); } catch (IllegalArgumentException iae) { } l = new Length("2.34cm"); assertEquals(23.4, l.getValueAsMillimeter(), 0.001); l = new Length("2.835pt"); assertEquals(1, l.getValueAsMillimeter(), 0.001); l = new Length("0.0393700in"); assertEquals(1, l.getValueAsMillimeter(), 0.001); }