1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
/*
* Copyright (C) 2008 Steve Ratcliffe
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*
*
* Author: Steve Ratcliffe
* Create date: 10-Jan-2009
*/
package func.lib;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
import uk.me.parabola.splitter.Main;
/**
* Useful routines to use during the functional tests.
*
* @author Steve Ratcliffe
* @author Gerd Petermann
*/
public class TestUtils {
private static final List<String> files = new ArrayList<>();
private static final Deque<Closeable> open = new ArrayDeque<>();
static {
files.add(Args.DEF_AREAS_KML);
files.add(Args.DEF_AREAS_LIST);
files.add(Args.DEF_AREAS_POLY);
files.add(Args.DEF_PROBLEM_LIST);
files.add(Args.DEF_DENSITIES);
files.add(Args.DEF_TEMPLATE);
Runnable r = new Runnable() {
public void run() {
deleteOutputFiles();
}
};
Thread t = new Thread(r);
Runtime.getRuntime().addShutdownHook(t);
}
/**
* Delete output files that were created by the tests.
* Used to clean up before/after a test.
*/
public static void deleteOutputFiles() {
for (String fname : files) {
File f = new File(fname);
if (f.exists())
assertTrue("delete existing file: " + f.getName(), f.delete());
}
}
public static void closeFiles() {
while (!open.isEmpty()) {
try {
open.remove().close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void registerFile(String ... names) {
Collections.addAll(files, names);
}
public static void registerFile(Closeable... fileList) {
Collections.addAll(open, fileList);
}
/**
* Run with a single argument. The standard arguments are added first.
* @param arg The argument.
*/
public static Outputs run(String arg) {
return run(new String[] {arg});
}
/**
* Run with the given args. Some standard arguments are added first.
*
* To run without the standard args, use runRaw().
* @param in The arguments to use.
*/
public static Outputs run(String ... in) {
List<String> args = new ArrayList<>(Arrays.asList(in));
OutputStream outsink = new ByteArrayOutputStream();
PrintStream out = new PrintStream(outsink);
OutputStream errsink = new ByteArrayOutputStream();
PrintStream err = new PrintStream(errsink);
PrintStream origout = System.out;
PrintStream origerr = System.err;
try {
System.setOut(out);
System.setErr(err);
Main.mainNoSystemExit(args.toArray(new String[args.size()]));
} finally {
out.close();
err.close();
System.setOut(origout);
System.setErr(origerr);
}
return new Outputs(outsink.toString(), errsink.toString());
}
}
|