| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2008 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.tools; |
| 17 | |
| 18 | import java.io.File; |
| 19 | import java.io.IOException; |
| 20 | import java.util.Arrays; |
| 21 | import java.util.LinkedList; |
| 22 | import java.util.List; |
| 23 | |
| 24 | import junit.framework.TestCase; |
| 25 | import net.sf.jomic.common.Settings; |
| 26 | import net.sf.wraplog.Logger; |
| 27 | |
| 28 | /** |
| 29 | * TestCase for ConsoleTools. |
| 30 | * |
| 31 | * @author Thomas Aglassinger |
| 32 | */ |
| 33 | public class ConsoleToolsTest extends TestCase implements ConsoleOutputListener |
| 34 | { |
| 35 | private static Logger logger = Logger.getLogger(ConsoleToolsTest.class); |
| 36 | private ConsoleTools consoleTools; |
| 37 | private StringTools stringTools; |
| 38 | private SystemTools systemTools; |
| 39 | private File tempDir; |
| 40 | private TestTools testTools; |
| 41 | private String unrarCommand; |
| 42 | |
| 43 | protected void setUp() |
| 44 | throws Exception { |
| 45 | super.setUp(); |
| 46 | testTools = TestTools.instance(); |
| 47 | consoleTools = ConsoleTools.instance(); |
| 48 | stringTools = StringTools.instance(); |
| 49 | systemTools = SystemTools.instance(); |
| 50 | tempDir = new File(System.getProperty("java.io.tmpdir")); |
| 51 | assertNotNull("tempDir", tempDir); |
| 52 | unrarCommand = Settings.instance().getUnrarCommand(); |
| 53 | } |
| 54 | |
| 55 | public void lineWritten(Process process, String line, int streamType) { |
| 56 | assert process != null; |
| 57 | assert line != null; |
| 58 | assert (streamType == STREAM_TYPE_ERR) || (streamType == STREAM_TYPE_OUT) : "streamType = " + streamType; |
| 59 | |
| 60 | if (logger.isDebugEnabled()) { |
| 61 | logger.debug("line written: " + line); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | public void testAsVersionNumber() { |
| 66 | assertEquals("2.3", consoleTools.asVersionNumber(2.3f)); |
| 67 | } |
| 68 | |
| 69 | public void testFailed() |
| 70 | throws IOException, InterruptedException { |
| 71 | try { |
| 72 | showDirectoryContents(1); |
| 73 | fail("ConsoleIOException expected"); |
| 74 | } catch (ConsoleIOException expectedError) { |
| 75 | if (logger.isInfoEnabled()) { |
| 76 | logger.info("command failed as expected", expectedError); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public void testGetVersion() { |
| 82 | testGetVersion(1, new String[]{"1.0"}); |
| 83 | testGetVersion(2.1f, new String[]{"hugo 2.1"}); |
| 84 | testGetVersion(2.2f, new String[]{"hugo 2.2 and some text"}); |
| 85 | testGetVersion(3.0f, new String[]{"title", "hugo 3.0 and some text"}); |
| 86 | testGetVersion(3.1f, new String[]{"title", "hugo 3.1 and some text", "some", "more", "lines"}); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Run command to show a directory. |
| 91 | */ |
| 92 | public void testShowDirectoryContents() |
| 93 | throws IOException, InterruptedException { |
| 94 | List output = showDirectoryContents(0); |
| 95 | |
| 96 | assertHasOutput(output); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Runs unrar without any arguments to obtain the help text. |
| 101 | */ |
| 102 | public void testUnrarHelp() |
| 103 | throws IOException, InterruptedException { |
| 104 | List output = consoleTools.run(new String[]{unrarCommand}, tempDir, 0, this); |
| 105 | |
| 106 | assertHasOutput(output); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Attempts to unrar a zip archive. |
| 111 | */ |
| 112 | public void testUnrarZip() |
| 113 | throws IOException, InterruptedException { |
| 114 | File zipFile = testTools.getTestComicFile(); |
| 115 | List output = consoleTools.run(new String[]{unrarCommand, "l", zipFile.getAbsolutePath()}, tempDir, 0, this); |
| 116 | |
| 117 | assertHasOutput(output); |
| 118 | |
| 119 | String lastLine = (String) output.get(output.size() - 1); |
| 120 | |
| 121 | if (logger.isInfoEnabled()) { |
| 122 | logger.info("as expected, cannot unrar zip archive: " + lastLine); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | private void assertHasOutput(List output) { |
| 127 | int outputSize = output.size(); |
| 128 | |
| 129 | assertTrue("outputSize must be > 0 but is " + outputSize, outputSize > 0); |
| 130 | } |
| 131 | |
| 132 | private List showDirectoryContents(int expectedExitCode) |
| 133 | throws IOException, InterruptedException { |
| 134 | List result; |
| 135 | |
| 136 | if (systemTools.isMacOSX()) { |
| 137 | result = consoleTools.run(new String[]{"ls", "-la"}, tempDir, expectedExitCode, this); |
| 138 | } else if (systemTools.isWindows()) { |
| 139 | result = consoleTools.run(new String[]{"dir"}, tempDir, expectedExitCode, this); |
| 140 | } else { |
| 141 | String platform = System.getProperty("os.name"); |
| 142 | String message = "skipped to show directory contents because it is unclear which command the platform " |
| 143 | + stringTools.sourced(platform) + " needs"; |
| 144 | |
| 145 | logger.warn(message); |
| 146 | result = new LinkedList(); |
| 147 | result.add(message); |
| 148 | } |
| 149 | return result; |
| 150 | } |
| 151 | |
| 152 | private void testGetVersion(float expected, String[] outLines) { |
| 153 | float actual = consoleTools.getVersion(Arrays.asList(outLines)); |
| 154 | |
| 155 | assertEquals(expected, actual, 0.001); |
| 156 | } |
| 157 | } |