001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.rms;
028:
029: import com.sun.midp.configurator.Constants;
030: import com.sun.midp.i3test.*;
031: import com.sun.midp.midlet.MIDletSuite;
032: import java.io.IOException;
033:
034: /**
035: * Test the global file resource limit by opening files up to and then past
036: * the limit. To open files, this test uses the RecordStoreFile class, which
037: * is a Java-based interface that's reasonably close to direct file access.
038: * RecordStoreFile is a private class inside com.sun.midp.rms, so that's why
039: * this test resides in that package.
040: */
041: public class TestFileRscLimit extends TestCase {
042:
043: /** Shorthand for the global file limit. */
044: static final int LIMIT = Constants.FILE_AMS_LIMIT;
045:
046: /** The suite name used when creating files. */
047: static final int SUITE = MIDletSuite.INTERNAL_SUITE_ID;
048:
049: /** The file extension used when creating files. */
050: static final int EXT = RecordStoreFile.DB_EXTENSION;
051:
052: /** An array of open RecordStoreFile objects. */
053: RecordStoreFile rsf[];
054:
055: /**
056: * Makes a filename.
057: *
058: * @param i a small integer used in creating the filename
059: * @return the filename just created
060: */
061: String makeName(int i) {
062: return "TestFileRscLimit" + i;
063: }
064:
065: /**
066: * Creates rsf, an array of LIMIT+1 RecordStoreFile objects and opens
067: * files up to LIMIT, leaving one slot unused.
068: */
069: public void setUp() throws IOException {
070: rsf = new RecordStoreFile[LIMIT + 1];
071: for (int i = 0; i < LIMIT; i++) {
072: rsf[i] = new RecordStoreFile(SUITE, makeName(i), EXT);
073: }
074: }
075:
076: /**
077: * Closes and deletes any RecordStoreFile objects found in the rsf array.
078: * Ignores any errors that might occur.
079: */
080: public void tearDown() {
081: for (int i = 0; i < rsf.length; i++) {
082: if (rsf[i] != null) {
083: try {
084: rsf[i].close();
085: } catch (IOException ioe) {
086: }
087: RecordStoreUtil
088: .quietDeleteFile(SUITE, makeName(i), EXT);
089: }
090: }
091: }
092:
093: /**
094: * Opens one more file than the global file resource limit and checks to
095: * see that the last one throws an exception.
096: */
097: void testFileHandlesRscLimit() throws IOException {
098: try {
099: boolean exceptionThrown = false;
100:
101: setUp();
102: try {
103: // The following line should hit the resource limit
104: // and therefore throw IOException.
105: rsf[LIMIT] = new RecordStoreFile(SUITE,
106: makeName(LIMIT), EXT);
107: } catch (IOException ioe) {
108: exceptionThrown = true;
109: }
110: assertTrue(exceptionThrown);
111: } finally {
112: tearDown();
113: }
114: }
115:
116: /** Run all tests. */
117: public void runTests() throws IOException {
118: declare("testFileHandlesRscLimit");
119: testFileHandlesRscLimit();
120: }
121: }
|