001 /*
002 * TestFiles.java
003 *
004 * Copyright (c) 1995-2010, The University of Sheffield. See the file
005 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006 *
007 * This file is part of GATE (see http://gate.ac.uk/), and is free
008 * software, licenced under the GNU Library General Public License,
009 * Version 2, June 1991 (in the distribution as file licence.html,
010 * and also available at http://gate.ac.uk/gate/licence.html).
011 *
012 * Hamish Cunningham, 10/June/00
013 *
014 * $Id: TestFiles.java 12919 2010-08-03 10:31:37Z valyt $
015 */
016
017 package gate.util;
018
019 import java.io.*;
020 import java.util.*;
021
022 import junit.framework.*;
023
024 /** Files test class.
025 */
026 public class TestFiles extends TestCase
027 {
028 /** Debug flag */
029 private static final boolean DEBUG = false;
030
031 /** Construction */
032 public TestFiles(String name) { super(name); }
033
034 /** Fixture set up */
035 public void setUp() {
036 } // setUp
037
038 /** Test the getResourceAs... methods. */
039 public void testGetResources() throws Exception {
040 assertTrue(true);
041 String japeResName = "jape/combined/testloc.jape";
042 String firstLine = "// testloc.jape";
043
044 BufferedReader bufResReader =
045 new BomStrippingInputStreamReader(Files.getGateResourceAsStream(japeResName));
046 assertTrue(bufResReader.readLine().equals(firstLine));
047 bufResReader.close();
048
049 String resString = Files.getGateResourceAsString(japeResName);
050 assertTrue(resString.startsWith(firstLine));
051
052 byte[] resBytes = Files.getGateResourceAsByteArray(japeResName);
053
054 /*
055 Out.println(new String(resBytes));
056 Out.println(resBytes.length);
057 Out.println(resString);
058 Out.println(resString.length());
059 */
060
061 char resChars[] = new char[firstLine.length()];
062 for(int i=0; i<resChars.length; i++) resChars[i] = (char)resBytes[i];
063 resString = new String(resChars);
064 assertTrue(resString, resString.equals(firstLine));
065
066 } // testGetResources()
067
068 /** Test the writeTempFile... method. */
069 public void testWriteTempFile() throws Exception {
070 assertTrue(true);
071 String japeResName = "jape/combined/testloc.jape";
072 String firstLine = "// testloc.jape";
073
074 File f = Files.writeTempFile(Files.getGateResourceAsStream(japeResName));
075 BufferedReader bfr = new BufferedReader(new FileReader(f));
076
077 String firstLn = bfr.readLine();
078 assertTrue("first line from jape/combined/testloc.jape doesn't match",
079 firstLine.equals(firstLn));
080
081 f.delete ();
082 } // testWriteTempFile()
083
084 /** Test suite routine for the test runner */
085 public static Test suite() {
086 return new TestSuite(TestFiles.class);
087 } // suite
088
089 public static void main(String args[]){
090 TestFiles app = new TestFiles("TestFiles");
091 try {
092 app.testJarFiles ();
093 app.testGetResources();
094 } catch (Exception e) {
095 e.printStackTrace (Err.getPrintWriter());
096 }
097 } // main
098
099 /** Test JarFiles methods */
100 public void testJarFiles() throws Exception {
101
102 JarFiles jarFiles = new JarFiles();
103 Set filesToMerge = new HashSet();
104 String jarFilePathFirst = "jartest/ajartest.jar";
105 String jarFilePathSecond ="jartest/bjartest.jar";
106 String jarPathFirst = null;;
107 String jarPathSecond = null;
108 String jarPathFinal = null;
109 File resourceFile = null;
110 File f1 = null;
111 File f2 = null;
112 FileInputStream fileStreamFirst = null;
113 FileInputStream fileStreamSecond = null;
114
115 //open first jar file in a temporal file
116 // Out.println(Files.getResourceAsStream(jarFilePathFirst));
117 f1 = Files.writeTempFile(Files.getGateResourceAsStream(jarFilePathFirst));
118
119 //open second jar file in a temporal file
120 f2 =Files.writeTempFile(Files.getGateResourceAsStream(jarFilePathSecond));
121
122
123 //create a temporal file in order to put the classes of jar files
124 resourceFile = File.createTempFile("jarfinal", ".tmp");
125 resourceFile.deleteOnExit();
126
127 //determin the paths of the temporal files
128 jarPathFirst = f1.getAbsolutePath();
129 jarPathSecond = f2.getAbsolutePath();
130 f1.deleteOnExit();
131 f2.deleteOnExit();
132 jarPathFinal = resourceFile.getAbsolutePath();
133 filesToMerge.add(jarPathFirst);
134 filesToMerge.add(jarPathSecond);
135
136 //close the temporal files
137 fileStreamFirst = new FileInputStream(f1);
138
139 fileStreamSecond = new FileInputStream(f2);
140
141 fileStreamFirst.close();
142
143 fileStreamSecond.close();
144
145 jarFiles.merge(filesToMerge,jarPathFinal);
146
147 } // testJarFiles
148
149 /** Test the find method. */
150 public void testFind(){
151 String regex = "z:/gate2/doc/.*.html";
152 String filePath = "z:/gate2/doc";
153 Iterator iter;
154 Files files = new Files();
155 Set regfind = new HashSet();
156
157 regfind = Files.Find(regex,filePath);
158 iter = regfind.iterator();
159 if (iter.hasNext()){
160 while (iter.hasNext()){
161 String verif = iter.next().toString();
162 //Out.println(verif);
163 }
164 }
165 } // testFind
166
167 /** Test the updateXmlElement method. */
168 public void testUpdateXmlElement() throws IOException {
169 String nl = Strings.getNl();
170 String configElementName = "GATECONFIG";
171 String configElement = "<" + configElementName + " FULLSIZE=\"yes\"/>";
172 String exampleXmlStart =
173 "<?xml version=\"1.0\"?>" + nl +
174 "<!-- a comment -->" + nl +
175 "<GATE>" + nl +
176 "" + nl +
177 "<CREOLE-DIRECTORY>http://on.the.net/</CREOLE-DIRECTORY>" + nl +
178 "<!--- The next element may be overwritten by the GUI --->" + nl;
179 String exampleXmlEnd =
180 "</GATE>" + nl;
181 String exampleXml = exampleXmlStart + configElement + nl + exampleXmlEnd;
182
183 // check that the getEmptyElement method works
184 assertTrue(
185 "the GATECONFIG element doesn't match",
186 getEmptyElement(exampleXml, configElementName).equals(configElement)
187 );
188
189 // a map of values to place in the new element
190 Map newAttrs = new HashMap();
191 newAttrs.put("a", "1");
192 newAttrs.put("b", "2");
193 newAttrs.put("c", "3");
194 newAttrs.put("d", "needs&escaping");
195
196 // test the files method
197 String newXml = Files.updateXmlElement(
198 new BufferedReader(new StringReader(exampleXml)),
199 configElementName,
200 newAttrs
201 );
202 assertTrue(
203 "newXml doesn't match (1): " + newXml.toString(),
204 newXml.toString().startsWith(exampleXmlStart) &&
205 newXml.toString().endsWith(exampleXmlEnd) &&
206 newXml.toString().contains("a=\"1\"") &&
207 newXml.toString().contains("d=\"needs&escaping\"")
208 );
209 if(DEBUG) Out.prln(newXml);
210
211 // write the example data into a temp file and try on that
212 File tempFile = Files.writeTempFile(exampleXml);
213 newXml = Files.updateXmlElement(tempFile, configElementName, newAttrs);
214 assertTrue(
215 "newXml doesn't match (2): " + newXml.toString(),
216 newXml.toString().startsWith(exampleXmlStart) &&
217 newXml.toString().endsWith(exampleXmlEnd) &&
218 newXml.toString().contains("a=\"1\"") &&
219 newXml.toString().contains("d=\"needs&escaping\"")
220 );
221 if(DEBUG) Out.prln(newXml);
222
223 // check that the file was overwritten successfully
224 newXml = Files.getString(tempFile);
225 assertTrue(
226 "newXml doesn't match (3): " + newXml.toString(),
227 newXml.toString().startsWith(exampleXmlStart) &&
228 newXml.toString().endsWith(exampleXmlEnd) &&
229 newXml.toString().contains("a=\"1\"") &&
230 newXml.toString().contains("d=\"needs&escaping\"")
231 );
232 if(DEBUG) Out.prln(newXml);
233
234 } // updateXmlElement
235
236 /**
237 * Helper method to extract an empty element from a string
238 * containing XML
239 */
240 String getEmptyElement(String xml, String elementName) {
241 // find the index of "<elementName"; find the next ">"
242 int start = xml.indexOf("<" + elementName);
243 int end = xml.indexOf(">", start);
244
245 // get the range between the two indices
246 StringBuffer xmlBuf = new StringBuffer(xml);
247 String substr = xmlBuf.substring(start, end + 1);
248 if(DEBUG) {
249 Out.prln("start=" + start + "; end=" + end + "; substr=" + substr);
250 }
251
252 return substr;
253 } // getEmptyElement
254
255 } // class TestFiles
|