File: jdk11.java

package info (click to toggle)
mauve 20080616-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 26,856 kB
  • ctags: 21,952
  • sloc: java: 234,107; sh: 2,834; xml: 208; makefile: 59
file content (345 lines) | stat: -rw-r--r-- 10,836 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*************************************************************************
/* jdk11.java -- java.io.File 1.1 tests
/*
/* Copyright (c) 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
/*
/* This program is free software; you can redistribute it and/or modify
/* it under the terms of the GNU General Public License as published 
/* by the Free Software Foundation, either version 2 of the License, or
/* (at your option) any later version.
/*
/* This program is distributed in the hope that it will be useful, but
/* WITHOUT ANY WARRANTY; without even the implied warranty of
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/* GNU General Public License for more details.
/*
/* You should have received a copy of the GNU General Public License
/* along with this program; if not, write to the Free Software Foundation
/* Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
/*************************************************************************/

// Tags: JDK1.1

package gnu.testlet.java.io.File;

import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;
import gnu.testlet.config;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.util.Date;

public class jdk11 implements Testlet, FilenameFilter
{
  
  public void test (TestHarness harness)
  {
    String srcdirstr = harness.getSourceDirectory ();
    String tmpdirstr = harness.getTempDirectory ();

    // File (String)
    File srcdir = new File (srcdirstr);
    File tmpdir = new File (tmpdirstr);
    String THIS_FILE = new String ("gnu" + 
				   File.separator + "testlet" +
				   File.separator + "java" + 
				   File.separator + "io" + 
				   File.separator + "File" + 
				   File.separator + "tmp");

    // File (File, String)
    File cons = new File (srcdir, THIS_FILE);
    // File (String, String)
    File cons2 = new File (srcdirstr, THIS_FILE);


    // mkdir ()
    harness.check (cons.mkdir (), "mkdir ()");

    // canRead ()
    harness.check (cons.canRead (), "canRead ()");
    // equals (Object)
    harness.check (cons.equals (cons2), "equals ()");
    // isDirectory ()
    harness.check (srcdir.isDirectory (), "isDirectory ()");
    harness.check (tmpdir.isDirectory (), "isDirectory ()");

    String TMP_FILENAME = "File.tst";
    String TMP_FILENAME2 = "Good.doc";
    String TMP_FILENAME3 = "File.doc";

    // create empty file
    File tmp = new File (cons, TMP_FILENAME);
    try 
      {
	FileOutputStream fos = new FileOutputStream (tmp);
	fos.close ();
      } 
    catch (FileNotFoundException fne) { }
    catch (IOException ioe) { }

    // create empty file
    File tmp2 = new File (cons, TMP_FILENAME2);
    try 
      {
	FileOutputStream fos = new FileOutputStream (tmp2);
	fos.close ();
      } 
    catch (FileNotFoundException fne) { }
    catch (IOException ioe) { }

    File tmp3 = new File (cons, TMP_FILENAME3);

    // canWrite ()
    harness.check (tmp.canWrite (), "canWrite()");
    // exists ()
    harness.check (tmp.exists (), "exists ()");
    // isFile ()
    harness.check (tmp.isFile (), "isFile ()");
    // length ()
    harness.check (tmp.length (), 0L, "length ()");
    byte[] b = new byte[2001];
    try 
      {
	FileOutputStream fos = new FileOutputStream (tmp);
	fos.write (b);
	fos.close ();
      } 
    catch (FileNotFoundException fne) { }
    catch (IOException ioe) { }
    harness.check (tmp.length (), b.length, "length ()");

    // toString ();
    String tmpstr = new String (srcdirstr + File.separator 
				+ THIS_FILE + File.separator 
				+ TMP_FILENAME);
    harness.debug (tmp.toString () + " =? " + tmpstr);
    harness.check (tmp.toString ().equals (tmpstr), "toString ()");

    // list ();
    String [] tmpdirlist = cons.list ();
    String [] expectedlist = new String[] {TMP_FILENAME, TMP_FILENAME2};
//      for (int ll=0; ll<tmpdirlist.length; ll++)
//        System.err.println (tmpdirlist[ll]);
    harness.check (compareStringArray (tmpdirlist, expectedlist), "list ()");
    
    // list (FilenameFilter);
    tmpdirlist = cons.list (this);
    expectedlist = new String[] {TMP_FILENAME2};
//      for (int ll=0; ll<tmpdirlist.length; ll++)
//        System.err.println (tmpdirlist[ll]);
    harness.check (compareStringArray (tmpdirlist, expectedlist), "list (FilenameFilter)");

    // renameTo (File);
    if (tmp3.exists ())
      tmp3.delete ();
    harness.check (tmp.renameTo (tmp3), "renameTo (File)");
    harness.check (tmp3.exists (), "renameTo (File)");

    // check delete of directory with something in it fails
    if (tmp.exists ())
      harness.check (tmp.delete (), "delete ()");
    if (tmp2.exists ())
      harness.check (tmp2.delete (), "delete ()");
    if (tmp3.exists ())
      harness.check (tmp3.delete (), "delete ()");
    harness.check (!tmp.exists (), "delete ()");
    harness.check (!tmp2.exists (), "delete ()");
    harness.check (!tmp3.exists (), "delete ()");
    
    // mkdir ();
    harness.check (tmp.mkdir (), "mkdir ()");
    harness.check (tmp.exists () && tmp.isDirectory (), "mkdir ()");
    
    // mkdirs ();
    File mkdirstest = new File (tmpdirstr, new String ("one" + File.separator
						       + "two" + File.separator
						       + "three"));
    harness.check (mkdirstest.mkdirs (), "mkdirs ()");
    harness.check (mkdirstest.exists () && mkdirstest.isDirectory (), "mkdirs ()");
    File mkdirstest2 = new File (tmpdirstr, new String ("one" + File.separator 
							+ "two"));
    harness.check (mkdirstest2.exists () && mkdirstest2.isDirectory (), "mkdirs ()");
    File mkdirstest1 = new File (tmpdirstr, new String ("one"));
    harness.check (mkdirstest1.exists () && mkdirstest1.isDirectory (), "mkdirs ()");

    harness.check (mkdirstest.delete (), "delete () of a directory");
    harness.check (!mkdirstest.exists (), "delete () of a directory");

    // negative test case
    harness.check (!mkdirstest1.delete (), "delete () of a directory");

    harness.check (mkdirstest2.delete (), "delete () of a directory");
    harness.check (!mkdirstest2.exists (), "delete () of a directory");

    harness.check (mkdirstest1.delete (), "delete () of a directory");
    harness.check (!mkdirstest1.exists (), "delete () of a directory");
    
    // check delete of an empty directory
    harness.check (tmp.delete (), "delete () of a directory");
    harness.check (!tmp.exists (), "delete () of a directory");

    harness.check (cons.delete (), "delete () of a directory");
    harness.check (!cons.exists (), "delete () of a directory");

    harness.check (File.pathSeparator.equals (config.pathSeparator), "pathSeparator");
    harness.check (new Character (File.pathSeparatorChar).toString ().equals (config.pathSeparator), "pathSeparatorChar");

    harness.check (File.separator.equals (config.separator), "separator");
    harness.check (new Character (File.separatorChar).toString ().equals (config.separator), "separatorChar");
    
    // getAbsolutePath ();
    harness.debug ("tmp3.getAbsolutePath () = " + tmp3.getAbsolutePath ());
    harness.debug ("equals? "  + srcdirstr
		   + File.separator
		   + THIS_FILE
		   + File.separator
		   + TMP_FILENAME3);
    harness.check (tmp3.getAbsolutePath ().equals (srcdirstr
						  + File.separator
						  + THIS_FILE
						  + File.separator
						  + TMP_FILENAME3), "getAbsolutePath ()");

    // getCanonicalPath ();

    try 
      {
	// Make sure that file exists.
	cons.mkdir ();
	FileOutputStream fos = new FileOutputStream (tmp3);
	fos.write (1);
	fos.close ();
	harness.debug ("tmp3.getCanonicalPath () = " + tmp3.getCanonicalPath ());
	harness.debug ("equals? " + srcdirstr + File.separator 
		       + THIS_FILE + File.separator
		       + TMP_FILENAME3);
	harness.check (tmp3.getCanonicalPath ().equals (srcdirstr
						       + File.separator 
						       + THIS_FILE
						       + File.separator
						       + TMP_FILENAME3), "getCanonicalPath ()");
	// Remove again
	tmp3.delete ();
	cons.delete ();
      } 
    catch (IOException ioe)
      {
	harness.check (false, "getCanonicalPath () " + ioe);
      }

    // Another getCanonicalPath() test.
    boolean ok = false;
    try
      {
	File x1 = new File ("").getCanonicalFile ();
	File x2 = new File (".").getCanonicalFile ();
	ok = x1.equals(x2);
      }
    catch (IOException ioe)
      {
	// Nothing.
      }
    harness.check (ok, "getCanonicalFile with empty path");

    // getName ();
    harness.debug ("tmp3.getName () = " + tmp3.getName ());
    harness.check (tmp3.getName ().equals (TMP_FILENAME3), "getName ()");

    // getParent ();
    harness.check (tmp3.getParent ().equals (srcdirstr 
					    + File.separator 
					    + THIS_FILE), "getParent ()");

    // getPath ();
    harness.debug ("tmp3.getPath () = " + tmp.getPath ());
    harness.check (tmp3.getPath ().equals (srcdirstr 
					  + File.separator
					  + THIS_FILE
					  + File.separator
					  + TMP_FILENAME3), "getPath ()");

    // hashCode ();
	int hc1 = tmp3.hashCode();
	int hc2 = tmp3.hashCode();
	harness.check (hc1, hc2, "hashCode()");
    
    // isAbsolute ();
    harness.check (tmp3.isAbsolute (), "isAbsolute ()");
    harness.check (! new File("").isAbsolute());

    // lastModified ();
    File lastmod = new File (tmpdir, "lastmod");
    if (lastmod.exists ())
      lastmod.delete ();
    Date now = new Date ();
    long time = now.getTime ();
    try 
      {
	Thread.sleep (1000);
      }
    catch (InterruptedException ie) { }
    try 
      {
	FileOutputStream fos = new FileOutputStream (lastmod);
	fos.close ();
      } 
    catch (FileNotFoundException fne) { }
    catch (IOException ioe) { }
    harness.debug (lastmod.lastModified () + " >= " + time);
    if (lastmod.lastModified () >= time)
      harness.check (true, "lastModified ()");
    else
      harness.check (false, "lastModified ()");
    if (lastmod.exists ())
      lastmod.delete ();
  }

  /**
   * Compare two String arrays, and if of the same length compare
   * contents for equality, order does not matter.
   */
  private boolean compareStringArray (String[] x, String[] y)
  {
    if (x.length != y.length)
      return false;

    boolean[] test = new boolean[y.length];
    for (int i = 0; i < test.length; i++)
      test[i] = true;

    for (int i = 0; i < x.length; i++)
      {
	boolean nomatch = true;
	for (int j = 0; j < y.length; j++)
	  {
	    if (test[j])
	      if (x[i].equals (y[j]))
		{
		  test[j] = false;
		  nomatch = false;
		  break;
		}
	  }
	if (nomatch)
	  return false;
      }
    return true;
  }

  /**
   * Defined by NameFilter. Only accepts files ending with .doc.
   */
  public boolean accept (File dir, String name)
  {
    if (name.endsWith (".doc"))
      return true;
    return false;
  }

}