1 17 package org.apache.geronimo.system.configuration; 18 19 import java.io.File ; 20 import java.io.FileOutputStream ; 21 import java.io.FileWriter ; 22 import java.io.IOException ; 23 import java.io.OutputStreamWriter ; 24 25 import junit.framework.TestCase; 26 27 30 public class ConfigurationStoreUtilTest extends TestCase { 31 private File basedir = new File (System.getProperty("basedir")); 32 private File testFile; 33 private static final String BAD_SUM = "Stinky Cheese"; 34 private File sumFile; 35 36 public void testChecksum() throws IOException { 37 String actualChecksum = ConfigurationStoreUtil.getActualChecksum(testFile); 38 assertNotNull(actualChecksum); 39 40 String expectedChecksum = ConfigurationStoreUtil.getExpectedChecksum(testFile); 42 assertNull(expectedChecksum); 43 44 ConfigurationStoreUtil.writeChecksumFor(testFile); 46 47 expectedChecksum = ConfigurationStoreUtil.getExpectedChecksum(testFile); 49 assertNotNull(expectedChecksum); 50 51 assertEquals(expectedChecksum, actualChecksum); 53 54 assertTrue(ConfigurationStoreUtil.verifyChecksum(testFile)); 55 } 56 57 public void testBadChecksum() throws IOException { 58 String actualChecksum = ConfigurationStoreUtil.getActualChecksum(testFile); 59 assertNotNull(actualChecksum); 60 61 String expectedChecksum = ConfigurationStoreUtil.getExpectedChecksum(testFile); 63 assertNull(expectedChecksum); 64 65 FileWriter writer = new FileWriter (sumFile); 67 writer.write(BAD_SUM); 68 writer.close(); 69 70 expectedChecksum = ConfigurationStoreUtil.getExpectedChecksum(testFile); 72 assertEquals(BAD_SUM, expectedChecksum); 73 74 assertFalse(expectedChecksum.equals(actualChecksum)); 76 77 assertFalse(ConfigurationStoreUtil.verifyChecksum(testFile)); 78 } 79 80 public void testEmptyFile() throws IOException { 81 testFile.delete(); 82 testFile.createNewFile(); 83 84 String actualChecksum = ConfigurationStoreUtil.getActualChecksum(testFile); 85 assertNotNull(actualChecksum); 86 87 88 String expectedChecksum = ConfigurationStoreUtil.getExpectedChecksum(testFile); 90 assertNull(expectedChecksum); 91 92 sumFile.createNewFile(); 94 95 expectedChecksum = ConfigurationStoreUtil.getExpectedChecksum(testFile); 96 assertNull(expectedChecksum); 97 } 98 99 public void testOutputStreamChecksum() throws Exception { 100 testFile.delete(); 101 ConfigurationStoreUtil.ChecksumOutputStream out = new ConfigurationStoreUtil.ChecksumOutputStream(new FileOutputStream (testFile)); 102 OutputStreamWriter writer = new OutputStreamWriter (out); 103 writer.write("cvmnxc,vmnx,cmvn,xmcvlsjnv,mcnvjshfgmsnfvoiwrhjfjlnvkjhnfgornfgonviohjfowehldnf\n"); 104 writer.write("uncwcdwncicjevhinfmcnrfviefjnvun4d49jf93efv78y4bhc3hf3jdf83hf8ejkdhyfuh9iuwdhfw\n"); 105 writer.write("cvmnxc,vmnx,cmvn,xmcvlsjnv,mcnvjshfgmsnfvoiwrhjfjlnvkjhnfgornfgonviohjfowehldnf\n"); 106 writer.write("uncwcdwncicjevhinfmcnrfviefjnvun4d49jf93efv78y4bhc3hf3jdf83hf8ejkdhyfuh9iuwdhfw\n"); 107 writer.write("cvmnxc,vmnx,cmvn,xmcvlsjnv,mcnvjshfgmsnfvoiwrhjfjlnvkjhnfgornfgonviohjfowehldnf\n"); 108 writer.write("uncwcdwncicjevhinfmcnrfviefjnvun4d49jf93efv78y4bhc3hf3jdf83hf8ejkdhyfuh9iuwdhfw\n"); 109 writer.write("cvmnxc,vmnx,cmvn,xmcvlsjnv,mcnvjshfgmsnfvoiwrhjfjlnvkjhnfgornfgonviohjfowehldnf\n"); 110 writer.write("uncwcdwncicjevhinfmcnrfviefjnvun4d49jf93efv78y4bhc3hf3jdf83hf8ejkdhyfuh9iuwdhfw\n"); 111 writer.write("cvmnxc,vmnx,cmvn,xmcvlsjnv,mcnvjshfgmsnfvoiwrhjfjlnvkjhnfgornfgonviohjfowehldnf\n"); 112 writer.write("uncwcdwncicjevhinfmcnrfviefjnvun4d49jf93efv78y4bhc3hf3jdf83hf8ejkdhyfuh9iuwdhfw\n"); 113 writer.write("cvmnxc,vmnx,cmvn,xmcvlsjnv,mcnvjshfgmsnfvoiwrhjfjlnvkjhnfgornfgonviohjfowehldnf\n"); 114 writer.write("uncwcdwncicjevhinfmcnrfviefjnvun4d49jf93efv78y4bhc3hf3jdf83hf8ejkdhyfuh9iuwdhfw\n"); 115 writer.write("cvmnxc,vmnx,cmvn,xmcvlsjnv,mcnvjshfgmsnfvoiwrhjfjlnvkjhnfgornfgonviohjfowehldnf\n"); 116 writer.write("uncwcdwncicjevhinfmcnrfviefjnvun4d49jf93efv78y4bhc3hf3jdf83hf8ejkdhyfuh9iuwdhfw\n"); 117 writer.write("end\n"); 118 writer.close(); 119 120 String actualChecksum = out.getChecksum(); 121 String expectedChecksum = ConfigurationStoreUtil.getActualChecksum(testFile); 122 assertEquals(expectedChecksum, actualChecksum); 123 } 124 125 protected void setUp() throws Exception { 126 super.setUp(); 127 testFile = new File (basedir, "target/checksumTest/test.data"); 128 testFile.getParentFile().mkdirs(); 129 FileWriter writer = new FileWriter (testFile); 130 writer.write("lflkfjkljkldfaskljsadflkjasdflweoiurhlmvniouwehnflikmnfubhgkajnbfgk;ausuhfoubhr\n"); 131 writer.write("uweyrueruewrncihweruhnwjnfduhfrwejbnruhnfuhfjnceucnfierouhfljkdnciuehfinbvcusdu\n"); 132 writer.write("lflkfjkljkldfaskljsadflkjasdflweoiurhlmvniouwehnflikmnfubhgkajnbfgk;ausuhfoubhr\n"); 133 writer.write("uweyrueruewrncihweruhnwjnfduhfrwejbnruhnfuhfjnceucnfierouhfljkdnciuehfinbvcusdu\n"); 134 writer.write("lflkfjkljkldfaskljsadflkjasdflweoiurhlmvniouwehnflikmnfubhgkajnbfgk;ausuhfoubhr\n"); 135 writer.write("uweyrueruewrncihweruhnwjnfduhfrwejbnruhnfuhfjnceucnfierouhfljkdnciuehfinbvcusdu\n"); 136 writer.write("lflkfjkljkldfaskljsadflkjasdflweoiurhlmvniouwehnflikmnfubhgkajnbfgk;ausuhfoubhr\n"); 137 writer.write("uweyrueruewrncihweruhnwjnfduhfrwejbnruhnfuhfjnceucnfierouhfljkdnciuehfinbvcusdu\n"); 138 writer.write("lflkfjkljkldfaskljsadflkjasdflweoiurhlmvniouwehnflikmnfubhgkajnbfgk;ausuhfoubhr\n"); 139 writer.write("uweyrueruewrncihweruhnwjnfduhfrwejbnruhnfuhfjnceucnfierouhfljkdnciuehfinbvcusdu\n"); 140 writer.write("lflkfjkljkldfaskljsadflkjasdflweoiurhlmvniouwehnflikmnfubhgkajnbfgk;ausuhfoubhr\n"); 141 writer.write("uweyrueruewrncihweruhnwjnfduhfrwejbnruhnfuhfjnceucnfierouhfljkdnciuehfinbvcusdu\n"); 142 writer.write("end\n"); 143 writer.close(); 144 sumFile = new File (testFile.getParentFile(), testFile.getName() + ".sha1"); 145 sumFile.delete(); 146 } 147 148 protected void tearDown() throws Exception { 149 testFile.delete(); 150 sumFile.delete(); 151 super.tearDown(); 152 } 153 } 154 | Popular Tags |