Home | History | Annotate | Download | only in internal
      1 /*******************************************************************************
      2  * Copyright (c) 2009, 2017 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.cli.internal;
     13 
     14 import static org.junit.Assert.assertEquals;
     15 import static org.junit.Assert.assertTrue;
     16 
     17 import java.io.PrintWriter;
     18 import java.io.StringWriter;
     19 import java.io.UnsupportedEncodingException;
     20 import java.net.URLDecoder;
     21 
     22 import org.junit.Before;
     23 
     24 /**
     25  * Base class for command tests.
     26  */
     27 public abstract class CommandTestBase {
     28 
     29 	protected StringWriter out;
     30 	protected StringWriter err;
     31 	protected int result;
     32 
     33 	@Before
     34 	public void before() {
     35 		out = new StringWriter();
     36 		err = new StringWriter();
     37 	}
     38 
     39 	protected int execute(String... args) throws Exception {
     40 		result = new Main(args).execute(new PrintWriter(out),
     41 				new PrintWriter(err));
     42 		return result;
     43 	}
     44 
     45 	protected void assertOk() {
     46 		assertEquals(err.toString(), 0, result);
     47 	}
     48 
     49 	protected void assertFailure() {
     50 		assertEquals(-1, result);
     51 	}
     52 
     53 	protected void assertNoOutput(StringWriter buffer) {
     54 		assertEquals("", buffer.toString());
     55 	}
     56 
     57 	protected void assertContains(String expected, StringWriter buffer) {
     58 		final String content = buffer.toString();
     59 		assertTrue(content, content.contains(expected));
     60 	}
     61 
     62 	protected String getClassPath() {
     63 		final String name = getClass().getName();
     64 		final String res = "/" + name.replace('.', '/') + ".class";
     65 		String loc = getClass().getResource(res).getFile();
     66 		try {
     67 			loc = URLDecoder.decode(loc, "UTF-8");
     68 		} catch (UnsupportedEncodingException e) {
     69 		}
     70 		return loc.substring(0, loc.length() - res.length());
     71 	}
     72 
     73 }
     74