KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > configuration > ConfigurationStoreUtilTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.system.configuration;
18
19 import java.io.File JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.FileWriter JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStreamWriter JavaDoc;
24
25 import junit.framework.TestCase;
26
27 /**
28  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
29  */

30 public class ConfigurationStoreUtilTest extends TestCase {
31     private File JavaDoc basedir = new File JavaDoc(System.getProperty("basedir"));
32     private File JavaDoc testFile;
33     private static final String JavaDoc BAD_SUM = "Stinky Cheese";
34     private File JavaDoc sumFile;
35
36     public void testChecksum() throws IOException JavaDoc {
37         String JavaDoc actualChecksum = ConfigurationStoreUtil.getActualChecksum(testFile);
38         assertNotNull(actualChecksum);
39
40         // we haven't writen the checksum yet so it should be null
41
String JavaDoc expectedChecksum = ConfigurationStoreUtil.getExpectedChecksum(testFile);
42         assertNull(expectedChecksum);
43
44         // write the sum file
45
ConfigurationStoreUtil.writeChecksumFor(testFile);
46
47         // get the new sum
48
expectedChecksum = ConfigurationStoreUtil.getExpectedChecksum(testFile);
49         assertNotNull(expectedChecksum);
50
51         // should be the same
52
assertEquals(expectedChecksum, actualChecksum);
53
54         assertTrue(ConfigurationStoreUtil.verifyChecksum(testFile));
55     }
56
57     public void testBadChecksum() throws IOException JavaDoc {
58         String JavaDoc actualChecksum = ConfigurationStoreUtil.getActualChecksum(testFile);
59         assertNotNull(actualChecksum);
60
61         // we haven't writen the checksum yet so it should be null
62
String JavaDoc expectedChecksum = ConfigurationStoreUtil.getExpectedChecksum(testFile);
63         assertNull(expectedChecksum);
64
65         // write the bad sum file
66
FileWriter JavaDoc writer = new FileWriter JavaDoc(sumFile);
67         writer.write(BAD_SUM);
68         writer.close();
69
70         // get the new sum
71
expectedChecksum = ConfigurationStoreUtil.getExpectedChecksum(testFile);
72         assertEquals(BAD_SUM, expectedChecksum);
73
74         // should not be the same
75
assertFalse(expectedChecksum.equals(actualChecksum));
76
77         assertFalse(ConfigurationStoreUtil.verifyChecksum(testFile));
78     }
79
80     public void testEmptyFile() throws IOException JavaDoc {
81         testFile.delete();
82         testFile.createNewFile();
83
84         String JavaDoc actualChecksum = ConfigurationStoreUtil.getActualChecksum(testFile);
85         assertNotNull(actualChecksum);
86
87
88         // we haven't writen the checksum yet so it should be null
89
String JavaDoc expectedChecksum = ConfigurationStoreUtil.getExpectedChecksum(testFile);
90         assertNull(expectedChecksum);
91
92         // create an empty sum file
93
sumFile.createNewFile();
94
95         expectedChecksum = ConfigurationStoreUtil.getExpectedChecksum(testFile);
96         assertNull(expectedChecksum);
97     }
98
99     public void testOutputStreamChecksum() throws Exception JavaDoc {
100         testFile.delete();
101         ConfigurationStoreUtil.ChecksumOutputStream out = new ConfigurationStoreUtil.ChecksumOutputStream(new FileOutputStream JavaDoc(testFile));
102         OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc(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 JavaDoc actualChecksum = out.getChecksum();
121         String JavaDoc expectedChecksum = ConfigurationStoreUtil.getActualChecksum(testFile);
122         assertEquals(expectedChecksum, actualChecksum);
123     }
124
125     protected void setUp() throws Exception JavaDoc {
126         super.setUp();
127         testFile = new File JavaDoc(basedir, "target/checksumTest/test.data");
128         testFile.getParentFile().mkdirs();
129         FileWriter JavaDoc writer = new FileWriter JavaDoc(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 JavaDoc(testFile.getParentFile(), testFile.getName() + ".sha1");
145         sumFile.delete();
146     }
147
148     protected void tearDown() throws Exception JavaDoc {
149         testFile.delete();
150         sumFile.delete();
151         super.tearDown();
152     }
153 }
154
Popular Tags