001/* 002 * Units of Measurement API 003 * Copyright (c) 2014-2023, Jean-Marie Dautelle, Werner Keil, Otavio Santana. 004 * 005 * All rights reserved. 006 * 007 * Redistribution and use in source and binary forms, with or without modification, 008 * are permitted provided that the following conditions are met: 009 * 010 * 1. Redistributions of source code must retain the above copyright notice, 011 * this list of conditions and the following disclaimer. 012 * 013 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions 014 * and the following disclaimer in the documentation and/or other materials provided with the distribution. 015 * 016 * 3. Neither the name of JSR-385 nor the names of its contributors may be used to endorse or promote products 017 * derived from this software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 023 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package javax.measure.test.format; 031 032import static org.junit.jupiter.api.Assertions.*; 033 034import java.io.IOException; 035 036import javax.measure.Quantity; 037import javax.measure.Unit; 038import javax.measure.format.MeasurementParseException; 039import javax.measure.format.UnitFormat; 040import javax.measure.quantity.Length; 041import javax.measure.quantity.Speed; 042import javax.measure.test.quantity.DistanceQuantity; 043import javax.measure.test.unit.DistanceUnit; 044import javax.measure.test.unit.SpeedUnit; 045import javax.measure.test.unit.TimeUnit; 046 047import org.junit.jupiter.api.BeforeEach; 048import org.junit.jupiter.api.Test; 049 050/** 051 * @author <a href="mailto:werner@uom.technology">Werner Keil</a> 052 * 053 */ 054public class UnitFormatTest { 055 private Quantity<Length> sut; 056 private UnitFormat format; 057 058 @BeforeEach 059 public void init() { 060 sut = new DistanceQuantity(10, DistanceUnit.m); 061 format = SimpleTestUnitFormat.getInstance(); 062 } 063 064 @Test 065 public void testFormatKph() { 066 Unit<Speed> kph = SpeedUnit.kmh; 067 assertEquals("km/h", kph.toString()); 068 } 069 070 @Test 071 public void testParseSimple() { 072 assertThrows(MeasurementParseException.class, () -> { 073 Unit<?> u = format.parse("s"); 074 assertNotNull(u); 075 assertEquals("s", u.getSymbol()); 076 }); 077 } 078 079 @Test 080 public void testFormatFromQuantity() { 081 assertThrows(IllegalArgumentException.class, () -> { 082 final Appendable a = new StringBuilder(); 083 try { 084 format.format(DistanceUnit.m, a); 085 } catch (IOException e) { 086 fail(e.getMessage()); 087 } 088 assertEquals(DistanceUnit.m, sut.getUnit()); 089 assertEquals("m", a.toString()); 090 091 final Appendable a2 = new StringBuilder(); 092 @SuppressWarnings("unchecked") 093 Unit<Speed> v = (Unit<Speed>) sut.getUnit().divide(TimeUnit.s); 094 try { 095 format.format(v, a2); 096 } catch (IOException e) { 097 fail(e.getMessage()); 098 } 099 assertEquals("m/s", a2.toString()); 100 }); 101 102 } 103 104 @Test 105 public void testParseIrregularString() { 106 assertThrows(MeasurementParseException.class, () -> { 107 @SuppressWarnings("unused") 108 Unit<?> u = format.parse("bl//^--1a"); 109 }); 110 } 111 112 @Test 113 public void testParserException() { 114 assertThrows(MeasurementParseException.class, () -> { 115 throw new MeasurementParseException(new IllegalArgumentException()); 116 }); 117 } 118 119 @Test 120 public void testParserExceptionWithPosition() { 121 MeasurementParseException pe = assertThrows(MeasurementParseException.class, () -> { 122 throw new MeasurementParseException("test", 1); 123 }); 124 assertEquals(1, pe.getPosition()); 125 assertEquals("test", pe.getParsedString()); 126 } 127 128 @Test 129 public void testParserExceptionWithNullString() { 130 final MeasurementParseException pe = assertThrows(MeasurementParseException.class, () -> { 131 throw new MeasurementParseException(null, 0); 132 }); 133 assertEquals(0, pe.getPosition()); 134 assertNull(pe.getParsedString()); 135 } 136 137 @Test 138 public void testLocalSensitive() { 139 assertFalse(format.isLocaleSensitive()); 140 } 141 142 @Test 143 public void testMoreLocalSensitive() { 144 final UnitFormat simple = SimpleTestUnitFormat.getInstance(); 145 assertFalse(simple.isLocaleSensitive()); 146 } 147}