View Javadoc

1   /**
2    * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    * * Redistributions of source code must retain the above copyright
8    * notice, this list of conditions and the following disclaimer.
9    * * Redistributions in binary form must reproduce the above copyright
10   * notice, this list of conditions and the following disclaimer in the
11   * documentation and/or other materials provided with the distribution.
12   * * Neither the name of the University of Konstanz nor the
13   * names of its contributors may be used to endorse or promote products
14   * derived from this software without specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   */
27  package org.perfidix.meter;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertTrue;
31  
32  import java.io.File;
33  import java.io.FileNotFoundException;
34  import java.io.IOException;
35  import java.io.RandomAccessFile;
36  import java.util.Random;
37  
38  import org.junit.Before;
39  import org.junit.Test;
40  
41  import com.google.common.io.Files;
42  
43  /**
44   * Testcase for FileMeter.
45   * 
46   * @author Sebastian Graf, University of Konstanz
47   */
48  public class FileMeterTest {
49  
50      /** Standard Random Generator. */
51      private static final Random RAN = new Random();
52      /**
53       * Byte meter variable.
54       */
55      private transient FileMeter byteMeter;
56  
57      /**
58       * KibiByte meter variable.
59       */
60      private transient FileMeter kibiByteMeter;
61  
62      /**
63       * MebiByte meter variable.
64       */
65      private transient FileMeter mebiByteMeter;
66  
67      /** Variable to store the size while creation. */
68      private transient long size;
69  
70      /**
71       * Setting up the meter.
72       * 
73       * @throws IOException
74       */
75      @Before
76      public void setUp() throws IOException {
77          final File file = Files.createTempDir();
78          byteMeter = new FileMeter(file, Memory.Byte);
79          kibiByteMeter = new FileMeter(file, Memory.KibiByte);
80          mebiByteMeter = new FileMeter(file, Memory.Mebibyte);
81          size = initializeStorage(file);
82      }
83  
84      /**
85       * Test method for {@link org.perfidix.meter.FileMeter#getValue()}.
86       */
87      @Test
88      public void testGetValue() {
89          final double dataB1 = byteMeter.getValue();
90          final double dataKB1 = kibiByteMeter.getValue();
91          final double dataMB1 = mebiByteMeter.getValue();
92  
93          assertTrue("Data check for byte", dataB1 == size / Memory.Byte.getNumberOfBytes());
94          assertTrue("Data check for KibiByte", dataKB1 == size / Memory.KibiByte.getNumberOfBytes());
95          assertTrue("Data check for MebiByte", dataMB1 == size / Memory.Mebibyte.getNumberOfBytes());
96      }
97  
98      /**
99       * Test method for {@link org.perfidix.meter.FileMeter#getUnit()}.
100      */
101     @Test
102     public void testGetUnit() {
103         assertEquals("Data check for unit for byte", Memory.Byte.getUnit(), byteMeter.getUnit());
104         assertEquals("Data check for unit for kibiByte", Memory.KibiByte.getUnit(), kibiByteMeter.getUnit());
105         assertEquals("Data check for unit for mebiByte", Memory.Mebibyte.getUnit(), mebiByteMeter.getUnit());
106     }
107 
108     /**
109      * Test method for {@link org.perfidix.meter.FileMeter#getUnitDescription()}.
110      */
111     @Test
112     public void testGetDescription() {
113         assertEquals("Data check for describtion for byte", Memory.Byte.getUnitDescription(), byteMeter
114             .getUnitDescription());
115         assertEquals("Data check for describtion for kibiByte", Memory.KibiByte.getUnitDescription(),
116             kibiByteMeter.getUnitDescription());
117         assertEquals("Data check for describtion for mebiByte", Memory.Mebibyte.getUnitDescription(),
118             mebiByteMeter.getUnitDescription());
119 
120     }
121 
122     /**
123      * Method for initializing the storage for testing.
124      * 
125      * @return long regarding the fileserver.
126      * @throws IOException
127      */
128     private long initializeStorage(File toCreate) throws IOException {
129         long size = 0;
130         final double goDownThreshold = 0.6;
131         final double createThreshold = 0.8;
132         final long maxSize = 500;
133         int i = 1;
134         do {
135             toCreate = new File(toCreate, "folder" + i);
136             toCreate.mkdir();
137             int j = 0;
138             do {
139                 File nextFile = new File(toCreate, "file" + j);
140                 long sizeToSet = Math.abs(RAN.nextLong() % maxSize);
141                 createStorageVolume(nextFile, sizeToSet);
142                 size = size + sizeToSet;
143                 j++;
144             } while (RAN.nextDouble() < createThreshold);
145             i++;
146         } while (RAN.nextDouble() < goDownThreshold);
147 
148         return size;
149     }
150 
151     /**
152      * Creating a new file if not existing at the path defined in the config.
153      * Note that it is advised to create the file beforehand.
154      * 
155      * @param pConf
156      *            configuration to be updated
157      * @return true if creation successful, false if file already exists.
158      * @throws IOException
159      *             if anything weird happens
160      */
161     private static synchronized boolean createStorageVolume(final File pToCreate, final long pLength)
162         throws IOException {
163         try {
164             // if file exists, remove it after questioning.
165             if (pToCreate.exists()) {
166                 if (!pToCreate.delete()) {
167                     return false;
168                 }
169             }
170 
171             // create file
172             final File parent = pToCreate.getCanonicalFile().getParentFile();
173             if (!parent.exists() && !parent.mkdirs()) {
174                 throw new FileNotFoundException("Unable to create directory: " + parent.getAbsolutePath());
175             }
176 
177             pToCreate.createNewFile();
178             RandomAccessFile file = new RandomAccessFile(pToCreate, "rw");
179             file.setLength(pLength);
180             file.close();
181             return true;
182         } catch (IOException e) {
183             throw e;
184         }
185 
186     }
187 }