Java tutorial
/** * Copyright 2014-2015 SHAF-WORK * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.shaf.core.process; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.IOException; import java.util.EventListener; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.shaf.core._helper_.DummyCompositeProcess; import org.shaf.core._helper_.DummyDistributedProcess; import org.shaf.core._helper_.DummyFaultyProcess; import org.shaf.core._helper_.DummyLocalProcess; import org.shaf.core._helper_.DummyProcessListener; import org.shaf.core.process.config.ProcessConfiguration; import org.shaf.core.process.handle.ProcessExecException; import org.shaf.core.util.IOUtils; /** * The class {@code ProcessExecutorTest} contains tests for the class * {@link ProcessExecutor}. * * @author Mykola Galushka */ public class ProcessExecutorTest { /** * Perform pre-test initialization. * * @throws Exception * if the initialization fails for some reason */ @Before public void setUp() throws Exception { FileSystem fs = IOUtils.getLocalFileSystem(); Path inputDir = new Path(System.getProperty("java.io.tmpdir"), "some/input"); if (!fs.exists(inputDir)) { fs.mkdirs(inputDir); fs.createNewFile(new Path(inputDir, "part")); } } /** * Perform post-test clean-up. * * @throws Exception * if the clean-up fails for some reason */ @After public void tearDown() throws Exception { FileSystem fs = IOUtils.getLocalFileSystem(); Path inputDir = new Path(System.getProperty("java.io.tmpdir"), "some/input"); if (fs.exists(inputDir)) { fs.delete(inputDir, true); } Path outputDir = new Path(System.getProperty("java.io.tmpdir"), "some/output"); if (fs.exists(outputDir)) { fs.delete(outputDir, true); } } /** * Run the local process execution tests. * * @throws Exception * if the test fails for some reason. */ @Test public void testLocalExecution() throws Exception { // --- Initializes process -------------------------------------------- DummyProcessListener pl = new DummyProcessListener(); ProcessExecutor pe = ProcessExecutor.forProcess(DummyLocalProcess.class, null, new ProcessConfiguration(), new EventListener[] { pl }); assertEquals(DummyLocalProcess.class, pe.getProcessClass()); // --- Test before ---------------------------------------------------- assertArrayEquals(new byte[] { 0, 0 }, pl.getMatrix()); assertFalse(pe.hasFailed()); assertNull(pe.getResult()); // --- Runs process --------------------------------------------------- pe.run(); // --- Test after ----------------------------------------------------- assertArrayEquals(new byte[] { 1, 1 }, pl.getMatrix()); try { pe.getResult(); } catch (NullPointerException e) { e.printStackTrace(); } assertFalse(pe.hasFailed()); assertEquals("some result", pe.getResult()); } /** * Run the distributed process execution tests. * * @throws Exception * if the test fails for some reason. */ @Test public void testDistributedExecution() throws Exception { // --- Initializes process -------------------------------------------- DummyProcessListener pl = new DummyProcessListener(); ProcessExecutor pe = ProcessExecutor.forProcess(DummyDistributedProcess.class, null, new ProcessConfiguration().setForceEmulatorMode(true).setBase(System.getProperty("java.io.tmpdir")) .addInput("some/input").setOutput("some/output"), new EventListener[] { pl }); assertEquals(DummyDistributedProcess.class, pe.getProcessClass()); // --- Test before ---------------------------------------------------- assertArrayEquals(new byte[] { 0, 0 }, pl.getMatrix()); assertFalse(pe.hasFailed()); assertNull(pe.getResult()); // --- Runs process --------------------------------------------------- pe.run(); // --- Test after ----------------------------------------------------- assertArrayEquals(new byte[] { 1, 1 }, pl.getMatrix()); assertFalse(pe.hasFailed()); assertNull(pe.getResult()); } /** * Run the composite process execution tests. * * @throws Exception * if the test fails for some reason. */ @Test public void testCompositeExecution() throws Exception { // --- Initializes process -------------------------------------------- DummyProcessListener pl = new DummyProcessListener(); ProcessExecutor pe = ProcessExecutor.forProcess(DummyCompositeProcess.class, null, new ProcessConfiguration().setForceEmulatorMode(true).setBase(System.getProperty("java.io.tmpdir")) .addInput("some/input").setOutput("some/output"), new EventListener[] { pl }); assertEquals(DummyCompositeProcess.class, pe.getProcessClass()); // --- Test before ---------------------------------------------------- assertArrayEquals(new byte[] { 0, 0 }, pl.getMatrix()); assertFalse(pe.hasFailed()); assertNull(pe.getResult()); // --- Runs process --------------------------------------------------- pe.run(); // --- Test after ----------------------------------------------------- assertArrayEquals(new byte[] { 1, 1 }, pl.getMatrix()); assertFalse(pe.hasFailed()); } /** * Run the faulty process execution tests. * * @throws Exception * if the test fails for some reason. */ @Test public void testFaultyExecution() throws Exception { // --- Initializes process -------------------------------------------- ProcessExecutor pe = ProcessExecutor.forProcess(DummyFaultyProcess.class, null, new ProcessConfiguration(), null); assertEquals(DummyFaultyProcess.class, pe.getProcessClass()); // --- Test before ---------------------------------------------------- assertNull(pe.getResult()); // --- Runs process --------------------------------------------------- pe.run(); // --- Test after ----------------------------------------------------- assertTrue(pe.hasFailed()); try { pe.getResult(); fail(); } catch (Exception exc) { assertEquals(ProcessExecException.class, exc.getClass()); assertEquals(IOException.class, exc.getCause().getClass()); } } /** * Run process interrupt tests. * * @throws Exception * if the test fails for some reason. */ @Test public void testInterrupt() throws Exception { // --- Initializes process -------------------------------------------- ProcessExecutor pe = ProcessExecutor.forProcess(DummyLocalProcess.class, null, new ProcessConfiguration(), null); assertEquals(DummyLocalProcess.class, pe.getProcessClass()); // --- Test before ---------------------------------------------------- assertNull(pe.getResult()); // --- Runs process --------------------------------------------------- pe.interrupt(); // --- Test after ----------------------------------------------------- assertTrue(pe.hasFailed()); try { pe.getResult(); fail(); } catch (Exception exc) { assertEquals(InterruptedException.class, exc.getCause().getClass()); } } }