View Javadoc

1   /**
2    * Copyright (c) 2008-2011, http://www.snakeyaml.org
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Test {@link issue69 http://code.google.com/p/snakeyaml/issues/detail?id=69}
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  }