Skip to main content
summaryrefslogtreecommitdiffstats
blob: 402fa38831c6b2c19f74505832c875e5499d6781 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*******************************************************************************
 * Copyright (c) 2005, 2007 committers of openArchitectureWare and others.
 * 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:
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/
package org.eclipse.xtend.expression;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringWriter;

import junit.framework.TestCase;

public class ResourceManagerDefaultImplTest extends TestCase {
	private static final String LINE_WRAP = System
			.getProperty("line.separator");
	private final static String CONTENT = tag("IMPORT ecore") + LINE_WRAP
			+ tag("AROUND Root FOR EPackage") + LINE_WRAP + tag("ENDAROUND");

	class TestResourcManager extends ResourceManagerDefaultImpl {
		public Reader _createReader(InputStream in) {
			return super.createReader(in);
		}
	};

	private TestResourcManager resMgr = new TestResourcManager();

	public void testCreateReader_UTF() throws Exception {
		checkResourceLoading("UTF8", "templatefile-utf8.xpt");
	}

	public void testCreateReader_ISO() throws Exception {
		checkResourceLoading("ISO-8859-1", "templatefile-iso-8859-1.xpt");
	}

	public void testCreateReader_UTF8_autodetect() throws Exception {
		checkResourceLoading(null, "templatefile-utf8.xpt");
	}

	public void testCreateReader_ISO_autodetect() throws Exception {
		checkResourceLoading(null, "templatefile-iso-8859-1.xpt");
	}

	public void testCreateReader_MacRoman_autodetect() throws Exception {
		checkResourceLoading(null, "templatefile-macroman.xpt");
	}

	private void checkResourceLoading(String encoding, String testfile)
			throws IOException {
		resMgr.setFileEncoding(encoding);
		InputStream is = getClass().getResourceAsStream(testfile);
		assertNotNull(is);
		Reader reader = resMgr._createReader(is);
		String read = read(reader);
		assertNotNull(read);
		assertEquals(CONTENT, read);

	}

	private String read(Reader r) throws IOException {
		StringWriter s = new StringWriter();
		char[] buf = new char[1024];
		int len;
		len = r.read(buf, 0, buf.length);
		while (len != -1) {
			s.write(buf, 0, len);
			len = r.read(buf, 0, buf.length);
		}
		return s.getBuffer().toString();
	}

	/**
	 * Returns a string encapsuled within Xpand guillemot brackets
	 * 
	 * @param s
	 *            Some string
	 * @return String with brackets - <code<b>«s»</b></code>
	 */
	protected static String tag(final String s) {
		return '\u00ab' + s + '\u00bb';
	}

}

Back to the top