Skip to content

Commit 8fbab07

Browse files
committed
Improve Android example
1 parent 98968fd commit 8fbab07

File tree

18 files changed

+531
-286
lines changed

18 files changed

+531
-286
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
18+
name: graalvm-reusable-test
19+
20+
on:
21+
workflow_call:
22+
inputs:
23+
log4j-version:
24+
description: 'Log4j version'
25+
required: true
26+
type: string
27+
28+
permissions: read-all
29+
30+
jobs:
31+
32+
build:
33+
34+
runs-on: ubuntu-latest
35+
36+
steps:
37+
38+
- name: Checkout repository
39+
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # 4.2.0
40+
41+
- name: Setup Java
42+
uses: actions/setup-java@b36c23c0d998641eff861008f374ee103c25ac73 # 4.4.0
43+
with:
44+
java-version: 17
45+
46+
- name: Setup Android SDK
47+
uses: android-actions/setup-android@00854ea68c109d98c75d956347303bf7c45b0277 # 3.2.1
48+
49+
- name: Build
50+
id: build
51+
shell: bash
52+
env:
53+
LOG4J_VERSION: ${{ inputs.log4j-version }}
54+
run: |
55+
log4j-samples-android/gradlew -p log4j-samples-gradlew \
56+
--console plain \
57+
-Plog4jVersion=$LOG4J_VERSION \
58+
build connectedCheck

log4j-samples-android/app/src/main/assets/log4j2.properties renamed to .github/workflows/android-test.yaml

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,23 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
#
17-
##
18-
# Root logger configuration
19-
status = error
20-
name = PropertiesConfig
2117

22-
# Appenders
23-
appender.console.type = Console
24-
appender.console.name = Console
25-
appender.console.target = SYSTEM_OUT
26-
appender.console.layout.type = PatternLayout
27-
appender.console.layout.pattern = [%d{yyyy-MM-dd HH:mm:ss.SSS}] %-5p- %m%n
18+
name: graalvm-test
2819

29-
# Root logger level and appenders
30-
rootLogger.level = INFO
31-
rootLogger.appenderRefs = stdout
32-
rootLogger.appenderRef.stdout.ref = Console
20+
on:
21+
workflow_dispatch:
22+
schedule:
23+
- cron: "29 17 * * *"
3324

34-
logger.com.example.log4japi.MainActivity.name = com.example.log4japi.MainActivity
35-
logger.com.example.log4japi.MainActivity.level = DEBUG
36-
logger.com.example.log4japi.MainActivity.additivity = false
37-
logger.com.example.log4japi.MainActivity.appenderRefs = stdout
38-
logger.com.example.log4japi.MainActivity.appenderRef.stdout.ref = Console
25+
permissions: read-all
26+
27+
jobs:
28+
29+
graalvm-test:
30+
strategy:
31+
fail-fast: false
32+
matrix:
33+
log4j-version: [ "2.25.0-SNAPSHOT" ]
34+
uses: apache/logging-log4j-samples/.github/workflows/android-reusable-test.yaml@feature/android-tests
35+
with:
36+
log4j-version: ${{ matrix.log4j-version }}

log4j-samples-android/app/build.gradle

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ plugins {
2020

2121
android {
2222
compileSdk 34
23+
namespace "org.apache.logging.log4j.samples.android"
2324

2425
defaultConfig {
2526
applicationId "org.apache.logging.log4j.samples.android"
@@ -48,17 +49,31 @@ android {
4849
}
4950
}
5051

52+
properties {
53+
log4jVersion = "2.24.1"
54+
}
55+
5156
dependencies {
5257

53-
implementation 'androidx.appcompat:appcompat:1.6.1'
54-
implementation 'com.google.android.material:material:1.11.0'
58+
implementation 'androidx.appcompat:appcompat:1.7.0'
59+
implementation 'com.google.android.material:material:1.12.0'
5560
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
5661

5762
testImplementation 'junit:junit:4.13.2'
5863
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
5964
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
65+
androidTestImplementation 'org.assertj:assertj-core:3.26.3'
6066

61-
//Log4j
62-
implementation 'org.apache.logging.log4j:log4j-api:2.24.1'
63-
implementation 'org.apache.logging.log4j:log4j-core:2.24.1'
64-
}
67+
// Log4j
68+
implementation "org.apache.logging.log4j:log4j-api:${log4jVersion}"
69+
implementation "org.apache.logging.log4j:log4j-core:${log4jVersion}"
70+
androidTestImplementation("org.apache.logging.log4j:log4j-core-test:${log4jVersion}") {
71+
exclude group: 'com.google.code.java-allocation-instrumenter'
72+
exclude group: 'org.apache.logging.log4j', module: 'log4j-api-test'
73+
exclude group: 'org.hamcrest'
74+
exclude group: 'org.junit.jupiter'
75+
exclude group: 'org.junit.platform'
76+
exclude group: 'org.springframework'
77+
}
78+
79+
}

log4j-samples-android/app/src/androidTest/java/org/apache/logging/log4j/samples/android/ExampleInstrumentedTest.java

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<Configuration xmlns="https://logging.apache.org/xml/ns"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="
21+
https://logging.apache.org/xml/ns
22+
https://logging.apache.org/xml/ns/log4j-config-2.xsd">
23+
<Appenders>
24+
<Console name="CONSOLE"
25+
ignoreExceptions="false">
26+
<PatternLayout pattern="%d %-5p [%t] %C:%M - %m%n"/>
27+
</Console>
28+
<List name="LIST"
29+
ignoreExceptions="false">
30+
<PatternLayout pattern="%p-%m"/>
31+
</List>
32+
</Appenders>
33+
<Loggers>
34+
<Root level="INFO">
35+
<AppenderRef ref="CONSOLE"/>
36+
<AppenderRef ref="LIST"/>
37+
</Root>
38+
<Logger name="org.apache.logging.log4j.samples.android.MainActivity"
39+
level="DEBUG"/>
40+
</Loggers>
41+
</Configuration>

log4j-samples-android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
~ limitations under the License.
1717
-->
1818
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
19-
xmlns:tools="http://schemas.android.com/tools"
20-
package="org.apache.logging.log4j.samples.android">
19+
xmlns:tools="http://schemas.android.com/tools">
2120

2221
<application
2322
android:allowBackup="true"

0 commit comments

Comments
 (0)