1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
45
46
47
48 public class FileMeterTest {
49
50
51 private static final Random RAN = new Random();
52
53
54
55 private transient FileMeter byteMeter;
56
57
58
59
60 private transient FileMeter kibiByteMeter;
61
62
63
64
65 private transient FileMeter mebiByteMeter;
66
67
68 private transient long size;
69
70
71
72
73
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
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
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
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
124
125
126
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
153
154
155
156
157
158
159
160
161 private static synchronized boolean createStorageVolume(final File pToCreate, final long pLength)
162 throws IOException {
163 try {
164
165 if (pToCreate.exists()) {
166 if (!pToCreate.delete()) {
167 return false;
168 }
169 }
170
171
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 }