1   /*
2    * Copyright 2008 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.ant;
17  
18  import java.io.ByteArrayOutputStream;
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.io.PrintStream;
24  import java.util.zip.GZIPInputStream;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.tools.ant.BuildException;
29  import org.apache.tools.ant.DefaultLogger;
30  import org.apache.tools.ant.Project;
31  import org.apache.tools.ant.ProjectHelper;
32  import org.apache.tools.bzip2.CBZip2InputStream;
33  import org.apache.tools.tar.TarEntry;
34  import org.apache.tools.tar.TarInputStream;
35  import org.vafer.jdeb.ar.ArEntry;
36  import org.vafer.jdeb.ar.ArInputStream;
37  import org.vafer.jdeb.ar.NonClosingInputStream;
38  
39  /**
40   * @author Emmanuel Bourg
41   */
42  public final class DebAntTaskTestCase extends TestCase {
43  
44  	private Project project;
45  
46  	protected void setUp() throws Exception {
47  		project = new Project();
48  		project.setCoreLoader(getClass().getClassLoader());
49  		project.init();
50  
51  		File buildFile = new File("target/test-classes/testbuild.xml");
52  		project.setBaseDir(buildFile.getParentFile());
53  
54  		final ProjectHelper helper = ProjectHelper.getProjectHelper();
55  		helper.parse(project, buildFile);
56  
57  		// remove the package previously build
58  		File deb = new File("target/test.deb");
59  		if (deb.exists()) {
60  			assertTrue("Unable to remove the test archive", deb.delete());
61  		}
62  	}
63  
64  	public void testMissingControl() {
65  		try {
66  			project.executeTarget("missing-control");
67  			fail("No exception thrown");
68  		} catch (BuildException e) {
69  			// expected
70  		}
71  	}
72  
73  	public void testInvalidControl() {
74  		try {
75  			project.executeTarget("invalid-control");
76  			fail("No exception thrown");
77  		} catch (BuildException e) {
78  			// expected
79  		}
80  	}
81  
82  	public void testMissingDestFile() {
83  		try {
84  			project.executeTarget("missing-destfile");
85  			fail("No exception thrown");
86  		} catch (BuildException e) {
87  			// expected
88  		}
89  	}
90  
91  	public void testEmptyPackage() {
92  		try {
93  			project.executeTarget("empty-package");
94  			fail("No exception thrown");
95  		} catch (BuildException e) {
96  			// expected
97  		}
98  	}
99  
100 	public void testPackageWithArchive() {
101 		project.executeTarget("with-archive");
102 
103 		assertTrue("package not build", new File("target/test-classes/test.deb").exists());
104 	}
105 
106 	public void testPackageWithMissingArchive() {
107 		try {
108 			project.executeTarget("with-missing-archive");
109 			fail("No exception thrown");
110 		} catch (BuildException e) {
111 			// expected
112 		}
113 	}
114 
115 	public void testPackageWithDirectory() {
116 		project.executeTarget("with-directory");
117 
118 		assertTrue("package not build", new File("target/test-classes/test.deb").exists());
119 	}
120 
121 	public void testPackageWithMissingDirectory() {
122 		try {
123 			project.executeTarget("with-missing-directory");
124 			fail("No exception thrown");
125 		} catch (BuildException e) {
126 			// expected
127 		}
128 	}
129 
130 	/**
131 	 * Redirects the Ant output to the specified stream.
132 	 */
133 	private void redirectOutput(OutputStream out) {
134 		DefaultLogger logger = new DefaultLogger();
135 		logger.setOutputPrintStream(new PrintStream(out));
136 		logger.setMessageOutputLevel(Project.MSG_INFO);
137 		project.addBuildListener(logger);
138 	}
139 
140 	public void testVerboseEnabled() {
141 		ByteArrayOutputStream out = new ByteArrayOutputStream();
142 		redirectOutput(out);
143 
144 		project.executeTarget("verbose-enabled");
145 
146 		assertTrue(out.toString().indexOf("Total size") != -1);
147 	}
148 
149 	public void testVerboseDisabled() {
150 		ByteArrayOutputStream out = new ByteArrayOutputStream();
151 		redirectOutput(out);
152 
153 		project.executeTarget("verbose-disabled");
154 
155 		assertTrue(out.toString().indexOf("Total size") == -1);
156 	}
157 
158 	public void testFileSet() {
159 		project.executeTarget("fileset");
160 
161 		assertTrue("package not build", new File("target/test-classes/test.deb").exists());
162 	}
163 
164 	public void testTarFileSet() throws Exception {
165 		project.executeTarget("tarfileset");
166 
167 		File deb = new File("target/test-classes/test.deb");
168 		assertTrue("package not build", deb.exists());
169 
170 		ArInputStream in = new ArInputStream(new FileInputStream(deb));
171 		ArEntry entry;
172 		while ((entry = in.getNextEntry()) != null) {
173 			if (entry.getName().equals("data.tar.gz")) {
174 				TarInputStream tar = new TarInputStream(new GZIPInputStream(new NonClosingInputStream(in)));
175 				TarEntry tarentry;
176 				while ((tarentry = tar.getNextEntry()) != null) {
177 					assertTrue("prefix", tarentry.getName().startsWith("/foo/"));
178 					if (tarentry.isDirectory()) {
179 						assertEquals("directory mode (" + tarentry.getName() + ")", 040700, tarentry.getMode());
180 					} else {
181 						assertEquals("file mode (" + tarentry.getName() + ")", 0100600, tarentry.getMode());
182 					}
183 					assertEquals("user", "ebourg", tarentry.getUserName());
184 					assertEquals("group", "ebourg", tarentry.getGroupName());
185 				}
186 				tar.close();
187 			} else {
188 				// skip to the next entry
189 				long skip = entry.getLength(); 
190 				while(skip > 0) {
191 					long skipped = in.skip(skip); 
192 					if (skipped == -1) {
193 						throw new IOException("Failed to skip");
194 					}
195 					skip -= skipped;
196 				}
197 			}
198 		}
199 		in.close();
200 	}
201 
202 	public void testUnkownCompression() throws Exception {
203 		try {
204 			project.executeTarget("unknown-compression");
205 			fail("No exception thrown");
206 		} catch (BuildException e) {
207 			// expected
208 		}
209 	}
210 
211 	public void testBZip2Compression() throws Exception {
212 		project.executeTarget("bzip2-compression");
213 
214 		File deb = new File("target/test-classes/test.deb");
215 		assertTrue("package not build", deb.exists());
216 
217 		boolean found = false;
218 
219 		ArInputStream in = new ArInputStream(new FileInputStream(deb));
220 		ArEntry entry;
221 		while ((entry = in.getNextEntry()) != null) {
222 			if (entry.getName().equals("data.tar.bz2")) {
223 				found = true;
224 
225 				assertEquals("header 0", (byte) 'B', in.read());
226 				assertEquals("header 1", (byte) 'Z', in.read());
227 
228 				TarInputStream tar = new TarInputStream(new CBZip2InputStream(in));
229 				while ((tar.getNextEntry()) != null);
230 				tar.close();
231 				break;
232 			} else {
233 				// skip to the next entry
234 				long skip = entry.getLength(); 
235 				while(skip > 0) {
236 					long skipped = in.skip(skip); 
237 					if (skipped == -1) {
238 						throw new IOException("Failed to skip");
239 					}
240 					skip -= skipped;
241 				}
242 			}
243 		}
244 		in.close();
245 
246 		assertTrue("bz2 file not found", found);
247 	}
248 
249 	public void testNoCompression() throws Exception {
250 		project.executeTarget("no-compression");
251 
252 		File deb = new File("target/test-classes/test.deb");
253 		assertTrue("package not build", deb.exists());
254 
255 		boolean found = false;
256 
257 		ArInputStream in = new ArInputStream(new FileInputStream(deb));
258 		ArEntry entry;
259 		while ((entry = in.getNextEntry()) != null) {
260 			if (entry.getName().equals("data.tar")) {
261 				found = true;
262 
263 				TarInputStream tar = new TarInputStream(new NonClosingInputStream(in));
264 				while ((tar.getNextEntry()) != null);
265 				tar.close();
266 			} else {
267 				// skip to the next entry
268 				long skip = entry.getLength(); 
269 				while(skip > 0) {
270 					long skipped = in.skip(skip); 
271 					if (skipped == -1) {
272 						throw new IOException("Failed to skip");
273 					}
274 					skip -= skipped;
275 				}
276 			}
277 		}
278 		in.close();
279 
280 		assertTrue("tar file not found", found);
281 	}
282 }