summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2006-03-06 11:10:46 (EST)
committerpelder2006-03-06 11:10:46 (EST)
commitbdd7661641a9ae84df16c5af1ec3410d9db3ea2e (patch)
tree5f50913b6af29aa2c148d75a6a2c94ee737b7069
parentea499c4f9a7265ef3d8d3e0f8d8423e4fb52c2e9 (diff)
downloadorg.eclipse.jet-bdd7661641a9ae84df16c5af1ec3410d9db3ea2e.zip
org.eclipse.jet-bdd7661641a9ae84df16c5af1ec3410d9db3ea2e.tar.gz
org.eclipse.jet-bdd7661641a9ae84df16c5af1ec3410d9db3ea2e.tar.bz2
[130552] Convert line separators when reading expected results from JUnit project.
-rw-r--r--tests/org.eclipse.jet.tests/META-INF/MANIFEST.MF4
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/FileUtils.java93
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TestJET2Parser.java25
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TestJETParser.java24
4 files changed, 103 insertions, 43 deletions
diff --git a/tests/org.eclipse.jet.tests/META-INF/MANIFEST.MF b/tests/org.eclipse.jet.tests/META-INF/MANIFEST.MF
index bd6898b..c8818c4 100644
--- a/tests/org.eclipse.jet.tests/META-INF/MANIFEST.MF
+++ b/tests/org.eclipse.jet.tests/META-INF/MANIFEST.MF
@@ -11,4 +11,6 @@ Require-Bundle: org.eclipse.jet,
org.eclipse.core.runtime,
org.eclipse.emf.ecore,
org.eclipse.emf.ecore.xmi,
- org.eclipse.xsd
+ org.eclipse.xsd,
+ org.eclipse.core.filebuffers,
+ org.eclipse.core.resources
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/FileUtils.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/FileUtils.java
new file mode 100644
index 0000000..69af4fb
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/FileUtils.java
@@ -0,0 +1,93 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2006 IBM Corporation 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:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id$
+ */
+
+package org.eclipse.jet.tests;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import org.eclipse.core.filebuffers.FileBuffers;
+import org.eclipse.core.filebuffers.ITextFileBuffer;
+import org.eclipse.core.filebuffers.ITextFileBufferManager;
+import org.eclipse.core.filebuffers.manipulation.ConvertLineDelimitersOperation;
+import org.eclipse.core.filebuffers.manipulation.FileBufferOperationRunner;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Platform;
+
+/**
+ * File utilities for Unit tests getting expected results from the JUnit project.
+ *
+ */
+public class FileUtils {
+
+ /**
+ *
+ *
+ */
+ private FileUtils() {
+ // prevent instantiation.
+ }
+
+ /**
+ * Resolve a URL to a location string (file system absolute path).
+ * @param urlString a URL string, possibly using the platform:/ format.
+ * @return an absolute file system path corresponding to <code>urlString</code>
+ * @throws IOException if an IO error occurs
+ * @throws MalformedURLException if urlString is an invalid URL
+ */
+ public static String resolveURLToLocationString(String urlString) throws IOException, MalformedURLException {
+ String fileName = Platform.resolve(new URL(urlString)).getFile();
+ return fileName;
+ }
+
+ /**
+ * Return the file contents as a string. This method also converts the file's line
+ * separator to the current system's line separator if <code>convertNewLines</code> is <code>true</code>.
+ * @param urlString a URL string, possibly using the platform:/ format.
+ * @param convertNewLines Convert the file line separators to the system line separator.
+ * @return the file contents
+ * @throws IOException if an IO error occurs
+ * @throws MalformedURLException if urlString is an invalid URL
+ * @throws CoreException if an error occurs reading the file.
+ */
+ public static String getFileContents(String urlString, boolean convertNewLines) throws MalformedURLException, IOException, CoreException {
+
+ final ITextFileBufferManager textBufferMgr = FileBuffers.getTextFileBufferManager();
+ String fileName = resolveURLToLocationString(urlString);
+
+ final IPath path = new Path(fileName);
+ textBufferMgr.connect(path, new NullProgressMonitor());
+ final ITextFileBuffer textFileBuffer = textBufferMgr.getTextFileBuffer(path);
+
+ if(convertNewLines) {
+ new FileBufferOperationRunner(textBufferMgr, null)
+ .execute(
+ new IPath[] { path },
+ new ConvertLineDelimitersOperation(System.getProperty("line.separator")),
+ new NullProgressMonitor());
+ }
+
+ final String result = textFileBuffer.getDocument().get();
+ textBufferMgr.disconnect(path, new NullProgressMonitor());
+ return result;
+ }
+
+}
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TestJET2Parser.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TestJET2Parser.java
index 200bfde..a18ced4 100644
--- a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TestJET2Parser.java
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TestJET2Parser.java
@@ -6,17 +6,11 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
-import java.net.URL;
-import java.nio.CharBuffer;
-import java.nio.MappedByteBuffer;
-import java.nio.channels.FileChannel;
-import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Map;
import junit.framework.TestCase;
-import org.eclipse.core.runtime.Platform;
import org.eclipse.jet.compiler.Comment;
import org.eclipse.jet.compiler.JET2ASTVisitor;
import org.eclipse.jet.compiler.JET2CompilationUnit;
@@ -29,6 +23,7 @@ import org.eclipse.jet.compiler.TextElement;
import org.eclipse.jet.compiler.XMLBodyElement;
import org.eclipse.jet.compiler.XMLEmptyElement;
import org.eclipse.jet.internal.compiler.templates.JET2JavaGenerator;
+import org.eclipse.jet.tests.FileUtils;
public class TestJET2Parser extends TestCase {
@@ -188,20 +183,8 @@ public class TestJET2Parser extends TestCase {
}
- private String getFileContents(String urlString) throws Exception {
- FileInputStream inputStream = getFileContentsAsStream(urlString);
- FileChannel channel = inputStream.getChannel();
- MappedByteBuffer mapBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
- Charset utf8Decoder = Charset.forName("UTF-8");
- CharBuffer charBuffer = utf8Decoder.decode(mapBuffer);
- String contents = charBuffer.toString();
- channel.close();
- inputStream.close();
- return contents;
- }
-
private FileInputStream getFileContentsAsStream(String urlString) throws IOException, MalformedURLException, FileNotFoundException {
- String fileName = Platform.resolve(new URL(urlString)).getFile();
+ String fileName = FileUtils.resolveURLToLocationString(urlString);
FileInputStream inputStream = new FileInputStream(new File(fileName));
return inputStream;
}
@@ -220,7 +203,7 @@ public class TestJET2Parser extends TestCase {
DumpAST astDumper = new DumpAST();
cu.accept(astDumper);
- assertEquals(getFileContents(urlString + ".astdump.expected"), astDumper.getLog());
+ assertEquals(FileUtils.getFileContents(urlString + ".astdump.expected", true), astDumper.getLog());
}
@@ -238,7 +221,7 @@ public class TestJET2Parser extends TestCase {
cu.setOutputJavaClassName("X");
String result = writer.generate(cu);
- assertEquals(getFileContents(urlString + ".expected.java"), result);
+ assertEquals(FileUtils.getFileContents(urlString + ".expected.java", true), result);
}
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TestJETParser.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TestJETParser.java
index fec9e49..5e9aabf 100644
--- a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TestJETParser.java
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TestJETParser.java
@@ -5,11 +5,6 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
-import java.net.URL;
-import java.nio.CharBuffer;
-import java.nio.MappedByteBuffer;
-import java.nio.channels.FileChannel;
-import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -17,7 +12,6 @@ import java.util.Map;
import junit.framework.TestCase;
-import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.codegen.jet.JETException;
import org.eclipse.jet.compiler.Problem;
import org.eclipse.jet.internal.compiler.CommentElementDelegate;
@@ -29,6 +23,7 @@ import org.eclipse.jet.internal.parser.JETParseEventListener2;
import org.eclipse.jet.internal.parser.JETParser;
import org.eclipse.jet.internal.parser.JETReader;
import org.eclipse.jet.internal.parser.ParseProblemSeverity;
+import org.eclipse.jet.tests.FileUtils;
public class TestJETParser extends TestCase {
@@ -247,7 +242,7 @@ public class TestJETParser extends TestCase {
}
private AllJET2SyntaxListener doJET2Parse(String urlString) throws IOException, MalformedURLException, FileNotFoundException, JETException {
- String fileName = Platform.resolve(new URL(urlString)).getFile();
+ String fileName = FileUtils.resolveURLToLocationString(urlString);
FileInputStream inputStream = new FileInputStream(new File(fileName));
JETReader reader = new JETReader(urlString, inputStream, "UTF8");
@@ -278,24 +273,11 @@ public class TestJETParser extends TestCase {
assertEquals(expectedProblemId, problem.getProblemId());
}
- private String getFileContents(String urlString) throws Exception {
- String fileName = Platform.resolve(new URL(urlString)).getFile();
- FileInputStream inputStream = new FileInputStream(new File(fileName));
- FileChannel channel = inputStream.getChannel();
- MappedByteBuffer mapBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
- Charset utf8Decoder = Charset.forName("UTF-8");
- CharBuffer charBuffer = utf8Decoder.decode(mapBuffer);
- String contents = charBuffer.toString();
- channel.close();
- inputStream.close();
- return contents;
- }
-
public void testAllJET2Syntax() throws Exception {
String urlString = "platform:/plugin/org.eclipse.jet.tests/data/alljet2.jet";
AllJET2SyntaxListener listener = doJET2Parse(urlString);
- assertEquals(getFileContents(urlString + ".expected"), listener.getLog());
+ assertEquals(FileUtils.getFileContents(urlString + ".expected", true), listener.getLog());
}
public void testUnclosedComment() throws Exception {