1+ /*
2+ * Licensed to the Apache Software Foundation (ASF) under one or more
3+ * contributor license agreements. See the NOTICE file distributed with
4+ * this work for additional information regarding copyright ownership.
5+ * The ASF licenses this file to you under the Apache License, Version 2.0
6+ * (the "License"); you may not use this file except in compliance with
7+ * the License. You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ */
17+ package org .apache .logging .log4j .samples .android ;
18+
19+ import static androidx .test .espresso .Espresso .onView ;
20+ import static androidx .test .espresso .action .ViewActions .click ;
21+ import static androidx .test .espresso .assertion .ViewAssertions .doesNotExist ;
22+ import static androidx .test .espresso .assertion .ViewAssertions .matches ;
23+ import static androidx .test .espresso .matcher .ViewMatchers .isDisplayed ;
24+ import static androidx .test .espresso .matcher .ViewMatchers .withId ;
25+ import static androidx .test .espresso .matcher .ViewMatchers .withText ;
26+ import static org .assertj .core .api .Assertions .assertThat ;
27+
28+ import androidx .test .ext .junit .rules .ActivityScenarioRule ;
29+ import androidx .test .ext .junit .runners .AndroidJUnit4 ;
30+ import java .util .AbstractMap ;
31+ import java .util .Arrays ;
32+ import java .util .Map ;
33+ import org .apache .logging .log4j .Level ;
34+ import org .apache .logging .log4j .Logger ;
35+ import org .apache .logging .log4j .core .Appender ;
36+ import org .apache .logging .log4j .core .LoggerContext ;
37+ import org .apache .logging .log4j .core .appender .ConsoleAppender ;
38+ import org .apache .logging .log4j .core .config .Configuration ;
39+ import org .apache .logging .log4j .core .config .LoggerConfig ;
40+ import org .apache .logging .log4j .core .test .appender .ListAppender ;
41+ import org .junit .BeforeClass ;
42+ import org .junit .Rule ;
43+ import org .junit .Test ;
44+ import org .junit .runner .RunWith ;
45+
46+ /**
47+ * Instrumented test, which will execute on an Android device.
48+ */
49+ @ RunWith (AndroidJUnit4 .class )
50+ public class MainActivityTest {
51+
52+ @ Rule
53+ public ActivityScenarioRule <MainActivity > activityScenarioRule = new ActivityScenarioRule <>(MainActivity .class );
54+
55+ private static Configuration configuration ;
56+ private static ListAppender appender ;
57+ private static Logger logger ;
58+
59+ @ BeforeClass
60+ public static void setup () {
61+ LoggerContext context = LoggerContext .getContext (false );
62+ configuration = context .getConfiguration ();
63+ appender = configuration .getAppender ("LIST" );
64+ logger = context .getLogger (MainActivity .class );
65+ }
66+
67+ @ Test
68+ public void hasExpectedConfiguration () {
69+ assertThat (configuration .getConfigurationSource ().getLocation ())
70+ .startsWith ("jar:file:" )
71+ .endsWith ("!/log4j2-test.xml" );
72+ // Check appenders
73+ Appender console = configuration .getAppender ("CONSOLE" );
74+ assertThat (console ).as ("Console Appender" ).isInstanceOf (ConsoleAppender .class );
75+ Appender list = configuration .getAppender ("LIST" );
76+ assertThat (list ).as ("List Appender" ).isInstanceOf (ListAppender .class );
77+ // Check logger configurations
78+ assertThat (configuration .getLoggers ()).hasSize (2 );
79+ assertThat (configuration .getRootLogger ().getExplicitLevel ()).isEqualTo (Level .INFO );
80+ assertThat (configuration .getRootLogger ().getAppenders ())
81+ .hasSize (2 )
82+ .containsExactly (entry ("CONSOLE" , console ), entry ("LIST" , list ));
83+ assertThat (configuration .getLoggerConfig (MainActivity .class .getName ()))
84+ .isNotNull ()
85+ .extracting (LoggerConfig ::getExplicitLevel )
86+ .isEqualTo (Level .DEBUG );
87+ }
88+
89+ @ Test
90+ public void logMessagesAreDelivered () {
91+ assertThat (logger .getLevel ()).isEqualTo (Level .DEBUG );
92+ appender .clear ();
93+ for (int buttonId : MainActivity .buttonIds ) {
94+ onView (withId (buttonId )).perform (click ());
95+ }
96+ Level [] expectedLevels = {Level .FATAL , Level .ERROR , Level .WARN , Level .INFO , Level .DEBUG };
97+ String [] expectedMessages =
98+ Arrays .stream (expectedLevels ).map (l -> l + "-Hello " + l + "!" ).toArray (String []::new );
99+ assertThat (appender .getMessages ()).hasSize (expectedLevels .length ).containsExactly (expectedMessages );
100+ }
101+
102+ @ Test
103+ public void logLevelChanges () {
104+ assertThat (logger .getLevel ()).isEqualTo (Level .DEBUG );
105+ onView (withId (R .id .setLogLevelBtn )).perform (click ());
106+ onView (withText ("Set log level" )).check (matches (isDisplayed ()));
107+ onView (withText ("ERROR" )).perform (click ());
108+ onView (withText ("Select" )).perform (click ());
109+ onView (withText ("Set log level" )).check (doesNotExist ());
110+ assertThat (logger .getLevel ()).isEqualTo (Level .ERROR );
111+ }
112+
113+ private static Map .Entry <String , Appender > entry (String name , Appender appender ) {
114+ return new AbstractMap .SimpleImmutableEntry <>(name , appender );
115+ }
116+ }
0 commit comments