Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 471fcbae8838937359e0997a770512c7addea01c (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2009, 2015 Alena Laskavaia
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Alena Laskavaia  - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.core.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URL;

/**
 * TODO: add description
 */
@SuppressWarnings("nls")
public class TestUtils {
	public static File saveFile(InputStream st, File testFile) throws FileNotFoundException, IOException {
		BufferedReader r = new BufferedReader(new InputStreamReader(st));
		String line;
		PrintStream wr = new PrintStream(testFile);
		try {
			while ((line = r.readLine()) != null) {
				wr.println(line);
			}
		} finally {
			wr.close();
		}
		return testFile;
	}

	public static String loadFile(InputStream st) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(st));
		String buffer;
		StringBuilder result = new StringBuilder();
		while ((buffer = br.readLine()) != null) {
			result.append(buffer);
		}
		st.close();
		return result.toString();
	}

	/**
	 * @param clazz
	 * @return
	 * @throws IOException
	 */
	public static InputStream getJavaFileText(String srcRoot, Class<?> clazz) throws IOException {
		CodanCoreTestActivator plugin = CodanCoreTestActivator.getDefault();
		String classFile = clazz.getName().replaceAll("\\.", "/");
		classFile += ".java";
		InputStream st = null;
		if (plugin != null) {
			URL resource = plugin.getBundle().getResource(srcRoot + "/" + classFile);
			if (resource == null)
				throw new IOException("Cannot find file " + srcRoot + "/" + classFile + " in bundle " + plugin.getBundle().getBundleId());
			st = resource.openStream();
		} else {
			st = clazz.getResourceAsStream("/" + classFile);
		}
		return st;
	}
}

Back to the top