Debian Codegraph @

test/func/lib/TestUtils.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2008 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Steve Ratcliffe
15 * Create date: 10-Jan-2009
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 * Useful routines to use during the functional tests.
48 *
49 * @author Steve Ratcliffe
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 * Delete output files that were created by the tests.
72 * Used to clean up before/after a test.
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 * Run with a single argument. The standard arguments are added first.
103 * @param arg The argument.
104 */
105 public static Outputs run(String arg) {
106 return run(new String[] {arg});
107 }
108
109 /**
110 * Run with the given args. Some standard arguments are added first.
111 *
112 * To run without the standard args, use runRaw().
113 * @param in The arguments to use.
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 * Run with the given args as a new process. Some standard arguments are added first.
144 *
145 * @param in The arguments to use.
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 // do nothing
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 * Create a rule set out of a string. The string is processed
192 * as if it were in a file and the levels spec had been set.
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 * Make a rule set from the "lines" file of the given StyleFileLoader.
204 *
205 * @param loader This will be used to load the file 'lines'. If that file includes any other file, then it
206 * should accessible from the loader too.
207 *
208 * @return A rule set for lines.
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 }