Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce code duplication down to 15 lines per piece #2916

Merged
merged 23 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
72632f2
#2863 add extra object to reduce code duplication
c71n93 Mar 4, 2024
4e872b4
#2863 make Home abstract class to reduce code duplication
c71n93 Mar 4, 2024
67606b9
#2863 create extra function to reduce code duplication in UnplaceMojo…
c71n93 Mar 4, 2024
34f68ad
#2863 get rid of ugly method using parametrized test
c71n93 Mar 4, 2024
31572d1
#2863 supress UnusedPrivateMethod warning
c71n93 Mar 4, 2024
0a6fa6f
#2863 add PhDecorator abstract class to reduce code duplication
c71n93 Mar 8, 2024
adee12e
#2863 delete some unnecessary methods overriding
c71n93 Mar 8, 2024
3842823
Merge branch 'master' into 2863-reduce-code-duplication-15
c71n93 Mar 11, 2024
74c589c
#2863 fix qulice warnings
c71n93 Mar 11, 2024
7c77831
Merge branch 'master' into 2863-reduce-code-duplication-15
c71n93 Mar 26, 2024
03e7d15
#2863 remove unnecessary test
c71n93 Mar 27, 2024
be129c9
Merge branch 'master' into 2863-reduce-code-duplication-15
c71n93 Apr 11, 2024
8b58618
#2863 remove duplicated test
c71n93 Apr 11, 2024
5be5468
#2863 remove sudden groovy libs
c71n93 Apr 11, 2024
cc49278
#2863 new line at the end of the file
c71n93 Apr 11, 2024
76f06cd
#2863 get rid of Home abstract class
c71n93 Apr 15, 2024
725d9b1
#2863 rollback to current master
c71n93 Apr 15, 2024
daaee28
#2863 remove PhDecorator
c71n93 Apr 15, 2024
c73be94
#2863 remove newline at the end of file
c71n93 Apr 15, 2024
9a959ae
#2863 try to get rid of default methods in Home interface
c71n93 May 2, 2024
bc09cd0
Merge branch 'master' into 2863-reduce-code-duplication-15
c71n93 May 2, 2024
3258acd
#2863 fix namings and qulice violations
c71n93 May 2, 2024
e422a9c
#2863 fix threshold
c71n93 May 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/simian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
- run: |
wget --quiet http://public.yegor256.com/simian.jar -O /tmp/simian.jar
- run: |
java -jar /tmp/simian.jar -threshold=19 -excludes=**/gen -excludes=**/it **/*.java
java -jar /tmp/simian.jar -threshold=14 -excludes=**/gen -excludes=**/it **/*.java
25 changes: 1 addition & 24 deletions eo-maven-plugin/src/main/java/org/eolang/maven/util/HmBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@
import com.jcabi.log.Logger;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import org.cactoos.Bytes;
import org.cactoos.Input;
import org.cactoos.Text;
import org.cactoos.bytes.BytesOf;
import org.cactoos.io.InputOf;
import org.cactoos.io.OutputTo;
import org.cactoos.io.TeeInput;
import org.cactoos.scalar.IoChecked;
Expand All @@ -45,7 +42,7 @@
* @since 0.27
*/
@SuppressWarnings("PMD.TooManyMethods")
public final class HmBase implements Home {
public final class HmBase extends Home {
/**
* Current working directory.
*/
Expand All @@ -69,26 +66,6 @@ public HmBase(final Path path) {
this.cwd = path;
}

@Override
public void save(final String str, final Path path) throws IOException {
this.save(new InputOf(str), path);
}

@Override
public void save(final Text txt, final Path path) throws IOException {
this.save(new InputOf(txt), path);
}

@Override
public void save(final InputStream stream, final Path path) throws IOException {
this.save(new InputOf(stream), path);
}

@Override
public void save(final byte[] bytes, final Path path) throws IOException {
this.save(new InputOf(bytes), path);
}

@Override
public void save(final Input input, final Path path) throws IOException {
final Path target = this.absolute(this.onlyRelative(path));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,16 @@

import com.jcabi.log.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import org.cactoos.Bytes;
import org.cactoos.Input;
import org.cactoos.Text;
import org.cactoos.io.InputOf;

/**
* Location for files that saves optionally.
* @since 0.32.0
*/
@SuppressWarnings("PMD.TooManyMethods")
public final class HmOptional implements Home {
public final class HmOptional extends Home {
/**
* Original home.
*/
Expand All @@ -59,26 +56,6 @@ public HmOptional(final Home home, final boolean rwte) {
this.rewrite = rwte;
}

@Override
public void save(final String str, final Path path) throws IOException {
this.save(new InputOf(str), path);
}

@Override
public void save(final Text txt, final Path path) throws IOException {
this.save(new InputOf(txt), path);
}

@Override
public void save(final InputStream stream, final Path path) throws IOException {
this.save(new InputOf(stream), path);
}

@Override
public void save(final byte[] bytes, final Path path) throws IOException {
this.save(new InputOf(bytes), path);
}

@Override
public void save(final Input input, final Path path) throws IOException {
final Path target = this.absolute(this.onlyRelative(path));
Expand Down
29 changes: 19 additions & 10 deletions eo-maven-plugin/src/main/java/org/eolang/maven/util/Home.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@
import org.cactoos.Bytes;
import org.cactoos.Input;
import org.cactoos.Text;
import org.cactoos.io.InputOf;

/**
* Location for the files.
* @since 0.32.0
*/
public interface Home {
public abstract class Home {
/**
* Saving string.
*
* @param str String
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
void save(String str, Path path) throws IOException;
public void save(final String str, final Path path) throws IOException {
this.save(new InputOf(str), path);
}

/**
* Saving text.
Expand All @@ -51,7 +54,9 @@ public interface Home {
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
void save(Text txt, Path path) throws IOException;
public void save(final Text txt, final Path path) throws IOException {
this.save(new InputOf(txt), path);
}

/**
* Saving stream.
Expand All @@ -60,7 +65,9 @@ public interface Home {
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
void save(InputStream stream, Path path) throws IOException;
public void save(final InputStream stream, final Path path) throws IOException {
this.save(new InputOf(stream), path);
}

/**
* Saving bytes.
Expand All @@ -69,7 +76,9 @@ public interface Home {
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
void save(byte[] bytes, Path path) throws IOException;
public void save(final byte[] bytes, final Path path) throws IOException {
this.save(new InputOf(bytes), path);
}

/**
* Saving input.
Expand All @@ -79,7 +88,7 @@ public interface Home {
* @throws IOException If fails
* @throws IllegalArgumentException If given path is absolute
*/
void save(Input input, Path path) throws IOException;
public abstract void save(Input input, Path path) throws IOException;

/**
* Check if exists.
Expand All @@ -88,7 +97,7 @@ public interface Home {
* @return True if exists
* @throws IllegalArgumentException If given path is absolute
*/
boolean exists(Path path);
public abstract boolean exists(Path path);

/**
* Load bytes from file by path.
Expand All @@ -99,21 +108,21 @@ public interface Home {
* if some exception happens during reading the file
* @throws IllegalArgumentException If given path is absolute
*/
Bytes load(Path path) throws IOException;
public abstract Bytes load(Path path) throws IOException;

/**
* Absolute path to a file.
*
* @param path Cwd-relative path to file
* @return Absolute path
*/
Path absolute(Path path);
public abstract Path absolute(Path path);

/**
* Verifies that given path is relative and throws exception.
* @param path Path to be verified
* @return Given path if it's relative
* @throws IllegalArgumentException If given path is Absolute
*/
Path onlyRelative(Path path);
public abstract Path onlyRelative(Path path);
}
124 changes: 57 additions & 67 deletions eo-maven-plugin/src/test/java/org/eolang/maven/UnplaceMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Stream;
import org.cactoos.text.TextOf;
import org.eolang.maven.tojos.PlacedTojo;
import org.eolang.maven.tojos.PlacedTojos;
Expand All @@ -41,6 +42,9 @@
import org.hamcrest.io.FileMatchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Test case for {@link UnplaceMojo}.
Expand Down Expand Up @@ -129,74 +133,46 @@ void keepsJarBecauseItIsStillInUse(@TempDir final Path temp) throws IOException
);
}

@Test
void unplacesWithKeepAndRemoveBinariesParamTogether(@TempDir final Path temp) throws Exception {
final Path placed = UnplaceMojoTest.placeClass(temp, UnplaceMojoTest.clazz(temp));
final Map<String, Path> res = new FakeMaven(temp)
.with("placed", placed.toFile())
.with("keepBinaries", UnplaceMojoTest.GLOB_PATTERN)
.with("removeBinaries", UnplaceMojoTest.GLOB_PATTERN)
.execute(UnplaceMojo.class)
.result();
MatcherAssert.assertThat(
"TO ADD ASSERTION MESSAGE",
res.values().stream().noneMatch(UnplaceMojoTest::isClass),
Matchers.is(true)
);
MatcherAssert.assertThat(
"TO ADD ASSERTION MESSAGE",
new TextOf(res.get(placed.getFileName().toString())).asString(),
Matchers.allOf(
Matchers.not(Matchers.containsString("false")),
Matchers.containsString("true")
)
);
}

@Test
void unplacesWithRemoveBinariesParam(@TempDir final Path temp) throws Exception {
final Path placed = UnplaceMojoTest.placeClass(temp, UnplaceMojoTest.clazz(temp));
final Map<String, Path> res = new FakeMaven(temp)
.with("placed", placed.toFile())
.with("removeBinaries", UnplaceMojoTest.GLOB_PATTERN)
.execute(UnplaceMojo.class)
.result();
MatcherAssert.assertThat(
"TO ADD ASSERTION MESSAGE",
res.values().stream().noneMatch(UnplaceMojoTest::isClass),
Matchers.is(true)
);
MatcherAssert.assertThat(
"TO ADD ASSERTION MESSAGE",
new TextOf(res.get(placed.getFileName().toString())).asString(),
Matchers.allOf(
Matchers.not(Matchers.containsString("false")),
Matchers.containsString("true")
)
);
}

@Test
void unplacesWithKeepBinariesParam(@TempDir final Path temp) throws Exception {
@ParameterizedTest
@MethodSource("testArgsProvider")
void unplacesWithKeepOrRemoveBinariesParam(final String[] params, @TempDir final Path temp)
throws Exception {
final Path placed = UnplaceMojoTest.placeClass(temp, UnplaceMojoTest.clazz(temp));
final Map<String, Path> res = new FakeMaven(temp)
.with("placed", placed.toFile())
.with("keepBinaries", UnplaceMojoTest.GLOB_PATTERN)
.execute(UnplaceMojo.class)
.result();
MatcherAssert.assertThat(
"TO ADD ASSERTION MESSAGE",
res.values().stream().anyMatch(UnplaceMojoTest::isClass),
Matchers.is(true)
);
MatcherAssert.assertThat(
"TO ADD ASSERTION MESSAGE",
new TextOf(res.get(placed.getFileName().toString())).asString(),
Matchers.allOf(
Matchers.containsString("false"),
Matchers.not(Matchers.containsString("true"))
)
);
final FakeMaven maven = new FakeMaven(temp)
.with("placed", placed.toFile());
for (final String param : params) {
maven.with(param, UnplaceMojoTest.GLOB_PATTERN);
}
final Map<String, Path> res = maven.execute(UnplaceMojo.class).result();
if (params.length == 1 && "keepBinaries".equals(params[0])) {
MatcherAssert.assertThat(
"TO ADD ASSERTION MESSAGE",
res.values().stream().anyMatch(UnplaceMojoTest::isClass),
Matchers.is(true)
);
MatcherAssert.assertThat(
"TO ADD ASSERTION MESSAGE",
new TextOf(res.get(placed.getFileName().toString())).asString(),
Matchers.allOf(
Matchers.containsString("false"),
Matchers.not(Matchers.containsString("true"))
)
);
} else {
MatcherAssert.assertThat(
"TO ADD ASSERTION MESSAGE",
res.values().stream().noneMatch(UnplaceMojoTest::isClass),
Matchers.is(true)
);
MatcherAssert.assertThat(
"TO ADD ASSERTION MESSAGE",
new TextOf(res.get(placed.getFileName().toString())).asString(),
Matchers.allOf(
Matchers.not(Matchers.containsString("false")),
Matchers.containsString("true")
)
);
}
}

@Test
Expand Down Expand Up @@ -307,5 +283,19 @@ private static Path clazz(final Path temp) throws IOException {
private static boolean isClass(final Path path) {
return path.toString().endsWith(".class");
}

/**
* Input arguments for unit tests.
*
* @return Stream of arguments.
*/
@SuppressWarnings("PMD.UnusedPrivateMethod")
private static Stream<Arguments> testArgsProvider() {
return Stream.of(
Arguments.of((Object) new String[]{"keepBinaries"}),
Arguments.of((Object) new String[]{"removeBinaries"}),
Arguments.of((Object) new String[]{"keepBinaries", "removeBinaries"})
);
}
}

Binary file added eo-runtime/lib/groovy-5.0.0-alpha-1-sources.jar
c71n93 marked this conversation as resolved.
Show resolved Hide resolved
Binary file not shown.
Binary file added eo-runtime/lib/groovy-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-ant-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-console-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-datetime-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-dateutil-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-ginq-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-groovysh-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-jmx-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-json-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-jsr223-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-macro-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-nio-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-servlet-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-sql-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-swing-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-templates-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-test-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-testng-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-toml-5.0.0-alpha-1.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added eo-runtime/lib/groovy-xml-5.0.0-alpha-1.jar
Binary file not shown.
Loading
Loading