1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.yaml.snakeyaml.representer;
18
19 import java.util.Iterator;
20
21 import junit.framework.TestCase;
22
23 import org.yaml.snakeyaml.Yaml;
24
25
26
27
28 public class RepresentIterableTest extends TestCase {
29
30 public void testIterable() {
31 Yaml yaml = new Yaml();
32 try {
33 yaml.dump(new CounterFactory());
34 fail("Iterable should not be treated as sequence by default.");
35 } catch (Exception e) {
36 assertEquals(
37 "No JavaBean properties found in org.yaml.snakeyaml.representer.RepresentIterableTest$CounterFactory",
38 e.getMessage());
39 }
40 }
41
42 public void testIterator() {
43 Yaml yaml = new Yaml();
44 String output = yaml.dump(new Counter(7));
45 assertEquals("[0, 1, 2, 3, 4, 5, 6]\n", output);
46 }
47
48 private class CounterFactory implements Iterable<Integer> {
49 public Iterator<Integer> iterator() {
50 return new Counter(10);
51 }
52 }
53
54 private class Counter implements Iterator<Integer> {
55 private int max = 0;
56 private int counter = 0;
57
58 public Counter(int max) {
59 this.max = max;
60 }
61
62 public boolean hasNext() {
63 return counter < max;
64 }
65
66 public Integer next() {
67 return counter++;
68 }
69
70 public void remove() {
71 throw new UnsupportedOperationException();
72 }
73 }
74 }