1
15
16 package gate.util;
17
18 import java.io.*;
19 import java.util.*;
20
21 import junit.framework.*;
22
23
25 public class TestFiles extends TestCase
26 {
27
28 private static final boolean DEBUG = false;
29
30
31 public TestFiles(String name) { super(name); }
32
33
34 public void setUp() {
35 }
37
38 public void testGetResources() throws Exception {
39 assertTrue(true);
40 String japeResName = "jape/combined/testloc.jape";
41 String firstLine = "// testloc.jape";
42
43 InputStreamReader resReader =
44 new InputStreamReader(Files.getGateResourceAsStream(japeResName));
45 BufferedReader bufResReader = new BufferedReader(resReader);
46 assertTrue(bufResReader.readLine().equals(firstLine));
47 resReader.close();
48
49 String resString = Files.getGateResourceAsString(japeResName);
50 assertTrue(resString.startsWith(firstLine));
51
52 byte[] resBytes = Files.getGateResourceAsByteArray(japeResName);
53
54
60
61 char resChars[] = new char[firstLine.length()];
62 for(int i=0; i<resChars.length; i++) resChars[i] = (char)resBytes[i];
63 resString = new String(resChars);
64 assertTrue(resString, resString.equals(firstLine));
65
66 }
68
69 public void testWriteTempFile() throws Exception {
70 assertTrue(true);
71 String japeResName = "jape/combined/testloc.jape";
72 String firstLine = "// testloc.jape";
73
74 File f = Files.writeTempFile(Files.getGateResourceAsStream(japeResName));
75 BufferedReader bfr = new BufferedReader(new FileReader(f));
76
77 String firstLn = bfr.readLine();
78 assertTrue("first line from jape/combined/testloc.jape doesn't match",
79 firstLine.equals(firstLn));
80
81 f.delete ();
82 }
84
85 public static Test suite() {
86 return new TestSuite(TestFiles.class);
87 }
89 public static void main(String args[]){
90 TestFiles app = new TestFiles("TestFiles");
91 try {
92 app.testJarFiles ();
93 app.testGetResources();
94 } catch (Exception e) {
95 e.printStackTrace (Err.getPrintWriter());
96 }
97 }
99
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 f1 = Files.writeTempFile(Files.getGateResourceAsStream(jarFilePathFirst));
118
119 f2 =Files.writeTempFile(Files.getGateResourceAsStream(jarFilePathSecond));
121
122
123 resourceFile = File.createTempFile("jarfinal", ".tmp");
125 resourceFile.deleteOnExit();
126
127 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 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 }
149
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 }
164 }
165 }
167
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 assertTrue(
185 "the GATECONFIG element doesn't match",
186 getEmptyElement(exampleXml, configElementName).equals(configElement)
187 );
188
189 Map newAttrs = new HashMap();
191 newAttrs.put("a", "1");
192 newAttrs.put("b", "2");
193 newAttrs.put("c", "3");
194
195 String newXml = Files.updateXmlElement(
197 new BufferedReader(new StringReader(exampleXml)),
198 configElementName,
199 newAttrs
200 );
201 assertTrue(
202 "newXml doesn't match (1): " + newXml.toString(),
203 newXml.toString().startsWith(exampleXmlStart) &&
204 newXml.toString().endsWith(exampleXmlEnd)
205 );
206 if(DEBUG) Out.prln(newXml);
207
208 File tempFile = Files.writeTempFile(exampleXml);
210 newXml = Files.updateXmlElement(tempFile, configElementName, newAttrs);
211 assertTrue(
212 "newXml doesn't match (2): " + newXml.toString(),
213 newXml.toString().startsWith(exampleXmlStart) &&
214 newXml.toString().endsWith(exampleXmlEnd)
215 );
216 if(DEBUG) Out.prln(newXml);
217
218 newXml = Files.getString(tempFile);
220 assertTrue(
221 "newXml doesn't match (3): " + newXml.toString(),
222 newXml.toString().startsWith(exampleXmlStart) &&
223 newXml.toString().endsWith(exampleXmlEnd)
224 );
225 if(DEBUG) Out.prln(newXml);
226
227 }
229
233 String getEmptyElement(String xml, String elementName) {
234 int start = xml.indexOf("<" + elementName);
236 int end = xml.indexOf(">", start);
237
238 StringBuffer xmlBuf = new StringBuffer(xml);
240 String substr = xmlBuf.substring(start, end + 1);
241 if(DEBUG) {
242 Out.prln("start=" + start + "; end=" + end + "; substr=" + substr);
243 }
244
245 return substr;
246 }
248 }