Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Nov 10, 2022
2 parents f32a3ce + 363e212 commit 40123ac
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 7 deletions.
68 changes: 61 additions & 7 deletions eo-maven-plugin/src/main/java/org/eolang/maven/OyRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,89 @@ final class OyRemote implements Objectionary {
/**
* The address template.
*/
private final String template;
private final UrlOy template;

/**
* Constructor.
* @param hash Commit hash
* @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.value(name);
Logger.debug(
this, "The object '%s' will be pulled from %s...",
name, url
);
return new InputOf(url);
}

/**
* Objectionary URL template.
* Assumes two placeholders in terms of
* {@link String#format(String, Object...)}: 1st for version hash,
* 2nd for program name.
* <br/>Example: "https://raw.githubusercontent.com/objectionary/home/%s/objects/%s.eo"
*
* @since 1.0
*/
static final class UrlOy {

/**
* URL template. Expects two placeholders in terms of
* {@link String#format(String, Object...)}: 1st for hash,
* 2nd for program name.
* <br/>Example: "https://raw.githubusercontent.com/objectionary/home/%s/objects/%s.eo"
*/
private final String template;

/**
* Objects version hash.
*/
private final String hash;

/**
* Ctor.
* @param template URL template.
* @param hash Objects 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 as specified by {@link Place}
* @return URL
* @throws MalformedURLException in case of incorrect URL
*/
public URL value(final String name) throws MalformedURLException {
return new URL(
String.format(
this.template,
this.hash,
name.replace(".", "/")
)
);
}

@Override
public String toString() {
return this.template;
}
}

}
63 changes: 63 additions & 0 deletions eo-maven-plugin/src/test/java/org/eolang/maven/OyRemoteTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 {
MatcherAssert.assertThat(
new OyRemote.UrlOy(
"https://raw/objectionary/home/%s/objects/%s.eo",
"abcde"
).value("org.eolang.app"),
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"
).value("org.eolang.app")
);
}
}

0 comments on commit 40123ac

Please sign in to comment.