Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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.onlab.stc; |
| 17 | |
| 18 | import org.junit.AfterClass; |
| 19 | import org.junit.BeforeClass; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.util.Tools; |
| 22 | |
| 23 | import java.io.File; |
| 24 | import java.io.IOException; |
| 25 | |
| 26 | import static org.junit.Assert.*; |
| 27 | |
| 28 | /** |
| 29 | * Test of the step processor. |
| 30 | */ |
| 31 | public class StepProcessorTest { |
| 32 | |
| 33 | private static final File DIR = new File("/tmp/stc/foo"); |
| 34 | |
| 35 | private final Listener delegate = new Listener(); |
| 36 | |
| 37 | @BeforeClass |
| 38 | public static void setUpClass() { |
| 39 | StepProcessor.launcher = ""; |
| 40 | DIR.mkdirs(); |
| 41 | } |
| 42 | |
| 43 | @AfterClass |
| 44 | public static void tearDownClass() throws IOException { |
| 45 | Tools.removeDirectory(DIR.getPath()); |
| 46 | } |
| 47 | |
| 48 | @Test |
| 49 | public void executed() { |
| 50 | Step step = new Step("foo", "ls /tmp", null); |
| 51 | StepProcessor processor = new StepProcessor(step, false, DIR, delegate); |
| 52 | processor.run(); |
| 53 | assertTrue("should be started", delegate.started); |
| 54 | assertTrue("should have output", delegate.output); |
| 55 | assertTrue("should be stopped", delegate.stopped); |
| 56 | assertEquals("incorrect code", 0, delegate.code); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | @Test |
| 61 | public void skipped() { |
| 62 | Step step = new Step("foo", "ls /tmp", null); |
| 63 | StepProcessor processor = new StepProcessor(step, true, DIR, delegate); |
| 64 | processor.run(); |
| 65 | assertTrue("should be started", delegate.started); |
| 66 | assertFalse("should have output", delegate.output); |
| 67 | assertTrue("should be stopped", delegate.stopped); |
| 68 | assertEquals("incorrect code", -1, delegate.code); |
| 69 | } |
| 70 | |
| 71 | private class Listener implements StepProcessListener { |
| 72 | |
| 73 | private int code = 123; |
| 74 | private boolean started, stopped, output; |
| 75 | |
| 76 | @Override |
| 77 | public void onStart(Step step) { |
| 78 | started = true; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void onCompletion(Step step, int exitCode) { |
| 83 | stopped = true; |
| 84 | this.code = exitCode; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void onOutput(Step step, String line) { |
| 89 | output = true; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | } |