Skip to content

Commit

Permalink
1419 Testable OyRemote
Browse files Browse the repository at this point in the history
  • Loading branch information
mximp committed Nov 9, 2022
1 parent 2dd0579 commit 2035041
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 8 deletions.
63 changes: 56 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 @@ -40,35 +40,84 @@ 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.get(name);
Logger.debug(
this, "The object '%s' will be pulled from %s...",
name, url
);
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;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
64 changes: 64 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,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")
);
}
}

0 comments on commit 2035041

Please sign in to comment.