1 /*Copyright (C) 2014 Red Hat, Inc. 2 3 This file is part of IcedTea. 4 5 IcedTea is free software; you can redistribute it and/or 6 modify it under the terms of the GNU General Public License as published by 7 the Free Software Foundation, version 2. 8 9 IcedTea is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with IcedTea; see the file COPYING. If not, write to 16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 17 02110-1301 USA. 18 19 Linking this library statically or dynamically with other modules is 20 making a combined work based on this library. Thus, the terms and 21 conditions of the GNU General Public License cover the whole 22 combination. 23 24 As a special exception, the copyright holders of this library give you 25 permission to link this library with independent modules to produce an 26 executable, regardless of the license terms of these independent 27 modules, and to copy and distribute the resulting executable under 28 terms of your choice, provided that you also meet, for each linked 29 independent module, the terms and conditions of the license of that 30 module. An independent module is a module which is not derived from 31 or based on this library. If you modify this library, you may extend 32 this exception to your version of the library, but you are not 33 obligated to do so. If you do not wish to do so, delete this 34 exception statement from your version. 35 */ 36 37 package net.sourceforge.jnlp.util; 38 39 import static org.junit.Assert.assertFalse; 40 import static org.junit.Assert.assertTrue; 41 42 import java.io.File; 43 import java.io.FileNotFoundException; 44 import java.util.Arrays; 45 46 import org.junit.After; 47 import org.junit.Before; 48 import org.junit.Test; 49 50 public class MD5SumWatcherTest { 51 52 private File file; 53 private MD5SumWatcher watcher; 54 55 @Before createNewFile()56 public void createNewFile() throws Exception { 57 file = File.createTempFile("md5sumwatchertest", "tmp"); 58 file.deleteOnExit(); 59 watcher = new MD5SumWatcher(file); 60 } 61 62 @After deleteTempFile()63 public void deleteTempFile() throws Exception { 64 if (file.exists()) { 65 file.delete(); 66 } 67 } 68 69 @Test testNonExistentFile()70 public void testNonExistentFile() { 71 file.delete(); 72 file.mkdirs(); 73 watcher = new MD5SumWatcher(file); 74 boolean gotException = false; 75 try { 76 watcher.update(); 77 } catch (final Exception e) { 78 gotException = true; 79 assertTrue("Should have received FileNotFoundException", e instanceof FileNotFoundException); 80 } 81 assertTrue("Should have received FileNotFoundException", gotException); 82 } 83 84 @Test testNoFileChangeGivesSameMd5()85 public void testNoFileChangeGivesSameMd5() throws Exception { 86 byte[] sum = watcher.getSum(); 87 byte[] sum2 = watcher.getSum(); 88 assertTrue("MD5 sums should be the same. first: " + Arrays.toString(sum) + ", second: " + Arrays.toString(sum2), 89 Arrays.equals(sum, sum2)); 90 } 91 92 @Test testSavingToFileChangesMd5()93 public void testSavingToFileChangesMd5() throws Exception { 94 byte[] original = watcher.getSum(); 95 FileUtils.saveFile("some test content\n", file); 96 byte[] changed = watcher.getSum(); 97 assertFalse("MD5 sum should have changed, but was constant as " + Arrays.toString(original), 98 Arrays.equals(original, changed)); 99 } 100 101 @Test testUnchangedContentUpdate()102 public void testUnchangedContentUpdate() throws Exception { 103 assertFalse("update() should return false", watcher.update()); 104 } 105 106 @Test testChangedContentUpdate()107 public void testChangedContentUpdate() throws Exception { 108 FileUtils.saveFile("some test content\n", file); 109 final boolean changed = watcher.update(); 110 assertTrue("update() should return true", changed); 111 } 112 113 } 114