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.spi; 031 032import org.junit.jupiter.api.Test; 033 034import static org.junit.jupiter.api.Assertions.*; 035import static javax.measure.spi.FormatService.FormatType.UNIT_FORMAT; 036 037import java.util.HashMap; 038import java.util.Map; 039import java.util.Objects; 040import java.util.Set; 041 042import javax.measure.format.QuantityFormat; 043import javax.measure.format.UnitFormat; 044 045/** 046 * Tests for {@link FormatService}. 047 */ 048public class FormatServiceTest { 049 050 /** 051 * Tests {@link ServiceProvider#current()} and {@link ServiceProvider#setCurrent(ServiceProvider)}. The getter and setter are tested in a single 052 * method for avoiding issues with the order in which JUnit executes tests. 053 */ 054 @Test 055 public void testGetUnitFormats() { 056 FormatService service = new TestFormatService(); 057 assertEquals(0, service.getAvailableFormatNames(UNIT_FORMAT).size()); 058 } 059 060 /** 061 * Tests FormatService#FormatType. 062 */ 063 @Test 064 public void testTypes() { 065 assertEquals(2, FormatService.FormatType.values().length); 066 } 067 068 /** 069 * Test format service. 070 */ 071 private static final class TestFormatService implements FormatService { 072 private final Map<String, UnitFormat> unitFormats = new HashMap<>(); 073 private final Map<String, QuantityFormat> quantityFormats = new HashMap<>(); 074 075 /* 076 * (non-Javadoc) 077 * 078 * @see UnitFormatService#getUnitFormat(String) 079 */ 080 @Override 081 public UnitFormat getUnitFormat(String formatName) { 082 Objects.requireNonNull(formatName, "Format name required"); 083 return unitFormats.get(formatName); 084 } 085 086 /* 087 * (non-Javadoc) 088 * 089 * @see UnitFormatService#getUnitFormat(String) 090 */ 091 @Override 092 public UnitFormat getUnitFormat(String formatName, String variant) { 093 Objects.requireNonNull(formatName, "Format name required"); 094 return unitFormats.get(formatName); 095 } 096 097 /* 098 * (non-Javadoc) 099 * 100 * @see UnitFormatService#getUnitFormat() 101 */ 102 @Override 103 public UnitFormat getUnitFormat() { 104 return getUnitFormat(""); 105 } 106 107 public Set<String> getAvailableFormatNames() { 108 return getAvailableFormatNames(FormatType.UNIT_FORMAT); 109 } 110 111 @Override 112 public QuantityFormat getQuantityFormat() { 113 return getQuantityFormat(""); 114 } 115 116 @Override 117 public QuantityFormat getQuantityFormat(String name) { 118 Objects.requireNonNull(name, "Format name required"); 119 return quantityFormats.get(name); 120 } 121 122 @Override 123 public Set<String> getAvailableFormatNames(FormatType type) { 124 switch (type) { 125 case QUANTITY_FORMAT: 126 return quantityFormats.keySet(); 127 default: 128 return unitFormats.keySet(); 129 } 130 } 131 } 132}