|
| 1 | +package unit; |
| 2 | + |
| 3 | +import dev.jacrispys.JavaBot.utils.SecretData; |
| 4 | +import org.junit.jupiter.api.Assertions; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | + |
| 9 | +import java.io.ByteArrayOutputStream; |
| 10 | +import java.io.File; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.PrintStream; |
| 13 | +import java.lang.reflect.InvocationTargetException; |
| 14 | +import java.lang.reflect.Method; |
| 15 | + |
| 16 | +public class BasicTests { |
| 17 | + |
| 18 | + @Test |
| 19 | + void verifyTrue() { |
| 20 | + Assertions.assertTrue(true); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + void verifyFalse() { |
| 25 | + Assertions.assertFalse(false); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void verifyInfoLogging() { |
| 30 | + final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 31 | + System.setOut(new PrintStream(out)); |
| 32 | + |
| 33 | + Logger logger = LoggerFactory.getLogger(BasicTests.class); |
| 34 | + logger.info("Hello, World!"); |
| 35 | + |
| 36 | + final String logMessage = out.toString(); |
| 37 | + Assertions.assertTrue(logMessage.contains("Hello, World!")); |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void verifyDebugLogging() { |
| 43 | + final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 44 | + System.setOut(new PrintStream(out)); |
| 45 | + |
| 46 | + Logger logger = LoggerFactory.getLogger(BasicTests.class); |
| 47 | + logger.debug("Hello, World!"); |
| 48 | + |
| 49 | + final String logMessage = out.toString(); |
| 50 | + Assertions.assertTrue(logMessage.contains("Hello, World!")); |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void verifyNoTraceLogging() { |
| 56 | + final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 57 | + System.setOut(new PrintStream(out)); |
| 58 | + |
| 59 | + Logger logger = LoggerFactory.getLogger(BasicTests.class); |
| 60 | + logger.trace("Hello, World!"); |
| 61 | + |
| 62 | + final String logMessage = out.toString(); |
| 63 | + Assertions.assertFalse(logMessage.contains("Hello, World!")); |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void configGenerated() throws IOException, InvocationTargetException, IllegalAccessException, NoSuchMethodException { |
| 69 | + SecretData.initLoginInfo(); |
| 70 | + Class<SecretData> clazz = SecretData.class; |
| 71 | + Method m = clazz.getDeclaredMethod("getClassPath"); |
| 72 | + m.setAccessible(true); |
| 73 | + String classPath = (String) m.invoke(null); |
| 74 | + m.setAccessible(false); |
| 75 | + String path = classPath + File.separator + "config" + File.separator + "loginInfo.yml"; |
| 76 | + File file = new File(path); |
| 77 | + Assertions.assertTrue(file.exists()); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +class TraceLoggingTest { |
| 82 | + @Test |
| 83 | + void verifyTraceLogging() { |
| 84 | + final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 85 | + System.setOut(new PrintStream(out)); |
| 86 | + |
| 87 | + Logger logger = LoggerFactory.getLogger(TraceLoggingTest.class); |
| 88 | + logger.trace("Hello, World!"); |
| 89 | + |
| 90 | + final String logMessage = out.toString(); |
| 91 | + Assertions.assertTrue(logMessage.contains("Hello, World!")); |
| 92 | + |
| 93 | + } |
| 94 | +} |
0 commit comments