Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
8fbab07
Improve Android example
ppkarwasz Oct 8, 2024
d7b58cd
Fix RAT failure
ppkarwasz Oct 9, 2024
e625caf
Replace `android-actions` with script
ppkarwasz Oct 9, 2024
7a9d57f
Rename tests appropriately
ppkarwasz Oct 9, 2024
65e8781
Use temurin as Java distribution
ppkarwasz Oct 9, 2024
2f187d2
Fix `curl` invocation
ppkarwasz Oct 9, 2024
8b202fa
Fix sdkmanager location
ppkarwasz Oct 9, 2024
caa27d1
Add exact number of licenses to accept
ppkarwasz Oct 9, 2024
a9dbcaa
Fix bug in project name
ppkarwasz Oct 9, 2024
bc45fbb
Try without setting up Android SDK
ppkarwasz Oct 9, 2024
c1262ac
Setup Gradle and list AVD devices
ppkarwasz Oct 9, 2024
1d5d03e
Find AVDManager on Github
ppkarwasz Oct 9, 2024
e99d9ab
Execute `avdmanager`
ppkarwasz Oct 9, 2024
02ca42e
Try setting up an AVD
ppkarwasz Oct 9, 2024
52d8627
Discover available system images
ppkarwasz Oct 9, 2024
24c9f58
Download system image for AVD
ppkarwasz Oct 9, 2024
4990c35
Accept license on image
ppkarwasz Oct 9, 2024
5e2f413
Try to start emulator
ppkarwasz Oct 9, 2024
56754eb
List installed packages on Github runner
ppkarwasz Oct 9, 2024
850f8e4
Also install `platform-tools`
ppkarwasz Oct 9, 2024
2c4dd97
Run emulator without sound and window
ppkarwasz Oct 9, 2024
3a9bc2a
Change platform to ARM8a
ppkarwasz Oct 9, 2024
a79e336
Fix package path
ppkarwasz Oct 9, 2024
f9b3e3a
Try another architecture
ppkarwasz Oct 9, 2024
f1a5820
Add emulator explicitly
ppkarwasz Oct 9, 2024
e8a8d64
Try default image
ppkarwasz Oct 9, 2024
9942c58
Try merging steps
ppkarwasz Oct 9, 2024
61b3d63
Try `reactivecircus/android-emulator-runner`
ppkarwasz Oct 9, 2024
a868370
Wait until `adb` detects the device
ppkarwasz Oct 9, 2024
d9fccac
Kill the emulator at the end
ppkarwasz Oct 9, 2024
7d2d8eb
Try using `logcat` to debug the AVD
ppkarwasz Oct 9, 2024
3e918f1
Don't block on logcat
ppkarwasz Oct 9, 2024
26a2fb8
Fix snippet that waits for the emulator
ppkarwasz Oct 10, 2024
b9c4887
Fix script
ppkarwasz Oct 10, 2024
2fa7bf0
Prevent script to fail on `adb` return value
ppkarwasz Oct 10, 2024
c2ea55f
Add README.adoc
ppkarwasz Oct 10, 2024
2824087
Hardcode Log4j version in Android sample
ppkarwasz Oct 10, 2024
25793cb
Merge remote-tracking branch 'apache/main' into feature/android-tests
ppkarwasz Oct 10, 2024
a4c8ef2
Allow overriding `log4j.version` from CLI
ppkarwasz Oct 10, 2024
6ae4ce7
Allow to specify a version in `android-test`
ppkarwasz Oct 10, 2024
2628492
Fix Spotless warnings
ppkarwasz Oct 10, 2024
3aa061e
Try to ensure emulator always stops
ppkarwasz Oct 10, 2024
d3d44a9
Fix typo in parameter name
ppkarwasz Oct 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions .github/workflows/android-reusable-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to you under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

name: android-reusable-test

on:
workflow_call:
inputs:
log4j-version:
description: 'Log4j version'
required: true
type: string

permissions: read-all

jobs:

build:

runs-on: ubuntu-latest

steps:

- name: Checkout repository
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # 4.2.0

- name: Setup Java
uses: actions/setup-java@b36c23c0d998641eff861008f374ee103c25ac73 # 4.4.0
with:
distribution: 'temurin'
java-version: 17

- name: Setup Gradle
uses: gradle/actions/setup-gradle@d156388eb19639ec20ade50009f3d199ce1e2808 # 4.1.0

- name: List AVD Devices
shell: bash
run: |
$ANDROID_HOME/cmdline-tools/latest/bin/avdmanager list

- name: Build
id: build
shell: bash
env:
LOG4J_VERSION: ${{ inputs.log4j-version }}
run: |
log4j-samples-android/gradlew -p log4j-samples-android \
--console plain \
-Plog4jVersion=$LOG4J_VERSION \
build connectedCheck
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
##
# Root logger configuration
status = error
name = PropertiesConfig

# Appenders
appender.console.type = Console
appender.console.name = Console
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%d{yyyy-MM-dd HH:mm:ss.SSS}] %-5p- %m%n
name: android-test

# Root logger level and appenders
rootLogger.level = INFO
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = Console
on:
workflow_dispatch:
schedule:
- cron: "29 17 * * *"

