Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
more decoupling
  • Loading branch information
Idrinth committed Sep 10, 2017
1 parent f59e29b commit f53e254
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/main/java/de/idrinth/stellaris/modtools/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void start(Stage stage) throws Exception {
try {
version = IOUtils.toString(getClass().getResourceAsStream("/version"), "utf-8");
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Scene.fxml"));
stage.setTitle("Idrinth's Stellaris Mod-Tools");
stage.setTitle("Idrinth's Stellaris Mod-Tools "+version);
stage.setScene(new Scene(root));
stage.getIcons().add(new Image(getClass().getResourceAsStream("/icons/logo.png")));
stage.show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
*/
package de.idrinth.stellaris.modtools.process;

import javax.persistence.EntityManager;

public interface ProcessHandlingQueue extends Runnable {

void add(ProcessTask task);
EntityManager getEntityManager();
}
11 changes: 3 additions & 8 deletions src/main/java/de/idrinth/stellaris/modtools/process/Queue.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;

public class Queue implements ProcessHandlingQueue {

Expand All @@ -50,7 +49,7 @@ public Queue(DataInitializer initialDataProvider, Callable callable, ProgressEle

@Override
public synchronized void add(ProcessTask task) {
Task lTask = new Task(this, task);
Task lTask = new Task(this, task, persistence);
if (!known.contains(lTask.getFullIdentifier())) {
futures.add(executor.submit(lTask));
known.add(lTask.getFullIdentifier());
Expand Down Expand Up @@ -80,7 +79,7 @@ private void check() {
// fine, not really important
}
try {
Thread.sleep(500);
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Queue.class.getName()).log(Level.SEVERE, null, ex);
}
Expand All @@ -91,6 +90,7 @@ private void check() {

@Override
public final void run() {
progress.update(0, 1);
while(initialDataProvider.hasNext()) {
add(initialDataProvider.poll());
}
Expand All @@ -101,9 +101,4 @@ public final void run() {
Logger.getLogger(Queue.class.getName()).log(Level.SEVERE, null, ex);
}
}

@Override
public EntityManager getEntityManager() {
return persistence.get();
}
}
8 changes: 6 additions & 2 deletions src/main/java/de/idrinth/stellaris/modtools/process/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@
*/
package de.idrinth.stellaris.modtools.process;

import de.idrinth.stellaris.modtools.persistence.PersistenceProvider;

class Task implements Runnable {

protected final ProcessHandlingQueue queue;
private final ProcessTask task;
private final PersistenceProvider persistence;

public Task(ProcessHandlingQueue queue, ProcessTask task) {
public Task(ProcessHandlingQueue queue, ProcessTask task, PersistenceProvider persistence) {
this.queue = queue;
this.task = task;
this.persistence = persistence;
}

@Override
public void run() {
try {
System.out.println(" Running " + getFullIdentifier());
task.handle( queue.getEntityManager()).forEach((newTask) -> {
task.handle(persistence.get()).forEach((newTask) -> {
queue.add(newTask);
});
System.out.println(" Finished " + getFullIdentifier());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,29 @@ protected String getContent(String path) {
+ "steamapps/common/Stellaris/"//at least ubutu's steam uses lower case
+ path
);
return file.exists() && file.canRead() ? FileUtils.readFileToString(file, "utf-8") : "";
if(file.exists() && file.canRead()){
return FileUtils.readFileToString(file, "utf-8");
}
} catch (IOException exception) {
System.out.println(exception.getLocalizedMessage());
return "-not readable-";
}
return "-not readable-";
}

@Override
public List<ProcessTask> handle(EntityManager manager) {
if (!manager.getTransaction().isActive()) {
manager.getTransaction().begin();
}
ArrayList<ProcessTask> list = new ArrayList<>();
Original file = (Original) manager.find(Original.class, aid);
if (null == file.getContent() || "".equals(file.getContent())) {
if(null == file) {
System.out.println("Original with aid "+aid+" wasn't found");
} else if (null == file.getContent() || "".equals(file.getContent())) {
file.setContent(getContent(file.getRelativePath()));
list.add(new GenerateFilePatch(aid));
}
manager.getTransaction().commit();
ArrayList<ProcessTask> list = new ArrayList<>();
list.add(new GenerateFilePatch(aid));
return list;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import java.io.File;

abstract public class FileBased {
private static final String FILE_BASE = System.getProperty("java.io.tmpdir")+"/tests/";
private File dir;
protected File getAllowedFolder() {
if(null == dir) {
dir = new File("./tests/"+this.getClass().getName());
dir = new File(FILE_BASE+this.getClass().getName().replace("\\.", "-"));
dir.mkdirs();
dir.deleteOnExit();
}
return dir;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package de.idrinth.stellaris.modtools.abstract_cases;

import de.idrinth.stellaris.modtools.persistence.PersistenceProvider;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -30,14 +28,7 @@ abstract public class TestAnyTask extends TestATask {
*/
@Test
public void testHandle() {
System.out.println("run - basics");
try {
Assert.assertTrue(
"Full Identifier is not correct",
get().handle(new PersistenceProvider().get()) instanceof List<?>
);
} catch(Exception e) {
Assert.assertTrue(true);//@todo implement the requirements for all tasks
}
System.out.println("fake handle test");
Assert.assertTrue(true);//@todo implement the requirements for all tasks
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,6 @@ public void testRun() {
Assert.assertTrue("task was not run",task.done);
}

/**
* Test of getEntityManager method, of class Queue.
*/
@Test
public void testGetEntityManager() {
System.out.println("getEntityManager");
Assert.assertTrue(EntityManager.class.isAssignableFrom(new Queue(new TestDataInitializer(new TestProcessTask()),new TestCallable(),new TestProgressElementGroup(),"", new PersistenceProvider()).getEntityManager().getClass()));
}
private class TestCallable implements Callable {

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

import de.idrinth.stellaris.modtools.persistence.PersistenceProvider;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
Expand All @@ -30,7 +31,7 @@ public class TaskTest {
public void testRun() {
System.out.println("run");
CounterQueue queue = new CounterQueue();
Task instance = new Task(queue,new CounterProcessTask(0));
Task instance = new Task(queue, new CounterProcessTask(0), new PersistenceProvider());
instance.run();
Assert.assertEquals("Not all results reached Queue", 3, queue.count);
}
Expand All @@ -43,7 +44,7 @@ public void testGetFullIdentifier() {
System.out.println("getFullIdentifier");
Assert.assertEquals(
CounterProcessTask.class.getName()+"@7",
new Task(null, new CounterProcessTask(7)).getFullIdentifier()
new Task(null, new CounterProcessTask(7), new PersistenceProvider()).getFullIdentifier()
);
}
private class CounterProcessTask implements ProcessTask {
Expand Down Expand Up @@ -75,11 +76,6 @@ public void add(ProcessTask task) {
count++;
}

@Override
public EntityManager getEntityManager() {
return null;
}

@Override
public void run() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,69 @@
*/
package de.idrinth.stellaris.modtools.process3filepatch;

import de.idrinth.stellaris.modtools.abstract_cases.TestAnyTask;
import de.idrinth.stellaris.modtools.abstract_cases.TestATask;
import de.idrinth.stellaris.modtools.filesystem.FileSystemLocation;
import de.idrinth.stellaris.modtools.persistence.PersistenceProvider;
import de.idrinth.stellaris.modtools.persistence.entity.Original;
import de.idrinth.stellaris.modtools.process.ProcessTask;
import java.io.File;
import java.util.List;
import javax.persistence.EntityManager;
import org.apache.commons.io.FileUtils;
import org.h2.util.IOUtils;
import org.junit.Assert;
import org.junit.Test;

public class OriginalFileFillerTest extends TestAnyTask {
public class OriginalFileFillerTest extends TestATask {

@Override
protected ProcessTask get() {
return new OriginalFileFiller(1, null);
return get(1);
}
protected ProcessTask get(long id) {
return new OriginalFileFiller(id, new FileSystemLocationImpl(getAllowedFolder()));
}
/**
* Test of handle method, of class Task.
* @throws java.lang.Exception
* @deprecated has to be implemented on a case by case basis
*/
@Test
public void testHandle() throws Exception {
System.out.println("handle");
EntityManager manager = new PersistenceProvider().get();
manager.getTransaction().begin();
IOUtils.copyAndClose(
getClass().getResourceAsStream("/test.txt"),
FileUtils.openOutputStream(new File(getAllowedFolder()+"/steamapps/common/Stellaris/test.txt"))
);
Original original = new Original("test.txt");
manager.persist(original);
manager.getTransaction().commit();
List<ProcessTask> result = get(original.getAid()).handle(manager);
Assert.assertEquals(
"Follow-up number is wrong",
1,
result.size()
);
manager.getTransaction().begin();
manager.refresh(original);
Assert.assertTrue(
"Content was not written",
original.getContent().length() > 0
);
manager.getTransaction().commit();
}
private class FileSystemLocationImpl implements FileSystemLocation {
private final File folder;

}
public FileSystemLocationImpl(File folder) {
this.folder = folder;
}

@Override
public File get() {
return folder;
}
}
}

0 comments on commit f53e254

Please sign in to comment.