View Javadoc

1   /*
2    * Grapht, an open source dependency injector.
3    * Copyright 2014-2015 various contributors (see CONTRIBUTORS.txt)
4    * Copyright 2010-2014 Regents of the University of Minnesota
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 2.1 of the
9    * License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14   * details.
15   *
16   * You should have received a copy of the GNU General Public License along with
17   * this program; if not, write to the Free Software Foundation, Inc., 51
18   * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19   */
20  package org.grouplens.grapht.util;
21  
22  import org.junit.Test;
23  
24  import java.util.Iterator;
25  import java.util.NoSuchElementException;
26  
27  import static org.hamcrest.Matchers.*;
28  import static org.junit.Assert.assertThat;
29  import static org.junit.Assert.fail;
30  
31  /**
32   * @author <a href="http://www.grouplens.org">GroupLens Research</a>
33   */
34  public class AbstractChainTest {
35      static class TestChain extends AbstractChain<String> {
36          TestChain(AbstractChain<String> prev, String tv) {
37              super(prev, tv);
38          }
39          public TestChain extend(String tok) {
40              return new TestChain(this, tok);
41          }
42      }
43  
44      static TestChain singleton(String tok) {
45          return new TestChain(null, tok);
46      }
47      static TestChain chain(String t1, String... toks) {
48          TestChain chain = singleton(t1);
49          for (String tok: toks) {
50              chain = chain.extend(tok);
51          }
52          return chain;
53      }
54  
55      @Test
56      public void testSingletonBasicProps() throws Exception {
57          TestChain chain = singleton("foo");
58          assertThat(chain.size(), equalTo(1));
59          assertThat(chain.isEmpty(), equalTo(false));
60          assertThat(chain.contains("foo"), equalTo(true));
61          assertThat(chain, contains("foo"));
62      }
63  
64      @Test
65      public void testSingletonsEqual() {
66          TestChain foo1 = singleton("foo");
67          TestChain foo2 = singleton("foo");
68          TestChain bar = singleton("bar");
69  
70          assertThat(foo1.equals(foo1), equalTo(true));
71          assertThat(foo1.equals(foo2), equalTo(true));
72          assertThat(foo1.equals(bar), equalTo(false));
73      }
74  
75      @Test
76      public void testSingletonGet() {
77          TestChain foo = singleton("foo");
78  
79          assertThat(foo.get(0), equalTo("foo"));
80      }
81  
82      @Test
83      public void testExtend() {
84          TestChain foo = singleton("foo");
85          TestChain foobar = foo.extend("bar");
86  
87          assertThat(foobar.size(), equalTo(2));
88  
89          Iterator<String> iter = foobar.iterator();
90          assertThat(iter.next(), equalTo("foo"));
91          assertThat(iter.next(), equalTo("bar"));
92          assertThat(iter.hasNext(), equalTo(false));
93  
94          assertThat(foobar.get(1), equalTo("bar"));
95          assertThat(foobar.get(0), equalTo("foo"));
96      }
97  
98      @Test
99      public void testReverseIteration() {
100         TestChain chain = chain("foobie", "bletch", "forever");
101         assertThat(chain, hasSize(3));
102         assertThat(chain.reverse(), contains("forever", "bletch", "foobie"));
103 
104         Iterator<String> iter = chain.reverseIterator();
105         iter.next();
106         iter.next();
107         iter.next();
108         try {
109             iter.next();
110             fail("must throw NoSuchElement");
111         } catch (Throwable th) {
112             assertThat(th, instanceOf(NoSuchElementException.class));
113         }
114     }
115 
116     @Test
117     public void testChainsEqual() {
118         TestChain base = chain("foo", "bar", "blatz");
119         TestChain copy = chain("foo", "bar", "blatz");
120         TestChain chend = chain("foo", "bar", "blam");
121         TestChain chstart = chain("f00", "bar", "blatz");
122 
123         assertThat(base.equals(base), equalTo(true));
124         assertThat(base.equals(copy), equalTo(true));
125         assertThat(base.equals(chend), equalTo(false));
126         assertThat(base.equals(chstart), equalTo(false));
127     }
128 }