1   /*
2    *  TestFiles.java
3    *
4    *  Copyright (c) 1998-2005, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Hamish Cunningham, 10/June/00
12   *
13   *  $Id: TestFiles.java,v 1.22 2005/01/11 13:51:37 ian Exp $
14   */
15  
16  package gate.util;
17  
18  import java.io.*;
19  import java.util.*;
20  
21  import junit.framework.*;
22  
23  /** Files test class.
24    */
25  public class TestFiles extends TestCase
26  {
27    /** Debug flag */
28    private static final boolean DEBUG = false;
29  
30    /** Construction */
31    public TestFiles(String name) { super(name); }
32  
33    /** Fixture set up */
34    public void setUp() {
35    } // setUp
36  
37    /** Test the getResourceAs... methods. */
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      /*
55      Out.println(new String(resBytes));
56      Out.println(resBytes.length);
57      Out.println(resString);
58      Out.println(resString.length());
59      */
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    } // testGetResources()
67  
68    /** Test the writeTempFile... method. */
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    } // testWriteTempFile()
83  
84    /** Test suite routine for the test runner */
85    public static Test suite() {
86      return new TestSuite(TestFiles.class);
87    } // suite
88  
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    } // main
98  
99    /** 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 
195     // test the files method
196     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     // write the example data into a temp file and try on that
209     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     // check that the file was overwritten successfully
219     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   } // updateXmlElement
228 
229   /**
230    * Helper method to extract an empty element from a string
231    * containing XML
232    */
233   String getEmptyElement(String xml, String elementName) {
234     // find the index of "<elementName"; find the next ">"
235     int start = xml.indexOf("<" + elementName);
236     int end = xml.indexOf(">", start);
237 
238     // get the range between the two indices
239     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   } // getEmptyElement
247 
248 } // class TestFiles
249