• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2009, 2015 Mountainminds GmbH & Co. KG and Contributors
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *    Marc R. Hoffmann - initial API and implementation
10  *
11  *******************************************************************************/
12 package org.jacoco.agent;
13 
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16 
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.jar.Manifest;
22 import java.util.zip.ZipEntry;
23 import java.util.zip.ZipInputStream;
24 
25 import org.junit.After;
26 import org.junit.Test;
27 
28 /**
29  * Unit tests for {@link AgentJar}.
30  */
31 public class AgentJarTest {
32 
33 	private File file;
34 
35 	@After
teardown()36 	public void teardown() {
37 		if (file != null) {
38 			file.delete();
39 		}
40 	}
41 
42 	@Test
testGetResource()43 	public void testGetResource() throws IOException {
44 		final InputStream in = AgentJar.getResource().openStream();
45 		assertAgentContents(in);
46 	}
47 
48 	@Test
testGetResourceAsStream()49 	public void testGetResourceAsStream() throws IOException {
50 		final InputStream in = AgentJar.getResourceAsStream();
51 		assertAgentContents(in);
52 	}
53 
54 	@Test
testExtractTo()55 	public void testExtractTo() throws IOException {
56 		file = File.createTempFile("agent", ".jar");
57 		AgentJar.extractTo(file);
58 		assertAgentContents(new FileInputStream(file));
59 	}
60 
61 	@Test(expected = IOException.class)
testExtractToNegative()62 	public void testExtractToNegative() throws IOException {
63 		file = File.createTempFile("folder", null);
64 		file.delete();
65 		file.mkdirs();
66 		AgentJar.extractTo(file);
67 	}
68 
69 	@Test
testExtractToTempLocation()70 	public void testExtractToTempLocation() throws IOException {
71 		file = AgentJar.extractToTempLocation();
72 		assertAgentContents(new FileInputStream(file));
73 		file.delete();
74 	}
75 
assertAgentContents(InputStream in)76 	private void assertAgentContents(InputStream in) throws IOException {
77 		final ZipInputStream zip = new ZipInputStream(in);
78 		while (true) {
79 			final ZipEntry entry = zip.getNextEntry();
80 			assertNotNull("Manifest not found.", entry);
81 			if ("META-INF/MANIFEST.MF".equals(entry.getName())) {
82 				final Manifest manifest = new Manifest(zip);
83 				assertEquals("JaCoCo Java Agent", manifest.getMainAttributes()
84 						.getValue("Implementation-Title"));
85 				in.close();
86 				break;
87 			}
88 		}
89 	}
90 
91 }
92