View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.list;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.time.LocalDateTime;
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  import java.util.List;
28  
29  import org.apache.commons.collections4.AbstractObjectTest;
30  import org.apache.commons.collections4.Factory;
31  import org.apache.commons.collections4.Transformer;
32  import org.junit.jupiter.api.Test;
33  
34  public class LazyListTest extends AbstractObjectTest {
35  
36      public LazyListTest() {
37          super(LazyListTest.class.getSimpleName());
38      }
39  
40      @Override
41      public Object makeObject() {
42          final Factory<LocalDateTime> dateFactory = LocalDateTime::now;
43          return new LazyList<>(new ArrayList<>(), dateFactory);
44      }
45  
46      @Test
47      @Override
48      public void testCanonicalEmptyCollectionExists() {
49          // Factory and Transformer are not serializable
50      }
51  
52      @Test
53      @Override
54      public void testCanonicalFullCollectionExists() {
55          // Factory and Transformer are not serializable
56      }
57  
58      @Test
59      public void testCreateNullGapsWithFactory() {
60          final Factory<LocalDateTime> dateFactory = LocalDateTime::now;
61          final List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), dateFactory);
62  
63          final LocalDateTime fourthElement = list.get(3);
64          assertFalse(list.isEmpty());
65          assertNotNull(fourthElement);
66      }
67  
68      @Test
69      public void testCreateNullGapsWithTransformer() {
70          final List<Integer> hours = Arrays.asList(7, 5, 8, 2);
71          final Transformer<Integer, LocalDateTime> dateFactory = input -> LocalDateTime.now().withHour(hours.get(input));
72          final List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), dateFactory);
73  
74          final LocalDateTime fourthElement = list.get(3);
75          assertFalse(list.isEmpty());
76          assertNotNull(fourthElement);
77      }
78  
79      @Test
80      public void testElementCreationWithFactory() {
81          final Factory<LocalDateTime> dateFactory = LocalDateTime::now;
82          final List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), dateFactory);
83  
84          assertTrue(list.isEmpty());
85  
86          final LocalDateTime firstElement = list.get(0);
87          assertNotNull(firstElement);
88          assertFalse(list.isEmpty());
89      }
90  
91      @Test
92      public void testElementCreationWithTransformer() {
93          final Factory<LocalDateTime> dateFactory = LocalDateTime::now;
94          final List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), dateFactory);
95  
96          assertTrue(list.isEmpty());
97  
98          final LocalDateTime firstElement = list.get(0);
99          assertNotNull(firstElement);
100         assertFalse(list.isEmpty());
101     }
102 
103     @Test
104     public void testGetWithNull() {
105         final List<Integer> hours = Arrays.asList(7, 5, 8, 2);
106         final Transformer<Integer, LocalDateTime> transformer = input -> LocalDateTime.now().withHour(hours.get(input));
107         final List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), transformer);
108         LocalDateTime fourthElement = list.get(3);
109         assertFalse(list.isEmpty());
110         assertNotNull(fourthElement);
111         list.remove(3);
112         list.add(3, null);
113         fourthElement = list.get(3);
114         assertNotNull(fourthElement);
115     }
116 
117     @Test
118     @Override
119     public void testSerializeDeserializeThenCompare() {
120         // Factory and Transformer are not serializable
121     }
122 
123     @Test
124     @Override
125     public void testSimpleSerialization() {
126         // Factory and Transformer are not serializable
127     }
128 
129     private void testSubList(final List<LocalDateTime> list) {
130         List<LocalDateTime> subList = list.subList(1, 3);
131         assertFalse(subList.isEmpty());
132         assertNotNull(subList);
133         assertEquals(2, subList.size());
134 
135         subList = list.subList(0, 1);
136         assertFalse(subList.isEmpty());
137         assertEquals(1, subList.size());
138 
139         subList = list.subList(1, 1);
140         assertTrue(subList.isEmpty());
141 
142         subList = list.subList(0, list.size());
143         assertFalse(subList.isEmpty());
144         assertEquals(list.size(), subList.size());
145     }
146 
147     @Test
148     public void testSubListWitheFactory() {
149         final Factory<LocalDateTime> dateFactory = LocalDateTime::now;
150         final List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), dateFactory);
151         final LocalDateTime fourthElement = list.get(3);
152         assertFalse(list.isEmpty());
153         assertNotNull(fourthElement);
154         testSubList(list);
155     }
156 
157     @Test
158     public void testSubListWithTransformer() {
159         final List<Integer> hours = Arrays.asList(7, 5, 8, 2);
160         final Transformer<Integer, LocalDateTime> transformer = input -> LocalDateTime.now().withHour(hours.get(input));
161         final List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), transformer);
162         final LocalDateTime fourthElement = list.get(3);
163         assertFalse(list.isEmpty());
164         assertNotNull(fourthElement);
165         testSubList(list);
166     }
167 
168 }