From 2035041e6742cecd938a87a27ac5790e062d6a2d Mon Sep 17 00:00:00 2001 From: mximp Date: Wed, 9 Nov 2022 14:16:50 +0300 Subject: [PATCH] 1419 Testable OyRemote --- .../main/java/org/eolang/maven/OyRemote.java | 63 ++++++++++++++++-- .../java/org/eolang/maven/ChRemoteTest.java | 2 +- .../java/org/eolang/maven/OyRemoteTest.java | 64 +++++++++++++++++++ 3 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 eo-maven-plugin/src/test/java/org/eolang/maven/OyRemoteTest.java diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/OyRemote.java b/eo-maven-plugin/src/main/java/org/eolang/maven/OyRemote.java index 1f42b1ae0d..411ed2034a 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/OyRemote.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/OyRemote.java @@ -40,7 +40,7 @@ final class OyRemote implements Objectionary { /** * The address template. */ - private final String template; + private final UrlOy template; /** * Constructor. @@ -48,22 +48,20 @@ final class OyRemote implements Objectionary { * @throws IOException if fails. */ OyRemote(final CommitHash hash) throws IOException { - this.template = String.format( - "https://raw.githubusercontent.com/objectionary/home/%s/objects/%%s.eo", + this.template = new UrlOy( + "https://raw.githubusercontent.com/objectionary/home/%s/objects/%s.eo", hash.value() ); } @Override public String toString() { - return this.template; + return this.template.toString(); } @Override public Input get(final String name) throws MalformedURLException { - final URL url = new URL( - String.format(this.template, name.replace(".", "/")) - ); + final URL url = this.template.get(name); Logger.debug( this, "The object '%s' will be pulled from %s...", name, url @@ -71,4 +69,55 @@ public Input get(final String name) throws MalformedURLException { return new InputOf(url); } + /** + * Objectionary URL template. + * + * @since 1.0 + */ + public static class UrlOy { + + /** + * URL template. Expects two placeholders in terms of + * {@link String#format(String, Object...)}: 1st for hash, + * 2nd for program name. + */ + private final String template; + + /** + * Objects version hash. + */ + private final String hash; + + /** + * Ctor. + * @param template URL template. + * @param hash Version hash. + */ + UrlOy(final String template, final String hash) { + this.template = template; + this.hash = hash; + } + + /** + * URL for the program. + * @param name Fully qualified EO program name + * @return URL + * @throws MalformedURLException in case of incorrect URL + */ + public URL get(final String name) throws MalformedURLException { + return new URL( + String.format( + this.template, + this.hash, + name.replace(".", "/") + ) + ); + } + + @Override + public String toString() { + return this.template; + } + } + } diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/ChRemoteTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/ChRemoteTest.java index e8a73c4210..2ae9ca80ce 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/ChRemoteTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/ChRemoteTest.java @@ -31,7 +31,7 @@ import org.junit.jupiter.api.extension.ExtendWith; /** - * Test case for {@link OyRemote}. + * Test case for {@link ChRemote}. * @since 0.26 */ @ExtendWith(WeAreOnline.class) diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/OyRemoteTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/OyRemoteTest.java new file mode 100644 index 0000000000..a2a38bbf68 --- /dev/null +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/OyRemoteTest.java @@ -0,0 +1,64 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2022 Objectionary.com + * + * 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 org.eolang.maven; + +import java.net.MalformedURLException; +import java.net.URL; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Test for {@link OyRemote}. + * + * @since 1.0 + */ +final class OyRemoteTest { + + @Test + void buildsCorrectUrl() throws Exception { + final URL url = new OyRemote.UrlOy( + "https://raw/objectionary/home/%s/objects/%s.eo", + "abcde" + ).get("org.eolang.app"); + MatcherAssert.assertThat( + url, + Matchers.is( + new URL("https://raw/objectionary/home/abcde/objects/org/eolang/app.eo") + ) + ); + } + + @Test + void throwsExceptionOnInvalidUrl() { + Assertions.assertThrows( + MalformedURLException.class, + () -> new OyRemote.UrlOy( + "hts:raw.githubusercontent.com/objectionary/home/%s/objects/%s.eo", + "abcde" + ).get("org.eolang.app") + ); + } +}