1 /* <lambda>null2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.tools.common 18 19 import org.junit.Assert 20 import org.junit.Test 21 22 class LoggerBuilderTest { 23 24 @Test 25 fun testOnV() { 26 var actualTag = "" 27 var actualMsg = "" 28 val logger = 29 LoggerBuilder() 30 .setV { tag, msg -> 31 actualTag = tag 32 actualMsg = msg 33 } 34 .build() 35 logger.v(EXPECTED_TAG, EXPECTED_MSG) 36 logger.d(ERROR, ERROR) 37 logger.i(ERROR, ERROR) 38 logger.w(ERROR, ERROR) 39 logger.e(ERROR, ERROR) 40 Assert.assertEquals(actualTag, EXPECTED_TAG) 41 Assert.assertEquals(actualMsg, EXPECTED_MSG) 42 } 43 44 @Test 45 fun testOnD() { 46 var actualTag = "" 47 var actualMsg = "" 48 val logger = 49 LoggerBuilder() 50 .setD { tag, msg -> 51 actualTag = tag 52 actualMsg = msg 53 } 54 .build() 55 logger.d(EXPECTED_TAG, EXPECTED_MSG) 56 logger.v(ERROR, ERROR) 57 logger.i(ERROR, ERROR) 58 logger.w(ERROR, ERROR) 59 logger.e(ERROR, ERROR) 60 Assert.assertEquals(actualTag, EXPECTED_TAG) 61 Assert.assertEquals(actualMsg, EXPECTED_MSG) 62 } 63 64 @Test 65 fun testOnI() { 66 var actualTag = "" 67 var actualMsg = "" 68 val logger = 69 LoggerBuilder() 70 .setI { tag, msg -> 71 actualTag = tag 72 actualMsg = msg 73 } 74 .build() 75 logger.i(EXPECTED_TAG, EXPECTED_MSG) 76 logger.d(ERROR, ERROR) 77 logger.v(ERROR, ERROR) 78 logger.w(ERROR, ERROR) 79 logger.e(ERROR, ERROR) 80 Assert.assertEquals(actualTag, EXPECTED_TAG) 81 Assert.assertEquals(actualMsg, EXPECTED_MSG) 82 } 83 84 @Test 85 fun testOnW() { 86 var actualTag = "" 87 var actualMsg = "" 88 val logger = 89 LoggerBuilder() 90 .setW { tag, msg -> 91 actualTag = tag 92 actualMsg = msg 93 } 94 .build() 95 logger.w(EXPECTED_TAG, EXPECTED_MSG) 96 logger.v(ERROR, ERROR) 97 logger.d(ERROR, ERROR) 98 logger.i(ERROR, ERROR) 99 logger.e(ERROR, ERROR) 100 Assert.assertEquals(actualTag, EXPECTED_TAG) 101 Assert.assertEquals(actualMsg, EXPECTED_MSG) 102 } 103 104 @Test 105 fun testOnEWithoutException() { 106 var actualTag = "" 107 var actualMsg = "" 108 var actualError: Throwable? = Exception(ERROR) 109 val logger = 110 LoggerBuilder() 111 .setE { tag, msg, error -> 112 actualTag = tag 113 actualMsg = msg 114 actualError = error 115 } 116 .build() 117 logger.e(EXPECTED_TAG, EXPECTED_MSG) 118 logger.v(ERROR, ERROR) 119 logger.d(ERROR, ERROR) 120 logger.i(ERROR, ERROR) 121 logger.w(ERROR, ERROR) 122 Assert.assertEquals(actualTag, EXPECTED_TAG) 123 Assert.assertEquals(actualMsg, EXPECTED_MSG) 124 Assert.assertNull(actualError) 125 } 126 127 @Test 128 fun testOnEWithException() { 129 var actualTag = "" 130 var actualMsg = "" 131 var actualError: Throwable? = Exception(ERROR) 132 val logger = 133 LoggerBuilder() 134 .setE { tag, msg, error -> 135 actualTag = tag 136 actualMsg = msg 137 actualError = error 138 } 139 .build() 140 logger.e(EXPECTED_TAG, EXPECTED_MSG, EXPECTED_ERROR) 141 logger.v(ERROR, ERROR) 142 logger.d(ERROR, ERROR) 143 logger.i(ERROR, ERROR) 144 logger.w(ERROR, ERROR) 145 Assert.assertEquals(actualTag, EXPECTED_TAG) 146 Assert.assertEquals(actualMsg, EXPECTED_MSG) 147 Assert.assertSame(actualError, EXPECTED_ERROR) 148 } 149 150 companion object { 151 const val EXPECTED_TAG = "TAG1" 152 const val EXPECTED_MSG = "MSG1" 153 const val ERROR = "ERROR" 154 val EXPECTED_ERROR = Exception("EXPECTED") 155 } 156 } 157