logger.com.example.log4japi.MainActivity.name = com.example.log4japi.MainActivity
logger.com.example.log4japi.MainActivity.level = DEBUG
logger.com.example.log4japi.MainActivity.additivity = false
logger.com.example.log4japi.MainActivity.appenderRefs = stdout
logger.com.example.log4japi.MainActivity.appenderRef.stdout.ref = Console
permissions: read-all

jobs:

android-test:
strategy:
fail-fast: false
matrix:
log4j-version: [ "2.25.0-SNAPSHOT" ]
uses: apache/logging-log4j-samples/.github/workflows/android-reusable-test.yaml@feature/android-tests
with:
log4j-version: ${{ matrix.log4j-version }}
27 changes: 21 additions & 6 deletions log4j-samples-android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ plugins {

android {
compileSdk 34
namespace "org.apache.logging.log4j.samples.android"

defaultConfig {
applicationId "org.apache.logging.log4j.samples.android"
Expand Down Expand Up @@ -48,17 +49,31 @@ android {
}
}

properties {
log4jVersion = "2.24.1"
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.11.0'
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
androidTestImplementation 'org.assertj:assertj-core:3.26.3'

//Log4j
implementation 'org.apache.logging.log4j:log4j-api:2.24.1'
implementation 'org.apache.logging.log4j:log4j-core:2.24.1'
}
// Log4j
implementation "org.apache.logging.log4j:log4j-api:${log4jVersion}"
implementation "org.apache.logging.log4j:log4j-core:${log4jVersion}"
androidTestImplementation("org.apache.logging.log4j:log4j-core-test:${log4jVersion}") {
exclude group: 'com.google.code.java-allocation-instrumenter'
exclude group: 'org.apache.logging.log4j', module: 'log4j-api-test'
exclude group: 'org.hamcrest'
exclude group: 'org.junit.jupiter'
exclude group: 'org.junit.platform'
exclude group: 'org.springframework'
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.logging.log4j.samples.android;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.assertj.core.api.Assertions.assertThat;

import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Map;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* Instrumented test, which will execute on an Android device.
*/
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

@Rule
public ActivityScenarioRule<MainActivity> activityScenarioRule = new ActivityScenarioRule<>(MainActivity.class);

private static Configuration configuration;
private static ListAppender appender;
private static Logger logger;

@BeforeClass
public static void setup() {
LoggerContext context = LoggerContext.getContext(false);
configuration = context.getConfiguration();
appender = configuration.getAppender("LIST");
logger = context.getLogger(MainActivity.class);
}

@Test
public void hasExpectedConfiguration() {
assertThat(configuration.getConfigurationSource().getLocation())
.startsWith("jar:file:")
.endsWith("!/log4j2-test.xml");
// Check appenders
Appender console = configuration.getAppender("CONSOLE");
assertThat(console).as("Console Appender").isInstanceOf(ConsoleAppender.class);
Appender list = configuration.getAppender("LIST");
assertThat(list).as("List Appender").isInstanceOf(ListAppender.class);
// Check logger configurations
assertThat(configuration.getLoggers()).hasSize(2);
assertThat(configuration.getRootLogger().getExplicitLevel()).isEqualTo(Level.INFO);
assertThat(configuration.getRootLogger().getAppenders())
.hasSize(2)
.containsExactly(entry("CONSOLE", console), entry("LIST", list));
assertThat(configuration.getLoggerConfig(MainActivity.class.getName()))
.isNotNull()
.extracting(LoggerConfig::getExplicitLevel)
.isEqualTo(Level.DEBUG);
}

@Test
public void logMessagesAreDelivered() {
assertThat(logger.getLevel()).isEqualTo(Level.DEBUG);
appender.clear();
for (int buttonId : MainActivity.buttonIds) {
onView(withId(buttonId)).perform(click());
}
Level[] expectedLevels = {Level.FATAL, Level.ERROR, Level.WARN, Level.INFO, Level.DEBUG};
String[] expectedMessages =
Arrays.stream(expectedLevels).map(l -> l + "-Hello " + l + "!").toArray(String[]::new);
assertThat(appender.getMessages()).hasSize(expectedLevels.length).containsExactly(expectedMessages);
}

@Test
public void logLevelChanges() {
assertThat(logger.getLevel()).isEqualTo(Level.DEBUG);
onView(withId(R.id.setLogLevelBtn)).perform(click());
onView(withText("Set log level")).check(matches(isDisplayed()));
onView(withText("ERROR")).perform(click());
onView(withText("Select")).perform(click());
onView(withText("Set log level")).check(doesNotExist());
assertThat(logger.getLevel()).isEqualTo(Level.ERROR);
}

private static Map.Entry<String, Appender> entry(String name, Appender appender) {
return new AbstractMap.SimpleImmutableEntry<>(name, appender);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to you under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<Configuration xmlns="https://logging.apache.org/xml/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-config-2.xsd">
<Appenders>
<Console name="CONSOLE"
ignoreExceptions="false">
<PatternLayout pattern="%d %-5p [%t] %C:%M - %m%n"/>
</Console>
<List name="LIST"
ignoreExceptions="false">
<PatternLayout pattern="%p-%m"/>
</List>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="CONSOLE"/>
<AppenderRef ref="LIST"/>
</Root>
<Logger name="org.apache.logging.log4j.samples.android.MainActivity"
level="DEBUG"/>
</Loggers>
</Configuration>
3 changes: 1 addition & 2 deletions log4j-samples-android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
~ limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.apache.logging.log4j.samples.android">
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
Expand Down
Loading
Loading