| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package func.lib; |
| 18 |
|
| 19 |
import java.io.BufferedReader; |
| 20 |
import java.io.ByteArrayOutputStream; |
| 21 |
import java.io.Closeable; |
| 22 |
import java.io.File; |
| 23 |
import java.io.FileNotFoundException; |
| 24 |
import java.io.IOException; |
| 25 |
import java.io.InputStreamReader; |
| 26 |
import java.io.OutputStream; |
| 27 |
import java.io.PrintStream; |
| 28 |
import java.util.ArrayDeque; |
| 29 |
import java.util.ArrayList; |
| 30 |
import java.util.Arrays; |
| 31 |
import java.util.Collections; |
| 32 |
import java.util.Deque; |
| 33 |
import java.util.List; |
| 34 |
import java.util.concurrent.TimeUnit; |
| 35 |
|
| 36 |
import uk.me.parabola.imgfmt.Utils; |
| 37 |
import uk.me.parabola.mkgmap.general.LevelInfo; |
| 38 |
import uk.me.parabola.mkgmap.main.Main; |
| 39 |
import uk.me.parabola.mkgmap.osmstyle.RuleFileReader; |
| 40 |
import uk.me.parabola.mkgmap.osmstyle.RuleSet; |
| 41 |
import uk.me.parabola.mkgmap.osmstyle.StyleFileLoader; |
| 42 |
import uk.me.parabola.mkgmap.reader.osm.FeatureKind; |
| 43 |
|
| 44 |
import static org.junit.Assert.*; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class TestUtils { |
| 52 |
private static final List<String> files = new ArrayList<>(); |
| 53 |
private static final Deque<Closeable> open = new ArrayDeque<>(); |
| 54 |
|
| 55 |
static { |
| 56 |
files.add(Args.DEF_MAP_FILENAME); |
| 57 |
files.add(Args.DEF_MAP_FILENAME2); |
| 58 |
files.add(Args.DEF_GMAPSUPP_FILENAME); |
| 59 |
files.add(Args.DEF_TDB_FILENAME); |
| 60 |
|
| 61 |
Runnable r = new Runnable() { |
| 62 |
public void run() { |
| 63 |
deleteOutputFiles(); |
| 64 |
} |
| 65 |
}; |
| 66 |
Thread t = new Thread(r); |
| 67 |
Runtime.getRuntime().addShutdownHook(t); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public static void deleteOutputFiles() { |
| 75 |
List<String> failList = new ArrayList<>(); |
| 76 |
for (String fname : files) { |
| 77 |
File f = new File(fname); |
| 78 |
|
| 79 |
if (f.exists()) { |
| 80 |
if (!f.delete()) |
| 81 |
failList.add(f.getName()); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
assertEquals("Files not deleted", Collections.emptyList(), failList); |
| 86 |
} |
| 87 |
|
| 88 |
public static void closeFiles() { |
| 89 |
while (!open.isEmpty()) |
| 90 |
Utils.closeFile(open.remove()); |
| 91 |
} |
| 92 |
|
| 93 |
public static void registerFile(String ... names) { |
| 94 |
Collections.addAll(files, names); |
| 95 |
} |
| 96 |
|
| 97 |
public static void registerFile(Closeable... files) { |
| 98 |
Collections.addAll(open, files); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public static Outputs run(String arg) { |
| 106 |
return run(new String[] {arg}); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public static Outputs run(String ... in) { |
| 116 |
List<String> args = new ArrayList<>(Arrays.asList(in)); |
| 117 |
args.add(0, Args.TEST_STYLE_ARG); |
| 118 |
|
| 119 |
OutputStream outsink = new ByteArrayOutputStream(); |
| 120 |
PrintStream out = new PrintStream(outsink); |
| 121 |
|
| 122 |
OutputStream errsink = new ByteArrayOutputStream(); |
| 123 |
PrintStream err = new PrintStream(errsink); |
| 124 |
|
| 125 |
PrintStream origout = System.out; |
| 126 |
PrintStream origerr = System.err; |
| 127 |
|
| 128 |
try { |
| 129 |
System.setOut(out); |
| 130 |
System.setErr(err); |
| 131 |
Main.mainNoSystemExit(args.toArray(new String[args.size()])); |
| 132 |
} finally { |
| 133 |
out.close(); |
| 134 |
err.close(); |
| 135 |
System.setOut(origout); |
| 136 |
System.setErr(origerr); |
| 137 |
} |
| 138 |
|
| 139 |
return new Outputs(outsink.toString(), errsink.toString()); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public static Outputs runAsProcess(String ... in) { |
| 148 |
List<String> args = new ArrayList<>(Arrays.asList( |
| 149 |
"java", |
| 150 |
"-classpath", |
| 151 |
"build/classes" + File.pathSeparator + "lib/compile/*", |
| 152 |
"uk.me.parabola.mkgmap.main.Main", |
| 153 |
Args.TEST_STYLE_ARG |
| 154 |
)); |
| 155 |
args.addAll(Arrays.asList(in)); |
| 156 |
ProcessBuilder pb = new ProcessBuilder(args); |
| 157 |
StringBuilder outBuilder = new StringBuilder(); |
| 158 |
StringBuilder errBuilder = new StringBuilder(); |
| 159 |
try { |
| 160 |
Process process = pb.start(); |
| 161 |
try (BufferedReader errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream())); |
| 162 |
BufferedReader outputStream = new BufferedReader(new InputStreamReader(process.getInputStream()))) { |
| 163 |
while (true) { |
| 164 |
getStreamOutput(outputStream, outBuilder); |
| 165 |
getStreamOutput(errorStream, errBuilder); |
| 166 |
if (process.waitFor(1, TimeUnit.MILLISECONDS)) { |
| 167 |
getStreamOutput(outputStream, outBuilder); |
| 168 |
getStreamOutput(errorStream, errBuilder); |
| 169 |
break; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} catch (IOException e) { |
| 174 |
|
| 175 |
} catch (InterruptedException e) { |
| 176 |
Thread.currentThread().interrupt(); |
| 177 |
} |
| 178 |
return new Outputs(outBuilder.toString(), errBuilder.toString()); |
| 179 |
} |
| 180 |
|
| 181 |
private static void getStreamOutput(BufferedReader stream, StringBuilder stringBuilder) throws IOException { |
| 182 |
char[] buff = new char[100000]; |
| 183 |
while (stream.ready()) { |
| 184 |
int count = stream.read(buff); |
| 185 |
if (count > 0) |
| 186 |
stringBuilder.append(buff, 0, count); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public static RuleSet makeRuleSet(String in) { |
| 195 |
StringStyleFileLoader loader = new StringStyleFileLoader(new String[][] { |
| 196 |
{"lines", in} |
| 197 |
}); |
| 198 |
|
| 199 |
return makeRuleSet(loader); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public static RuleSet makeRuleSet(StyleFileLoader loader) { |
| 211 |
RuleSet rs = new RuleSet(); |
| 212 |
RuleFileReader rr = new RuleFileReader(FeatureKind.POLYLINE, LevelInfo.createFromString("0:24 1:20 2:18 3:16 4:14"), |
| 213 |
rs, false, null); |
| 214 |
try { |
| 215 |
rr.load(loader, "lines"); |
| 216 |
} catch (FileNotFoundException e) { |
| 217 |
throw new AssertionError("Failed to open file: lines"); |
| 218 |
} |
| 219 |
return rs; |
| 220 |
} |
| 221 |
|
| 222 |
} |