|
| 1 | +package com.baeldung.measurement; |
| 2 | + |
| 3 | +import javax.measure.Quantity; |
| 4 | +import javax.measure.quantity.Area; |
| 5 | +import javax.measure.quantity.Length; |
| 6 | +import javax.measure.quantity.Pressure; |
| 7 | +import javax.measure.quantity.Volume; |
| 8 | + |
| 9 | +import javax.measure.Unit; |
| 10 | +import javax.measure.UnitConverter; |
| 11 | + |
| 12 | +import static org.junit.Assert.assertEquals; |
| 13 | +import static org.junit.Assert.assertTrue; |
| 14 | +import org.junit.Test; |
| 15 | + |
| 16 | +import com.baeldung.measurement.WaterTank; |
| 17 | + |
| 18 | +import tec.units.ri.format.SimpleUnitFormat; |
| 19 | +import tec.units.ri.quantity.Quantities; |
| 20 | +import tec.units.ri.unit.MetricPrefix; |
| 21 | +import static tec.units.ri.unit.Units.*; |
| 22 | + |
| 23 | +public class WaterTankTests { |
| 24 | + |
| 25 | + @Test |
| 26 | + public void givenQuantity_whenGetUnitAndConvertValue_thenSuccess() { |
| 27 | + WaterTank waterTank = new WaterTank(); |
| 28 | + waterTank.setCapacityMeasure(Quantities.getQuantity(9.2, LITRE)); |
| 29 | + assertEquals(LITRE, waterTank.getCapacityMeasure().getUnit()); |
| 30 | + |
| 31 | + Quantity<Volume> waterCapacity = waterTank.getCapacityMeasure(); |
| 32 | + double volumeInLitre = waterCapacity.getValue().doubleValue(); |
| 33 | + assertEquals(9.2, volumeInLitre, 0.0f); |
| 34 | + |
| 35 | + double volumeInMilliLitre = waterCapacity.to(MetricPrefix.MILLI(LITRE)).getValue().doubleValue(); |
| 36 | + assertEquals(9200.0, volumeInMilliLitre, 0.0f); |
| 37 | + |
| 38 | + // compilation error |
| 39 | + // volumeInMilliLitre = waterCapacity.to(MetricPrefix.MILLI(KILOGRAM)); |
| 40 | + |
| 41 | + Unit<Length> Kilometer = MetricPrefix.KILO(METRE); |
| 42 | + |
| 43 | + // compilation error |
| 44 | + // Unit<Length> Centimeter = MetricPrefix.CENTI(LITRE); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void givenUnit_whenAlternateUnit_ThenGetAlternateUnit() { |
| 49 | + |
| 50 | + Unit<Pressure> PASCAL = NEWTON.divide(METRE.pow(2)).alternate("Pa").asType(Pressure.class); |
| 51 | + assertTrue(SimpleUnitFormat.getInstance().parse("Pa").equals(PASCAL)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void givenUnit_whenProduct_ThenGetProductUnit() { |
| 56 | + Unit<Area> squareMetre = METRE.multiply(METRE).asType(Area.class); |
| 57 | + Quantity<Length> line = Quantities.getQuantity(2, METRE); |
| 58 | + assertEquals(line.multiply(line).getUnit(), squareMetre); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void givenMeters_whenConvertToKilometer_ThenConverted() { |
| 63 | + double distanceInMeters = 50.0; |
| 64 | + UnitConverter metreToKilometre = METRE.getConverterTo(MetricPrefix.KILO(METRE)); |
| 65 | + double distanceInKilometers = metreToKilometre.convert(distanceInMeters); |
| 66 | + assertEquals(0.05, distanceInKilometers, 0.00f); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void givenSymbol_WhenCompareToSystemUnit_ThenSuccess() { |
| 71 | + assertTrue(SimpleUnitFormat.getInstance().parse("kW").equals(MetricPrefix.KILO(WATT))); |
| 72 | + assertTrue(SimpleUnitFormat.getInstance().parse("ms").equals(SECOND.divide(1000))); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void givenUnits_WhenAdd_ThenSuccess() { |
| 77 | + Quantity<Length> total = Quantities.getQuantity(2, METRE).add(Quantities.getQuantity(3, METRE)); |
| 78 | + assertEquals(total.getValue().intValue(), 5); |
| 79 | + |
| 80 | + // compilation error |
| 81 | + // Quantity<Length> total = Quantities.getQuantity(2, METRE).add(Quantities.getQuantity(3, LITRE)); |
| 82 | + |
| 83 | + Quantity<Length> totalKm = Quantities.getQuantity(2, METRE).add(Quantities.getQuantity(3, MetricPrefix.KILO(METRE))); |
| 84 | + assertEquals(totalKm.getValue().intValue(), 3002); |
| 85 | + } |
| 86 | +} |
0 commit comments