diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..3908207 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,8 @@ +# Check out all text files in UNIX format, with LF as end of line +# Don't change this file. If you have any ideas about it, please +# submit a separate issue about it and we'll discuss. + +* text=auto eol=lf +*.java ident +*.xml ident +*.png binary diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml new file mode 100644 index 0000000..5a7096f --- /dev/null +++ b/.github/workflows/codecov.yml @@ -0,0 +1,27 @@ +--- +name: codecov +on: + push: + branches: + - master +jobs: + codecov: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-java@v4 + with: + distribution: 'temurin' + java-version: 11 + - uses: actions/cache@v3 + with: + path: ~/.m2/repository + key: maven-${{ hashFiles('**/pom.xml') }} + restore-keys: | + maven- + - run: mvn install -Pjacoco + - uses: codecov/codecov-action@v4.0.0-beta.3 + with: + token: ${{ secrets.CODECOV_TOKEN }} + file: ./target/site/jacoco/jacoco.xml + fail_ci_if_error: true diff --git a/.github/workflows/mvn.yml b/.github/workflows/mvn.yml new file mode 100644 index 0000000..a900f46 --- /dev/null +++ b/.github/workflows/mvn.yml @@ -0,0 +1,31 @@ +--- +name: mvn +on: + push: + branches: + - master + pull_request: + branches: + - master +jobs: + mvn: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-20.04, macos-12, windows-2022] + java: [11, 17] + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-java@v4 + with: + distribution: 'temurin' + java-version: ${{ matrix.java }} + - uses: actions/cache@v3 + with: + path: ~/.m2/repository + key: ${{ runner.os }}-jdk-${{ matrix.java }}-maven-${{ hashFiles('**/pom.xml') }} + restore-keys: | + ${{ runner.os }}-jdk-${{ matrix.java }}-maven- + - run: java -version + - run: mvn -version + - run: mvn --errors --batch-mode clean install -Pqulice diff --git a/.github/workflows/pdd.yml b/.github/workflows/pdd.yml new file mode 100644 index 0000000..78277a2 --- /dev/null +++ b/.github/workflows/pdd.yml @@ -0,0 +1,15 @@ +--- +name: pdd +on: + push: + branches: + - master + pull_request: + branches: + - master +jobs: + pdd: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + - uses: volodya-lombrozo/pdd-action@master diff --git a/.github/workflows/up.yml b/.github/workflows/up.yml new file mode 100644 index 0000000..4e622ea --- /dev/null +++ b/.github/workflows/up.yml @@ -0,0 +1,21 @@ +--- +name: up +on: + push: +jobs: + up: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + - run: |- + git fetch --tags --force && \ + latest=$(git tag --sort=creatordate | tail -1) && \ + sed -E -i "s/[^<]+/${latest}/g" README.md + - uses: peter-evans/create-pull-request@v5 + with: + commit-message: 'new version in README' + delete-branch: true + title: 'New version in README' + assignees: yegor256 + branch: up + base: master \ No newline at end of file diff --git a/.github/workflows/xcop.yml b/.github/workflows/xcop.yml new file mode 100644 index 0000000..db3354a --- /dev/null +++ b/.github/workflows/xcop.yml @@ -0,0 +1,15 @@ +--- +name: xcop +on: + push: + branches: + - master + pull_request: + branches: + - master +jobs: + xcop: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + - uses: g4s8/xcop-action@master diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..65cde29 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +target/ +.idea +.DS_Store +*.iml +.project +.settings +.classpath +.recommenders +.mvn/wrapper/maven-wrapper.jar diff --git a/.rultor.yml b/.rultor.yml new file mode 100644 index 0000000..0db8972 --- /dev/null +++ b/.rultor.yml @@ -0,0 +1,18 @@ +docker: + image: yegor256/rultor-image:1.22.0 +assets: + settings.xml: yegor256/home#assets/jping/settings.xml + pubring.gpg: yegor256/home#assets/pubring.gpg + secring.gpg: yegor256/home#assets/secring.gpg +install: | + pdd --file=/dev/null +merge: + script: | + mvn clean install -Pqulice --errors -Dstyle.color=never +release: + script: |- + [[ "${tag}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || exit -1 + mvn versions:set "-DnewVersion=${tag}" -Dstyle.color=never + git commit -am "${tag}" + mvn clean install -Dinvoker.skip + mvn clean deploy -Pjping -Psonatype --errors --settings ../settings.xml -Dstyle.color=never diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..666872e --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2023 Yegor Bugayenko + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..41dde5e --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +[![EO principles respected here](https://www.elegantobjects.org/badge.svg)](https://www.elegantobjects.org) +[![DevOps By Rultor.com](http://www.rultor.com/b/yegor256/jping)](http://www.rultor.com/p/yegor256/jping) +[![We recommend IntelliJ IDEA](https://www.elegantobjects.org/intellij-idea.svg)](https://www.jetbrains.com/idea/) + +[![mvn](https://github.com/yegor256/jping/actions/workflows/mvn.yml/badge.svg)](https://github.com/yegor256/jping/actions/workflows/mvn.yml) +[![PDD status](http://www.0pdd.com/svg?name=yegor256/jping)](http://www.0pdd.com/p?name=yegor256/jping) +[![Maven Central](https://img.shields.io/maven-central/v/com.yegor256/jping.svg)](https://maven-badges.herokuapp.com/maven-central/com.yegor256/jping) +[![Javadoc](http://www.javadoc.io/badge/com.yegor256/jping.svg)](http://www.javadoc.io/doc/com.yegor256/jping) +[![codecov](https://codecov.io/gh/yegor256/jping/branch/master/graph/badge.svg)](https://codecov.io/gh/yegor256/jping) +[![Hits-of-Code](https://hitsofcode.com/github/yegor256/jping)](https://hitsofcode.com/view/github/yegor256/jping) +[![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/yegor256/jping/blob/master/LICENSE.txt) + +JUnit5 execution condition that checks whether a connection to public Internet is available. + +First, you add this to your `pom.xml`: + +```xml + + com.yegor256 + jping + 0.0.0 + +``` + +Then, you use it like this: + +```java +import com.yegor256.WeAreOnline; + +@ExtendWith(WeAreOnline.class) +final class MyTest { + // Your test methods +} +``` + + +## How to Contribute + +Fork repository, make changes, send us a [pull request](https://www.yegor256.com/2014/04/15/github-guidelines.html). +We will review your changes and apply them to the `master` branch shortly, +provided they don't violate our quality standards. To avoid frustration, +before sending us your pull request please run full Maven build: + +```bash +$ mvn clean install -Pqulice +``` + +You will need Maven 3.3+ and Java 8+. diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a69f1b6 --- /dev/null +++ b/pom.xml @@ -0,0 +1,132 @@ + + + + 4.0.0 + + com.jcabi + parent + 0.66.0 + + com.yegor256 + jping + 1.0-SNAPSHOT + jar + jping + JUnit execution condition to check that we are online + https://github.com/yegor256/jping + 2023 + + yegor256 + https://www.yegor256.com + + + + MIT + https://raw.githubusercontent.com/yegor256/jping/master/LICENSE.txt + site + + + + + boom + file:///tmp/boom + + true + + + + + + 1 + Yegor Bugayenko + yegor256@gmail.com + yegor256.com + https://www.yegor256.com + + Architect + Developer + + +3 + + + + GitHub + https://github.com/yegor256/jping/issues + + + scm:git:git@github.com:yegor256/jping.git + scm:git:git@github.com:yegor256/jping.git + https://github.com/yegor256/jping + + + rultor + https://www.rultor.com/s/yegor256/jping + + + + github-pages + https://github.com/yegor256/jping + + + + + org.junit.jupiter + junit-jupiter-api + 5.9.3 + provided + + + org.hamcrest + hamcrest + 2.2 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.9.3 + test + + + + + qulice + + + + com.qulice + qulice-maven-plugin + 0.22.0 + + + duplicatefinder:.* + + + + + + + + diff --git a/renovate.json b/renovate.json new file mode 100644 index 0000000..39a2b6e --- /dev/null +++ b/renovate.json @@ -0,0 +1,6 @@ +{ + "$schema": "https://docs.renovatebot.com/renovate-schema.json", + "extends": [ + "config:base" + ] +} diff --git a/src/main/java/com/yegor256/WeAreOnline.java b/src/main/java/com/yegor256/WeAreOnline.java new file mode 100644 index 0000000..9c1cec8 --- /dev/null +++ b/src/main/java/com/yegor256/WeAreOnline.java @@ -0,0 +1,89 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2023 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.yegor256; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URLConnection; +import org.junit.jupiter.api.extension.ConditionEvaluationResult; +import org.junit.jupiter.api.extension.ExecutionCondition; +import org.junit.jupiter.api.extension.ExtensionContext; + +/** + * JUnit5 Execution Condition to check that a public Internet connection is online. + * + *

