001: /*
002: *
003: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025:
026: package com.sun.midp.io.j2me.storage;
027:
028: import java.io.*;
029: import javax.microedition.io.*;
030: import com.sun.midp.i3test.*;
031: import com.sun.midp.configurator.Constants;
032:
033: /**
034: * Unit test for File.java.
035: */
036: public class TestFileSystem extends TestCase {
037:
038: final static String EMPTY_STR = "";
039: final static String TEST_FILE_A = File
040: .getStorageRoot(Constants.INTERNAL_STORAGE_ID)
041: + "A";
042: final static String TEST_FILE_B = File
043: .getStorageRoot(Constants.INTERNAL_STORAGE_ID)
044: + "B";
045:
046: File fs;
047: RandomAccessStream ras;
048:
049: void setUp() {
050: fs = new File();
051: ras = new RandomAccessStream();
052: }
053:
054: /*
055: * - Check storage root for null
056: * - Create a file "A" under the root and write to it
057: */
058: public void testGetStorageRoot() {
059:
060: // Storage root should not be null
061: assertTrue("getStorageRoot == null", File
062: .getStorageRoot(Constants.INTERNAL_STORAGE_ID) != null);
063:
064: // Storage root should be writable
065: try {
066: createTestFile(TEST_FILE_A, 1024);
067: assertTrue(TEST_FILE_A + " exists", fs.exists(TEST_FILE_A));
068: // Leave "A" on FS, so a later test can use it
069: } catch (IOException ioe) {
070: fail("Could not create a file in storage root");
071: }
072: }
073:
074: /*
075: * - Check config root for null
076: */
077: public void testGetConfigRoot() {
078: assertTrue("getConfigRoot != null", File
079: .getConfigRoot(Constants.INTERNAL_STORAGE_ID) != null);
080: }
081:
082: /*
083: * - a-z, 0-9 unchanged, convert back unchanged
084: * - A-Z convert to #A-#Z, convert back correctly
085: * - 0-1, 9+1, a-1, z+1, A-1, Z+1, 0xFFFF to not be unchanged,
086: * convert back correctly
087: * - empty string unchanged, convert back unchanged
088: * - 0x100 convert to "%0100", convert back correctly
089: */
090: public void testUnicodeToAsciiFilenameAndReverse() {
091: String unicode;
092: String ascii;
093: String result;
094: char[] buf = new char[1];
095:
096: for (char c = 'a'; c <= 'z'; c++) {
097: buf[0] = c;
098: unicode = new String(buf);
099: ascii = File.unicodeToAsciiFilename(unicode);
100: assertEquals(unicode, ascii);
101: result = File.asciiFilenameToUnicode(ascii);
102: assertEquals(unicode, result);
103: }
104:
105: for (char c = '0'; c <= '9'; c++) {
106: buf[0] = c;
107: unicode = new String(buf);
108: ascii = File.unicodeToAsciiFilename(unicode);
109: assertEquals(unicode, ascii);
110: result = File.asciiFilenameToUnicode(ascii);
111: assertEquals(unicode, result);
112: }
113:
114: for (char c = 'A'; c <= 'Z'; c++) {
115: buf[0] = c;
116: unicode = new String(buf);
117: ascii = File.unicodeToAsciiFilename(unicode);
118: assertEquals('#', ascii.charAt(0));
119: assertEquals(c, ascii.charAt(1));
120: result = File.asciiFilenameToUnicode(ascii);
121: assertEquals(unicode, result);
122: }
123:
124: unicode = EMPTY_STR;
125: ascii = File.unicodeToAsciiFilename(unicode);
126: assertEquals(unicode, ascii);
127:
128: buf[0] = 'a' - 1;
129: unicode = new String(buf);
130: ascii = File.unicodeToAsciiFilename(unicode);
131: assertNotEquals(unicode, ascii);
132:
133: buf[0] = 'z' + 1;
134: unicode = new String(buf);
135: ascii = File.unicodeToAsciiFilename(unicode);
136: assertNotEquals(unicode, ascii);
137:
138: buf[0] = '0' - 1;
139: unicode = new String(buf);
140: ascii = File.unicodeToAsciiFilename(unicode);
141: assertNotEquals(unicode, ascii);
142:
143: buf[0] = '9' + 1;
144: unicode = new String(buf);
145: ascii = File.unicodeToAsciiFilename(unicode);
146: assertNotEquals(unicode, ascii);
147:
148: buf[0] = 'A' - 1;
149: unicode = new String(buf);
150: ascii = File.unicodeToAsciiFilename(unicode);
151: assertNotEquals(unicode, ascii);
152:
153: buf[0] = 'Z' + 1;
154: unicode = new String(buf);
155: ascii = File.unicodeToAsciiFilename(unicode);
156: assertNotEquals(unicode, ascii);
157:
158: buf[0] = 0xFFFF;
159: unicode = new String(buf);
160: ascii = File.unicodeToAsciiFilename(unicode);
161: assertNotEquals(unicode, ascii);
162:
163: buf[0] = 0x100;
164: result = "%0100";
165: unicode = new String(buf);
166: ascii = File.unicodeToAsciiFilename(unicode);
167: assertEquals(result, ascii);
168: result = File.asciiFilenameToUnicode(ascii);
169: assertEquals(unicode, result);
170: }
171:
172: /*
173: * - Delete "B" just in case it then
174: * rename file "A" to "B" and
175: * check exists("B") and check non-exists("A")
176: * - rename file "B" to EMPTY name
177: * and expects a IOException
178: */
179: public void testRename() {
180:
181: // Make sure B does not exist
182: try {
183: fs.delete(TEST_FILE_B);
184: } catch (IOException e) {
185: // This will happen most of the time since "B" should not exist
186: }
187:
188: // Rename A to B
189: try {
190: fs.rename(TEST_FILE_A, TEST_FILE_B);
191: assertTrue("A does not exist", !fs.exists(TEST_FILE_A));
192: assertTrue("B does exist", fs.exists(TEST_FILE_B));
193:
194: try {
195: fs.rename(TEST_FILE_B, EMPTY_STR);
196: fail("rename to empty string");
197: } catch (IOException ioe) {
198: assertTrue(true);
199: }
200: } catch (Throwable t) {
201: fail("Exception when renaming: ");
202: t.printStackTrace();
203:
204: // Clean-up by deleting A
205: try {
206: fs.delete(TEST_FILE_A);
207: } catch (IOException ioe) {
208: // ignore
209: }
210: }
211: }
212:
213: /**
214: * Asserts that two objects are equal according to the equals() method.
215: *
216: * @param message the message to be emitted if the assertion fails
217: * @param expected an object containing the expected value
218: * @param actual an object containing the actual value
219: */
220: public void assertNotEquals(Object expected, Object actual) {
221: if (expected != actual) {
222: if (!expected.equals(actual)) {
223: return;
224: }
225:
226: fail("expected NOT equals actual");
227: return;
228: }
229: }
230:
231: /**
232: * exists(EMPTY) return false.
233: */
234: void testExists() {
235: assertTrue(!fs.exists(EMPTY_STR));
236: }
237:
238: /**
239: * - delete("B") and check for non-exists("B")
240: * - delete("DontExist") expects IOException
241: * - delete(EMPTY) expects IllegalArgumentException
242: */
243: void testDelete() {
244: try {
245: // Create B first
246: createTestFile(TEST_FILE_B, 1024);
247:
248: // Delete
249: fs.delete(TEST_FILE_B);
250:
251: // Test for existence
252: assertTrue(fs.exists(TEST_FILE_B) == false);
253:
254: } catch (Throwable t) {
255: fail("Unexpected Exception while deleting:");
256: t.printStackTrace();
257: }
258:
259: // Delete a non-existing file
260: boolean ioeThrowed = false;
261: try {
262: fs.delete("DontExist");
263: } catch (IOException ioe) {
264: ioeThrowed = true;
265: }
266:
267: assertTrue(ioeThrowed);
268: }
269:
270: /**
271: * - Call and store the size as initialSpace, expect > 0
272: * - Create a file "A" and write 1KB data, and call size again
273: * as lessSpace
274: * expect: 0 < diff - 1KB < 512B
275: * - Delete file "A" and call size again as moreSpace
276: expect moreSpace - lessSpace > 1KB
277: expect abs(moreSpace - lessSpace) < 512B
278: * - Repeat from step 2 for 50 times
279: */
280: void testAvailable() {
281:
282: int SIZE = 2048; // bytes
283: int OVERHEAD = 512; // bytes
284:
285: long initialSpace = fs
286: .getBytesAvailableForFiles(Constants.INTERNAL_STORAGE_ID);
287: assertTrue("initialSpace=" + initialSpace, initialSpace > 0);
288:
289: try {
290: // Loop many times to ensure no space leak
291: for (int i = 0; i < 50; i++) {
292: // Create a file with certain size
293: // expect overhead be less than allowed number
294: createTestFile(TEST_FILE_A, SIZE);
295: long lessSpace = fs
296: .getBytesAvailableForFiles(Constants.INTERNAL_STORAGE_ID);
297:
298: long delta = initialSpace - lessSpace;
299:
300: assertTrue("space used=" + delta, delta >= SIZE);
301:
302: assertTrue("create overhead=" + (delta - SIZE),
303: delta < SIZE + OVERHEAD);
304:
305: // expect delete a file will give back some space
306: // expect less than overhead bytes will be kept
307: fs.delete(TEST_FILE_A);
308: long moreSpace = fs
309: .getBytesAvailableForFiles(Constants.INTERNAL_STORAGE_ID);
310:
311: delta = moreSpace - lessSpace;
312:
313: assertTrue("freeup space=" + delta, delta >= SIZE);
314:
315: if (moreSpace < initialSpace) {
316: assertTrue("delete overhead="
317: + (initialSpace - moreSpace), initialSpace
318: - moreSpace < OVERHEAD);
319: } else {
320: assertTrue("delete overhead="
321: + (moreSpace - initialSpace), moreSpace
322: - initialSpace < OVERHEAD);
323: }
324: }
325: } catch (IOException ioe) {
326: ioe.printStackTrace();
327: }
328: }
329:
330: void tearDown() {
331: }
332:
333: void createTestFile(String name, int size) throws IOException {
334: ras.connect(name, Connector.WRITE);
335: ras.writeBytes(new byte[size], 0, size);
336: ras.commitWrite();
337: ras.disconnect();
338: }
339:
340: public void runTests() {
341: setUp();
342:
343: declare("testGetStorageRoot");
344: testGetStorageRoot();
345:
346: declare("testGetConfigRoot");
347: testGetConfigRoot();
348:
349: declare("testUnicodeToAsciiFilenameAndReverse");
350: testUnicodeToAsciiFilenameAndReverse();
351:
352: declare("testRename");
353: testRename();
354:
355: declare("testExists");
356: testExists();
357:
358: declare("testDelete");
359: testDelete();
360:
361: declare("testGetBytesAvailableForFiles");
362: testAvailable();
363:
364: tearDown();
365: }
366: }
|