View Javadoc

1   // Copyright (C) 2005 - 2012 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.util;
23  
24  import static net.grinder.testutility.FileUtilities.createRandomFile;
25  
26  import java.io.File;
27  import java.io.FileReader;
28  import java.io.IOException;
29  import java.io.Reader;
30  import java.io.Writer;
31  
32  import net.grinder.testutility.AbstractFileTestCase;
33  
34  
35  /**
36   * Unit tests for {@link DelayedCreationFileWriter}.
37   *
38   * @author Philip Aston
39   */
40  public final class TestDelayedCreationFileWriter extends AbstractFileTestCase {
41  
42    public void testConstructor() throws Exception {
43      final File file = new File(getDirectory(), "blah");
44  
45      final Writer w0 = new DelayedCreationFileWriter(file, false);
46      assertFalse(file.exists());
47  
48      createRandomFile(file);
49      assertTrue(file.exists());
50  
51      final Writer w1 = new DelayedCreationFileWriter(file, true);
52      assertTrue(file.exists());
53  
54      // Creating with append=false deletes existing file.
55      final Writer w2 = new DelayedCreationFileWriter(file, false);
56      assertFalse(file.exists());
57  
58      w0.close();
59      w1.close();
60      w2.close();
61      assertFalse(file.exists());
62    }
63  
64    public void testWriteAndFlush() throws Exception {
65  
66      final File file = new File(getDirectory(), "blah");
67  
68      final Writer w0 = new DelayedCreationFileWriter(file, false);
69      w0.flush();
70      assertFalse(file.exists());
71  
72      final String string0 = "Those evil chemicals.";
73      final String string1 = "Egg & Chips";
74  
75      w0.write(string0);
76      assertTrue(file.exists());
77  
78      w0.write(string1);
79  
80      w0.flush();
81      w0.close();
82  
83      final String string2 = "The light at the end of the tunnel";
84      final Writer w1 = new DelayedCreationFileWriter(file, true);
85      w1.write(string2);
86      w1.close();
87  
88      final Reader reader = new FileReader(file);
89      final char[] chars = new char[100];
90      final int n = reader.read(chars);
91      assertEquals(string0 + string1 + string2, new String(chars, 0, n));
92      reader.close();
93  
94      final File brokenFile = new File(file, "blah");
95      final Writer w2 = new DelayedCreationFileWriter(brokenFile, false);
96  
97      try {
98        w2.write("");
99        fail("Expected IOException");
100     }
101     catch (IOException e) {
102     }
103 
104     w2.close();
105   }
106 }