1 | // Jomic - a viewer for comic book archives. |
2 | // Copyright (C) 2004-2011 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 org.apache.commons.logging.Log; |
26 | import org.apache.commons.logging.LogFactory; |
27 | |
28 | /** |
29 | * TestCase for ConsoleTools. |
30 | * |
31 | * @author Thomas Aglassinger |
32 | */ |
33 | public class ConsoleToolsTest extends TestCase implements ConsoleOutputListener |
34 | { |
35 | private static Log logger = LogFactory.getLog(ConsoleToolsTest.class); |
36 | private ConsoleTools consoleTools; |
37 | private StringTools stringTools; |
38 | private SystemTools systemTools; |
39 | private File tempDir; |
40 | |
41 | protected void setUp() |
42 | throws Exception { |
43 | super.setUp(); |
44 | consoleTools = ConsoleTools.instance(); |
45 | stringTools = StringTools.instance(); |
46 | systemTools = SystemTools.instance(); |
47 | tempDir = new File(System.getProperty("java.io.tmpdir")); |
48 | assertNotNull("tempDir", tempDir); |
49 | } |
50 | |
51 | public void lineWritten(Process process, String line, int streamType) { |
52 | assert process != null; |
53 | assert line != null; |
54 | assert (streamType == STREAM_TYPE_ERR) || (streamType == STREAM_TYPE_OUT) : "streamType = " + streamType; |
55 | |
56 | if (logger.isDebugEnabled()) { |
57 | logger.debug("line written: " + line); |
58 | } |
59 | } |
60 | |
61 | public void testAsVersionNumber() { |
62 | assertEquals("2.3", consoleTools.asVersionNumber(2.3f)); |
63 | } |
64 | |
65 | public void testFailed() |
66 | throws IOException, InterruptedException { |
67 | try { |
68 | showDirectoryContents(1); |
69 | fail("ConsoleIOException expected"); |
70 | } catch (ConsoleIOException expectedError) { |
71 | if (logger.isInfoEnabled()) { |
72 | logger.info("command failed as expected", expectedError); |
73 | } |
74 | } |
75 | } |
76 | |
77 | public void testGetVersion() { |
78 | testGetVersion(1, new String[]{"1.0"}); |
79 | testGetVersion(2.1f, new String[]{"hugo 2.1"}); |
80 | testGetVersion(2.2f, new String[]{"hugo 2.2 and some text"}); |
81 | testGetVersion(3.0f, new String[]{"title", "hugo 3.0 and some text"}); |
82 | testGetVersion(3.1f, new String[]{"title", "hugo 3.1 and some text", "some", "more", "lines"}); |
83 | } |
84 | |
85 | /** |
86 | * Run command to show a directory. |
87 | */ |
88 | public void testShowDirectoryContents() |
89 | throws IOException, InterruptedException { |
90 | List output = showDirectoryContents(0); |
91 | |
92 | assertHasOutput(output); |
93 | } |
94 | |
95 | private void assertHasOutput(List output) { |
96 | int outputSize = output.size(); |
97 | |
98 | assertTrue("outputSize must be > 0 but is " + outputSize, outputSize > 0); |
99 | } |
100 | |
101 | private List showDirectoryContents(int expectedExitCode) |
102 | throws IOException, InterruptedException { |
103 | List result; |
104 | |
105 | if (systemTools.isMacOSX()) { |
106 | result = consoleTools.run(new String[]{"ls", "-la"}, tempDir, expectedExitCode, this); |
107 | } else if (systemTools.isWindows()) { |
108 | result = consoleTools.run(new String[]{"dir"}, tempDir, expectedExitCode, this); |
109 | } else { |
110 | String platform = System.getProperty("os.name"); |
111 | String message = "skipped to show directory contents because it is unclear which command the platform " |
112 | + stringTools.sourced(platform) + " needs"; |
113 | |
114 | logger.warn(message); |
115 | result = new LinkedList(); |
116 | result.add(message); |
117 | } |
118 | return result; |
119 | } |
120 | |
121 | private void testGetVersion(float expected, String[] outLines) { |
122 | float actual = consoleTools.getVersion(Arrays.asList(outLines)); |
123 | |
124 | assertEquals(expected, actual, 0.001); |
125 | } |
126 | } |