KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > bpe > util > FileSystemJarInputStreamTest


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.servicemix.bpe.util;
18
19 import java.io.BufferedOutputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.OutputStreamWriter JavaDoc;
24 import java.io.Writer JavaDoc;
25 import java.util.jar.JarEntry JavaDoc;
26 import java.util.jar.JarInputStream JavaDoc;
27
28 import org.apache.servicemix.bpe.util.FileSystemJarInputStream;
29
30 import junit.framework.TestCase;
31
32 public class FileSystemJarInputStreamTest extends TestCase {
33
34     private static final int BUFFER = 2048;
35
36     public void testInputStream() throws Exception JavaDoc {
37         File JavaDoc f = new File JavaDoc("target/test-data");
38         f.mkdirs();
39         Writer JavaDoc w = new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(new File JavaDoc(f, "test.txt")));
40         w.write("<hello>world</hello>");
41         w.close();
42         
43         FileSystemJarInputStream fsjis = new FileSystemJarInputStream(f);
44         JarInputStream JavaDoc jis = new JarInputStream JavaDoc(fsjis);
45
46         JarEntry JavaDoc entry = jis.getNextJarEntry();
47         assertNotNull(entry);
48         assertEquals("test.txt", entry.getName());
49
50         // Copy data from jar file into byte array
51
BufferedOutputStream JavaDoc dest = null;
52         ByteArrayOutputStream JavaDoc baos = null;
53         int count; // buffer counter
54
byte data[] = new byte[BUFFER];
55         baos = new ByteArrayOutputStream JavaDoc();
56         dest = new BufferedOutputStream JavaDoc(baos, BUFFER);
57         while ((count = jis.read(data, 0, BUFFER)) != -1) {
58             dest.write(data, 0, count);
59         }
60         dest.close();
61         System.out.println(entry.getName() + ": " + baos.toString());
62         
63         assertEquals("<hello>world</hello>", baos.toString());
64     }
65
66 }
67
Popular Tags