From 2af0eac2fa49b9fe92dd911ebc5955d65657394a Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 27 Oct 2013 14:35:16 +0100 Subject: [PATCH] Add blacklist filter. --- pom.xml | 12 ++++ .../rhynodge/filters/BlacklistFilter.java | 77 ++++++++++++++++++++++ .../rhynodge/filters/BlacklistFilterTest.java | 71 ++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 src/main/java/net/pterodactylus/rhynodge/filters/BlacklistFilter.java create mode 100644 src/test/java/net/pterodactylus/rhynodge/filters/BlacklistFilterTest.java diff --git a/pom.xml b/pom.xml index 4fd28a0..cbbb5b9 100644 --- a/pom.xml +++ b/pom.xml @@ -75,6 +75,18 @@ + junit + junit + 4.11 + test + + + org.hamcrest + hamcrest-library + 1.3 + test + + com.google.guava guava 14.0-rc1 diff --git a/src/main/java/net/pterodactylus/rhynodge/filters/BlacklistFilter.java b/src/main/java/net/pterodactylus/rhynodge/filters/BlacklistFilter.java new file mode 100644 index 0000000..8ce25d6 --- /dev/null +++ b/src/main/java/net/pterodactylus/rhynodge/filters/BlacklistFilter.java @@ -0,0 +1,77 @@ +/* + * rhynodge - BlacklistFilter.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.rhynodge.filters; + +import static com.google.common.base.Preconditions.checkState; +import static com.google.common.collect.FluentIterable.from; +import static java.util.Arrays.asList; + +import java.util.List; + +import net.pterodactylus.rhynodge.Filter; +import net.pterodactylus.rhynodge.State; +import net.pterodactylus.rhynodge.states.FailedState; +import net.pterodactylus.rhynodge.states.TorrentState; +import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile; + +import com.google.common.base.Predicate; + +/** + * Filter for {@link TorrentState}s that removes all {@link TorrentFile}s whose + * names match a list of bad words. + * + * @author David ‘Bombe’ Roden + */ +public class BlacklistFilter implements Filter { + + private final Iterable filterWords; + + public BlacklistFilter(List filterWords) { + this.filterWords = filterWords; + } + + @Override + public State filter(State state) { + if (!state.success()) { + return FailedState.from(state); + } + checkState(state instanceof TorrentState, "state is not a TorrentState but a %s!", state.getClass()); + + TorrentState torrentState = (TorrentState) state; + return new TorrentState(from(torrentState.torrentFiles()).filter(new Predicate() { + @Override + public boolean apply(TorrentFile torrentFile) { + return (torrentFile == null) ? false : nameDoesNotMatchAFilterWord(torrentFile.name()); + } + + private boolean nameDoesNotMatchAFilterWord(final String name) { + return !from(filterWords).anyMatch(new Predicate() { + @Override + public boolean apply(String word) { + return name.toLowerCase().contains(word.toLowerCase()); + } + }); + } + }).toList()); + } + + public static BlacklistFilter createDefaultBlacklistFilter() { + return new BlacklistFilter(asList("-juggs", "-MAX", "-3LT0N", "-Haggebulle", "-FooKaS", "-HELLRAZ0R", "-PLAYNOW", "-UnKnOwN", "-EDAW", "-NYDIC", "-TASTE", "-AQOS", "-AMIABLE", "-NoGRP", "-TAMILROCKERS", "-ADTRG", "-LEGi0N", "-DiRTYMARY", "-SUFFiCE", "-CM", "-AXED", "-AN0NYM0US", "-S4A", "-MiLLENiUM")); + } + +} diff --git a/src/test/java/net/pterodactylus/rhynodge/filters/BlacklistFilterTest.java b/src/test/java/net/pterodactylus/rhynodge/filters/BlacklistFilterTest.java new file mode 100644 index 0000000..ed8e3a3 --- /dev/null +++ b/src/test/java/net/pterodactylus/rhynodge/filters/BlacklistFilterTest.java @@ -0,0 +1,71 @@ +/* + * rhynodge - BlacklistFilterTest.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.rhynodge.filters; + +import static java.util.Arrays.asList; +import static net.pterodactylus.rhynodge.filters.BlacklistFilter.createDefaultBlacklistFilter; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; + +import net.pterodactylus.rhynodge.State; +import net.pterodactylus.rhynodge.states.FailedState; +import net.pterodactylus.rhynodge.states.TorrentState; +import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile; + +import org.junit.Test; + +/** + * Unit test for {@link BlacklistFilter}. + * + * @author David ‘Bombe’ Roden + */ +public class BlacklistFilterTest { + + private final BlacklistFilter blacklistFilter = new BlacklistFilter(asList("-Haggebulle", "-FooKaS")); + private final TorrentFile notMatchingTorrentFile1 = new TorrentFile("The Vampire Diaries S05E05 2013 HDRip 720p-3LT0N", "1.1G", "magnet:?xt=a", "http://", 1, 2, 3); + private final TorrentFile notMatchingTorrentFile2 = new TorrentFile("The Vampire Diaries S05E05 2013 HDRip 720p-HELLRAZ0R", "1.1G", "magnet:?xt=b", "http://", 1, 2, 3); + private final TorrentFile matchingTorrentFile1 = new TorrentFile("The Vampire Diaries S05E05 2013 HDRip 720p-Haggebulle", "1.1G", "magnet:?xt=c", "http://", 1, 2, 3); + private final TorrentFile matchingTorrentFile2 = new TorrentFile("The Vampire Diaries S05E05 2013 HDRip 720p-FooKaS", "1.1G", "magnet:?xt=d", "http://", 1, 2, 3); + + @Test + public void verifyThatMatchingTorrentsAreRemoved() { + TorrentState torrentState = new TorrentState(); + torrentState.addTorrentFile(matchingTorrentFile1); + torrentState.addTorrentFile(notMatchingTorrentFile1); + torrentState.addTorrentFile(matchingTorrentFile2); + torrentState.addTorrentFile(notMatchingTorrentFile2); + TorrentState filteredState = (TorrentState) blacklistFilter.filter(torrentState); + assertThat(filteredState, notNullValue()); + assertThat(filteredState.torrentFiles(), containsInAnyOrder(notMatchingTorrentFile1, notMatchingTorrentFile2)); + } + + @Test + public void verifyThatAFailedStateResultsInAFailedState() { + State state = blacklistFilter.filter(new FailedState()); + assertThat(state, notNullValue()); + assertThat(state.success(), is(false)); + } + + @Test + public void verifyThatABlacklistFilterIsCreated() { + assertThat(createDefaultBlacklistFilter(), notNullValue()); + } + +} -- 2.7.4