View Javadoc

1   // Copyright (C) 2004 - 2011 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.FileOutputStream;
28  import java.io.OutputStream;
29  
30  import net.grinder.testutility.AbstractFileTestCase;
31  import net.grinder.testutility.AssertUtilities;
32  import net.grinder.testutility.Serializer;
33  
34  
35  /**
36   * Unit test case for {@link FileContents}.
37   *
38   * @author Philip Aston
39   */
40  public class TestFileContents extends AbstractFileTestCase {
41  
42    public void testConstruction() throws Exception {
43  
44      final String[] files = {
45        "file1",
46        "another/file",
47      };
48  
49      for (int i=0; i<files.length; ++i) {
50        final File relativePath = new File(files[i]);
51        final File fullPath = new File(getDirectory(), relativePath.getPath());
52  
53        fullPath.getParentFile().mkdirs();
54        final OutputStream outputStream = new FileOutputStream(fullPath);
55        final byte[] bytes = new byte[500];
56        s_random.nextBytes(bytes);
57        outputStream.write(bytes);
58        outputStream.close();
59  
60        final FileContents fileContents =
61          new FileContents(getDirectory(), relativePath);
62  
63        assertEquals(relativePath, fileContents.getFilename());
64        AssertUtilities.assertArraysEqual(bytes, fileContents.getContents());
65  
66        final FileContents fileContents2 = Serializer.serialize(fileContents);
67  
68        assertEquals(relativePath, fileContents2.getFilename());
69        AssertUtilities.assertArraysEqual(bytes, fileContents2.getContents());
70  
71        final String s = fileContents.toString();
72        assertTrue(s.indexOf(relativePath.getPath()) >= 0);
73        assertTrue(s.indexOf(Integer.toString(bytes.length)) >= 0);
74      }
75    }
76  
77    public void testBadConstruction() throws Exception {
78  
79      try {
80        new FileContents(getDirectory(), getDirectory());
81        fail("Expected FileContentsException");
82      }
83      catch (FileContents.FileContentsException e) {
84      }
85  
86      try {
87        new FileContents(new File("non existing"), new File("file"));
88        fail("Expected FileContentsException");
89      }
90      catch (FileContents.FileContentsException e) {
91      }
92    }
93  
94    public void testCreate() throws Exception {
95  
96      final String[] files = {
97        "file1",
98        "another/file",
99      };
100 
101     for (int i=0; i<files.length; ++i ) {
102       final File relativePath = new File(files[i]);
103       final File fullPath = new File(getDirectory(), relativePath.getPath());
104 
105       createRandomFile(fullPath);
106 
107       final FileContents fileContents =
108         new FileContents(getDirectory(), relativePath);
109 
110       final File outputDirectory = new File(getDirectory(), "output");
111       outputDirectory.mkdir();
112 
113       fileContents.create(new Directory(outputDirectory));
114 
115       final FileContents fileContents2 =
116         new FileContents(outputDirectory, relativePath);
117 
118       assertEquals(fileContents.getFilename(), fileContents2.getFilename());
119       AssertUtilities.assertArraysEqual(fileContents.getContents(),
120                                         fileContents2.getContents());
121     }
122   }
123 }