View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.config;
29  
30  import static org.hamcrest.MatcherAssert.assertThat;
31  import static org.hamcrest.Matchers.equalTo;
32  import static org.hamcrest.Matchers.notNullValue;
33  import static org.hamcrest.Matchers.nullValue;
34  import static org.hamcrest.CoreMatchers.is;
35  
36  import org.hamcrest.CoreMatchers;
37  import org.junit.jupiter.api.Test;
38  
39  /**
40   * Tests for {@link NamedElementChain}.
41   */
42  public class TestNamedElementChain {
43  
44      @Test
45      public void testBasics() {
46          final NamedElementChain<Character> list = new NamedElementChain<>();
47          assertThat(list.getFirst(), CoreMatchers.nullValue());
48          assertThat(list.getLast(), CoreMatchers.nullValue());
49  
50          final NamedElementChain<Character>.Node nodeA = list.addFirst('a', "a");
51  
52          assertThat(list.getFirst(), CoreMatchers.sameInstance(nodeA));
53          assertThat(list.getLast(), CoreMatchers.sameInstance(nodeA));
54  
55          final NamedElementChain<Character>.Node nodeB = list.addLast('b', "b");
56  
57          assertThat(list.getFirst(), CoreMatchers.sameInstance(nodeA));
58          assertThat(list.getLast(), CoreMatchers.sameInstance(nodeB));
59  
60          final NamedElementChain<Character>.Node nodeZ = list.addLast('z', "z");
61  
62          assertThat(list.getFirst(), CoreMatchers.sameInstance(nodeA));
63          assertThat(list.getLast(), CoreMatchers.sameInstance(nodeZ));
64  
65          assertThat(nodeA.getPrevious(), CoreMatchers.nullValue());
66          assertThat(nodeA.getNext(), CoreMatchers.sameInstance(nodeB));
67          assertThat(nodeB.getPrevious(), CoreMatchers.sameInstance(nodeA));
68          assertThat(nodeB.getNext(), CoreMatchers.sameInstance(nodeZ));
69          assertThat(nodeZ.getPrevious(), CoreMatchers.sameInstance(nodeB));
70          assertThat(nodeZ.getNext(), CoreMatchers.nullValue());
71  
72          final NamedElementChain<Character>.Node nodeD = list.addAfter("b", 'd', "d");
73          assertThat(nodeD.getPrevious(), CoreMatchers.sameInstance(nodeB));
74          assertThat(nodeD.getNext(), CoreMatchers.sameInstance(nodeZ));
75          assertThat(nodeB.getNext(), CoreMatchers.sameInstance(nodeD));
76          assertThat(nodeZ.getPrevious(), CoreMatchers.sameInstance(nodeD));
77  
78          final NamedElementChain<Character>.Node nodeC = list.addBefore("d", 'c', "c");
79          assertThat(nodeC.getPrevious(), CoreMatchers.sameInstance(nodeB));
80          assertThat(nodeC.getNext(), CoreMatchers.sameInstance(nodeD));
81          assertThat(nodeB.getNext(), CoreMatchers.sameInstance(nodeC));
82          assertThat(nodeD.getPrevious(), CoreMatchers.sameInstance(nodeC));
83          assertThat(list.getSize(), CoreMatchers.equalTo(5));
84  
85          assertThat(list.remove("a"), CoreMatchers.is(true));
86          assertThat(list.remove("z"), CoreMatchers.is(true));
87          assertThat(list.remove("c"), CoreMatchers.is(true));
88          assertThat(list.remove("c"), CoreMatchers.is(false));
89          assertThat(list.remove("blah"), CoreMatchers.is(false));
90  
91          assertThat(list.getFirst(), CoreMatchers.sameInstance(nodeB));
92          assertThat(list.getLast(), CoreMatchers.sameInstance(nodeD));
93  
94          assertThat(list.getSize(), CoreMatchers.equalTo(2));
95          assertThat(list.addBefore("blah", 'e', "e"), CoreMatchers.nullValue());
96          assertThat(list.getSize(), CoreMatchers.equalTo(2));
97  
98          assertThat(list.addAfter("yada", 'e', "e"), CoreMatchers.nullValue());
99          assertThat(list.getSize(), CoreMatchers.equalTo(2));
100     }
101 
102     @Test
103     public void testFind() {
104         final NamedElementChain<Character> list = new NamedElementChain<>();
105         list.addLast('c', "c");
106         assertThat(list.find("c"), notNullValue());
107         assertThat(list.find("a"), nullValue());
108     }
109 
110     @Test
111     public void testReplace() {
112         final NamedElementChain<Character> list = new NamedElementChain<>();
113         list.addLast('c', "c");
114         final boolean found = list.replace("c",'z' );
115         assertThat(found, is(true));
116         assertThat(list.find("c").getValue(), equalTo('z'));
117         assertThat(list.find("c").getName(), equalTo("c"));
118         final boolean notFound = list.replace("X",'z' );
119         assertThat(notFound, is(false));
120 
121     }
122 }