001 /*
002 * YamFormatterTest.java Copyright (c) 1998-2008, The University of Sheffield.
003 *
004 * This code is from the GATE project (http://gate.ac.uk/) and is free software
005 * licenced under the GNU General Public License version 3. It is distributed
006 * without any warranty. For more details see COPYING.txt in the top level
007 * directory (or at http://gatewiki.sf.net/COPYING.txt).
008 *
009 * Hamish Cunningham, 25 August 2009
010 */
011
012 package gate.yam.format;
013
014 import java.io.*;
015 import junit.framework.*;
016 import org.apache.log4j.Logger;
017 import org.springframework.core.io.*;
018 import org.springframework.context.support.*;
019 import gate.util.*;
020 import gate.yam.*;
021 import gate.yam.parse.*;
022 import gate.yam.translate.*;
023 import gate.yam.convert.*;
024 import gate.yam.format.*;
025
026 /**
027 * Unit test for the YamFile class.
028 */
029 public class YamFormatterTests extends TestCase {
030
031 /** Create the test case */
032 public YamFormatterTests(String testName) {
033 super(testName);
034 }
035
036 /** Logger */
037 static Logger log = Logger.getLogger("gate.yam.format.YamFormatterTests");
038
039 /**
040 * Test word wrapping.
041 */
042 public void testWrapping() throws Exception {
043 log.info("==== testWrapping... =======================");
044
045 String testInName = "/gate/yam/resources/formatter-test-in.txt";
046 String testOutName = "/gate/yam/resources/formatter-test-out.txt";
047
048 // input to test on
049 String testInFilePath = this.getClass().getResource(testInName).getPath();
050
051 // output to compare with
052 String testOutFilePath = this.getClass().getResource(testOutName).getPath();
053
054 BufferedReader textReader = null;
055 StringBuilder inputText = new StringBuilder();
056 StringBuilder toCompareWith = new StringBuilder();
057
058 try {
059 // reading the input text
060 textReader = new BufferedReader(new FileReader(testInFilePath));
061 String line = textReader.readLine();
062 while(line != null) {
063 inputText.append(line);
064 line = textReader.readLine();
065 if(line != null) inputText.append("\n");
066 }
067 textReader.close();
068
069 // reading the text to compare with
070 textReader = new BufferedReader(new FileReader(testOutFilePath));
071 line = textReader.readLine();
072 while(line != null) {
073 toCompareWith.append(line);
074 line = textReader.readLine();
075 if(line != null) toCompareWith.append("\n");
076 }
077 } finally {
078 if(textReader != null) textReader.close();
079 }
080
081 // test one
082 int test1Start = inputText.indexOf("#test1#") + 8; // length + \n
083 int test1End = inputText.indexOf("#test1-end#") - 1; // \n
084 YamFormatter.wrap(inputText, test1Start, test1End, 0, 78);
085
086 // test two (unordered list item)
087 int test2Start = inputText.indexOf("#test2#") + 12; // length + \n + " - "
088 int test2End = inputText.indexOf("#test2-end#") - 1; // \n
089 YamFormatter.wrap(inputText, test2Start, test2End, 4, 78);
090
091 // test three (tab test)
092 int test3Start = inputText.indexOf("#test3#") + 12; // length + \n + " - "
093 int test3End = inputText.indexOf("#test3-end#") - 1; // \n
094 YamFormatter.wrap(inputText, test3Start, test3End, 4, 78);
095
096 // compare strings
097 assertEquals(inputText.toString(), toCompareWith.toString());
098
099 log.info("====================================");
100 } // testWrapping()
101 } // YamFormatterTests
|