001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /**
020: * Package to test FileServer methods
021: */package org.apache.jmeter.services;
022:
023: import java.io.IOException;
024:
025: import org.apache.jmeter.junit.JMeterTestCase;
026: import org.apache.jmeter.util.JMeterUtils;
027:
028: public class TestFileServer extends JMeterTestCase {
029:
030: private static final FileServer FS = FileServer.getFileServer();
031:
032: public TestFileServer() {
033: super ();
034: }
035:
036: public TestFileServer(String arg0) {
037: super (arg0);
038: }
039:
040: public void tearDown() throws IOException {
041: FS.closeFiles();
042: FS.resetBase();
043: }
044:
045: public void testopen() throws Exception {
046: try {
047: FS.readLine("test");
048: fail("Expected IOException");
049: } catch (IOException ignored) {
050: }
051: try {
052: FS.write("test", "");
053: fail("Expected IOException");
054: } catch (IOException ignored) {
055: }
056: assertFalse("Should not have any files open", FS.filesOpen());
057: FS.closeFile("xxx"); // Unrecognised files are ignored
058: assertFalse("Should not have any files open", FS.filesOpen());
059: String infile = "testfiles/test.csv";
060: FS.reserveFile(infile); // Does not open file
061: assertFalse("Should not have any files open", FS.filesOpen());
062: assertEquals("a1,b1,c1,d1", FS.readLine(infile));
063: assertTrue("Should have some files open", FS.filesOpen());
064: assertNotNull(FS.readLine(infile));
065: assertNotNull(FS.readLine(infile));
066: assertNotNull(FS.readLine(infile));
067: assertEquals("a1,b1,c1,d1", FS.readLine(infile));// Re-read 1st line
068: assertNotNull(FS.readLine(infile));
069: try {
070: FS.write(infile, "");// should not be able to write to it ...
071: fail("Expected IOException");
072: } catch (IOException ignored) {
073: }
074: FS.closeFile(infile); // does not remove the entry
075: assertFalse("Should not have any files open", FS.filesOpen());
076: assertEquals("a1,b1,c1,d1", FS.readLine(infile));// Re-read 1st line
077: assertTrue("Should have some files open", FS.filesOpen());
078: FS.closeFiles(); // removes all entries
079: assertFalse("Should not have any files open", FS.filesOpen());
080: try {
081: FS.readLine(infile);
082: fail("Expected IOException");
083: } catch (IOException ignored) {
084: }
085: String base = FS.getBaseDir();
086: infile = base + "/testfiles/test.csv";
087: FS.reserveFile(infile); // Does not open file
088: assertFalse("Should not have any files open", FS.filesOpen());
089: assertEquals("a1,b1,c1,d1", FS.readLine(infile));
090: try {
091: FS.setBasedir("x");
092: fail("Expected IOException");
093: } catch (IOException ignored) {
094: }
095: FS.closeFile(infile);
096: FS.setBasedir("y");
097: FS.closeFiles();
098: FS.setBasedir(JMeterUtils.getProperty("user.dir"));
099: }
100: }
|