1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * Copyright (C) 2007 University of Waikato |
---|
19 | */ |
---|
20 | |
---|
21 | package weka.core; |
---|
22 | |
---|
23 | import weka.filters.unsupervised.attribute.StringToWordVector; |
---|
24 | |
---|
25 | import junit.framework.Test; |
---|
26 | import junit.framework.TestCase; |
---|
27 | import junit.framework.TestSuite; |
---|
28 | |
---|
29 | /** |
---|
30 | * Tests Utils. Run from the command line with:<p/> |
---|
31 | * java weka.core.UtilsTest |
---|
32 | * |
---|
33 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
34 | * @version $Revision: 1.1 $ |
---|
35 | */ |
---|
36 | public class UtilsTest |
---|
37 | extends TestCase { |
---|
38 | |
---|
39 | /** |
---|
40 | * Constructs the <code>UtilsTest</code>. |
---|
41 | * |
---|
42 | * @param name the name of the test class |
---|
43 | */ |
---|
44 | public UtilsTest(String name) { |
---|
45 | super(name); |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * Called by JUnit before each test method. |
---|
50 | * |
---|
51 | * @throws Exception if an error occurs |
---|
52 | */ |
---|
53 | protected void setUp() throws Exception { |
---|
54 | super.setUp(); |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * Called by JUnit after each test method |
---|
59 | * |
---|
60 | * @throws Exception if an error occurs |
---|
61 | */ |
---|
62 | protected void tearDown() throws Exception { |
---|
63 | super.tearDown(); |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * tests splitOptions and joinOptions |
---|
68 | * |
---|
69 | * @see Utils#splitOptions(String) |
---|
70 | * @see Utils#joinOptions(String[]) |
---|
71 | */ |
---|
72 | public void testSplittingAndJoining() { |
---|
73 | String[] options; |
---|
74 | String[] newOptions; |
---|
75 | String joined; |
---|
76 | int i; |
---|
77 | |
---|
78 | try { |
---|
79 | options = new StringToWordVector().getOptions(); |
---|
80 | joined = Utils.joinOptions(options); |
---|
81 | newOptions = Utils.splitOptions(joined); |
---|
82 | assertEquals("Same number of options", options.length, newOptions.length); |
---|
83 | for (i = 0; i < options.length; i++) { |
---|
84 | if (!options[i].equals(newOptions[i])) |
---|
85 | fail("Option " + (i+1) + " differs"); |
---|
86 | } |
---|
87 | } |
---|
88 | catch (Exception e) { |
---|
89 | fail("Exception: " + e); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * tests quote and unquote |
---|
95 | * |
---|
96 | * @see Utils#quote(String) |
---|
97 | * @see Utils#unquote(String) |
---|
98 | */ |
---|
99 | public void testQuoting() { |
---|
100 | String input; |
---|
101 | String output; |
---|
102 | |
---|
103 | input = "blahblah"; |
---|
104 | output = Utils.quote(input); |
---|
105 | assertTrue("No quoting necessary", !output.startsWith("'") && !output.endsWith("'")); |
---|
106 | |
---|
107 | input = ""; |
---|
108 | output = Utils.quote(input); |
---|
109 | assertTrue("Empty string quoted", output.startsWith("'") && output.endsWith("'")); |
---|
110 | assertTrue("Empty string restored", input.equals(Utils.unquote(output))); |
---|
111 | |
---|
112 | input = " "; |
---|
113 | output = Utils.quote(input); |
---|
114 | assertTrue("Blank quoted", output.startsWith("'") && output.endsWith("'")); |
---|
115 | assertTrue("Blank restored", input.equals(Utils.unquote(output))); |
---|
116 | |
---|
117 | input = "{"; |
---|
118 | output = Utils.quote(input); |
---|
119 | assertTrue(">" + input + "< quoted", output.startsWith("'") && output.endsWith("'")); |
---|
120 | assertTrue(">" + input + "< restored", input.equals(Utils.unquote(output))); |
---|
121 | |
---|
122 | input = "}"; |
---|
123 | output = Utils.quote(input); |
---|
124 | assertTrue(">" + input + "< quoted", output.startsWith("'") && output.endsWith("'")); |
---|
125 | assertTrue(">" + input + "< restored", input.equals(Utils.unquote(output))); |
---|
126 | |
---|
127 | input = ","; |
---|
128 | output = Utils.quote(input); |
---|
129 | assertTrue(">" + input + "< quoted", output.startsWith("'") && output.endsWith("'")); |
---|
130 | assertTrue(">" + input + "< restored", input.equals(Utils.unquote(output))); |
---|
131 | |
---|
132 | input = "?"; |
---|
133 | output = Utils.quote(input); |
---|
134 | assertTrue(">" + input + "< quoted", output.startsWith("'") && output.endsWith("'")); |
---|
135 | assertTrue(">" + input + "< restored", input.equals(Utils.unquote(output))); |
---|
136 | |
---|
137 | input = "\r\n\t'\"%"; |
---|
138 | output = Utils.quote(input); |
---|
139 | assertTrue(">" + input + "< quoted", output.startsWith("'") && output.endsWith("'")); |
---|
140 | assertTrue(">" + input + "< restored", input.equals(Utils.unquote(output))); |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * tests backQuoteChars and unbackQuoteChars |
---|
145 | * |
---|
146 | * @see Utils#backQuoteChars(String) |
---|
147 | * @see Utils#unbackQuoteChars(String) |
---|
148 | */ |
---|
149 | public void testBackQuoting() { |
---|
150 | String input; |
---|
151 | String output; |
---|
152 | |
---|
153 | input = "blahblah"; |
---|
154 | output = Utils.backQuoteChars(input); |
---|
155 | assertTrue("No backquoting necessary", input.equals(output)); |
---|
156 | |
---|
157 | input = "\r\n\t'\"%"; |
---|
158 | output = Utils.backQuoteChars(input); |
---|
159 | assertTrue(">" + input + "< restored", input.equals(Utils.unbackQuoteChars(output))); |
---|
160 | |
---|
161 | input = "\\r\\n\\t\\'\\\"\\%"; |
---|
162 | output = Utils.backQuoteChars(input); |
---|
163 | assertTrue(">" + input + "< restored", input.equals(Utils.unbackQuoteChars(output))); |
---|
164 | |
---|
165 | input = Utils.joinOptions(new StringToWordVector().getOptions()); |
---|
166 | output = Utils.backQuoteChars(input); |
---|
167 | assertTrue(">" + input + "< restored", input.equals(Utils.unbackQuoteChars(output))); |
---|
168 | } |
---|
169 | |
---|
170 | public static Test suite() { |
---|
171 | return new TestSuite(UtilsTest.class); |
---|
172 | } |
---|
173 | |
---|
174 | public static void main(String[] args){ |
---|
175 | junit.textui.TestRunner.run(suite()); |
---|
176 | } |
---|
177 | } |
---|