Skip to content

Commit

Permalink
Adding tests for process, refs #7
Browse files Browse the repository at this point in the history
removing a case of implementation instead of interface being passed around
  • Loading branch information
Idrinth committed Sep 4, 2017
1 parent 1c47175 commit ac9f858
Show file tree
Hide file tree
Showing 14 changed files with 316 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
package de.idrinth.stellaris.modtools.gui;

import de.idrinth.stellaris.modtools.process.FillerThread;
import de.idrinth.stellaris.modtools.gui.ClickableTableView;
import de.idrinth.stellaris.modtools.gui.CollisionTableView;
import de.idrinth.stellaris.modtools.gui.FileDataRow;
import de.idrinth.stellaris.modtools.gui.ModDataRow;
import de.idrinth.stellaris.modtools.gui.ModTableView;
import de.idrinth.stellaris.modtools.gui.Progress;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
Expand Down Expand Up @@ -62,7 +56,7 @@ public class FXMLController implements Initializable {
private Button button;

@FXML
private Progress test;
private ProgressElementGroup test;

private Stage popup;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import javafx.scene.control.Tooltip;
import javafx.scene.layout.AnchorPane;

public class Progress extends AnchorPane {
public class Progress extends AnchorPane implements ProgressElementGroup {

private final ArrayList<ProgressSet> sets = new ArrayList<>();
private int setNum = 0;
Expand All @@ -39,10 +39,12 @@ public Progress(ArrayList<String> stepLabels) {
this.setVisible(true);
}

@Override
public void addToStepLabels(String text) {
stepLabels.add(text);
}

@Override
public void update(int current, int maximum) {
Platform.runLater(() -> {
if (sets.size() <= setNum || null == sets.get(setNum)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2017 Björn Büttner
*
* 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 <http://www.gnu.org/licenses/>.
*/
package de.idrinth.stellaris.modtools.gui;

public interface ProgressElementGroup {

void addToStepLabels(String text);

void update(int current, int maximum);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package de.idrinth.stellaris.modtools.process;

import de.idrinth.stellaris.modtools.gui.Progress;
import de.idrinth.stellaris.modtools.gui.ProgressElementGroup;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.List;
Expand All @@ -33,16 +33,16 @@ abstract public class AbstractQueue implements Runnable {
private final List<Future<?>> futures = new ArrayList<>();
private final List<String> known = new ArrayList<>();
private final Callable callable;
private final Progress progress;
private final ProgressElementGroup progress;

public AbstractQueue(Callable callable, Progress progress, String label, ExecutorService executor) {
public AbstractQueue(Callable callable, ProgressElementGroup progress, String label, ExecutorService executor) {
this.callable = callable;
this.progress = progress;
this.progress.addToStepLabels(label);
this.executor = executor;
}

public AbstractQueue(Callable callable, Progress progress, String label) {
public AbstractQueue(Callable callable, ProgressElementGroup progress, String label) {
this(callable, progress, label, Executors.newFixedThreadPool(20));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package de.idrinth.stellaris.modtools.process;

import de.idrinth.stellaris.modtools.gui.ClickableTableView;
import de.idrinth.stellaris.modtools.gui.Progress;
import de.idrinth.stellaris.modtools.gui.ProgressElementGroup;
import de.idrinth.stellaris.modtools.service.PersistenceProvider;
import java.util.ArrayList;
import java.util.LinkedList;
Expand All @@ -29,7 +29,7 @@ public class FillerThread implements Runnable, Callable {
private final ArrayList<ClickableTableView> list;
private final LinkedList<AbstractQueue> tasks = new LinkedList<>();

public FillerThread(ArrayList<ClickableTableView> list, Progress progress) {
public FillerThread(ArrayList<ClickableTableView> list, ProgressElementGroup progress) {
this.list = list;
tasks.add(new de.idrinth.stellaris.modtools.process1datacollection.Queue(this, progress));
tasks.add(new de.idrinth.stellaris.modtools.process2prepatchcleaning.Queue(this, progress));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import de.idrinth.stellaris.modtools.process.FillerThread;
import de.idrinth.stellaris.modtools.service.DirectoryLookup;
import de.idrinth.stellaris.modtools.gui.Progress;
import de.idrinth.stellaris.modtools.gui.ProgressElementGroup;
import de.idrinth.stellaris.modtools.process.AbstractQueue;
import java.io.File;
import java.io.IOException;
Expand All @@ -28,7 +28,7 @@

public class Queue extends AbstractQueue {

public Queue(FillerThread c, Progress progress) {
public Queue(FillerThread c, ProgressElementGroup progress) {
super(c, progress, "Collecting data", Executors.newFixedThreadPool(20));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
package de.idrinth.stellaris.modtools.process2prepatchcleaning;

import de.idrinth.stellaris.modtools.entity.Original;
import de.idrinth.stellaris.modtools.gui.Progress;
import de.idrinth.stellaris.modtools.gui.ProgressElementGroup;
import de.idrinth.stellaris.modtools.process.AbstractQueue;
import de.idrinth.stellaris.modtools.process.FillerThread;
import de.idrinth.stellaris.modtools.service.PersistenceProvider;

public class Queue extends AbstractQueue {

public Queue(FillerThread c, Progress progress) {
public Queue(FillerThread c, ProgressElementGroup progress) {
super(c, progress, "Removing manually patched");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
package de.idrinth.stellaris.modtools.process3filepatch;

import de.idrinth.stellaris.modtools.entity.Patch;
import de.idrinth.stellaris.modtools.gui.Progress;
import de.idrinth.stellaris.modtools.gui.ProgressElementGroup;
import de.idrinth.stellaris.modtools.process.AbstractQueue;
import de.idrinth.stellaris.modtools.service.PersistenceProvider;
import java.util.concurrent.Callable;

public class Queue extends AbstractQueue {

public Queue(Callable callable, Progress progress) {
public Queue(Callable callable, ProgressElementGroup progress) {
super(callable, progress, "Creating patches");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
package de.idrinth.stellaris.modtools.process4applypatch;

import de.idrinth.stellaris.modtools.entity.Original;
import de.idrinth.stellaris.modtools.gui.Progress;
import de.idrinth.stellaris.modtools.gui.ProgressElementGroup;
import de.idrinth.stellaris.modtools.process.AbstractQueue;
import de.idrinth.stellaris.modtools.service.PersistenceProvider;
import java.util.concurrent.Callable;

public class Queue extends AbstractQueue {

public Queue(Callable callable, Progress progress) {
public Queue(Callable callable, ProgressElementGroup progress) {
super(callable, progress, "Merging patches");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
*/
package de.idrinth.stellaris.modtools.process5modcreation;

import de.idrinth.stellaris.modtools.gui.Progress;
import de.idrinth.stellaris.modtools.gui.ProgressElementGroup;
import de.idrinth.stellaris.modtools.process.AbstractQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

public class Queue extends AbstractQueue {

public Queue(Callable callable, Progress progress) {
public Queue(Callable callable, ProgressElementGroup progress) {
super(callable, progress, "Building Mod", Executors.newSingleThreadExecutor());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.github.sarxos.winreg.WindowsRegistry;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.commons.lang3.SystemUtils;

public class DirectoryLookup {
Expand Down Expand Up @@ -48,15 +49,12 @@ public static File getSteamDir() throws RegistryException, IOException {
} else if(SystemUtils.IS_OS_MAC) {
steamDir = test(new File("~/Library/Application Support/Steam"));
} else if(SystemUtils.IS_OS_LINUX) {
try {
steamDir = test(new File("~/.steam/steam"));
} catch(IOException exio1) {
try {
steamDir = test(new File("~/.local/share/Steam"));
} catch(IOException exio2) {
throw new IOException(exio1.getMessage()+exio2.getMessage());
}
}
ArrayList<File> fl = new ArrayList<>();
fl.add(new File("~/.steam/steam"));
fl.add(new File("~/.steam/Steam"));
fl.add(new File("~/.local/share/steam"));
fl.add(new File("~/.local/share/Steam"));
steamDir = test(fl);
}
}
return steamDir;
Expand All @@ -68,4 +66,16 @@ private static File test(File file) throws IOException {
}
return file;
}

private static File test(ArrayList<File> files) throws IOException {
StringBuilder sb = new StringBuilder();
for(File file:files) {
try {
return test(file);
} catch(IOException e) {
sb.append(e.getMessage());
}
}
throw new IOException(sb.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (C) 2017 Björn Büttner
*
* 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 <http://www.gnu.org/licenses/>.
*/
package de.idrinth.stellaris.modtools.process;

import de.idrinth.stellaris.modtools.gui.ProgressElementGroup;
import java.util.concurrent.Callable;
import junit.framework.Assert;
import org.junit.Test;

public class AbstractQueueTest {

public AbstractQueueTest() {
}

/**
* Test of add method, of class AbstractQueue.
*/
@Test
public void testAdd() {
System.out.println("add");
AbstractQueueImpl queue = new AbstractQueueImpl();
queue.addList();
queue.add(new TestRunnable(queue));
queue.add(new TestRunnable(queue,"ääää"));
queue.run();
Assert.assertEquals(2, queue.increment);
}

/**
* Test of run method, of class AbstractQueue.
*/
@Test
public void testRun() {
System.out.println("run");
AbstractQueueImpl queue = new AbstractQueueImpl();
queue.addList();
queue.run();
Assert.assertEquals(1, queue.increment);
}

private class AbstractQueueImpl extends AbstractQueue {
public volatile int increment=0;
public AbstractQueueImpl() {
super(new TestCallable(), new TestProgressElementGroup(), "");
}

@Override
public void addList() {
add(new TestRunnable(this));
}
}
private class TestProgressElementGroup implements ProgressElementGroup {

@Override
public void addToStepLabels(String text) {
//done
}

@Override
public void update(int current, int maximum) {
//done
}

}
private class TestCallable implements Callable {
@Override
public Object call() {
return null;//nothing to do;
}
}
private class TestRunnable extends Task {
private final String identifier;
public TestRunnable(AbstractQueueImpl queue, String identifier) {
super(queue);
this.identifier = identifier;
}
public TestRunnable(AbstractQueueImpl queue) {
this(queue, "-");
}
@Override
public void run() {
fill();
}
@Override
protected void fill() {
((AbstractQueueImpl) queue).increment++;
}

@Override
protected String getIdentifier() {
return identifier;
}
}
}
Loading

0 comments on commit ac9f858

Please sign in to comment.