[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | package org.apache.hadoop.util; |
---|
| 19 | |
---|
| 20 | import junit.framework.TestCase; |
---|
| 21 | |
---|
| 22 | import java.io.BufferedReader; |
---|
| 23 | import java.io.IOException; |
---|
| 24 | |
---|
| 25 | public class TestShell extends TestCase { |
---|
| 26 | |
---|
| 27 | private static class Command extends Shell { |
---|
| 28 | private int runCount = 0; |
---|
| 29 | |
---|
| 30 | private Command(long interval) { |
---|
| 31 | super(interval); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | protected String[] getExecString() { |
---|
| 35 | return new String[] {"echo", "hello"}; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | protected void parseExecResult(BufferedReader lines) throws IOException { |
---|
| 39 | ++runCount; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | public int getRunCount() { |
---|
| 43 | return runCount; |
---|
| 44 | } |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | public void testInterval() throws IOException { |
---|
| 48 | testInterval(Long.MIN_VALUE / 60000); // test a negative interval |
---|
| 49 | testInterval(0L); // test a zero interval |
---|
| 50 | testInterval(10L); // interval equal to 10mins |
---|
| 51 | testInterval(System.currentTimeMillis() / 60000 + 60); // test a very big interval |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | /** |
---|
| 55 | * Assert that a string has a substring in it |
---|
| 56 | * @param string string to search |
---|
| 57 | * @param search what to search for it |
---|
| 58 | */ |
---|
| 59 | private void assertInString(String string, String search) { |
---|
| 60 | assertNotNull("Empty String", string); |
---|
| 61 | if (!string.contains(search)) { |
---|
| 62 | fail("Did not find \"" + search + "\" in " + string); |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | public void testShellCommandExecutorToString() throws Throwable { |
---|
| 67 | Shell.ShellCommandExecutor sce=new Shell.ShellCommandExecutor( |
---|
| 68 | new String[] { "ls","..","arg 2"}); |
---|
| 69 | String command = sce.toString(); |
---|
| 70 | assertInString(command,"ls"); |
---|
| 71 | assertInString(command, " .. "); |
---|
| 72 | assertInString(command, "\"arg 2\""); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | private void testInterval(long interval) throws IOException { |
---|
| 76 | Command command = new Command(interval); |
---|
| 77 | |
---|
| 78 | command.run(); |
---|
| 79 | assertEquals(1, command.getRunCount()); |
---|
| 80 | |
---|
| 81 | command.run(); |
---|
| 82 | if (interval > 0) { |
---|
| 83 | assertEquals(1, command.getRunCount()); |
---|
| 84 | } else { |
---|
| 85 | assertEquals(2, command.getRunCount()); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | } |
---|