This project has retired. For details please refer to its Attic page.
ItemIterableTest xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.client.runtime;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Properties;
26  
27  import org.apache.chemistry.opencmis.client.api.ItemIterable;
28  import org.apache.chemistry.opencmis.client.runtime.util.AbstractPageFetcher;
29  import org.apache.chemistry.opencmis.client.runtime.util.CollectionIterable;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.log4j.PropertyConfigurator;
33  import org.junit.Test;
34  
35  import static org.junit.Assert.*;
36  
37  public class ItemIterableTest {
38  
39      private static final Log log = LogFactory.getLog(ItemIterableTest.class);
40      static {
41          Properties p = new Properties();
42          try {
43              p.load(ItemIterableTest.class.getResourceAsStream("/log4j.properties"));
44          } catch (IOException e) {
45              throw new RuntimeException(e);
46          }
47          PropertyConfigurator.configure(p);
48  
49      }
50      private final String[] data10 = { "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9" };
51      private final String[] data1 = { "A0" };
52      private final String[] data0 = {};
53  
54      private ItemIterable<String> getIterable(final String[] data, long pageSize) {
55          return new CollectionIterable<String>(new AbstractPageFetcher<String>(pageSize) {
56  
57              @Override
58              protected Page<String> fetchPage(long skipCount) {
59                  boolean hasMoreItems = true;
60                  List<String> page = new ArrayList<String>();
61  
62                  ItemIterableTest.log.info("(" + skipCount + "|" + this.maxNumItems + ") ");
63  
64                  int from = (int) skipCount;
65                  int to = (int) (skipCount + this.maxNumItems);
66  
67                  if (to >= data.length) {
68                      to = data.length;
69                      hasMoreItems = false;
70                  }
71  
72                  // . simulate rolling total number of items (for repositories
73                  // that don't necessarily know up front)
74                  int totalItems = (to == data.length) ? to : to + 1;
75  
76                  for (int i = from; i < to; i++) {
77                      page.add(data[i]);
78                  }
79  
80                  Page<String> result = new AbstractPageFetcher.Page<String>(
81                          page, totalItems, hasMoreItems);
82  
83                  return result;
84              }
85          });
86      }
87  
88      @Test
89      public void loopAll() {
90          this.loopAll(this.data10, 100); // tolerate out of bound
91          this.loopAll(this.data10, 10);
92          this.loopAll(this.data10, 9);
93          this.loopAll(this.data10, 8);
94  
95          this.loopAll(this.data10, 2);
96          this.loopAll(this.data10, 1);
97          // this.loopAll(0); maxNumItems must be > 0
98  
99          this.loopAll(this.data1, 1);
100         this.loopAll(this.data1, 5);
101 
102         this.loopAll(this.data0, 1);
103         this.loopAll(this.data0, 5);
104     }
105 
106     @Test
107     public void loopSubPage() {
108         this.loopSubPage(this.data10, 0, 3, 5);
109         this.loopSubPage(this.data10, 2, 3, 5);
110         this.loopSubPage(this.data10, 9, 3, 5);
111         this.loopSubPage(this.data10, 10, 3, 5);
112 
113         this.loopSubPage(this.data10, 2, 3, 3);
114         this.loopSubPage(this.data10, 2, 1000, 1000);
115     }
116 
117     @Test
118     public void loopSkip() {
119         this.loopSkip(this.data10, 0, 5);
120         this.loopSkip(this.data10, 1, 5);
121         this.loopSkip(this.data10, 2, 5);
122         this.loopSkip(this.data10, 3, 5);
123 
124         this.loopSkip(this.data10, 8, 5);
125         this.loopSkip(this.data10, 9, 5);
126         this.loopSkip(this.data10, 10, 5);
127         // this.loopSkip(100, 5); skip out of bound
128 
129         // this.loopSkip(0, 0);
130         this.loopSkip(this.data10, 0, 1);
131         this.loopSkip(this.data10, 0, 10);
132         this.loopSkip(this.data10, 0, 100);
133 
134         // this.loopSkip(0, 0);
135         this.loopSkip(this.data10, 10, 1);
136         this.loopSkip(this.data10, 10, 10);
137         this.loopSkip(this.data10, 10, 100);
138 
139         this.loopSkip(this.data1, 0, 5);
140         this.loopSkip(this.data1, 1, 5);
141 
142         this.loopSkip(this.data0, 0, 5);
143     }
144 
145     @Test
146     public void loopPage() {
147         this.loopPage(this.data10, 0, 5);
148         this.loopPage(this.data10, 1, 5);
149         this.loopPage(this.data10, 2, 5);
150         this.loopPage(this.data10, 3, 5);
151 
152         this.loopPage(this.data10, 8, 5);
153         this.loopPage(this.data10, 9, 5);
154         this.loopPage(this.data10, 10, 5);
155         // this.loopPage(100, 5); skip out of bound
156 
157         // this.loopPage(0, 0);
158         this.loopPage(this.data10, 0, 1);
159         this.loopPage(this.data10, 0, 10);
160         this.loopPage(this.data10, 0, 100);
161 
162         // this.loopPage(0, 0);
163         this.loopPage(this.data10, 10, 1);
164         this.loopPage(this.data10, 10, 10);
165         this.loopPage(this.data10, 10, 100);
166 
167         this.loopPage(this.data1, 0, 5);
168         this.loopPage(this.data1, 1, 5);
169 
170         this.loopPage(this.data0, 0, 5);
171     }
172 
173     @Test
174     public void totalNumItems() {
175         ItemIterableTest.log.info("totalNumItems");
176 
177         int pageSize = 5;
178         ItemIterable<String> p = this.getIterable(this.data10, pageSize);
179         assertNotNull(p);
180         Iterator<String> i = p.iterator();
181         assertNotNull(i);
182         assertEquals(pageSize + 1, p.getTotalNumItems());
183         for (int idx = 0; i.hasNext() && idx < (pageSize + 1); idx++) {
184             assertNotNull(i.next());
185         }
186         assertEquals(this.data10.length, p.getTotalNumItems());
187     }
188 
189     @Test
190     public void hasMoreItems() {
191         ItemIterableTest.log.info("totalHasMoreItems");
192 
193         int pageSize = 5;
194         ItemIterable<String> p = this.getIterable(this.data10, pageSize);
195         assertNotNull(p);
196         Iterator<String> i = p.iterator();
197         assertNotNull(i);
198         assertTrue(p.getHasMoreItems());
199         for (int idx = 0; i.hasNext() && idx < (pageSize + 1); idx++) {
200             i.next();
201         }
202         assertFalse(p.getHasMoreItems());
203     }
204 
205     @Test
206     public void pageNumItems() {
207         ItemIterableTest.log.info("totalPageNumItems");
208 
209         int pageSize = 7;
210         ItemIterable<String> p = this.getIterable(this.data10, pageSize);
211         assertNotNull(p);
212         Iterator<String> i = p.iterator();
213         assertNotNull(i);
214         assertEquals(pageSize, p.getPageNumItems());
215         for (int idx = 0; i.hasNext() && idx < (pageSize + 1); idx++) {
216             assertNotNull(i.next());
217         }
218         assertEquals(this.data10.length - pageSize, p.getPageNumItems());
219     }
220 
221     private void loopSubPage(String[] data, int skipCount, int maxItems, int pageSize) {
222         ItemIterableTest.log.info("loopSubPage (" + skipCount + ", " + maxItems + ", " + pageSize + ")");
223         String msg = "";
224 
225         ItemIterable<String> p = this.getIterable(data, pageSize);
226         assertNotNull(p);
227         ItemIterable<String> pp = p.skipTo(skipCount);
228         assertNotNull(pp);
229         ItemIterable<String> ppp = pp.getPage(maxItems);
230         assertNotNull(ppp);
231 
232         int count = 0;
233         for (String s : ppp) {
234             assertNotNull(s);
235             assertEquals("A" + (count + skipCount), s);
236             msg += (s + " ");
237             count++;
238         }
239         ItemIterableTest.log.info(msg);
240         assertTrue(count <= pageSize);
241     }
242 
243     private void loopSkip(String[] data, int skipCount, int pageSize) {
244         ItemIterableTest.log.info("loopSkip (" + skipCount + ", " + pageSize + ")");
245         String msg = "";
246 
247         ItemIterable<String> p = this.getIterable(data, pageSize);
248         assertNotNull(p);
249         ItemIterable<String> pp = p.skipTo(skipCount);
250         assertNotNull(pp);
251 
252         int count = 0;
253         for (String s : pp) {
254             assertNotNull(s);
255             assertEquals("A" + (count + skipCount), s);
256             msg += (s + " ");
257             count++;
258         }
259         ItemIterableTest.log.info(msg);
260         assertEquals(data.length - skipCount, count);
261     }
262 
263     private void loopAll(String[] data, int pageSize) {
264         ItemIterableTest.log.info("loopAll (" + pageSize + ")");
265         String msg = "";
266 
267         ItemIterable<String> p = this.getIterable(data, pageSize);
268         assertNotNull(p);
269 
270         int count = 0;
271         for (String s : p) {
272             assertNotNull(s);
273             assertEquals("A" + count, s);
274             msg += (s + " ");
275             count++;
276         }
277         ItemIterableTest.log.info(msg);
278         assertEquals(data.length, count);
279     }
280 
281     private void loopPage(String[] data, int skipCount, int pageSize) {
282         ItemIterableTest.log.info("loopPage (" + skipCount + ", " + pageSize + ")");
283         String msg = "";
284 
285         ItemIterable<String> p = this.getIterable(data, pageSize);
286         assertNotNull(p);
287         ItemIterable<String> pp = p.skipTo(skipCount).getPage();
288         assertNotNull(pp);
289 
290         int count = 0;
291         for (String s : pp) {
292             assertNotNull(s);
293             assertEquals("A" + (count + skipCount), s);
294             msg += (s + " ");
295             count++;
296         }
297         ItemIterableTest.log.info(msg);
298         assertEquals(Math.min(data.length - skipCount, pageSize), count);
299     }
300 
301 }