|
| 1 | +package com.codename1.io; |
| 2 | + |
| 3 | +import com.codename1.impl.CodenameOneImplementation; |
| 4 | +import com.codename1.ui.events.ActionEvent; |
| 5 | +import com.codename1.ui.events.ActionListener; |
| 6 | +import org.junit.jupiter.api.AfterEach; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.OutputStreamWriter; |
| 13 | +import java.io.Writer; |
| 14 | +import java.lang.reflect.Field; |
| 15 | +import java.nio.charset.StandardCharsets; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | +import java.util.concurrent.atomic.AtomicReference; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.*; |
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.ArgumentMatchers.anyString; |
| 23 | +import static org.mockito.Mockito.doAnswer; |
| 24 | +import static org.mockito.Mockito.mock; |
| 25 | +import static org.mockito.Mockito.verify; |
| 26 | + |
| 27 | +class LogTest { |
| 28 | + private Log originalLog; |
| 29 | + private CodenameOneImplementation originalImplementation; |
| 30 | + private CodenameOneImplementation mockImplementation; |
| 31 | + private TestLog testLog; |
| 32 | + private Field initializedField; |
| 33 | + private boolean originalInitialized; |
| 34 | + private final List<String> systemOutMessages = new ArrayList<>(); |
| 35 | + private final AtomicReference<ActionListener> listenerRef = new AtomicReference<>(); |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + void setup() throws Exception { |
| 39 | + originalLog = Log.getInstance(); |
| 40 | + originalImplementation = Util.getImplementation(); |
| 41 | + |
| 42 | + mockImplementation = mock(CodenameOneImplementation.class); |
| 43 | + doAnswer(invocation -> { |
| 44 | + systemOutMessages.add(invocation.getArgument(0, String.class)); |
| 45 | + return null; |
| 46 | + }).when(mockImplementation).systemOut(anyString()); |
| 47 | + doAnswer(invocation -> null).when(mockImplementation).cleanup(any()); |
| 48 | + doAnswer(invocation -> { |
| 49 | + Writer writer = invocation.getArgument(1); |
| 50 | + writer.write("stack"); |
| 51 | + return null; |
| 52 | + }).when(mockImplementation).printStackTraceToStream(any(), any()); |
| 53 | + doAnswer(invocation -> { |
| 54 | + listenerRef.set(invocation.getArgument(0)); |
| 55 | + return null; |
| 56 | + }).when(mockImplementation).setLogListener(any()); |
| 57 | + |
| 58 | + Util.setImplementation(mockImplementation); |
| 59 | + |
| 60 | + testLog = new TestLog(); |
| 61 | + Log.install(testLog); |
| 62 | + |
| 63 | + initializedField = Log.class.getDeclaredField("initialized"); |
| 64 | + initializedField.setAccessible(true); |
| 65 | + originalInitialized = initializedField.getBoolean(null); |
| 66 | + initializedField.setBoolean(null, true); |
| 67 | + |
| 68 | + systemOutMessages.clear(); |
| 69 | + listenerRef.set(null); |
| 70 | + Log.setLevel(Log.DEBUG); |
| 71 | + } |
| 72 | + |
| 73 | + @AfterEach |
| 74 | + void tearDown() throws Exception { |
| 75 | + Log.install(originalLog); |
| 76 | + Util.setImplementation(originalImplementation); |
| 77 | + if (initializedField != null) { |
| 78 | + initializedField.setBoolean(null, originalInitialized); |
| 79 | + } |
| 80 | + Log.setLevel(Log.DEBUG); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void printRespectsLogLevelAndWritesToWriter() throws IOException { |
| 85 | + testLog.resetContent(); |
| 86 | + systemOutMessages.clear(); |
| 87 | + Log.setLevel(Log.WARNING); |
| 88 | + |
| 89 | + Log.p("ignored", Log.INFO); |
| 90 | + assertEquals("", testLog.getWrittenContent()); |
| 91 | + |
| 92 | + Log.p("accepted", Log.ERROR); |
| 93 | + String content = testLog.getWrittenContent(); |
| 94 | + assertTrue(content.contains("accepted")); |
| 95 | + assertTrue(systemOutMessages.stream().anyMatch(s -> s.contains("accepted"))); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void logThrowableWritesExceptionDetails() throws IOException { |
| 100 | + testLog.resetContent(); |
| 101 | + systemOutMessages.clear(); |
| 102 | + |
| 103 | + RuntimeException failure = new RuntimeException("boom"); |
| 104 | + Log.e(failure); |
| 105 | + |
| 106 | + String content = testLog.getWrittenContent(); |
| 107 | + assertTrue(content.contains("Exception: java.lang.RuntimeException - boom")); |
| 108 | + assertTrue(content.contains("stack")); |
| 109 | + verify(mockImplementation).printStackTraceToStream(any(), any()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void setFileURLRecreatesWriter() throws IOException { |
| 114 | + testLog.resetContent(); |
| 115 | + Log.p("initial", Log.INFO); |
| 116 | + int createdInitially = testLog.getCreateWriterCalls(); |
| 117 | + |
| 118 | + testLog.setFileURL("file:///tmp/log-a.txt"); |
| 119 | + int afterUpdate = testLog.getCreateWriterCalls(); |
| 120 | + assertTrue(afterUpdate > createdInitially); |
| 121 | + |
| 122 | + testLog.resetContent(); |
| 123 | + Log.p("after", Log.INFO); |
| 124 | + assertTrue(testLog.getWrittenContent().contains("after")); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + void trackFileSystemLogsEvents() throws IOException { |
| 129 | + Log.p("warmup", Log.INFO); |
| 130 | + testLog.resetContent(); |
| 131 | + systemOutMessages.clear(); |
| 132 | + |
| 133 | + testLog.trackFileSystem(); |
| 134 | + ActionListener listener = listenerRef.get(); |
| 135 | + assertNotNull(listener); |
| 136 | + |
| 137 | + listener.actionPerformed(new ActionEvent("stream opened")); |
| 138 | + |
| 139 | + String content = testLog.getWrittenContent(); |
| 140 | + assertTrue(content.contains("stream opened")); |
| 141 | + assertTrue(systemOutMessages.stream().anyMatch(s -> s.contains("stream opened"))); |
| 142 | + } |
| 143 | + |
| 144 | + private static class TestLog extends Log { |
| 145 | + private final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 146 | + private OutputStreamWriter writer = new OutputStreamWriter(buffer, StandardCharsets.UTF_8); |
| 147 | + private int createWriterCalls; |
| 148 | + |
| 149 | + @Override |
| 150 | + protected Writer createWriter() { |
| 151 | + createWriterCalls++; |
| 152 | + writer = new OutputStreamWriter(buffer, StandardCharsets.UTF_8); |
| 153 | + return writer; |
| 154 | + } |
| 155 | + |
| 156 | + void resetContent() throws IOException { |
| 157 | + writer.flush(); |
| 158 | + buffer.reset(); |
| 159 | + } |
| 160 | + |
| 161 | + String getWrittenContent() throws IOException { |
| 162 | + writer.flush(); |
| 163 | + return new String(buffer.toByteArray(), StandardCharsets.UTF_8); |
| 164 | + } |
| 165 | + |
| 166 | + int getCreateWriterCalls() { |
| 167 | + return createWriterCalls; |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments