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; 031 032import static org.junit.jupiter.api.Assertions.*; 033 034import javax.measure.IncommensurableException; 035import javax.measure.MeasurementError; 036import javax.measure.MeasurementException; 037import javax.measure.UnconvertibleException; 038 039import org.junit.jupiter.api.Test; 040 041/** 042 * @author <a href="mailto:werner@uom.technology">Werner Keil</a> 043 * @version 2.0 044 */ 045public class ExceptionsTest { 046 047 @SuppressWarnings("serial") 048 static class TestException extends MeasurementException { 049 TestException() { 050 super(); 051 } 052 } 053 054 @Test 055 public void testMeasurementException() { 056 MeasurementException e = assertThrows(MeasurementException.class, () -> { 057 throw new TestException(); 058 }); 059 assertNull(e.getMessage()); 060 assertNull(e.getCause()); 061 } 062 063 @Test 064 public void testMeasurementExceptionWithMessage() { 065 MeasurementException e = assertThrows(MeasurementException.class, () -> { 066 throw new MeasurementException("error"); 067 }); 068 069 assertEquals("error", e.getMessage()); 070 assertNull(e.getCause()); 071 } 072 073 @Test 074 public void testMeasurementExceptionWithCause() { 075 assertThrows(MeasurementException.class, () -> { 076 throw new MeasurementException(new IllegalArgumentException()); 077 }); 078 } 079 080 @Test 081 public void testMeasurementExceptionWithMessageAndCause() { 082 Exception cause = new IllegalStateException(); 083 MeasurementException e = assertThrows(MeasurementException.class, () -> { 084 throw new MeasurementException("state error", cause); 085 }); 086 assertEquals("state error", e.getMessage()); 087 assertEquals(cause, e.getCause()); 088 } 089 090 @Test 091 public void testMeasurementError() { 092 MeasurementError e = assertThrows(MeasurementError.class, () -> { 093 throw new MeasurementError(); 094 }); 095 096 assertNull(e.getMessage()); 097 assertNull(e.getCause()); 098 } 099 100 @Test 101 public void testMeasurementErrorWithMessage() { 102 MeasurementError e = assertThrows(MeasurementError.class, () -> { 103 throw new MeasurementError("error"); 104 }); 105 106 assertEquals("error", e.getMessage()); 107 assertNull(e.getCause()); 108 } 109 110 @Test 111 public void testMeasurementErrorWithCause() { 112 MeasurementError e = assertThrows(MeasurementError.class, () -> { 113 throw new MeasurementError(new TestException()); 114 }); 115 116 assertNotNull(e.getMessage()); 117 assertEquals("javax.measure.test.ExceptionsTest$TestException", e.getMessage()); 118 // Throwable stores the toString() of a cause 119 assertNotNull(e.getCause()); 120 } 121 122 @Test 123 public void testMeasurementErrorWithMessageAndCause() { 124 MeasurementError e = assertThrows(MeasurementError.class, () -> { 125 throw new MeasurementError("error", new TestException()); 126 }); 127 128 assertNotNull(e.getMessage()); 129 assertEquals("error", e.getMessage()); 130 assertNotNull(e.getCause()); 131 } 132 133 @Test 134 public void testIncommensurableException() { 135 IncommensurableException ie = new IncommensurableException("error"); 136 assertEquals("error", ie.getMessage()); 137 assertNull(ie.getCause()); 138 } 139 140 @Test 141 public void testIncommensurableExceptionWithCause() { 142 Exception cause = new IllegalArgumentException(); 143 IncommensurableException ie = new IncommensurableException(cause); 144 assertEquals(cause, ie.getCause()); 145 } 146 147 @Test 148 public void testIncommensurableExceptionWithMessageAndCause() { 149 Exception cause = new IllegalArgumentException(); 150 IncommensurableException ie = new IncommensurableException("yet another error", cause); 151 assertEquals("yet another error", ie.getMessage()); 152 assertEquals(cause, ie.getCause()); 153 } 154 155 @Test 156 public void testUnconvertibleException() { 157 UnconvertibleException e = assertThrows(UnconvertibleException.class, () -> { 158 throw new UnconvertibleException("error"); 159 }); 160 assertEquals("error", e.getMessage()); 161 assertNull(e.getCause()); 162 } 163 164 @Test 165 public void testUnconvertibleExceptionWithCause() { 166 assertThrows(UnconvertibleException.class, () -> { 167 Exception cause = new IllegalArgumentException(); 168 UnconvertibleException e = new UnconvertibleException(cause); 169 assertEquals(cause, e.getCause()); 170 throw e; 171 }); 172 } 173 174 @Test 175 public void testUnconvertibleExceptionWithMessageAndCause() { 176 Exception cause = new IllegalStateException(); 177 UnconvertibleException e = assertThrows(UnconvertibleException.class, () -> { 178 throw new UnconvertibleException("state error", cause); 179 }); 180 assertEquals("state error", e.getMessage()); 181 assertEquals(cause, e.getCause()); 182 } 183}