Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Dec 15, 2023
2 parents c2c4e97 + ccf30b9 commit 1d4d694
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 15 deletions.
13 changes: 11 additions & 2 deletions eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.cactoos.experimental.Threads;
import org.cactoos.iterable.Mapped;
import org.cactoos.number.SumOf;
import org.eolang.maven.tojos.AttributeNotFoundException;
import org.eolang.maven.tojos.ForeignTojo;
import org.eolang.maven.util.HmBase;
import org.eolang.maven.util.Rel;
Expand Down Expand Up @@ -184,15 +185,23 @@ public void exec() throws IOException {
* @throws java.io.IOException If any issues with I/O
*/
private int transpile(final ForeignTojo tojo) throws IOException {
final int saved;
final Path file = tojo.verified();
final Path file;
try {
file = tojo.verified();
} catch (final AttributeNotFoundException exception) {
throw new IllegalStateException(
"You should check that 'Verify' goal of the plugin was run first",
exception
);
}
final XML input = new XMLDocument(file);
final String name = input.xpath("/program/@name").get(0);
final Place place = new Place(name);
final Path target = place.make(
this.targetDir.toPath().resolve(TranspileMojo.DIR),
TranspileMojo.EXT
);
final int saved;
if (
target.toFile().exists()
&& target.toFile().lastModified() >= file.toFile().lastModified()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 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.tojos;

/**
* If the attributes were not found in the Tojo.
* @since 0.35.0
*/
public class AttributeNotFoundException extends RuntimeException {

/**
* Ctor.
* @param attribute The attribute of Tojo.
*/
AttributeNotFoundException(final ForeignTojos.Attribute attribute) {
super(
String.format(
"There is no '%s' attribute in the tojo", attribute
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,55 +58,55 @@ public ForeignTojo(final Tojo original) {
* @return The id of the tojo.
*/
public String identifier() {
return this.delegate.get(ForeignTojos.Attribute.ID.key());
return this.attribute(ForeignTojos.Attribute.ID);
}

/**
* The tojo xmir.
* @return The xmir.
*/
public Path xmir() {
return Paths.get(this.delegate.get(ForeignTojos.Attribute.XMIR.key()));
return Paths.get(this.attribute(ForeignTojos.Attribute.XMIR));
}

/**
* The tojo optimized xmir.
* @return The optimized xmir.
*/
public Path optimized() {
return Paths.get(this.delegate.get(ForeignTojos.Attribute.OPTIMIZED.key()));
return Paths.get(this.attribute(ForeignTojos.Attribute.OPTIMIZED));
}

/**
* The tojo verified xmir.
* @return The verified xmir.
*/
public Path verified() {
return Paths.get(this.delegate.get(ForeignTojos.Attribute.VERIFIED.key()));
return Paths.get(this.attribute(ForeignTojos.Attribute.VERIFIED));
}

/**
* The tojo shaken xmir.
* @return The shaken xmir.
*/
public Path shaken() {
return Paths.get(this.delegate.get(ForeignTojos.Attribute.SHAKEN.key()));
return Paths.get(this.attribute(ForeignTojos.Attribute.SHAKEN));
}

/**
* The tojo eo object.
* @return The eo object.
*/
public Path source() {
return Paths.get(this.delegate.get(ForeignTojos.Attribute.EO.key()));
return Paths.get(this.attribute(ForeignTojos.Attribute.EO));
}

/**
* The tojo version.
* @return The version.
*/
public String version() {
return this.delegate.get(ForeignTojos.Attribute.VERSION.key());
return this.attribute(ForeignTojos.Attribute.VERSION);
}

/**
Expand All @@ -116,7 +116,7 @@ public String version() {
public String description() {
return String.format(
"%s:%s",
this.delegate.get(ForeignTojos.Attribute.ID.key()),
this.attribute(ForeignTojos.Attribute.ID),
this.version()
);
}
Expand All @@ -126,15 +126,15 @@ public String description() {
* @return The hash.
*/
public String hash() {
return this.delegate.get(ForeignTojos.Attribute.HASH.key());
return this.attribute(ForeignTojos.Attribute.HASH);
}

/**
* The tojo probed.
* @return The probed.
*/
public String probed() {
return this.delegate.get(ForeignTojos.Attribute.PROBED.key());
return this.attribute(ForeignTojos.Attribute.PROBED);
}

/**
Expand Down Expand Up @@ -365,7 +365,7 @@ public ForeignTojo withScope(final String scope) {
* @return The scope.
*/
public String scope() {
return this.delegate.get(ForeignTojos.Attribute.SCOPE.key());
return this.attribute(ForeignTojos.Attribute.SCOPE);
}

/**
Expand All @@ -383,7 +383,7 @@ public ForeignTojo withVer(final String ver) {
* @return The version.
*/
public String ver() {
return this.delegate.get(ForeignTojos.Attribute.VER.key());
return this.attribute(ForeignTojos.Attribute.VER);
}

@Override
Expand All @@ -404,4 +404,17 @@ public boolean equals(final Object other) {
public int hashCode() {
return Objects.hash(this.delegate);
}

/**
* Return the attribute from the tojo.
* @param attribute The attribute from ForeignTojos.Attribute.
* @return The attribute.
*/
private String attribute(final ForeignTojos.Attribute attribute) {
final String attr = this.delegate.get(attribute.key());
if (attr == null) {
throw new AttributeNotFoundException(attribute);
}
return attr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@
import org.cactoos.text.Randomized;
import org.cactoos.text.TextOf;
import org.eolang.jucs.ClasspathSource;
import org.eolang.maven.log.CaptureLogs;
import org.eolang.maven.log.Logs;
import org.eolang.maven.util.HmBase;
import org.eolang.xax.XaxStory;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.io.FileMatchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand All @@ -50,7 +53,7 @@
*
* @since 0.1
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
@SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods"})
final class TranspileMojoTest {

/**
Expand Down Expand Up @@ -218,6 +221,29 @@ void transpilesAndCleansGarbageFromDirtyDependency(@TempDir final Path temp)
);
}

@Test
@CaptureLogs
void throwsExpectionIfWasNotVerified(
@TempDir final Path temp, final Logs out) {
Assertions.assertThrows(
IllegalStateException.class,
() -> new FakeMaven(temp)
.withHelloWorld()
.execute(ParseMojo.class)
.execute(OptimizeMojo.class)
.execute(ShakeMojo.class)
.execute(TranspileMojo.class)
);
Assertions.assertTrue(
out.captured().stream().anyMatch(
log -> log.contains(
"You should check that 'Verify' goal of the plugin was run first"
)
),
"Should throw an exception if VerifyMojo wasn't run before TranspileMojo"
);
}

/**
* Get all classes in directory.
* @param root Directory to get classes from.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.cactoos.Func;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Tests from {@link ForeignTojos}.
Expand Down Expand Up @@ -116,8 +120,73 @@ void findsAnyTojoIfSeveralTojosWithTheSameIdWereAdded() {
);
}

@ParameterizedTest
@MethodSource("tojoFunctionsWithoutDefaultValues")
void throwsExceptionIfKeyWasNotFoundInTojo(
final String key,
final Func<ForeignTojo, Object> method) {
final ForeignTojo tojo = this.tojos.add("string");
Assertions.assertThrows(
AttributeNotFoundException.class,
() -> method.apply(tojo),
String.format("Should throw an exception if key='%s' was not found in Tojo", key)
);
}

@Test
void getsExceptionMessageIfKeyWasNotFoundInTojo() {
final ForeignTojo tojo = this.tojos.add("string");
final AttributeNotFoundException thrown = Assertions.assertThrows(
AttributeNotFoundException.class,
tojo::verified
);
Assertions.assertEquals(
"There is no 'VERIFIED' attribute in the tojo",
thrown.getMessage(),
"Should throw an exception if key 'VERIFIED' was not found in Tojo"
);
}

@ParameterizedTest
@MethodSource("tojoFunctionsWithDefaultValues")
void doesNotThrowsAnException(
final String key,
final Func<ForeignTojo, ?> method
) throws Exception {
final ForeignTojo tojo = this.tojos.add("string");
Assertions.assertEquals(
method.apply(tojo),
key,
String.format("Shouldn't throw an exception if key='%s' was found in Tojo", key)
);
}

@AfterEach
void tearDown() throws IOException {
this.tojos.close();
}

@SuppressWarnings("PMD.UnusedPrivateMethod")
private static Stream<Arguments> tojoFunctionsWithoutDefaultValues() {
return Stream.of(
Arguments.of("XMIR", (Func<ForeignTojo, Object>) ForeignTojo::xmir),
Arguments.of("OPTIMIZED", (Func<ForeignTojo, Object>) ForeignTojo::optimized),
Arguments.of("VERIFIED", (Func<ForeignTojo, Object>) ForeignTojo::verified),
Arguments.of("SHAKEN", (Func<ForeignTojo, Object>) ForeignTojo::shaken),
Arguments.of("EO", (Func<ForeignTojo, Object>) ForeignTojo::source),
Arguments.of("VERSION", (Func<ForeignTojo, Object>) ForeignTojo::version),
Arguments.of("ID", (Func<ForeignTojo, Object>) ForeignTojo::description),
Arguments.of("HASH", (Func<ForeignTojo, Object>) ForeignTojo::hash),
Arguments.of("PROBED", (Func<ForeignTojo, Object>) ForeignTojo::probed),
Arguments.of("VER", (Func<ForeignTojo, Object>) ForeignTojo::ver)
);
}

@SuppressWarnings("PMD.UnusedPrivateMethod")
private static Stream<Arguments> tojoFunctionsWithDefaultValues() {
return Stream.of(
Arguments.of("string", (Func<ForeignTojo, Object>) ForeignTojo::identifier),
Arguments.of("compile", (Func<ForeignTojo, Object>) ForeignTojo::scope)
);
}
}

0 comments on commit 1d4d694

Please sign in to comment.