Use in together with {@link ExecutionCondition}:

+ * + *
 import com.yegor256.WeAreOnline;
+ * @ExtendWith(WeAreOnline.class)
+ * final class MyTest {
+ *   // Your test methods
+ * }
+ * + *

This will guarantee you that the test methods inside this class + * will only be executed if a public Internet connection is available.

+ * + * @since 0.0.1 + */ +public final class WeAreOnline implements ExecutionCondition { + + @Override + public ConditionEvaluationResult evaluateExecutionCondition( + final ExtensionContext context) { + ConditionEvaluationResult ret; + try { + if (WeAreOnline.ping()) { + ret = ConditionEvaluationResult.enabled("We are online!"); + } else { + ret = ConditionEvaluationResult.disabled("We are offline"); + } + } catch (final IOException ex) { + ret = ConditionEvaluationResult.disabled( + String.format("Failed to check online status: %s", ex.getMessage()) + ); + } + return ret; + } + + /** + * Ping. + * @return TRUE if we are online + * @throws IOException In case of check failure + */ + private static boolean ping() throws IOException { + boolean online = true; + try { + final URLConnection conn = new URI("https://www.google.com").toURL().openConnection(); + conn.connect(); + conn.getInputStream().close(); + } catch (final IOException ignored) { + online = false; + } catch (final URISyntaxException ex) { + throw new IllegalStateException(ex); + } + return online; + } + +} diff --git a/src/main/java/com/yegor256/package-info.java b/src/main/java/com/yegor256/package-info.java new file mode 100644 index 0000000..80a8105 --- /dev/null +++ b/src/main/java/com/yegor256/package-info.java @@ -0,0 +1,30 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2023 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * JUnit5 Execution Condition to check that a public Internet connection is online. + * + * @since 0.0.1 + */ +package com.yegor256; diff --git a/src/test/java/com/yegor256/WeAreOnlineTest.java b/src/test/java/com/yegor256/WeAreOnlineTest.java new file mode 100644 index 0000000..7d9e5e5 --- /dev/null +++ b/src/test/java/com/yegor256/WeAreOnlineTest.java @@ -0,0 +1,45 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2023 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.yegor256; + +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; + +/** + * Test case for {@link WeAreOnline}. + * + * @since 0.1.0 + */ +final class WeAreOnlineTest { + + @Test + void checksOnlineStatus() { + MatcherAssert.assertThat( + new WeAreOnline().evaluateExecutionCondition(null).isDisabled(), + Matchers.is(false) + ); + } + +} diff --git a/src/test/java/com/yegor256/package-info.java b/src/test/java/com/yegor256/package-info.java new file mode 100644 index 0000000..a6fee0e --- /dev/null +++ b/src/test/java/com/yegor256/package-info.java @@ -0,0 +1,30 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2023 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * Tests for the main class. + * + * @since 0.0.1 + */ +package com.yegor256; diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties new file mode 100644 index 0000000..47fbf6e --- /dev/null +++ b/src/test/resources/log4j.properties @@ -0,0 +1,8 @@ +log4j.rootLogger=WARN, CONSOLE + +log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender +log4j.appender.CONSOLE.layout=com.jcabi.log.MulticolorLayout +log4j.appender.CONSOLE.layout.ConversionPattern=[%color{%p}] %c: %m%n + +log4j.logger.com.yegor256=DEBUG +log4j.logger.com.jcabi.log.VerboseProcess=DEBUG