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 19 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
91 changes: 91 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/util/DataSaved.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 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.util;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import org.cactoos.Input;
import org.cactoos.Text;
import org.cactoos.io.InputOf;

/**
* The data is saved to a file.
* @since 0.32.0
*/
public interface DataSaved {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c71n93 interface with default methods - it's almost the same as abstract class. You split the logic and keep it in different places.

/**
* Saving string.
*
* @param str String
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
default void save(final String str, final Path path) throws IOException {
this.save(new InputOf(str), path);
}

/**
* Saving text.
*
* @param txt Text
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
default void save(final Text txt, final Path path) throws IOException {
this.save(new InputOf(txt), path);
}

/**
* Saving stream.
*
* @param stream Input stream
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
default void save(final InputStream stream, final Path path) throws IOException {
this.save(new InputOf(stream), path);
}

/**
* Saving bytes.
*
* @param bytes Byte array
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
default void save(final byte[] bytes, final Path path) throws IOException {
this.save(new InputOf(bytes), path);
}

/**
* Saving input.
*
* @param input Input
* @param path Cwd-relative path to file
* @throws IOException If fails
* @throws IllegalArgumentException If given path is absolute
*/
void save(Input input, Path path) throws IOException;
}
23 changes: 0 additions & 23 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 Down Expand Up @@ -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,12 +25,9 @@

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.
Expand Down Expand Up @@ -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
51 changes: 1 addition & 50 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 @@ -24,63 +24,14 @@
package org.eolang.maven.util;

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;

/**
* Location for the files.
* @since 0.32.0
*/
public interface 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;

/**
* Saving text.
*
* @param txt Text
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
void save(Text txt, Path path) throws IOException;

/**
* Saving stream.
*
* @param stream Input stream
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
void save(InputStream stream, Path path) throws IOException;

/**
* Saving bytes.
*
* @param bytes Byte array
* @param path Cwd-relative path to file
* @throws IOException If fails
*/
void save(byte[] bytes, Path path) throws IOException;

/**
* Saving input.
*
* @param input Input
* @param path Cwd-relative path to file
* @throws IOException If fails
* @throws IllegalArgumentException If given path is absolute
*/
void save(Input input, Path path) throws IOException;

public interface Home extends DataSaved {
/**
* Check if exists.
*
Expand Down
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"})
);
}
}

Loading
Loading