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      @Override
37      public Object makeObject() {
38          final Factory<LocalDateTime> dateFactory = LocalDateTime::now;
39          return new LazyList<>(new ArrayList<>(), dateFactory);
40      }
41  
42      @Test
43      @Override
44      public void testCanonicalEmptyCollectionExists() {
45          // Factory and Transformer are not serializable
46      }
47  
48      @Test
49      @Override
50      public void testCanonicalFullCollectionExists() {
51          // Factory and Transformer are not serializable
52      }
53  
54      @Test
55      public void testCreateNullGapsWithFactory() {
56          final Factory<LocalDateTime> dateFactory = LocalDateTime::now;
57          final List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), dateFactory);
58  
59          final LocalDateTime fourthElement = list.get(3);
60          assertFalse(list.isEmpty());
61          assertNotNull(fourthElement);
62      }
63  
64      @Test
65      public void testCreateNullGapsWithTransformer() {
66          final List<Integer> hours = Arrays.asList(7, 5, 8, 2);
67          final Transformer<Integer, LocalDateTime> dateFactory = input -> LocalDateTime.now().withHour(hours.get(input));
68          final List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), dateFactory);
69  
70          final LocalDateTime fourthElement = list.get(3);
71          assertFalse(list.isEmpty());
72          assertNotNull(fourthElement);
73      }
74  
75      @Test
76      public void testElementCreationWithFactory() {
77          final Factory<LocalDateTime> dateFactory = LocalDateTime::now;
78          final List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), dateFactory);
79  
80          assertTrue(list.isEmpty());
81  
82          final LocalDateTime firstElement = list.get(0);
83          assertNotNull(firstElement);
84          assertFalse(list.isEmpty());
85      }
86  
87      @Test
88      public void testElementCreationWithTransformer() {
89          final Factory<LocalDateTime> dateFactory = LocalDateTime::now;
90          final List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), dateFactory);
91  
92          assertTrue(list.isEmpty());
93  
94          final LocalDateTime firstElement = list.get(0);
95          assertNotNull(firstElement);
96          assertFalse(list.isEmpty());
97      }
98  
99      @Test
100     public void testGetWithNull() {
101         final List<Integer> hours = Arrays.asList(7, 5, 8, 2);
102         final Transformer<Integer, LocalDateTime> transformer = input -> LocalDateTime.now().withHour(hours.get(input));
103         final List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), transformer);
104         LocalDateTime fourthElement = list.get(3);
105         assertFalse(list.isEmpty());
106         assertNotNull(fourthElement);
107         list.remove(3);
108         list.add(3, null);
109         fourthElement = list.get(3);
110         assertNotNull(fourthElement);
111     }
112 
113     @Test
114     @Override
115     public void testSerializeDeserializeThenCompare() {
116         // Factory and Transformer are not serializable
117     }
118 
119     @Test
120     @Override
121     public void testSimpleSerialization() {
122         // Factory and Transformer are not serializable
123     }
124 
125     private void testSubList(final List<LocalDateTime> list) {
126         List<LocalDateTime> subList = list.subList(1, 3);
127         assertFalse(subList.isEmpty());
128         assertNotNull(subList);
129         assertEquals(2, subList.size());
130 
131         subList = list.subList(0, 1);
132         assertFalse(subList.isEmpty());
133         assertEquals(1, subList.size());
134 
135         subList = list.subList(1, 1);
136         assertTrue(subList.isEmpty());
137 
138         subList = list.subList(0, list.size());
139         assertFalse(subList.isEmpty());
140         assertEquals(list.size(), subList.size());
141     }
142 
143     @Test
144     public void testSubListWitheFactory() {
145         final Factory<LocalDateTime> dateFactory = LocalDateTime::now;
146         final List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), dateFactory);
147         final LocalDateTime fourthElement = list.get(3);
148         assertFalse(list.isEmpty());
149         assertNotNull(fourthElement);
150         testSubList(list);
151     }
152 
153     @Test
154     public void testSubListWithTransformer() {
155         final List<Integer> hours = Arrays.asList(7, 5, 8, 2);
156         final Transformer<Integer, LocalDateTime> transformer = input -> LocalDateTime.now().withHour(hours.get(input));
157         final List<LocalDateTime> list = new LazyList<>(new ArrayList<>(), transformer);
158         final LocalDateTime fourthElement = list.get(3);
159         assertFalse(list.isEmpty());
160         assertNotNull(fourthElement);
161         testSubList(list);
162     }
163 
164 }