|
| 1 | +/* |
| 2 | + * Copyright 2000-2023 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.vaadin.flow.server; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.UncheckedIOException; |
| 21 | +import java.net.MalformedURLException; |
| 22 | +import java.util.Set; |
| 23 | + |
| 24 | +import org.junit.After; |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | +import org.mockito.ArgumentMatchers; |
| 28 | +import org.mockito.MockedStatic; |
| 29 | +import org.mockito.Mockito; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +public class DefaultErrorHandlerTest { |
| 34 | + |
| 35 | + MockedStatic<LoggerFactory> loggerFactory; |
| 36 | + Logger logger; |
| 37 | + |
| 38 | + @Before |
| 39 | + public void setUp() throws Exception { |
| 40 | + logger = Mockito |
| 41 | + .spy(LoggerFactory.getLogger(DefaultErrorHandler.class)); |
| 42 | + loggerFactory = Mockito.mockStatic(LoggerFactory.class); |
| 43 | + loggerFactory |
| 44 | + .when(() -> LoggerFactory |
| 45 | + .getLogger(DefaultErrorHandler.class.getName())) |
| 46 | + .thenReturn(logger); |
| 47 | + Mockito.when(logger.isDebugEnabled()).thenReturn(false); |
| 48 | + } |
| 49 | + |
| 50 | + @After |
| 51 | + public void tearDown() throws Exception { |
| 52 | + loggerFactory.close(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void error_acceptedException_errorHandled() { |
| 57 | + DefaultErrorHandler errorHandler = Mockito |
| 58 | + .spy(new DefaultErrorHandler(Set.of(IOException.class.getName(), |
| 59 | + MalformedURLException.class.getName()))); |
| 60 | + |
| 61 | + Throwable throwable = new RuntimeException(); |
| 62 | + errorHandler.error(new ErrorEvent(throwable)); |
| 63 | + Mockito.verify(logger).error("", throwable); |
| 64 | + |
| 65 | + throwable = new IllegalArgumentException(); |
| 66 | + errorHandler.error(new ErrorEvent(throwable)); |
| 67 | + Mockito.verify(logger).error("", throwable); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void error_ignoredException_notHandled() { |
| 72 | + DefaultErrorHandler errorHandler = Mockito |
| 73 | + .spy(new DefaultErrorHandler(Set.of(IOException.class.getName(), |
| 74 | + MalformedURLException.class.getName(), |
| 75 | + "com.vaadin.flow.server.DefaultErrorHandlerTest$InnerException"))); |
| 76 | + |
| 77 | + errorHandler.error(new ErrorEvent(new IOException())); |
| 78 | + errorHandler.error(new ErrorEvent(new MalformedURLException())); |
| 79 | + errorHandler.error(new ErrorEvent(new InnerException())); |
| 80 | + |
| 81 | + Mockito.verify(logger, Mockito.never()).error( |
| 82 | + ArgumentMatchers.anyString(), |
| 83 | + ArgumentMatchers.any(Throwable.class)); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void error_subclassOfIgnoredException_errorHandled() { |
| 88 | + DefaultErrorHandler errorHandler = Mockito.spy( |
| 89 | + new DefaultErrorHandler(Set.of(IOException.class.getName()))); |
| 90 | + |
| 91 | + Throwable throwable = new MalformedURLException(); |
| 92 | + errorHandler.error(new ErrorEvent(throwable)); |
| 93 | + Mockito.verify(logger).error("", throwable); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void error_loggerAtDebugLevel_errorHandled() { |
| 98 | + Mockito.reset(logger); |
| 99 | + Mockito.doReturn(true).when(logger).isDebugEnabled(); |
| 100 | + |
| 101 | + DefaultErrorHandler errorHandler = Mockito |
| 102 | + .spy(new DefaultErrorHandler(Set.of(IOException.class.getName(), |
| 103 | + MalformedURLException.class.getName(), |
| 104 | + "com.vaadin.flow.server.DefaultErrorHandlerTest$InnerException"))); |
| 105 | + |
| 106 | + Throwable throwable = new RuntimeException(); |
| 107 | + errorHandler.error(new ErrorEvent(throwable)); |
| 108 | + Mockito.verify(logger).error("", throwable); |
| 109 | + |
| 110 | + throwable = new IOException(); |
| 111 | + errorHandler.error(new ErrorEvent(throwable)); |
| 112 | + Mockito.verify(logger).error("", throwable); |
| 113 | + |
| 114 | + throwable = new MalformedURLException(); |
| 115 | + errorHandler.error(new ErrorEvent(throwable)); |
| 116 | + Mockito.verify(logger).error("", throwable); |
| 117 | + |
| 118 | + throwable = new InnerException(); |
| 119 | + errorHandler.error(new ErrorEvent(throwable)); |
| 120 | + Mockito.verify(logger).error("", throwable); |
| 121 | + |
| 122 | + throwable = new UncheckedIOException(new IOException()); |
| 123 | + errorHandler.error(new ErrorEvent(throwable)); |
| 124 | + Mockito.verify(logger).error("", throwable); |
| 125 | + } |
| 126 | + |
| 127 | + public static class InnerException extends Exception { |
| 128 | + } |
| 129 | +} |
0 commit comments