Skip to content

Commit

Permalink
test: Fix the example on how to use Slf4j 1.x and 2.x and test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsbasjes committed Aug 19, 2023
1 parent 81060db commit 4f54a01
Show file tree
Hide file tree
Showing 12 changed files with 257 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public Demo() {
.withCache(1234)
.withField("DeviceClass")
.withAllFields()
.preheat()
.build();
}

Expand Down
26 changes: 26 additions & 0 deletions analyzer/src/it/Examples/java-jdk8-only/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Yet Another UserAgent Analyzer
* Copyright (C) 2013-2023 Niels Basjes
*
* Licensed 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
*
* https://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.
*/

File buildLog = new File( basedir, 'build.log' )
assert buildLog.exists()

assert 1 <= buildLog.getText().count("YauaaVersion")
assert 1 == buildLog.getText().count("For more information: https://yauaa.basjes.nl")
assert 1 == buildLog.getText().count("using expression: classpath*:UserAgents/")
assert 1 == buildLog.getText().count("Building all matchers for all possible fields.")
assert 1 == buildLog.getText().count("Preheating JVM by running ")
assert 1 == buildLog.getText().count("Initializing Analyzer data structures")
4 changes: 2 additions & 2 deletions analyzer/src/it/Examples/java-slf4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<packaging>jar</packaging>

<artifactId>yauaa-example-java-slf4j</artifactId>
<name>Yauaa : Examples : Java (SLF4J)</name>
<name>Yauaa : Examples : Java (SLF4J 1.x)</name>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand All @@ -54,7 +54,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j2.version}</version>
<version>${slf4j1.version}</version>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public Demo() {
.withCache(1234)
.withField("DeviceClass")
.withAllFields()
.preheat()
.build();
}

Expand Down
26 changes: 26 additions & 0 deletions analyzer/src/it/Examples/java-slf4j/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Yet Another UserAgent Analyzer
* Copyright (C) 2013-2023 Niels Basjes
*
* Licensed 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
*
* https://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.
*/

File buildLog = new File( basedir, 'build.log' )
assert buildLog.exists()

assert 1 <= buildLog.getText().count("YauaaVersion")
assert 1 == buildLog.getText().count("For more information: https://yauaa.basjes.nl")
assert 1 == buildLog.getText().count("using expression: classpath*:UserAgents/")
assert 1 == buildLog.getText().count("Building all matchers for all possible fields.")
assert 1 == buildLog.getText().count("Preheating JVM by running ")
assert 1 == buildLog.getText().count("Initializing Analyzer data structures")
68 changes: 68 additions & 0 deletions analyzer/src/it/Examples/java-slf4j2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Yet Another UserAgent Analyzer
~ Copyright (C) 2013-2023 Niels Basjes
~
~ Licensed 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
~
~ https://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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>nl.basjes.parse.useragent</groupId>
<artifactId>yauaa-example-parent-it</artifactId>
<version>0.0.1</version>
</parent>

<packaging>jar</packaging>

<artifactId>yauaa-example-java-slf4j</artifactId>
<name>Yauaa : Examples : Java (SLF4J 2.x)</name>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.release>17</maven.compiler.release>
</properties>

<dependencies>
<dependency>
<groupId>nl.basjes.parse.useragent</groupId>
<artifactId>yauaa</artifactId>
<version>${yauaa.version}</version>
</dependency>

<!-- The default logging implementation for Yauaa -->
<!-- Send all Log4j2 calls to SLF4J 2.x -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>${log4j2.version}</version>
<exclusions>
<exclusion>
<!-- Make sure the Slf4j 1.x api is excluded because we want the 2.x version -->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- Just the simplest of logger for this demo. -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j2.version}</version>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Yet Another UserAgent Analyzer
* Copyright (C) 2013-2023 Niels Basjes
*
* Licensed 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
*
* https://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 nl.basjes.parse.useragent.example;

import nl.basjes.parse.useragent.UserAgent;
import nl.basjes.parse.useragent.UserAgentAnalyzer;

public class Demo {

private final UserAgentAnalyzer uaa;

public Demo() {
uaa = UserAgentAnalyzer
.newBuilder()
.withCache(1234)
.withField("DeviceClass")
.withAllFields()
.preheat()
.build();
}

public UserAgent parse(String userAgent) {
return uaa.parse(userAgent);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Yet Another UserAgent Analyzer
* Copyright (C) 2013-2023 Niels Basjes
*
* Licensed 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
*
* https://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 nl.basjes.parse.useragent.example;

import nl.basjes.parse.useragent.UserAgent;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

class TestDemo {
@Test
void testParser() {
Demo demo = new Demo();

String userAgent = "Mozilla/5.0 (Linux; Android 7.0; Nexus 6 Build/NBD90Z) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.124 Mobile Safari/537.36";

UserAgent result = demo.parse(userAgent);

assertTrue(result.toXML().contains("<DeviceName>Google Nexus 6</DeviceName>"),
"The parser must extract the correct DeviceName");
}
}
26 changes: 26 additions & 0 deletions analyzer/src/it/Examples/java-slf4j2/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Yet Another UserAgent Analyzer
* Copyright (C) 2013-2023 Niels Basjes
*
* Licensed 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
*
* https://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.
*/

File buildLog = new File( basedir, 'build.log' )
assert buildLog.exists()

assert 1 <= buildLog.getText().count("YauaaVersion")
assert 1 == buildLog.getText().count("For more information: https://yauaa.basjes.nl")
assert 1 == buildLog.getText().count("using expression: classpath*:UserAgents/")
assert 1 == buildLog.getText().count("Building all matchers for all possible fields.")
assert 1 == buildLog.getText().count("Preheating JVM by running ")
assert 1 == buildLog.getText().count("Initializing Analyzer data structures")
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public Demo() {
.withCache(1234)
.withField("DeviceClass")
.withAllFields()
.preheat()
.build();
}

Expand Down
26 changes: 26 additions & 0 deletions analyzer/src/it/Examples/java/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Yet Another UserAgent Analyzer
* Copyright (C) 2013-2023 Niels Basjes
*
* Licensed 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
*
* https://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.
*/

File buildLog = new File( basedir, 'build.log' )
assert buildLog.exists()

assert 1 <= buildLog.getText().count("YauaaVersion")
assert 1 == buildLog.getText().count("For more information: https://yauaa.basjes.nl")
assert 1 == buildLog.getText().count("using expression: classpath*:UserAgents/")
assert 1 == buildLog.getText().count("Building all matchers for all possible fields.")
assert 1 == buildLog.getText().count("Preheating JVM by running ")
assert 1 == buildLog.getText().count("Initializing Analyzer data structures")
3 changes: 1 addition & 2 deletions documentation/content/using/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ Only during development and during unit tests the Useragents are logged.
This is because it was designed to run in very large scale batch and streaming situations (very large as in "Let's analyze these 10^10 records").

### Bring your own batteries
To assist in running Yauaa without the logj4-core jar an example was created that only uses SLF4J: [here](https://github.com/nielsbasjes/yauaa/tree/main/analyzer/src/it/Examples/java-slf4j).

To assist in running Yauaa without the logj4-core jar an example was created that only uses [SLF4J 1.x](https://github.com/nielsbasjes/yauaa/tree/main/analyzer/src/it/Examples/java-slf4j) and and example that only uses [SLF4J 2.x](https://github.com/nielsbasjes/yauaa/tree/main/analyzer/src/it/Examples/java-slf4j2).

## Serialization
If your application needs to serialize the instance of the UserAgentAnalyzer then both the standard Java serialization and
Expand Down

0 comments on commit 4f54a01

Please sign in to comment.