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

Adds protection against duplicate url #162

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
42 changes: 30 additions & 12 deletions src/main/java/com/rarchives/ripme/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,8 @@ private void changeLocale() {
}

private void setupHandlers() {
ripButton.addActionListener(new RipButtonHandler());
ripTextfield.addActionListener(new RipButtonHandler());
ripButton.addActionListener(new RipButtonHandler(this));
ripTextfield.addActionListener(new RipButtonHandler(this));
ripTextfield.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
Expand Down Expand Up @@ -1383,10 +1383,25 @@ private boolean canRip(String urlString) {
}
}

class RipButtonHandler implements ActionListener {

public static JTextField getRipTextfield() {
return ripTextfield;
}

public static DefaultListModel<Object> getQueueListModel() {
return queueListModel;
}
static class RipButtonHandler implements ActionListener {

private MainWindow mainWindow;

public RipButtonHandler(MainWindow mainWindow) {
this.mainWindow = mainWindow;
}
public void actionPerformed(ActionEvent event) {
String url = ripTextfield.getText();
if (!queueListModel.contains(url) && !url.equals("")) {
boolean url_not_empty = !url.equals("");
if (!queueListModel.contains(url) && url_not_empty) {
// Check if we're ripping a range of urls
if (url.contains("{")) {
// Make sure the user hasn't forgotten the closing }
Expand All @@ -1396,22 +1411,25 @@ public void actionPerformed(ActionEvent event) {
int rangeEnd = Integer.parseInt(rangeToParse.split("-")[1]);
for (int i = rangeStart; i < rangeEnd + 1; i++) {
String realURL = url.replaceAll("\\{\\S*\\}", Integer.toString(i));
if (canRip(realURL)) {
queueListModel.add(queueListModel.size(), realURL);
if (mainWindow.canRip(realURL)) {
queueListModel.addElement(realURL);
ripTextfield.setText("");
} else {
displayAndLogError("Can't find ripper for " + realURL, Color.RED);
mainWindow.displayAndLogError("Can't find ripper for " + realURL, Color.RED);
}
}
}
} else {
queueListModel.add(queueListModel.size(), ripTextfield.getText());
queueListModel.addElement(url);
ripTextfield.setText("");
}
} else {
if (!isRipping) {
ripNextAlbum();
}
} else if (url_not_empty) {
mainWindow.displayAndLogError("This URL is already in queue: " + url, Color.RED);
mainWindow.statusWithColor("This URL is already in queue: " + url, Color.ORANGE);
ripTextfield.setText("");
}
else if(!mainWindow.isRipping){
mainWindow.ripNextAlbum();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,18 @@
public class ChanRipperTest extends RippersTest {
@Test
@Tag("flaky")
public void testChanURLPasses() throws IOException, URISyntaxException {
public void testChanURLPasses() throws IOException, URISyntaxException {
List<URL> passURLs = new ArrayList<>();
// URLs that should work
passURLs.add(new URI("http://desuchan.net/v/res/7034.html").toURL());
passURLs.add(new URI("https://boards.4chan.org/hr/thread/3015701").toURL());
passURLs.add(new URI("https://boards.420chan.org/420/res/232066.php").toURL());
// passURLs.add(new URI("https://boards.420chan.org/420/res/232066.php").toURL()); - Dead link
passURLs.add(new URI("http://7chan.org/gif/res/25873.html").toURL());
passURLs.add(new URI("https://rbt.asia/g/thread/70643087/").toURL()); //must work with TLDs with len of 4
for (URL url : passURLs) {
ChanRipper ripper = new ChanRipper(url);
// Use CompletableFuture to run setup() asynchronously
CompletableFuture<Void> setupFuture = CompletableFuture.runAsync(() -> {
try {
ripper.setup();
} catch (IOException | URISyntaxException e) {
throw new RuntimeException(e);
}
});

try {
// Wait for up to 5 seconds for setup() to complete
setupFuture.get(5, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException |
TimeoutException e) {
e.printStackTrace(); // Handle exceptions as needed
}
ripper.setup();
assert (ripper.canRip(url));
Assertions.assertNotNull(ripper.getWorkingDir(), "Ripper for " + url + " did not have a valid working directory.");
deleteDir(ripper.getWorkingDir());
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/com/rarchives/ripme/ui/RipButtonHandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.rarchives.ripme.ui;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.*;

public class RipButtonHandlerTest {


@Test
@Tag("flaky")
public void duplicateUrlTestCase() throws IOException {
// Simulating the MainWindow in our test
MainWindow testMainWindow = new MainWindow();
SwingUtilities.invokeLater(testMainWindow);

MainWindow.RipButtonHandler rbHandler = new MainWindow.RipButtonHandler(testMainWindow);
// Creating a RipButtonHandler instance - Changing fake text to cause github to rebuild 1.

// Add some URL to the model (assuming there's a method for adding URLs)
testMainWindow.getRipTextfield().setText("http://example.com");
rbHandler.actionPerformed(null);
testMainWindow.getRipTextfield().setText("http://example.com");
rbHandler.actionPerformed(null);

// Assuming your MainWindow or RipButtonHandler sets some flag or state
// indicating that a duplicate URL was encountered
assertEquals(testMainWindow.getRipTextfield().getText(), "");
}

}
5 changes: 5 additions & 0 deletions src/test/java/com/rarchives/ripme/ui/UIContextMenuTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,39 @@ void tearDown() {
}

@Test
@Tag("flaky")
void testCut() {
// Simulate a cut event
simulateCutEvent();
// Add assertions if needed
}

@Test
@Tag("flaky")
void testCopy() {
// Simulate a copy event
simulateCopyEvent();
// Add assertions if needed
}

@Test
@Tag("flaky")
void testPaste() {
// Simulate a paste event
simulatePasteEvent();
// Add assertions if needed
}

@Test
@Tag("flaky")
void testSelectAll() {
// Simulate a select all event
simulateSelectAllEvent();
// Add assertions if needed
}

@Test
@Tag("flaky")
void testUndo() {
// Simulate an undo event
simulateUndoEvent();
Expand Down