|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.internal; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 11 | +import static org.mockito.ArgumentMatchers.*; |
| 12 | +import static org.mockito.Mockito.CALLS_REAL_METHODS; |
| 13 | +import static org.mockito.Mockito.doAnswer; |
| 14 | +import static org.mockito.Mockito.mock; |
| 15 | +import static org.mockito.Mockito.mockStatic; |
| 16 | +import static org.mockito.Mockito.verify; |
| 17 | +import static org.mockito.Mockito.when; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.DisplayName; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.mockito.MockedStatic; |
| 26 | + |
| 27 | +import io.jooby.*; |
| 28 | +import io.jooby.output.OutputFactory; |
| 29 | + |
| 30 | +public class MutedServerTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + @DisplayName("Test early-exit muting logic, logger aggregation, and null vararg safety") |
| 34 | + void testMutedServer() { |
| 35 | + Server delegate = mock(Server.class); |
| 36 | + when(delegate.getLoggerOff()).thenReturn(Collections.singletonList("base.logger")); |
| 37 | + when(delegate.getName()).thenReturn("mock-server"); |
| 38 | + when(delegate.toString()).thenReturn("MockServer"); |
| 39 | + |
| 40 | + ServerOptions options = new ServerOptions(); |
| 41 | + when(delegate.getOptions()).thenReturn(options); |
| 42 | + OutputFactory outFactory = mock(OutputFactory.class); |
| 43 | + when(delegate.getOutputFactory()).thenReturn(outFactory); |
| 44 | + |
| 45 | + LoggingService loggingService = mock(LoggingService.class); |
| 46 | + |
| 47 | + // Mock logOff to execute the lambda synchronously |
| 48 | + doAnswer( |
| 49 | + inv -> { |
| 50 | + Runnable action = inv.getArgument(1); |
| 51 | + action.run(); |
| 52 | + return null; |
| 53 | + }) |
| 54 | + .when(loggingService) |
| 55 | + .logOff(anyList(), any(SneakyThrows.Runnable.class)); |
| 56 | + |
| 57 | + Jooby app = new Jooby(); |
| 58 | + |
| 59 | + // MOCK MutedServer.class INSTEAD OF ServiceLoader |
| 60 | + try (MockedStatic<MutedServer> mockedServer = |
| 61 | + mockStatic(MutedServer.class, CALLS_REAL_METHODS)) { |
| 62 | + // Force our extracted method to return the mocked logging service |
| 63 | + mockedServer |
| 64 | + .when(() -> MutedServer.loadLoggingService(any())) |
| 65 | + .thenReturn(Optional.of(loggingService)); |
| 66 | + |
| 67 | + // 1. Create MutedServer |
| 68 | + Server muted1 = MutedServer.mute(delegate, "new.logger"); |
| 69 | + assertTrue(muted1 instanceof MutedServer, "Should be wrapped in MutedServer"); |
| 70 | + |
| 71 | + // 2. Test Chaining MutedServer & Null Varargs Safety |
| 72 | + Server muted2 = MutedServer.mute(muted1, (String[]) null); |
| 73 | + muted2 = MutedServer.mute(muted2, "another.logger"); |
| 74 | + assertTrue(muted2 instanceof MutedServer); |
| 75 | + |
| 76 | + // Verify simple delegates |
| 77 | + assertEquals("mock-server", muted2.getName()); |
| 78 | + assertSame(options, muted2.getOptions()); |
| 79 | + assertSame(outFactory, muted2.getOutputFactory()); |
| 80 | + assertEquals("MockServer", muted2.toString()); |
| 81 | + |
| 82 | + // Verify merged loggers |
| 83 | + List<String> combinedMute = muted2.getLoggerOff(); |
| 84 | + assertTrue(combinedMute.contains("base.logger")); |
| 85 | + assertTrue(combinedMute.contains("new.logger")); |
| 86 | + assertTrue(combinedMute.contains("another.logger")); |
| 87 | + |
| 88 | + // 3. Verify Fluent Fixes |
| 89 | + ServerOptions newOpts = new ServerOptions(); |
| 90 | + assertSame(muted2, muted2.setOptions(newOpts)); |
| 91 | + verify(delegate).setOptions(newOpts); |
| 92 | + |
| 93 | + assertSame(muted2, muted2.start(app)); |
| 94 | + verify(delegate).start(app); |
| 95 | + verify(loggingService).logOff(eq(combinedMute), any(SneakyThrows.Runnable.class)); |
| 96 | + |
| 97 | + assertSame(muted2, muted2.stop()); |
| 98 | + verify(delegate).stop(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + @DisplayName("Test early exit when no LoggingService is present") |
| 104 | + void testMuteEarlyExitNoLoggingService() { |
| 105 | + Server delegate = mock(Server.class); |
| 106 | + |
| 107 | + // MOCK MutedServer.class |
| 108 | + try (MockedStatic<MutedServer> mockedServer = |
| 109 | + mockStatic(MutedServer.class, CALLS_REAL_METHODS)) { |
| 110 | + // Force it to return empty, triggering the if (loggingService.isEmpty()) branch |
| 111 | + mockedServer.when(() -> MutedServer.loadLoggingService(any())).thenReturn(Optional.empty()); |
| 112 | + |
| 113 | + Server unmuted = MutedServer.mute(delegate, "some.logger"); |
| 114 | + |
| 115 | + // Asserts that the original delegate is returned untouched |
| 116 | + assertSame(delegate, unmuted); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments