1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.vafer.jdeb;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.util.HashSet;
21  import java.util.Set;
22  import java.util.zip.GZIPInputStream;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.tools.tar.TarEntry;
27  import org.apache.tools.tar.TarInputStream;
28  import org.vafer.jdeb.ar.ArEntry;
29  import org.vafer.jdeb.ar.ArInputStream;
30  import org.vafer.jdeb.ar.NonClosingInputStream;
31  import org.vafer.jdeb.descriptors.PackageDescriptor;
32  import org.vafer.jdeb.producers.DataProducerArchive;
33  import org.vafer.jdeb.producers.DataProducerDirectory;
34  
35  public final class DataProducerTestCase extends TestCase {
36  
37  	public void testCreation() throws Exception {
38  
39  		final Processor processor = new Processor(new Console() {
40  			public void println(String s) {
41  			}
42  		}, null);
43  		
44  		final File control = new File(getClass().getResource("deb/control/control").toURI());
45  		final File archive1 = new File(getClass().getResource("deb/data.tgz").toURI());
46  		final File archive2 = new File(getClass().getResource("deb/data.tar.bz2").toURI());
47  		final File directory = new File(getClass().getResource("deb/data").toURI());
48  		
49  		final DataProducer[] data = new DataProducer[] {
50  				new DataProducerArchive(archive1, null, null, null),
51  				new DataProducerArchive(archive2, null, null, null),
52  				new DataProducerDirectory(directory, null, new String[] { "**/.svn/**" }, null)
53  		};
54  		
55  		final File deb = File.createTempFile("jdeb", ".deb");
56  		
57  		final PackageDescriptor packageDescriptor = processor.createDeb(new File[] { control }, data, deb, "gzip");
58  		
59  		assertTrue(packageDescriptor.isValid());
60  		
61  		final Set filesInDeb = new HashSet();
62  
63  		final ArInputStream ar = new ArInputStream(new FileInputStream(deb));
64  		while(true) {
65  			final ArEntry arEntry = ar.getNextEntry();
66  			if (arEntry == null) {
67  				break;
68  			}
69  			
70  			if ("data.tar.gz".equals(arEntry.getName())) {
71  				
72  				final TarInputStream tar = new TarInputStream(new GZIPInputStream(new NonClosingInputStream(ar)));
73  				
74  				while(true) {
75  					final TarEntry tarEntry = tar.getNextEntry();
76  					if (tarEntry == null) {
77  						break;
78  					}
79  					
80  					filesInDeb.add(tarEntry.getName());
81  				}
82  				
83  				tar.close();
84  				break;
85  			}
86  			for (int i = 0; i < arEntry.getLength(); i++) {
87  				ar.read();
88  			}
89  		}
90  
91  		ar.close();
92  		
93  		assertTrue("" + filesInDeb, filesInDeb.contains("/test/testfile"));
94  		assertTrue("" + filesInDeb, filesInDeb.contains("/test/testfile2"));
95  		assertTrue("" + filesInDeb, filesInDeb.contains("/test/testfile3"));
96  
97  		assertTrue("Cannot delete the file " + deb, deb.delete());
98  	}
99  }