|
| 1 | +package com.fasterxml.uuid.ext; |
| 2 | + |
| 3 | +import org.junit.Rule; |
| 4 | +import org.junit.Test; |
| 5 | +import org.junit.rules.TemporaryFolder; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.FileNotFoundException; |
| 9 | +import java.io.FileWriter; |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.util.UUID; |
| 13 | +import java.util.concurrent.ThreadLocalRandom; |
| 14 | + |
| 15 | +import static com.fasterxml.uuid.ext.LockedFile.READ_ERROR; |
| 16 | +import static org.junit.Assert.*; |
| 17 | + |
| 18 | +public class LockedFileTest |
| 19 | +{ |
| 20 | + @Rule |
| 21 | + public TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 22 | + |
| 23 | + @Test |
| 24 | + public void constructor_givenNull_shouldThrowNullPointerException() throws IOException { |
| 25 | + try { |
| 26 | + new LockedFile(null); |
| 27 | + fail("This should have thrown a null pointer exception"); |
| 28 | + } catch (NullPointerException nullPointerException) { |
| 29 | + ; // good |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void constructor_givenEmptyFile_shouldLeaveFileAsIs() throws IOException { |
| 35 | + // given |
| 36 | + File emptyFile = temporaryFolder.newFile(); |
| 37 | + |
| 38 | + // when |
| 39 | + new LockedFile(emptyFile); |
| 40 | + |
| 41 | + // then |
| 42 | + assertTrue(emptyFile.exists()); |
| 43 | + assertTrue(emptyFile.canRead()); |
| 44 | + assertTrue(emptyFile.canWrite()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void constructor_givenNonExistentFile_shouldCreateANewFile() throws IOException { |
| 49 | + // given |
| 50 | + File blankFile = temporaryFolder.newFile(); |
| 51 | + File nonExistentFile = new File(blankFile + ".nonexistent"); |
| 52 | + |
| 53 | + if (Files.exists(nonExistentFile.toPath())) { |
| 54 | + fail("temp file should not exist"); |
| 55 | + } |
| 56 | + |
| 57 | + // when |
| 58 | + new LockedFile(nonExistentFile); |
| 59 | + |
| 60 | + // then - the nonexistent file now exists? |
| 61 | + assertTrue(Files.exists(nonExistentFile.toPath())); |
| 62 | + assertTrue(nonExistentFile.canRead()); |
| 63 | + assertTrue(nonExistentFile.canWrite()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void constructor_canOnlyTakeAFile_shouldThrowFileNotFoundException() throws IOException { |
| 68 | + // given |
| 69 | + File blankFolder = temporaryFolder.newFolder(); |
| 70 | + |
| 71 | + // when |
| 72 | + try { |
| 73 | + new LockedFile(blankFolder); |
| 74 | + fail("This should not succeed"); |
| 75 | + } catch (FileNotFoundException fileNotFoundException) { |
| 76 | + // then |
| 77 | + assertEquals( |
| 78 | + String.format("%s (Is a directory)", blankFolder.getPath()), |
| 79 | + fileNotFoundException.getMessage() |
| 80 | + ); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void readStamp_givenEmptyFile_shouldReturnREADERROR() throws IOException { |
| 86 | + // given |
| 87 | + File emptyFile = temporaryFolder.newFile(); |
| 88 | + |
| 89 | + // when |
| 90 | + LockedFile lockedFile = new LockedFile(emptyFile); |
| 91 | + long stamp = lockedFile.readStamp(); |
| 92 | + |
| 93 | + // then |
| 94 | + assertEquals(READ_ERROR, stamp); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void readStamp_givenGibberishFile_shouldReturnREADERROR() throws IOException { |
| 99 | + // given |
| 100 | + File gibberishFile = temporaryFolder.newFile(); |
| 101 | + try(FileWriter fileWriter = new FileWriter(gibberishFile)) { |
| 102 | + fileWriter.write(UUID.randomUUID().toString().substring(0, 22)); |
| 103 | + fileWriter.flush(); |
| 104 | + } |
| 105 | + |
| 106 | + assertEquals(22, Files.size(gibberishFile.toPath())); |
| 107 | + |
| 108 | + // when |
| 109 | + LockedFile lockedFile = new LockedFile(gibberishFile); |
| 110 | + long stamp = lockedFile.readStamp(); |
| 111 | + |
| 112 | + // then |
| 113 | + assertEquals(READ_ERROR, stamp); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void readStamp_givenTimestampedFile_shouldReturnValueInside() throws IOException { |
| 118 | + // given |
| 119 | + File timeStampedFile = temporaryFolder.newFile(); |
| 120 | + try(FileWriter fileWriter = new FileWriter(timeStampedFile)) { |
| 121 | + // we are faking the timestamp format |
| 122 | + fileWriter.write("[0x0000000000000001]"); |
| 123 | + fileWriter.flush(); |
| 124 | + } |
| 125 | + |
| 126 | + // when |
| 127 | + LockedFile lockedFile = new LockedFile(timeStampedFile); |
| 128 | + long stamp = lockedFile.readStamp(); |
| 129 | + |
| 130 | + // then |
| 131 | + long expectedTimestamp = 1; |
| 132 | + assertEquals(expectedTimestamp, stamp); |
| 133 | + } |
| 134 | + |
| 135 | + // test for overflows |
| 136 | + @Test |
| 137 | + public void readStamp_givenOverflowedDigitFile_shouldReturnREADERROR() throws IOException { |
| 138 | + // given |
| 139 | + File timeStampedFile = temporaryFolder.newFile(); |
| 140 | + try(FileWriter fileWriter = new FileWriter(timeStampedFile)) { |
| 141 | + // we are faking an overflowed timestamp |
| 142 | + fileWriter.write("[0x10000000000000000]"); |
| 143 | + fileWriter.flush(); |
| 144 | + } |
| 145 | + |
| 146 | + // when |
| 147 | + LockedFile lockedFile = new LockedFile(timeStampedFile); |
| 148 | + long stamp = lockedFile.readStamp(); |
| 149 | + |
| 150 | + // then |
| 151 | + assertEquals(READ_ERROR, stamp); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void readStamp_givenMaxLongFile_shouldReturnLargeTimestamp() throws IOException { |
| 156 | + // given |
| 157 | + File timeStampedFile = temporaryFolder.newFile(); |
| 158 | + try(FileWriter fileWriter = new FileWriter(timeStampedFile)) { |
| 159 | + // we are faking an overflowed timestamp |
| 160 | + fileWriter.write("[0x7fffffffffffffff]"); |
| 161 | + fileWriter.flush(); |
| 162 | + } |
| 163 | + |
| 164 | + // when |
| 165 | + LockedFile lockedFile = new LockedFile(timeStampedFile); |
| 166 | + long stamp = lockedFile.readStamp(); |
| 167 | + |
| 168 | + // then |
| 169 | + assertEquals(Long.MAX_VALUE, stamp); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void writeStamp_givenNegativeTimestamps_shouldThrowIOException() throws IOException { |
| 174 | + // given |
| 175 | + File timeStampedFile = temporaryFolder.newFile(); |
| 176 | + |
| 177 | + // when |
| 178 | + LockedFile lockedFile = new LockedFile(timeStampedFile); |
| 179 | + try { |
| 180 | + lockedFile.writeStamp(Long.MIN_VALUE); |
| 181 | + fail("This should throw an exception"); |
| 182 | + } catch (IOException ioException) { |
| 183 | + // then |
| 184 | + assertTrue(ioException.getMessage().contains("trying to overwrite existing value")); |
| 185 | + assertTrue(ioException.getMessage().contains("with an earlier timestamp")); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + public void writeStamp_givenTimestampedFile_withLowerValue_shouldOverrideValue() throws IOException { |
| 191 | + // given |
| 192 | + String inputValue = "[0x0000000000000000]"; |
| 193 | + long numericInputValue = 0L; |
| 194 | + long newTimestamp = ThreadLocalRandom.current().nextLong(Long.MAX_VALUE); |
| 195 | + |
| 196 | + File timeStampedFile = temporaryFolder.newFile(); |
| 197 | + try(FileWriter fileWriter = new FileWriter(timeStampedFile)) { |
| 198 | + fileWriter.write(inputValue); |
| 199 | + fileWriter.flush(); |
| 200 | + } |
| 201 | + |
| 202 | + // when |
| 203 | + LockedFile lockedFile = new LockedFile(timeStampedFile); |
| 204 | + |
| 205 | + lockedFile.writeStamp(newTimestamp); |
| 206 | + long stamp = lockedFile.readStamp(); |
| 207 | + |
| 208 | + // then |
| 209 | + assertNotEquals(numericInputValue, stamp); |
| 210 | + assertEquals(newTimestamp, stamp); |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + public void writeStamp_givenNewerTimestampedFile_writeNegativeTimestamp_shouldThrowException() throws IOException { |
| 215 | + // given |
| 216 | + String inputValue = "[0x7fffffffffffffff]"; |
| 217 | + long newTimestamp = Long.MIN_VALUE; |
| 218 | + |
| 219 | + File timeStampedFile = temporaryFolder.newFile(); |
| 220 | + try(FileWriter fileWriter = new FileWriter(timeStampedFile)) { |
| 221 | + fileWriter.write(inputValue); |
| 222 | + fileWriter.flush(); |
| 223 | + } |
| 224 | + |
| 225 | + // when |
| 226 | + LockedFile lockedFile = new LockedFile(timeStampedFile); |
| 227 | + |
| 228 | + try { |
| 229 | + lockedFile.writeStamp(newTimestamp); |
| 230 | + fail("This should throw an exception"); |
| 231 | + } catch (IOException ioException) { |
| 232 | + // then |
| 233 | + assertTrue(ioException.getMessage().contains("trying to overwrite existing value")); |
| 234 | + assertTrue(ioException.getMessage().contains("with an earlier timestamp")); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + @Test |
| 239 | + public void writeStamp_givenTimestampedFile_writeSameTimestamp_shouldLeaveFileAlone() throws IOException { |
| 240 | + // given |
| 241 | + String inputValue = "[0x7fffffffffffffff]"; |
| 242 | + long numericInputValue = Long.MAX_VALUE; |
| 243 | + long newTimestamp = Long.MAX_VALUE; |
| 244 | + |
| 245 | + File timeStampedFile = temporaryFolder.newFile(); |
| 246 | + try(FileWriter fileWriter = new FileWriter(timeStampedFile)) { |
| 247 | + fileWriter.write(inputValue); |
| 248 | + fileWriter.flush(); |
| 249 | + } |
| 250 | + |
| 251 | + // when |
| 252 | + LockedFile lockedFile = new LockedFile(timeStampedFile); |
| 253 | + |
| 254 | + lockedFile.writeStamp(newTimestamp); |
| 255 | + long stamp = lockedFile.readStamp(); |
| 256 | + |
| 257 | + // then |
| 258 | + assertEquals(numericInputValue, stamp); |
| 259 | + assertEquals(newTimestamp, stamp); |
| 260 | + } |
| 261 | +} |
0 commit comments