• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * 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 org.apache.test.android;
18 
19 import java.util.Hashtable;
20 import java.util.Properties;
21 
22 /**
23  * Logs messages into Android's logcat and throws any reported Throwable directly, because
24  * we run the tests as JUnit tests on Android.
25  */
26 public class TestLogger implements org.apache.qetest.Logger {
27     private Properties prop = new Properties();
28 
29     @Override
getDescription()30     public String getDescription() {
31         return "Android test logger printing information into logcat.";
32     }
33 
34     @Override
getParameterInfo()35     public String[][] getParameterInfo() {
36         return new String[0][];
37     }
38 
39     @Override
getProperties()40     public Properties getProperties() {
41         return prop;
42     }
43 
44     @Override
setProperties(Properties p)45     public void setProperties(Properties p) {
46         prop = (Properties) p.clone();
47     }
48 
49     @Override
initialize(Properties p)50     public boolean initialize(Properties p) {
51         setProperties(p);
52 
53         return true;
54     }
55 
56     @Override
isReady()57     public boolean isReady() {
58         return true;
59     }
60 
61     @Override
flush()62     public void flush() {}
63 
64     @Override
close()65     public void close() {}
66 
67     @Override
testFileInit(String name, String comment)68     public void testFileInit(String name, String comment) {
69         logMsg(INFOMSG, "testFileInit: " + name + " - comment: " + comment);
70     }
71 
72     @Override
testFileClose(String msg, String result)73     public void testFileClose(String msg, String result) {
74         logMsg(INFOMSG, "testFileClose" + msg + " - result: " + result);
75     }
76 
77     @Override
testCaseInit(String comment)78     public void testCaseInit(String comment) {
79         logMsg(INFOMSG, comment);
80     }
81 
82     @Override
testCaseClose(String msg, String result)83     public void testCaseClose(String msg, String result) {
84         logMsg(CRITICALMSG, "testCaseClose" + msg + " - result: " + result);
85     }
86 
87     @Override
logMsg(int level, String msg)88     public void logMsg(int level, String msg) {
89         switch (level) {
90             case ERRORMSG, FAILSONLY -> System.logE(msg);
91             case CRITICALMSG, WARNINGMSG -> System.logW(msg);
92             default -> System.logI(msg);
93         }
94     }
95 
96     @Override
logArbitrary(int level, String msg)97     public void logArbitrary(int level, String msg) {
98         logMsg(level, msg);
99     }
100 
101     @Override
logStatistic(int level, long lVal, double dVal, String msg)102     public void logStatistic(int level, long lVal, double dVal, String msg) {
103         logMsg(level, "logStatistic(lVal: " + lVal + ", dVal:" + dVal + "): " + msg);
104     }
105 
106     @Override
logThrowable(int level, Throwable throwable, String msg)107     public void logThrowable(int level, Throwable throwable, String msg) {
108         logMsg(level, msg);
109         throw new AssertionError(msg, throwable);
110     }
111 
112     @Override
logElement(int level, String element, Hashtable attrs, Object msg)113     public void logElement(int level, String element, Hashtable attrs, Object msg) {
114         logMsg(level, msg + " - element: " + element + "\nAttributes below:\n" + attrs);
115 
116     }
117 
118     @Override
logHashtable(int level, Hashtable hash, String msg)119     public void logHashtable(int level, Hashtable hash, String msg) {
120         logMsg(level, msg + "\nTable below:\n" + hash);
121     }
122 
123     @Override
checkPass(String comment)124     public void checkPass(String comment) {
125         logMsg(INFOMSG, "checkPass: " + comment);
126     }
127 
128     @Override
checkAmbiguous(String comment)129     public void checkAmbiguous(String comment) {
130         logMsg(INFOMSG, "checkAmbiguous: " + comment);
131     }
132 
133     @Override
checkFail(String comment)134     public void checkFail(String comment) {
135         logMsg(FAILSONLY, "checkFail: " + comment);
136     }
137 
138     @Override
checkErr(String comment)139     public void checkErr(String comment) {
140         logMsg(ERRORMSG, "checkErr: " + comment);
141     }
142 
143     @Override
checkPass(String comment, String id)144     public void checkPass(String comment, String id) {
145         logMsg(INFOMSG, "checkPass (" + id + "): " + comment);
146     }
147 
148     @Override
checkAmbiguous(String comment, String id)149     public void checkAmbiguous(String comment, String id) {
150         logMsg(INFOMSG, "checkAmbiguous (" + id + "): " + comment);
151     }
152 
153     @Override
checkFail(String comment, String id)154     public void checkFail(String comment, String id) {
155         logMsg(FAILSONLY, "checkFail (" + id + "): " + comment);
156     }
157 
158     @Override
checkErr(String comment, String id)159     public void checkErr(String comment, String id) {
160         logMsg(ERRORMSG, "checkErr (" + id + "): " + comment);
161     }
162 }
163