001/* 002 * Units of Measurement TCK 003 * Copyright © 2005-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 tech.units.tck.tests.format; 031 032import static tech.units.tck.TCKRunner.SECTION_PREFIX; 033import static tech.units.tck.TCKRunner.SPEC_ID; 034import static tech.units.tck.TCKRunner.SPEC_VERSION; 035import static tech.units.tck.util.TestGroups.FORMAT; 036import static org.hamcrest.MatcherAssert.assertThat; 037import static org.hamcrest.Matchers.empty; 038import static org.hamcrest.core.Is.is; 039import static org.hamcrest.core.IsNot.not; 040import static org.testng.Assert.assertNotNull; 041import static tech.units.tck.util.TestUtils.MSG_NO_TCK_CONFIG; 042import static tech.units.tck.util.TestUtils.testHasPublicMethod; 043 044import javax.measure.Unit; 045import javax.measure.format.UnitFormat; 046 047import org.jboss.test.audit.annotations.SpecAssertion; 048import org.jboss.test.audit.annotations.SpecVersion; 049import org.testng.annotations.Test; 050 051import tech.units.tck.TCKSetup; 052 053/** 054 * Tests for UnitFormat 055 * @version 2.3, October 4, 2023 056 * @since 1.0 057 * @author <a href="mailto:[email protected]">Werner Keil</a> 058 */ 059@SpecVersion(spec = SPEC_ID, version = SPEC_VERSION) 060public class UnitFormatTest { 061 private static final String SECTION_NUM = "4.5"; 062 063 /** 064 * Ensure at least one UnitFormat implementation 065 * is available/registered. 066 */ 067 @SpecAssertion(section = SECTION_NUM, id = "45-A1") 068 @Test(groups = { FORMAT }, description = SECTION_NUM + " Ensure at least one UnitFormat implementation is available/registered.") 069 public void testEnsureGotUnitFormat() { 070 assertNotNull(TCKSetup.getConfiguration(), MSG_NO_TCK_CONFIG); 071 assertThat("No UnitFormat implementation found.", TCKSetup.getConfiguration().getUnitFormats4Test(), is(not(empty()))); 072 } 073 074 /** 075 * Ensure the format() operation is implemented. 076 */ 077 @SpecAssertion(section = SECTION_NUM, id = "45-A2") 078 @Test(groups = { FORMAT }, description = SECTION_NUM + " Ensure the format() operation is implemented.") 079 public void testUnitFormatFormat() { 080 for (UnitFormat format : TCKSetup.getConfiguration().getUnitFormats4Test()) { 081 Class<?> type = format.getClass(); 082 testHasPublicMethod(SECTION_PREFIX + SECTION_NUM, type, false, String.class, "format", Unit.class); 083 } 084 } 085 086 /** 087 * Ensure the appendable format() operation is implemented. 088 */ 089 @SpecAssertion(section = SECTION_NUM, id = "45-A3") 090 @Test(groups = { FORMAT }, description = SECTION_NUM + " Ensure the appendable format() operation is implemented.") 091 public void testUnitFormatFormatAppendable() { 092 for (UnitFormat format : TCKSetup.getConfiguration().getUnitFormats4Test()) { 093 Class<?> type = format.getClass(); 094 testHasPublicMethod(SECTION_PREFIX + SECTION_NUM, type, false, Appendable.class, "format", Unit.class, Appendable.class); 095 } 096 } 097 098 /** 099 * Ensure the isLocaleSensitive() method is implemented. 100 */ 101 @SpecAssertion(section = SECTION_NUM, id = "45-A4") 102 @Test(groups = { FORMAT }, description = SECTION_NUM + " Ensure the isLocaleSensitive() method is implemented.") 103 public void testUnitFormatFormatIsLocalSensitive() { 104 for (UnitFormat format : TCKSetup.getConfiguration().getUnitFormats4Test()) { 105 Class<?> type = format.getClass(); 106 testHasPublicMethod(SECTION_PREFIX + SECTION_NUM, type, "isLocaleSensitive", false); 107 } 108 } 109 110 /** 111 * Ensure the label() operation is implemented. 112 */ 113 @SpecAssertion(section = SECTION_NUM, id = "45-A5") 114 @Test(groups = { FORMAT }, description = SECTION_NUM + " Ensure the label() operation is implemented.") 115 public void testUnitFormatLabel() { 116 for (UnitFormat format : TCKSetup.getConfiguration().getUnitFormats4Test()) { 117 Class<?> type = format.getClass(); 118 testHasPublicMethod(SECTION_PREFIX + SECTION_NUM, type, "label", true); 119 } 120 } 121 122 /** 123 * Ensure the parse() operation is implemented. 124 */ 125 @SpecAssertion(section = SECTION_NUM, id = "45-A6") 126 @Test(groups = { FORMAT }, description = SECTION_NUM + " Ensure the parse() operation is implemented.") 127 public void testUnitFormatParse() { 128 for (UnitFormat format : TCKSetup.getConfiguration().getUnitFormats4Test()) { 129 Class<?> type = format.getClass(); 130 testHasPublicMethod(SECTION_PREFIX + SECTION_NUM, type, "parse", true); 131 } 132 } 133}