Home | History | Annotate | Download | only in runtime
      1 /*******************************************************************************
      2  * Copyright (c) 2009, 2018 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.core.runtime;
     13 
     14 import static org.junit.Assert.assertEquals;
     15 import static org.junit.Assert.assertFalse;
     16 import static org.junit.Assert.assertTrue;
     17 
     18 import java.io.ByteArrayInputStream;
     19 import java.io.IOException;
     20 import java.io.OutputStream;
     21 
     22 import org.jacoco.core.data.ExecutionDataReader;
     23 import org.jacoco.core.data.ExecutionDataReaderWriterTest;
     24 import org.jacoco.core.data.ExecutionDataWriter;
     25 import org.junit.Before;
     26 import org.junit.Test;
     27 
     28 /**
     29  * Unit tests for {@link ExecutionDataReader} and {@link ExecutionDataWriter}.
     30  * The tests don't care about the written binary format, they just verify
     31  * symmetry.
     32  */
     33 public class RemoteControlReaderWriterTest extends
     34 		ExecutionDataReaderWriterTest {
     35 
     36 	private RemoteControlWriter writer;
     37 
     38 	@Before
     39 	@Override
     40 	public void setup() throws IOException {
     41 		super.setup();
     42 		writer = createWriter(buffer);
     43 	}
     44 
     45 	@Test(expected = IOException.class)
     46 	public void testNoRemoteCommandVisitor() throws IOException {
     47 		writer.visitDumpCommand(false, false);
     48 		final RemoteControlReader reader = createReader();
     49 		reader.read();
     50 	}
     51 
     52 	@Test
     53 	public void testVisitDump1() throws IOException {
     54 		testVisitDump(false, false);
     55 	}
     56 
     57 	@Test
     58 	public void testVisitDump2() throws IOException {
     59 		testVisitDump(false, true);
     60 	}
     61 
     62 	@Test
     63 	public void testVisitDump3() throws IOException {
     64 		testVisitDump(true, false);
     65 	}
     66 
     67 	@Test
     68 	public void testVisitDump4() throws IOException {
     69 		testVisitDump(true, true);
     70 	}
     71 
     72 	private void testVisitDump(boolean doDump, boolean doReset)
     73 			throws IOException {
     74 		writer.visitDumpCommand(doDump, doReset);
     75 		final RemoteControlReader reader = createReader();
     76 		final StringBuilder calls = new StringBuilder();
     77 		reader.setRemoteCommandVisitor(new IRemoteCommandVisitor() {
     78 
     79 			public void visitDumpCommand(boolean dump, boolean reset) {
     80 				calls.append("cmd(" + dump + "," + reset + ")");
     81 			}
     82 		});
     83 		assertFalse(reader.read());
     84 		assertEquals("cmd(" + doDump + "," + doReset + ")", calls.toString());
     85 	}
     86 
     87 	@Test
     88 	public void testSendCmdOk() throws IOException {
     89 		writer.sendCmdOk();
     90 		final RemoteControlReader reader = createReader();
     91 		assertTrue(reader.read());
     92 	}
     93 
     94 	@Override
     95 	protected RemoteControlReader createReader() throws IOException {
     96 		return new RemoteControlReader(new ByteArrayInputStream(
     97 				buffer.toByteArray()));
     98 	}
     99 
    100 	@Override
    101 	protected RemoteControlWriter createWriter(OutputStream out)
    102 			throws IOException {
    103 		return new RemoteControlWriter(out);
    104 	}
    105 
    106 }
    107