7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /**
25 * @test
26 * @bug 4313885 4926319 4927634 5032610 5032622 5049968 5059533 6223711 6277261 6269946 6288823
27 * @summary Basic tests of java.util.Scanner methods
28 * @key randomness
29 * @run main/othervm ScanTest
30 */
31
32 import java.util.*;
33 import java.text.*;
34 import java.io.*;
35 import java.nio.*;
36 import java.util.regex.*;
37 import java.math.*;
38
39 public class ScanTest {
40
41 private static boolean failure = false;
42 private static int failCount = 0;
43 private static int NUM_SOURCE_TYPES = 2;
44
45 public static void main(String[] args) throws Exception {
46 Locale reservedLocale = Locale.getDefault();
47 String lang = reservedLocale.getLanguage();
48 try {
49 if (!"en".equals(lang) &&
50 !"zh".equals(lang) &&
51 !"ko".equals(lang) &&
52 !"ja".equals(lang)) {
53 //Before we have resource to improve the test to be ready for
54 //arbitrary locale, force the default locale to be "English"
55 //for now.
56 Locale.setDefault(Locale.ENGLISH);
57 }
58 skipTest();
59 findInLineTest();
60 findWithinHorizonTest();
61 findInEmptyLineTest();
62 removeTest();
63 fromFileTest();
64 ioExceptionTest();
65 matchTest();
66 delimiterTest();
67 useLocaleTest();
68 closeTest();
69 cacheTest();
70 cacheTest2();
71 nonASCIITest();
72 resetTest();
73
74 for (int j=0; j<NUM_SOURCE_TYPES; j++) {
75 hasNextTest(j);
76 nextTest(j);
77 hasNextPatternTest(j);
78 nextPatternTest(j);
79 booleanTest(j);
80 byteTest(j);
81 shortTest(j);
82 intTest(j);
83 longTest(j);
84 floatTest(j);
85 doubleTest(j);
86 integerPatternTest(j);
87 floatPatternTest(j);
88 bigIntegerPatternTest(j);
89 bigDecimalPatternTest(j);
90 hasNextLineTest(j);
91 nextLineTest(j);
92 singleDelimTest(j);
93 }
94
98 //example3();
99
100 // Usage cases
101 useCase1();
102 useCase2();
103 useCase3();
104 useCase4();
105 useCase5();
106
107 if (failure)
108 throw new RuntimeException("Failure in the scanning tests.");
109 else
110 System.err.println("OKAY: All tests passed.");
111 } finally {
112 // restore the default locale
113 Locale.setDefault(reservedLocale);
114 }
115 }
116
117 public static void useCase1() throws Exception {
118 File f = new File(System.getProperty("test.src", "."), "input.txt");
119 Scanner sc = new Scanner(f);
120 sc.findWithinHorizon("usage case 1", 0);
121 String[] names = new String[4];
122 for (int i=0; i<4; i++) {
123 while(sc.hasNextFloat())
124 sc.nextFloat();
125 names[i] = sc.next();
126 sc.nextLine();
127 }
128 if (!names[0].equals("Frank"))
129 failCount++;
130 if (!names[1].equals("Joe"))
131 failCount++;
132 if (!names[2].equals("Mary"))
133 failCount++;
134 if (!names[3].equals("Michelle"))
135 failCount++;
136 sc.close();
137 report("Use case 1");
138 }
139
140 public static void useCase2() throws Exception {
141 File f = new File(System.getProperty("test.src", "."), "input.txt");
142 Scanner sc = new Scanner(f).useDelimiter("-");
143 String testDataTag = sc.findWithinHorizon("usage case 2\n", 0);
144 if (!testDataTag.equals("usage case 2\n"))
145 failCount++;
146 if (!sc.next().equals("cat"))
147 failCount++;
148 if (sc.nextInt() != 9)
149 failCount++;
150 if (!sc.next().equals("dog"))
151 failCount++;
152 if (sc.nextInt() != 6)
153 failCount++;
154 if (!sc.next().equals("pig"))
155 failCount++;
156 if (sc.nextInt() != 2)
157 failCount++;
158 if (!sc.next().equals(""))
159 failCount++;
160 if (sc.nextInt() != 5)
161 failCount++;
162 sc.close();
163 report("Use case 2");
164 }
165
166 public static void useCase3() throws Exception {
167 File f = new File(System.getProperty("test.src", "."), "input.txt");
168 Scanner sc = new Scanner(f);
169 String testDataTag = sc.findWithinHorizon("usage case 3\n", 0);
170 if (!testDataTag.equals("usage case 3\n"))
171 failCount++;
172 Pattern tagPattern = Pattern.compile("@[a-z]+");
173 Pattern endPattern = Pattern.compile("\\*\\/");
174 String tag;
175 String end = sc.findInLine(endPattern);
176
177 while (end == null) {
178 if ((tag = sc.findInLine(tagPattern)) != null) {
179 String text = sc.nextLine();
180 text = text.substring(0, text.length() - 1);
181 //System.out.println(text);
182 } else {
183 sc.nextLine();
184 }
185 end = sc.findInLine(endPattern);
186 }
187 report("Use case 3");
188 }
189
190 public static void useCase4() throws Exception {
191 File f = new File(System.getProperty("test.src", "."), "input.txt");
192 Scanner sc = new Scanner(f);
193 String testDataTag = sc.findWithinHorizon("usage case 4\n", 0);
194 if (!testDataTag.equals("usage case 4\n"))
195 failCount++;
196
197 // Read some text parts of four hrefs
198 String[] expected = { "Diffs", "Sdiffs", "Old", "New" };
199 for (int i=0; i<4; i++) {
200 sc.findWithinHorizon("<a href", 1000);
201 sc.useDelimiter("[<>\n]+");
202 sc.next();
203 String textOfRef = sc.next();
204 if (!textOfRef.equals(expected[i]))
205 failCount++;
206 }
207 // Read some html tags using < and > as delimiters
208 if (!sc.next().equals("/a"))
209 failCount++;
210 if (!sc.next().equals("b"))
211 failCount++;
212
213 // Scan some html tags using skip and next
214 Pattern nonTagStart = Pattern.compile("[^<]+");
215 Pattern tag = Pattern.compile("<[^>]+?>");
216 Pattern spotAfterTag = Pattern.compile("(?<=>)");
217 String[] expected2 = { "</b>", "<p>", "<ul>", "<li>" };
218 sc.useDelimiter(spotAfterTag);
219 int tagsFound = 0;
220 while(tagsFound < 4) {
221 if (!sc.hasNext(tag)) {
222 // skip text between tags
223 sc.skip(nonTagStart);
224 }
225 String tagContents = sc.next(tag);
226 if (!tagContents.equals(expected2[tagsFound]))
227 failCount++;
228 tagsFound++;
229 }
230
231 report("Use case 4");
232 }
233
234 public static void useCase5() throws Exception {
235 File f = new File(System.getProperty("test.src", "."), "input.txt");
236 Scanner sc = new Scanner(f);
237 String testDataTag = sc.findWithinHorizon("usage case 5\n", 0);
238 if (!testDataTag.equals("usage case 5\n"))
239 failCount++;
240
241 sc.findWithinHorizon("Share Definitions", 0);
242 sc.nextLine();
243 sc.next("\\[([a-z]+)\\]");
244 String shareName = sc.match().group(1);
245 if (!shareName.equals("homes"))
246 failCount++;
247
248 String[] keys = { "comment", "browseable", "writable", "valid users" };
249 String[] vals = { "Home Directories", "no", "yes", "%S" };
250 for (int i=0; i<4; i++) {
251 sc.useDelimiter("=");
252 String key = sc.next().trim();
253 if (!key.equals(keys[i]))
254 failCount++;
255 sc.skip("[ =]+");
256 sc.useDelimiter("\n");
257 String value = sc.next();
258 if (!value.equals(vals[i]))
259 failCount++;
260 sc.nextLine();
261 }
262
263 report("Use case 5");
264 }
265
266 public static void nonASCIITest() throws Exception {
267 String yourBasicTibetanNumberZero = "\u0f20";
268 String yourBasicTibetanFloatingNumber = "\u0f23.\u0f27";
269 String weirdMixtureOfTibetanAndASCII = "\u0f23.7";
270 String weirdMixtureOfASCIIAndTibetan = "3.\u0f27";
271 Scanner sc = new Scanner(yourBasicTibetanNumberZero);
272 int i = sc.nextInt();
273 if (i != 0)
274 failCount++;
275 sc = new Scanner(yourBasicTibetanFloatingNumber);
276 float f = sc.nextFloat();
277 if (f != Float.parseFloat("3.7"))
278 failCount++;
279 sc = new Scanner(weirdMixtureOfTibetanAndASCII);
280 f = sc.nextFloat();
281 if (f != Float.parseFloat("3.7"))
428 if (!sc.hasNextLine()) failCount++;
429 if (sc.nextInt() != 3) failCount++;
430 if (!sc.hasNextLine()) failCount++;
431 if (!sc.nextLine().equals(" 3")) failCount++;
432 if (!sc.hasNextLine()) failCount++;
433 if (sc.nextInt() != 4) failCount++;
434 if (!sc.hasNextLine()) failCount++;
435 if (sc.nextInt() != 4) failCount++;
436 if (!sc.hasNextLine()) failCount++;
437 if (!sc.nextLine().equals(" 4")) failCount++;
438 if (!sc.hasNextLine()) failCount++;
439 if (!sc.nextLine().equals("5")) failCount++;
440 if (sc.hasNextLine()) failCount++;
441 sc = new Scanner("blah blah blah blah blah blah");
442 if (!sc.hasNextLine()) failCount++;
443 if (!sc.nextLine().equals("blah blah blah blah blah blah"))
444 failCount++;
445 if (sc.hasNextLine()) failCount++;
446
447 // Go through all the lines in a file
448 File f = new File(System.getProperty("test.src", "."), "input.txt");
449 sc = new Scanner(f);
450 String lastLine = "blah";
451 while(sc.hasNextLine())
452 lastLine = sc.nextLine();
453 if (!lastLine.equals("# Data for usage case 6")) failCount++;
454
455 report("Has next line test");
456 }
457
458 public static void nextLineTest(int sourceType) throws Exception {
459 Scanner sc = scannerFor("1\n2\n3 3\r\n4 4 4\r5", sourceType);
460 if (!sc.nextLine().equals("1"))
461 failCount++;
462 if (sc.nextInt() != 2)
463 failCount++;
464 if (!sc.nextLine().equals(""))
465 failCount++;
466 if (sc.nextInt() != 3)
467 failCount++;
468 if (!sc.nextLine().equals(" 3"))
469 failCount++;
470 if (sc.nextInt() != 4)
471 failCount++;
472 if (sc.nextInt() != 4)
473 failCount++;
612 System.out.println("wrong radix cache is used");
613 failCount++;
614 }
615 scanner = new Scanner("10");
616 scanner.hasNextBigInteger(16);
617 if (scanner.nextBigInteger(10).intValue() != 10) {
618 System.out.println("wrong radix cache is used");
619 failCount++;
620 }
621 report("Cache test2");
622 }
623
624
625 public static void closeTest() throws Exception {
626 Scanner sc = new Scanner("testing");
627 sc.close();
628 sc.ioException();
629 sc.delimiter();
630 sc.useDelimiter("blah");
631 sc.useDelimiter(Pattern.compile("blah"));
632 for (int i=0; i<NUM_METHODS; i++) {
633 try {
634 methodCall(sc, i);
635 failCount++;
636 } catch (IllegalStateException ise) {
637 // Correct
638 }
639 }
640 report("Close test");
641 }
642
643 private static int NUM_METHODS = 23;
644
645 private static void methodCall(Scanner sc, int i) {
646 switch(i) {
647 case 0: sc.hasNext(); break;
648 case 1: sc.next(); break;
649 case 2: sc.hasNext(Pattern.compile("blah")); break;
650 case 3: sc.next(Pattern.compile("blah")); break;
651 case 4: sc.hasNextBoolean(); break;
652 case 5: sc.nextBoolean(); break;
653 case 6: sc.hasNextByte(); break;
654 case 7: sc.nextByte(); break;
655 case 8: sc.hasNextShort(); break;
656 case 9: sc.nextShort(); break;
657 case 10: sc.hasNextInt(); break;
658 case 11: sc.nextInt(); break;
659 case 12: sc.hasNextLong(); break;
660 case 13: sc.nextLong(); break;
661 case 14: sc.hasNextFloat(); break;
662 case 15: sc.nextFloat(); break;
663 case 16: sc.hasNextDouble(); break;
664 case 17: sc.nextDouble(); break;
665 case 18: sc.hasNextBigInteger(); break;
666 case 19: sc.nextBigInteger(); break;
667 case 20: sc.hasNextBigDecimal(); break;
668 case 21: sc.nextBigDecimal(); break;
669 case 22: sc.hasNextLine(); break;
670 default:
671 break;
672 }
673 }
674
675 public static void removeTest() throws Exception {
676 Scanner sc = new Scanner("testing");
677 try {
678 sc.remove();
679 failCount++;
680 } catch (UnsupportedOperationException uoe) {
681 // Correct result
682 }
683 report("Remove test");
684 }
685
686 public static void delimiterTest() throws Exception {
687 Scanner sc = new Scanner("blah");
688 Pattern test = sc.delimiter();
689 if (!test.toString().equals("\\p{javaWhitespace}+"))
690 failCount++;
691 sc.useDelimiter("a");
692 test = sc.delimiter();
693 if (!test.toString().equals("a"))
847 failCount++;
848 } catch (NoSuchElementException nse) {
849 // Correct result
850 }
851 try {
852 sc.nextDouble();
853 failCount++;
854 } catch (NoSuchElementException nse) {
855 // Correct result
856 }
857 try {
858 sc.nextDouble();
859 failCount++;
860 } catch (NoSuchElementException nse) {
861 // Correct result
862 }
863 }
864
865 public static void fromFileTest() throws Exception {
866 File f = new File(System.getProperty("test.src", "."), "input.txt");
867 Scanner sc = new Scanner(f).useDelimiter("\n+");
868 String testDataTag = sc.findWithinHorizon("fromFileTest", 0);
869 if (!testDataTag.equals("fromFileTest"))
870 failCount++;
871
872 int count = 0;
873 while (sc.hasNextLong()) {
874 long blah = sc.nextLong();
875 count++;
876 }
877 if (count != 7)
878 failCount++;
879 sc.close();
880 report("From file");
881 }
882
883 private static void example1() throws Exception {
884 Scanner s = new Scanner("1 fish 2 fish red fish blue fish");
885 s.useDelimiter("\\s*fish\\s*");
886 List <String> results = new ArrayList<String>();
887 while(s.hasNext())
888 results.add(s.next());
889 System.out.println(results);
890 }
891
892 private static void example2() throws Exception {
893 Scanner s = new Scanner("1 fish 2 fish red fish blue fish");
894 s.useDelimiter("\\s*fish\\s*");
895 System.out.println(s.nextInt());
896 System.out.println(s.nextInt());
897 System.out.println(s.next());
898 System.out.println(s.next());
899 }
900
901 private static void example3() throws Exception {
902 Scanner s = new Scanner("1 fish 2 fish red fish blue fish");
903 s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
904 for (int i=1; i<=s.match().groupCount(); i++)
905 System.out.println(s.match().group(i));
906 }
907
1455 sc.useDelimiter(a);
1456 Locale dummy = new Locale("en", "US", "dummy");
1457 sc.useLocale(dummy);
1458 sc.useRadix(16);
1459 if (sc.radix() != 16 ||
1460 !sc.locale().equals(dummy) ||
1461 !sc.delimiter().pattern().equals(a.pattern())) {
1462 failCount++;
1463 } else {
1464 sc.reset();
1465 if (sc.radix() != radix ||
1466 !sc.locale().equals(locale) ||
1467 !sc.delimiter().pattern().equals(delimiter.pattern())) {
1468 failCount++;
1469 }
1470 }
1471 sc.close();
1472 report("Reset test");
1473 }
1474
1475 private static void report(String testName) {
1476 int spacesToAdd = 30 - testName.length();
1477 StringBuffer paddedNameBuffer = new StringBuffer(testName);
1478 for (int i=0; i<spacesToAdd; i++)
1479 paddedNameBuffer.append(" ");
1480 String paddedName = paddedNameBuffer.toString();
1481 System.err.println(paddedName + ": " +
1482 (failCount==0 ? "Passed":"Failed("+failCount+")"));
1483 if (failCount > 0)
1484 failure = true;
1485 failCount = 0;
1486 }
1487
1488 static Scanner scannerFor(String input, int sourceType) {
1489 if (sourceType == 1)
1490 return new Scanner(input);
1491 else
1492 return new Scanner(new StutteringInputStream(input));
1493 }
1494
1495 static class ThrowingReadable implements Readable {
1496 ThrowingReadable() {
1497 }
1498 public int read(java.nio.CharBuffer cb) throws IOException {
1499 throw new IOException("ThrowingReadable always throws");
1500 }
1501 }
1502 }
|
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /**
25 * @test
26 * @bug 4313885 4926319 4927634 5032610 5032622 5049968 5059533 6223711 6277261 6269946 6288823
27 * 8072722
28 * @summary Basic tests of java.util.Scanner methods
29 * @key randomness
30 * @run main/othervm ScanTest
31 */
32
33 import java.io.*;
34 import java.math.*;
35 import java.nio.*;
36 import java.text.*;
37 import java.util.*;
38 import java.util.function.Consumer;
39 import java.util.regex.*;
40 import java.util.stream.*;
41
42 public class ScanTest {
43
44 private static boolean failure = false;
45 private static int failCount = 0;
46 private static int NUM_SOURCE_TYPES = 2;
47 private static File inputFile = new File(System.getProperty("test.src", "."), "input.txt");
48
49 public static void main(String[] args) throws Exception {
50
51 Locale reservedLocale = Locale.getDefault();
52 String lang = reservedLocale.getLanguage();
53 try {
54 if (!"en".equals(lang) &&
55 !"zh".equals(lang) &&
56 !"ko".equals(lang) &&
57 !"ja".equals(lang)) {
58 //Before we have resource to improve the test to be ready for
59 //arbitrary locale, force the default locale to be "English"
60 //for now.
61 Locale.setDefault(Locale.ENGLISH);
62 }
63 skipTest();
64 findInLineTest();
65 findWithinHorizonTest();
66 findInEmptyLineTest();
67 removeTest();
68 fromFileTest();
69 ioExceptionTest();
70 matchTest();
71 delimiterTest();
72 useLocaleTest();
73 closeTest();
74 cacheTest();
75 cacheTest2();
76 nonASCIITest();
77 resetTest();
78 streamCloseTest();
79 streamComodTest();
80
81 for (int j = 0; j < NUM_SOURCE_TYPES; j++) {
82 hasNextTest(j);
83 nextTest(j);
84 hasNextPatternTest(j);
85 nextPatternTest(j);
86 booleanTest(j);
87 byteTest(j);
88 shortTest(j);
89 intTest(j);
90 longTest(j);
91 floatTest(j);
92 doubleTest(j);
93 integerPatternTest(j);
94 floatPatternTest(j);
95 bigIntegerPatternTest(j);
96 bigDecimalPatternTest(j);
97 hasNextLineTest(j);
98 nextLineTest(j);
99 singleDelimTest(j);
100 }
101
105 //example3();
106
107 // Usage cases
108 useCase1();
109 useCase2();
110 useCase3();
111 useCase4();
112 useCase5();
113
114 if (failure)
115 throw new RuntimeException("Failure in the scanning tests.");
116 else
117 System.err.println("OKAY: All tests passed.");
118 } finally {
119 // restore the default locale
120 Locale.setDefault(reservedLocale);
121 }
122 }
123
124 public static void useCase1() throws Exception {
125 try (Scanner sc = new Scanner(inputFile)) {
126 sc.findWithinHorizon("usage case 1", 0);
127 String[] names = new String[4];
128 for (int i=0; i<4; i++) {
129 while (sc.hasNextFloat())
130 sc.nextFloat();
131 names[i] = sc.next();
132 sc.nextLine();
133 }
134 if (!names[0].equals("Frank"))
135 failCount++;
136 if (!names[1].equals("Joe"))
137 failCount++;
138 if (!names[2].equals("Mary"))
139 failCount++;
140 if (!names[3].equals("Michelle"))
141 failCount++;
142 }
143 report("Use case 1");
144 }
145
146 public static void useCase2() throws Exception {
147 try (Scanner sc = new Scanner(inputFile).useDelimiter("-")) {
148 String testDataTag = sc.findWithinHorizon("usage case 2\n", 0);
149 if (!testDataTag.equals("usage case 2\n"))
150 failCount++;
151 if (!sc.next().equals("cat"))
152 failCount++;
153 if (sc.nextInt() != 9)
154 failCount++;
155 if (!sc.next().equals("dog"))
156 failCount++;
157 if (sc.nextInt() != 6)
158 failCount++;
159 if (!sc.next().equals("pig"))
160 failCount++;
161 if (sc.nextInt() != 2)
162 failCount++;
163 if (!sc.next().equals(""))
164 failCount++;
165 if (sc.nextInt() != 5)
166 failCount++;
167 }
168 report("Use case 2");
169 }
170
171 public static void useCase3() throws Exception {
172 try (Scanner sc = new Scanner(inputFile)) {
173 String testDataTag = sc.findWithinHorizon("usage case 3\n", 0);
174 if (!testDataTag.equals("usage case 3\n"))
175 failCount++;
176 Pattern tagPattern = Pattern.compile("@[a-z]+");
177 Pattern endPattern = Pattern.compile("\\*\\/");
178 String tag;
179 String end = sc.findInLine(endPattern);
180
181 while (end == null) {
182 if ((tag = sc.findInLine(tagPattern)) != null) {
183 String text = sc.nextLine();
184 text = text.substring(0, text.length() - 1);
185 //System.out.println(text);
186 } else {
187 sc.nextLine();
188 }
189 end = sc.findInLine(endPattern);
190 }
191 }
192 report("Use case 3");
193 }
194
195 public static void useCase4() throws Exception {
196 try (Scanner sc = new Scanner(inputFile)) {
197 String testDataTag = sc.findWithinHorizon("usage case 4\n", 0);
198 if (!testDataTag.equals("usage case 4\n"))
199 failCount++;
200
201 // Read some text parts of four hrefs
202 String[] expected = { "Diffs", "Sdiffs", "Old", "New" };
203 for (int i=0; i<4; i++) {
204 sc.findWithinHorizon("<a href", 1000);
205 sc.useDelimiter("[<>\n]+");
206 sc.next();
207 String textOfRef = sc.next();
208 if (!textOfRef.equals(expected[i]))
209 failCount++;
210 }
211 // Read some html tags using < and > as delimiters
212 if (!sc.next().equals("/a"))
213 failCount++;
214 if (!sc.next().equals("b"))
215 failCount++;
216
217 // Scan some html tags using skip and next
218 Pattern nonTagStart = Pattern.compile("[^<]+");
219 Pattern tag = Pattern.compile("<[^>]+?>");
220 Pattern spotAfterTag = Pattern.compile("(?<=>)");
221 String[] expected2 = { "</b>", "<p>", "<ul>", "<li>" };
222 sc.useDelimiter(spotAfterTag);
223 int tagsFound = 0;
224 while (tagsFound < 4) {
225 if (!sc.hasNext(tag)) {
226 // skip text between tags
227 sc.skip(nonTagStart);
228 }
229 String tagContents = sc.next(tag);
230 if (!tagContents.equals(expected2[tagsFound]))
231 failCount++;
232 tagsFound++;
233 }
234 }
235
236 report("Use case 4");
237 }
238
239 public static void useCase5() throws Exception {
240 try (Scanner sc = new Scanner(inputFile)) {
241 String testDataTag = sc.findWithinHorizon("usage case 5\n", 0);
242 if (!testDataTag.equals("usage case 5\n"))
243 failCount++;
244
245 sc.findWithinHorizon("Share Definitions", 0);
246 sc.nextLine();
247 sc.next("\\[([a-z]+)\\]");
248 String shareName = sc.match().group(1);
249 if (!shareName.equals("homes"))
250 failCount++;
251
252 String[] keys = { "comment", "browseable", "writable", "valid users" };
253 String[] vals = { "Home Directories", "no", "yes", "%S" };
254 for (int i=0; i<4; i++) {
255 sc.useDelimiter("=");
256 String key = sc.next().trim();
257 if (!key.equals(keys[i]))
258 failCount++;
259 sc.skip("[ =]+");
260 sc.useDelimiter("\n");
261 String value = sc.next();
262 if (!value.equals(vals[i]))
263 failCount++;
264 sc.nextLine();
265 }
266 }
267
268 report("Use case 5");
269 }
270
271 public static void nonASCIITest() throws Exception {
272 String yourBasicTibetanNumberZero = "\u0f20";
273 String yourBasicTibetanFloatingNumber = "\u0f23.\u0f27";
274 String weirdMixtureOfTibetanAndASCII = "\u0f23.7";
275 String weirdMixtureOfASCIIAndTibetan = "3.\u0f27";
276 Scanner sc = new Scanner(yourBasicTibetanNumberZero);
277 int i = sc.nextInt();
278 if (i != 0)
279 failCount++;
280 sc = new Scanner(yourBasicTibetanFloatingNumber);
281 float f = sc.nextFloat();
282 if (f != Float.parseFloat("3.7"))
283 failCount++;
284 sc = new Scanner(weirdMixtureOfTibetanAndASCII);
285 f = sc.nextFloat();
286 if (f != Float.parseFloat("3.7"))
433 if (!sc.hasNextLine()) failCount++;
434 if (sc.nextInt() != 3) failCount++;
435 if (!sc.hasNextLine()) failCount++;
436 if (!sc.nextLine().equals(" 3")) failCount++;
437 if (!sc.hasNextLine()) failCount++;
438 if (sc.nextInt() != 4) failCount++;
439 if (!sc.hasNextLine()) failCount++;
440 if (sc.nextInt() != 4) failCount++;
441 if (!sc.hasNextLine()) failCount++;
442 if (!sc.nextLine().equals(" 4")) failCount++;
443 if (!sc.hasNextLine()) failCount++;
444 if (!sc.nextLine().equals("5")) failCount++;
445 if (sc.hasNextLine()) failCount++;
446 sc = new Scanner("blah blah blah blah blah blah");
447 if (!sc.hasNextLine()) failCount++;
448 if (!sc.nextLine().equals("blah blah blah blah blah blah"))
449 failCount++;
450 if (sc.hasNextLine()) failCount++;
451
452 // Go through all the lines in a file
453 try (Scanner sc2 = new Scanner(inputFile)) {
454 String lastLine = "blah";
455 while (sc2.hasNextLine())
456 lastLine = sc2.nextLine();
457 if (!lastLine.equals("# Data for usage case 6")) failCount++;
458 }
459
460 report("Has next line test");
461 }
462
463 public static void nextLineTest(int sourceType) throws Exception {
464 Scanner sc = scannerFor("1\n2\n3 3\r\n4 4 4\r5", sourceType);
465 if (!sc.nextLine().equals("1"))
466 failCount++;
467 if (sc.nextInt() != 2)
468 failCount++;
469 if (!sc.nextLine().equals(""))
470 failCount++;
471 if (sc.nextInt() != 3)
472 failCount++;
473 if (!sc.nextLine().equals(" 3"))
474 failCount++;
475 if (sc.nextInt() != 4)
476 failCount++;
477 if (sc.nextInt() != 4)
478 failCount++;
617 System.out.println("wrong radix cache is used");
618 failCount++;
619 }
620 scanner = new Scanner("10");
621 scanner.hasNextBigInteger(16);
622 if (scanner.nextBigInteger(10).intValue() != 10) {
623 System.out.println("wrong radix cache is used");
624 failCount++;
625 }
626 report("Cache test2");
627 }
628
629
630 public static void closeTest() throws Exception {
631 Scanner sc = new Scanner("testing");
632 sc.close();
633 sc.ioException();
634 sc.delimiter();
635 sc.useDelimiter("blah");
636 sc.useDelimiter(Pattern.compile("blah"));
637
638 for (Consumer<Scanner> method : methodList) {
639 try {
640 method.accept(sc);
641 failCount++;
642 } catch (IllegalStateException ise) {
643 // Correct
644 }
645 }
646
647 report("Close test");
648 }
649
650 static List<Consumer<Scanner>> methodList = Arrays.asList(
651 Scanner::hasNext,
652 Scanner::next,
653 sc -> sc.hasNext(Pattern.compile("blah")),
654 sc -> sc.next(Pattern.compile("blah")),
655 Scanner::hasNextBoolean,
656 Scanner::nextBoolean,
657 Scanner::hasNextByte,
658 Scanner::nextByte,
659 Scanner::hasNextShort,
660 Scanner::nextShort,
661 Scanner::hasNextInt,
662 Scanner::nextInt,
663 Scanner::hasNextLong,
664 Scanner::nextLong,
665 Scanner::hasNextFloat,
666 Scanner::nextFloat,
667 Scanner::hasNextDouble,
668 Scanner::nextDouble,
669 Scanner::hasNextBigInteger,
670 Scanner::nextBigInteger,
671 Scanner::hasNextBigDecimal,
672 Scanner::nextBigDecimal,
673 Scanner::hasNextLine,
674 Scanner::tokens,
675 sc -> sc.findAll(Pattern.compile("blah")),
676 sc -> sc.findAll("blah")
677 );
678
679 public static void removeTest() throws Exception {
680 Scanner sc = new Scanner("testing");
681 try {
682 sc.remove();
683 failCount++;
684 } catch (UnsupportedOperationException uoe) {
685 // Correct result
686 }
687 report("Remove test");
688 }
689
690 public static void delimiterTest() throws Exception {
691 Scanner sc = new Scanner("blah");
692 Pattern test = sc.delimiter();
693 if (!test.toString().equals("\\p{javaWhitespace}+"))
694 failCount++;
695 sc.useDelimiter("a");
696 test = sc.delimiter();
697 if (!test.toString().equals("a"))
851 failCount++;
852 } catch (NoSuchElementException nse) {
853 // Correct result
854 }
855 try {
856 sc.nextDouble();
857 failCount++;
858 } catch (NoSuchElementException nse) {
859 // Correct result
860 }
861 try {
862 sc.nextDouble();
863 failCount++;
864 } catch (NoSuchElementException nse) {
865 // Correct result
866 }
867 }
868
869 public static void fromFileTest() throws Exception {
870 File f = new File(System.getProperty("test.src", "."), "input.txt");
871 try (Scanner sc = new Scanner(f)) {
872 sc.useDelimiter("\n+");
873 String testDataTag = sc.findWithinHorizon("fromFileTest", 0);
874 if (!testDataTag.equals("fromFileTest"))
875 failCount++;
876
877 int count = 0;
878 while (sc.hasNextLong()) {
879 long blah = sc.nextLong();
880 count++;
881 }
882 if (count != 7)
883 failCount++;
884 }
885 report("From file");
886 }
887
888 private static void example1() throws Exception {
889 Scanner s = new Scanner("1 fish 2 fish red fish blue fish");
890 s.useDelimiter("\\s*fish\\s*");
891 List <String> results = new ArrayList<String>();
892 while (s.hasNext())
893 results.add(s.next());
894 System.out.println(results);
895 }
896
897 private static void example2() throws Exception {
898 Scanner s = new Scanner("1 fish 2 fish red fish blue fish");
899 s.useDelimiter("\\s*fish\\s*");
900 System.out.println(s.nextInt());
901 System.out.println(s.nextInt());
902 System.out.println(s.next());
903 System.out.println(s.next());
904 }
905
906 private static void example3() throws Exception {
907 Scanner s = new Scanner("1 fish 2 fish red fish blue fish");
908 s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
909 for (int i=1; i<=s.match().groupCount(); i++)
910 System.out.println(s.match().group(i));
911 }
912
1460 sc.useDelimiter(a);
1461 Locale dummy = new Locale("en", "US", "dummy");
1462 sc.useLocale(dummy);
1463 sc.useRadix(16);
1464 if (sc.radix() != 16 ||
1465 !sc.locale().equals(dummy) ||
1466 !sc.delimiter().pattern().equals(a.pattern())) {
1467 failCount++;
1468 } else {
1469 sc.reset();
1470 if (sc.radix() != radix ||
1471 !sc.locale().equals(locale) ||
1472 !sc.delimiter().pattern().equals(delimiter.pattern())) {
1473 failCount++;
1474 }
1475 }
1476 sc.close();
1477 report("Reset test");
1478 }
1479
1480 /*
1481 * Test that closing the stream also closes the underlying Scanner.
1482 * The cases of attempting to open streams on a closed Scanner are
1483 * covered by closeTest().
1484 */
1485 public static void streamCloseTest() throws Exception {
1486 Scanner sc;
1487
1488 sc = new Scanner("xyzzy");
1489 sc.tokens().close();
1490 try {
1491 sc.hasNext();
1492 failCount++;
1493 } catch (IllegalStateException ise) {
1494 // Correct result
1495 }
1496
1497 sc = new Scanner("xyzzy");
1498 sc.findAll("q").close();
1499 try {
1500 sc.hasNext();
1501 failCount++;
1502 } catch (IllegalStateException ise) {
1503 // Correct result
1504 }
1505
1506 report("Streams Close test");
1507 }
1508
1509 /*
1510 * Test ConcurrentModificationException
1511 */
1512 public static void streamComodTest() {
1513 try {
1514 Scanner sc = new Scanner("a b c d e f");
1515 sc.tokens()
1516 .peek(s -> sc.hasNext())
1517 .count();
1518 failCount++;
1519 } catch (ConcurrentModificationException cme) {
1520 // Correct result
1521 }
1522
1523 try {
1524 Scanner sc = new Scanner("a b c d e f");
1525 Iterator<String> it = sc.tokens().iterator();
1526 it.next();
1527 sc.next();
1528 it.next();
1529 failCount++;
1530 } catch (ConcurrentModificationException cme) {
1531 // Correct result
1532 }
1533
1534 try {
1535 String input = IntStream.range(0, 100)
1536 .mapToObj(String::valueOf)
1537 .collect(Collectors.joining(" "));
1538 Scanner sc = new Scanner(input);
1539 sc.findAll("[0-9]+")
1540 .peek(s -> sc.hasNext())
1541 .count();
1542 failCount++;
1543 } catch (ConcurrentModificationException cme) {
1544 // Correct result
1545 }
1546
1547 try {
1548 String input = IntStream.range(0, 100)
1549 .mapToObj(String::valueOf)
1550 .collect(Collectors.joining(" "));
1551 Scanner sc = new Scanner(input);
1552 Iterator<MatchResult> it = sc.findAll("[0-9]+").iterator();
1553 it.next();
1554 sc.next();
1555 it.next();
1556 failCount++;
1557 } catch (ConcurrentModificationException cme) {
1558 // Correct result
1559 }
1560
1561 report("Streams Comod test");
1562 }
1563
1564 private static void report(String testName) {
1565 System.err.printf("%-30s: %s%n", testName,
1566 (failCount == 0) ? "Passed" : String.format("Failed(%d)", failCount));
1567
1568 if (failCount > 0)
1569 failure = true;
1570 failCount = 0;
1571 }
1572
1573 static Scanner scannerFor(String input, int sourceType) {
1574 if (sourceType == 1)
1575 return new Scanner(input);
1576 else
1577 return new Scanner(new StutteringInputStream(input));
1578 }
1579
1580 static class ThrowingReadable implements Readable {
1581 ThrowingReadable() {
1582 }
1583 public int read(java.nio.CharBuffer cb) throws IOException {
1584 throw new IOException("ThrowingReadable always throws");
1585 }
1586 }
1587 }
|