| 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
|---|
| 2 | //
|
|---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
|---|
| 4 | // you may not use this file except in compliance with the License.
|
|---|
| 5 | // You may obtain a copy of the License at
|
|---|
| 6 | //
|
|---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0
|
|---|
| 8 | //
|
|---|
| 9 | // Unless required by applicable law or agreed to in writing, software
|
|---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
|---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|---|
| 12 | // See the License for the specific language governing permissions and
|
|---|
| 13 | // limitations under the License.
|
|---|
| 14 |
|
|---|
| 15 | package de.ugoe.cs.util;
|
|---|
| 16 |
|
|---|
| 17 | import java.io.File;
|
|---|
| 18 |
|
|---|
| 19 | import junitx.framework.FileAssert;
|
|---|
| 20 |
|
|---|
| 21 | import org.junit.*;
|
|---|
| 22 |
|
|---|
| 23 | import static org.junit.Assert.*;
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * The class <code>FileToolsTest</code> contains tests for the class
|
|---|
| 27 | * <code>{@link FileTools}</code>.
|
|---|
| 28 | *
|
|---|
| 29 | * @author Steffen Herbold
|
|---|
| 30 | * @version 1.0
|
|---|
| 31 | */
|
|---|
| 32 | public class FileToolsTest {
|
|---|
| 33 |
|
|---|
| 34 | final static String filenameNoCR = "testdata/de.ugoe.cs.util.FileToolsTest/testdata-nocr.txt";
|
|---|
| 35 | final static String filenameCR = "testdata/de.ugoe.cs.util.FileToolsTest/testdata-cr.txt";
|
|---|
| 36 | final static String filenameNotExists = "testdata/de.ugoe.cs.util.FileToolsTest/doesnotexist.txt";
|
|---|
| 37 |
|
|---|
| 38 | final static String[] expectedLines = { "line1", "line2", "line3", "",
|
|---|
| 39 | "line5" };
|
|---|
| 40 |
|
|---|
| 41 | final static String[] expectedLinesCR = { "line1\r", "line2\r", "line3\r",
|
|---|
| 42 | "\r", "line5" };
|
|---|
| 43 |
|
|---|
| 44 | final static String[] expectedWrongCR = { "line1\nline2\nline3\n\nline5" };
|
|---|
| 45 |
|
|---|
| 46 | @Test
|
|---|
| 47 | public void testGetLinesFromFile_1() throws Exception {
|
|---|
| 48 | String[] result = FileTools.getLinesFromFile(filenameCR, true);
|
|---|
| 49 | assertArrayEquals(expectedLines, result);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | @Test
|
|---|
| 53 | public void testGetLinesFromFile_2() throws Exception {
|
|---|
| 54 | String[] result = FileTools.getLinesFromFile(filenameNoCR, true);
|
|---|
| 55 | assertArrayEquals(expectedWrongCR, result);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | @Test
|
|---|
| 59 | public void testGetLinesFromFile_3() throws Exception {
|
|---|
| 60 | String[] result = FileTools.getLinesFromFile(filenameNoCR, false);
|
|---|
| 61 | assertArrayEquals(expectedLines, result);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | @Test
|
|---|
| 65 | public void testGetLinesFromFile_4() throws Exception {
|
|---|
| 66 | String[] result = FileTools.getLinesFromFile(filenameCR, false);
|
|---|
| 67 | assertArrayEquals(expectedLinesCR, result);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | @Test(expected = java.io.FileNotFoundException.class)
|
|---|
| 71 | public void testGetLinesFromFile_5() throws Exception {
|
|---|
| 72 | FileTools.getLinesFromFile(filenameNotExists, true);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | @Test(expected = java.io.FileNotFoundException.class)
|
|---|
| 76 | public void testGetLinesFromFile_6() throws Exception {
|
|---|
| 77 | FileTools.getLinesFromFile(filenameNotExists, false);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | @Test
|
|---|
| 81 | public void testWriteArrayToFile_1() throws Exception {
|
|---|
| 82 | String[] array = new String[]{"foo", "bar"};
|
|---|
| 83 | String filename = "testdata/de.ugoe.cs.util.FileToolsTest/writeArrayToFile-actual_01.txt";
|
|---|
| 84 | String separator = "";
|
|---|
| 85 | boolean lineBreak = false;
|
|---|
| 86 |
|
|---|
| 87 | // cleanup old result
|
|---|
| 88 | new File(filename).delete();
|
|---|
| 89 |
|
|---|
| 90 | FileTools.writeArrayToFile(array, filename, separator, lineBreak);
|
|---|
| 91 |
|
|---|
| 92 | FileAssert.assertEquals(new File("testdata/de.ugoe.cs.util.FileToolsTest/writeArrayToFile-expected_01.txt"), new File(filename));
|
|---|
| 93 |
|
|---|
| 94 | // cleanup after successful test
|
|---|
| 95 | new File(filename).delete();
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | @Test
|
|---|
| 99 | public void testWriteArrayToFile_2() throws Exception {
|
|---|
| 100 | String[] array = new String[]{"foo", "bar"};
|
|---|
| 101 | String filename = "testdata/de.ugoe.cs.util.FileToolsTest/writeArrayToFile-actual_02.txt";
|
|---|
| 102 | String separator = " - ";
|
|---|
| 103 | boolean lineBreak = false;
|
|---|
| 104 |
|
|---|
| 105 | // cleanup old result
|
|---|
| 106 | new File(filename).delete();
|
|---|
| 107 |
|
|---|
| 108 | FileTools.writeArrayToFile(array, filename, separator, lineBreak);
|
|---|
| 109 |
|
|---|
| 110 | FileAssert.assertEquals(new File("testdata/de.ugoe.cs.util.FileToolsTest/writeArrayToFile-expected_02.txt"), new File(filename));
|
|---|
| 111 |
|
|---|
| 112 | // cleanup after successful test
|
|---|
| 113 | new File(filename).delete();
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | @Test
|
|---|
| 117 | public void testWriteArrayToFile_3() throws Exception {
|
|---|
| 118 | String[] array = new String[]{"foo", "bar"};
|
|---|
| 119 | String filename = "testdata/de.ugoe.cs.util.FileToolsTest/writeArrayToFile-actual_03.txt";
|
|---|
| 120 | String separator = "";
|
|---|
| 121 | boolean lineBreak = true;
|
|---|
| 122 |
|
|---|
| 123 | // cleanup old result
|
|---|
| 124 | new File(filename).delete();
|
|---|
| 125 |
|
|---|
| 126 | FileTools.writeArrayToFile(array, filename, separator, lineBreak);
|
|---|
| 127 |
|
|---|
| 128 | String expectedFile;
|
|---|
| 129 | if( System.getProperty("line.separator").equals("\r\n") ) {
|
|---|
| 130 | expectedFile = "testdata/de.ugoe.cs.util.FileToolsTest/writeArrayToFile-expected_03_cr.txt";
|
|---|
| 131 | } else {
|
|---|
| 132 | expectedFile = "testdata/de.ugoe.cs.util.FileToolsTest/writeArrayToFile-expected_03_nocr.txt";
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | FileAssert.assertEquals(new File(expectedFile), new File(filename));
|
|---|
| 136 |
|
|---|
| 137 | // cleanup after successful test
|
|---|
| 138 | new File(filename).delete();
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | public static void main(String[] args) {
|
|---|
| 142 | new org.junit.runner.JUnitCore().run(FileToolsTest.class);
|
|---|
| 143 | }
|
|---|
| 144 | } |
|---|