Modified: incubator/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/util/DefaultPagingIterator.java URL: http://svn.apache.org/viewvc/incubator/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/util/DefaultPagingIterator.java?rev=936922&r1=936921&r2=936922&view=diff ============================================================================== --- incubator/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/util/DefaultPagingIterator.java (original) +++ incubator/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/util/DefaultPagingIterator.java Thu Apr 22 16:04:19 2010 @@ -30,136 +30,136 @@ import org.apache.chemistry.opencmis.cli */ public class DefaultPagingIterator<T> implements PagingIterator<T> { - private long skipCount; - private int skipOffset = 0; + private long skipCount; + private int skipOffset = 0; - private AbstractPageFetch<T> pageFetch; + private AbstractPageFetch<T> pageFetch; - private Long totalItems = null; - private PageFetchResult<T> page = null; + private Long totalItems = null; + private PageFetchResult<T> page = null; - /** - * Construct - * - * @param skipCount - * @param pageFetch - */ - public DefaultPagingIterator(long skipCount, AbstractPageFetch<T> pageFetch) { - this.skipCount = skipCount; - this.pageFetch = pageFetch; - } - - /* - * (non-Javadoc) - * - * @see - * org.apache.chemistry.opencmis.client.api.util.PagingIterator#getPosition - * () - */ - public long getPosition() { - return skipCount + skipOffset; - } - - /* - * (non-Javadoc) - * - * @see - * org.apache.chemistry.opencmis.client.api.util.PagingIterator#getTotalNumItems - * () - */ - public long getTotalNumItems() { - if (totalItems == null) { - PageFetchResult<T> page = getPage(); - if (page != null) { - // set number of items - if (page.getTotalItems() != null) { - totalItems = page.getTotalItems().longValue(); - } else { - totalItems = -1L; - } - } - } - return totalItems; - } - - /* - * (non-Javadoc) - * - * @see java.util.Iterator#hasNext() - */ - public boolean hasNext() { - if (!hasMoreItems()) { - return false; - } - - long totalItems = getTotalNumItems(); - if (totalItems < 0) { - // we don't know better - return true; - } - - return (skipCount + skipOffset) < totalItems; - } - - /* - * (non-Javadoc) - * - * @see java.util.Iterator#next() - */ - public T next() { - PageFetchResult<T> currentPage = getPage(); -// skipOffset++; - - List<T> items = currentPage.getPage(); - if (items == null || items.isEmpty()) { - return null; - } - - if (skipOffset == items.size()) { - skipCount += skipOffset; - skipOffset = 0; - this.page = pageFetch.fetchPage(skipCount); - currentPage = this.page; - if (currentPage != null) { - items = currentPage.getPage(); - } - } - - if (items == null || items.isEmpty() || skipOffset == items.size()) { - return null; - } - - return items.get(skipOffset++); - } - - /* - * (non-Javadoc) - * - * @see java.util.Iterator#remove() - */ - public void remove() { - throw new UnsupportedOperationException(); - } - - private boolean hasMoreItems() { - PageFetchResult<T> page = getPage(); - if (page == null) { - return false; - } - if (skipOffset < page.getPage().size()) { - return true; - } - if (page.getHasMoreItems() != null) { - return page.getHasMoreItems().booleanValue(); - } - return false; - } - - private PageFetchResult<T> getPage() { - if (page == null) { - page = pageFetch.fetchPage(skipCount); - } - return page; - } + /** + * Construct + * + * @param skipCount + * @param pageFetch + */ + public DefaultPagingIterator(long skipCount, AbstractPageFetch<T> pageFetch) { + this.skipCount = skipCount; + this.pageFetch = pageFetch; + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.chemistry.opencmis.client.api.util.PagingIterator#getPosition + * () + */ + public long getPosition() { + return skipCount + skipOffset; + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.chemistry.opencmis.client.api.util.PagingIterator#getTotalNumItems + * () + */ + public long getTotalNumItems() { + if (totalItems == null) { + PageFetchResult<T> page = getPage(); + if (page != null) { + // set number of items + if (page.getTotalItems() != null) { + totalItems = page.getTotalItems().longValue(); + } else { + totalItems = -1L; + } + } + } + return totalItems; + } + + /* + * (non-Javadoc) + * + * @see java.util.Iterator#hasNext() + */ + public boolean hasNext() { + if (!hasMoreItems()) { + return false; + } + + long totalItems = getTotalNumItems(); + if (totalItems < 0) { + // we don't know better + return true; + } + + return (skipCount + skipOffset) < totalItems; + } + + /* + * (non-Javadoc) + * + * @see java.util.Iterator#next() + */ + public T next() { + PageFetchResult<T> currentPage = getPage(); + // skipOffset++; + + List<T> items = currentPage.getPage(); + if (items == null || items.isEmpty()) { + return null; + } + + if (skipOffset == items.size()) { + skipCount += skipOffset; + skipOffset = 0; + this.page = pageFetch.fetchPage(skipCount); + currentPage = this.page; + if (currentPage != null) { + items = currentPage.getPage(); + } + } + + if (items == null || items.isEmpty() || skipOffset == items.size()) { + return null; + } + + return items.get(skipOffset++); + } + + /* + * (non-Javadoc) + * + * @see java.util.Iterator#remove() + */ + public void remove() { + throw new UnsupportedOperationException(); + } + + private boolean hasMoreItems() { + PageFetchResult<T> page = getPage(); + if (page == null) { + return false; + } + if (skipOffset < page.getPage().size()) { + return true; + } + if (page.getHasMoreItems() != null) { + return page.getHasMoreItems().booleanValue(); + } + return false; + } + + private PageFetchResult<T> getPage() { + if (page == null) { + page = pageFetch.fetchPage(skipCount); + } + return page; + } }
Modified: incubator/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/CacheTest.java URL: http://svn.apache.org/viewvc/incubator/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/CacheTest.java?rev=936922&r1=936921&r2=936922&view=diff ============================================================================== --- incubator/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/CacheTest.java (original) +++ incubator/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/CacheTest.java Thu Apr 22 16:04:19 2010 @@ -38,108 +38,108 @@ import com.sun.xml.ws.util.ByteArrayBuff public class CacheTest { - @Before - public void setup() { - } - - @Test - public void cacheSingleObjectTest() { - Cache cache = CacheImpl.newInstance(); - - String id = "1"; - // String path = "/1"; - String cacheKey = "key"; - - // add object - CmisObject obj1 = this.createCmisObject(id); - cache.put(obj1, cacheKey); - - // access object - Assert.assertTrue(cache.containsId(id, cacheKey)); - - // access object - CmisObject obj2 = cache.getById(id, cacheKey); - Assert.assertEquals(obj1, obj2); - - // clear cache - cache.clear(); - - // access object (not found) - Assert.assertFalse(cache.containsId(id, cacheKey)); - - // access object (not found) - CmisObject obj4 = cache.getById(id, cacheKey); - Assert.assertNull(obj4); - } - - @Test - public void cacheSizeTest() { - int cacheSize = 50000; - Cache cache = CacheImpl.newInstance(cacheSize); - Assert.assertEquals(cacheSize, cache.getCacheSize()); - } - - @Test - public void lruTest() { - int cacheSize = 3; - Cache cache = CacheImpl.newInstance(cacheSize); - - String cacheKey = "key"; - - for (int i = 0; i < cacheSize + 1; i++) { - CmisObject obj = this.createCmisObject("id" + i); - cache.put(obj, cacheKey); - } - - Assert.assertNull(cache.getById("id0", cacheKey)); // thrown out - Assert.assertNotNull(cache.getById("id1", cacheKey)); - Assert.assertNotNull(cache.getById("id2", cacheKey)); - Assert.assertNotNull(cache.getById("id3", cacheKey)); - } - - @Test - public void serializationTest() throws IOException, ClassNotFoundException { - int cacheSize = 10; - Cache cache = CacheImpl.newInstance(cacheSize); - - String cacheKey = "key"; - - for (int i = 0; i < cacheSize; i++) { - CmisObject obj = this.createCmisObject("id" + i); - cache.put(obj, cacheKey); - } - - ByteArrayBuffer buffer = new ByteArrayBuffer(); - ObjectOutputStream out = new ObjectOutputStream(buffer); - out.writeObject(cache); - out.close(); - - ObjectInputStream in = new ObjectInputStream(buffer.newInputStream()); - Cache cache2 = (Cache) in.readObject(); - in.close(); - - for (int k = 0; k < cacheSize; k++) { - CmisObject o1 = cache.getById("id" + k, cacheKey); - CmisObject o2 = cache2.getById("id" + k, cacheKey); - Assert.assertEquals(o1.getId(), o2.getId()); - } - - } - - /** - * Create a Mock for testing Cache is sufficient. - * - * @param id - * @param path - * @return a mocked object - */ - private CmisObject createCmisObject(String id) { - CmisObject obj = createNiceMock(CmisObject.class); + @Before + public void setup() { + } + + @Test + public void cacheSingleObjectTest() { + Cache cache = CacheImpl.newInstance(); + + String id = "1"; + // String path = "/1"; + String cacheKey = "key"; + + // add object + CmisObject obj1 = this.createCmisObject(id); + cache.put(obj1, cacheKey); + + // access object + Assert.assertTrue(cache.containsId(id, cacheKey)); + + // access object + CmisObject obj2 = cache.getById(id, cacheKey); + Assert.assertEquals(obj1, obj2); + + // clear cache + cache.clear(); + + // access object (not found) + Assert.assertFalse(cache.containsId(id, cacheKey)); + + // access object (not found) + CmisObject obj4 = cache.getById(id, cacheKey); + Assert.assertNull(obj4); + } + + @Test + public void cacheSizeTest() { + int cacheSize = 50000; + Cache cache = CacheImpl.newInstance(cacheSize); + Assert.assertEquals(cacheSize, cache.getCacheSize()); + } + + @Test + public void lruTest() { + int cacheSize = 3; + Cache cache = CacheImpl.newInstance(cacheSize); + + String cacheKey = "key"; + + for (int i = 0; i < cacheSize + 1; i++) { + CmisObject obj = this.createCmisObject("id" + i); + cache.put(obj, cacheKey); + } + + Assert.assertNull(cache.getById("id0", cacheKey)); // thrown out + Assert.assertNotNull(cache.getById("id1", cacheKey)); + Assert.assertNotNull(cache.getById("id2", cacheKey)); + Assert.assertNotNull(cache.getById("id3", cacheKey)); + } + + @Test + public void serializationTest() throws IOException, ClassNotFoundException { + int cacheSize = 10; + Cache cache = CacheImpl.newInstance(cacheSize); + + String cacheKey = "key"; + + for (int i = 0; i < cacheSize; i++) { + CmisObject obj = this.createCmisObject("id" + i); + cache.put(obj, cacheKey); + } + + ByteArrayBuffer buffer = new ByteArrayBuffer(); + ObjectOutputStream out = new ObjectOutputStream(buffer); + out.writeObject(cache); + out.close(); + + ObjectInputStream in = new ObjectInputStream(buffer.newInputStream()); + Cache cache2 = (Cache) in.readObject(); + in.close(); + + for (int k = 0; k < cacheSize; k++) { + CmisObject o1 = cache.getById("id" + k, cacheKey); + CmisObject o2 = cache2.getById("id" + k, cacheKey); + Assert.assertEquals(o1.getId(), o2.getId()); + } + + } + + /** + * Create a Mock for testing Cache is sufficient. + * + * @param id + * @param path + * @return a mocked object + */ + private CmisObject createCmisObject(String id) { + CmisObject obj = createNiceMock(CmisObject.class); - expect(obj.getId()).andReturn(id).anyTimes(); + expect(obj.getId()).andReturn(id).anyTimes(); - replay(obj); + replay(obj); - return obj; - } + return obj; + } } Modified: incubator/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/PagingListTest.java URL: http://svn.apache.org/viewvc/incubator/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/PagingListTest.java?rev=936922&r1=936921&r2=936922&view=diff ============================================================================== --- incubator/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/PagingListTest.java (original) +++ incubator/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/test/java/org/apache/chemistry/opencmis/client/runtime/PagingListTest.java Thu Apr 22 16:04:19 2010 @@ -33,135 +33,129 @@ import org.junit.Test; public class PagingListTest { - private String[] data10 = { "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", - "A8", "A9" }; - private String[] data1 = { "A0" }; - private String[] data0 = { }; - - private PagingIterable<String> getIterable(final String[] data, final long pageSize) { - return new DefaultPagingIterable<String>( - new AbstractPageFetch<String>() { - - @Override - protected PageFetchResult<String> fetchPage(long skipCount) { - Boolean hasMoreItems = Boolean.TRUE; - List<String> page = new ArrayList<String>(); - - System.out.print("(" + skipCount + "|" + pageSize - + ") "); - - int from = (int) skipCount; - int to = (int) (skipCount + pageSize); - - if (to >= data.length) { - to = data.length; - hasMoreItems = Boolean.FALSE; - } - - for (int i = from; i < to; i++) { - page.add(data[i]); - } - - PageFetchResult<String> result = new AbstractPageFetch.PageFetchResult<String>( - page, - BigInteger - .valueOf(data.length), - hasMoreItems); - - return result; - } - }); - } - - @Test - public void loopAll() { - this.loopAll(this.data10, 100); // tolerate out of bound - this.loopAll(this.data10, 10); - this.loopAll(this.data10, 9); - this.loopAll(this.data10, 8); - - this.loopAll(this.data10, 2); - this.loopAll(this.data10, 1); - // this.loopAll(0); pageSize must be > 0 - - this.loopAll(this.data1, 1); - this.loopAll(this.data1, 5); - - this.loopAll(this.data0, 1); - this.loopAll(this.data0, 5); -} - - @Test - public void loopSkip() { - this.loopSkip(this.data10, 0, 5); - this.loopSkip(this.data10, 1, 5); - this.loopSkip(this.data10, 2, 5); - this.loopSkip(this.data10, 3, 5); - - this.loopSkip(this.data10, 8, 5); - this.loopSkip(this.data10, 9, 5); - this.loopSkip(this.data10, 10, 5); -// this.loopSkip(100, 5); skip out of bound - -// this.loopSkip(0, 0); - this.loopSkip(this.data10, 0, 1); - this.loopSkip(this.data10, 0, 10); - this.loopSkip(this.data10, 0, 100); - -// this.loopSkip(0, 0); - this.loopSkip(this.data10, 10, 1); - this.loopSkip(this.data10, 10, 10); - this.loopSkip(this.data10, 10, 100); - - this.loopSkip(this.data1, 0, 5); - this.loopSkip(this.data1, 1, 5); - - this.loopSkip(this.data0, 0, 5); -} - - private void loopSkip(String [] data, int skipCount, int pageSize) { - System.out.println("\nloopSkip (" + skipCount + ", " + pageSize + ")"); - - PagingIterable<String> p = this.getIterable(data, pageSize); - assertNotNull(p); - PagingIterator<String> i = (PagingIterator<String>) p.iterator(); - assertNotNull(i); - assertEquals(data.length, i.getTotalNumItems()); - - PagingIterable<String> pp = p.skipTo(skipCount); - assertNotNull(pp); - PagingIterator<String> ii = (PagingIterator<String>) pp.iterator(); - assertNotNull(ii); - assertEquals(data.length, ii.getTotalNumItems()); - - int count = 0; - for (String s : pp) { - assertNotNull(s); - assertEquals("A" + (count + skipCount), s); - System.out.print(s + " "); - count++; - } - System.out.print("\n"); - assertEquals(data.length - skipCount, count); - } - - private void loopAll(String [] data, int pageSize) { - System.out.println("\nloopAll (" + pageSize + ")"); - - PagingIterable<String> p = this.getIterable(data, pageSize); - assertNotNull(p); - PagingIterator<String> i = (PagingIterator<String>) p.iterator(); - assertNotNull(i); - assertEquals(data.length, i.getTotalNumItems()); - - int count = 0; - for (String s : p) { - assertNotNull(s); - System.out.print(s + " "); - count++; - } - System.out.print("\n"); - assertEquals(data.length, count); - } + private String[] data10 = { "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9" }; + private String[] data1 = { "A0" }; + private String[] data0 = {}; + + private PagingIterable<String> getIterable(final String[] data, final long pageSize) { + return new DefaultPagingIterable<String>(new AbstractPageFetch<String>() { + + @Override + protected PageFetchResult<String> fetchPage(long skipCount) { + Boolean hasMoreItems = Boolean.TRUE; + List<String> page = new ArrayList<String>(); + + System.out.print("(" + skipCount + "|" + pageSize + ") "); + + int from = (int) skipCount; + int to = (int) (skipCount + pageSize); + + if (to >= data.length) { + to = data.length; + hasMoreItems = Boolean.FALSE; + } + + for (int i = from; i < to; i++) { + page.add(data[i]); + } + + PageFetchResult<String> result = new AbstractPageFetch.PageFetchResult<String>(page, BigInteger + .valueOf(data.length), hasMoreItems); + + return result; + } + }); + } + + @Test + public void loopAll() { + this.loopAll(this.data10, 100); // tolerate out of bound + this.loopAll(this.data10, 10); + this.loopAll(this.data10, 9); + this.loopAll(this.data10, 8); + + this.loopAll(this.data10, 2); + this.loopAll(this.data10, 1); + // this.loopAll(0); pageSize must be > 0 + + this.loopAll(this.data1, 1); + this.loopAll(this.data1, 5); + + this.loopAll(this.data0, 1); + this.loopAll(this.data0, 5); + } + + @Test + public void loopSkip() { + this.loopSkip(this.data10, 0, 5); + this.loopSkip(this.data10, 1, 5); + this.loopSkip(this.data10, 2, 5); + this.loopSkip(this.data10, 3, 5); + + this.loopSkip(this.data10, 8, 5); + this.loopSkip(this.data10, 9, 5); + this.loopSkip(this.data10, 10, 5); + // this.loopSkip(100, 5); skip out of bound + + // this.loopSkip(0, 0); + this.loopSkip(this.data10, 0, 1); + this.loopSkip(this.data10, 0, 10); + this.loopSkip(this.data10, 0, 100); + + // this.loopSkip(0, 0); + this.loopSkip(this.data10, 10, 1); + this.loopSkip(this.data10, 10, 10); + this.loopSkip(this.data10, 10, 100); + + this.loopSkip(this.data1, 0, 5); + this.loopSkip(this.data1, 1, 5); + + this.loopSkip(this.data0, 0, 5); + } + + private void loopSkip(String[] data, int skipCount, int pageSize) { + System.out.println("\nloopSkip (" + skipCount + ", " + pageSize + ")"); + + PagingIterable<String> p = this.getIterable(data, pageSize); + assertNotNull(p); + PagingIterator<String> i = (PagingIterator<String>) p.iterator(); + assertNotNull(i); + assertEquals(data.length, i.getTotalNumItems()); + + PagingIterable<String> pp = p.skipTo(skipCount); + assertNotNull(pp); + PagingIterator<String> ii = (PagingIterator<String>) pp.iterator(); + assertNotNull(ii); + assertEquals(data.length, ii.getTotalNumItems()); + + int count = 0; + for (String s : pp) { + assertNotNull(s); + assertEquals("A" + (count + skipCount), s); + System.out.print(s + " "); + count++; + } + System.out.print("\n"); + assertEquals(data.length - skipCount, count); + } + + private void loopAll(String[] data, int pageSize) { + System.out.println("\nloopAll (" + pageSize + ")"); + + PagingIterable<String> p = this.getIterable(data, pageSize); + assertNotNull(p); + PagingIterator<String> i = (PagingIterator<String>) p.iterator(); + assertNotNull(i); + assertEquals(data.length, i.getTotalNumItems()); + + int count = 0; + for (String s : p) { + assertNotNull(s); + System.out.print(s + " "); + count++; + } + System.out.print("\n"); + assertEquals(data.length, count); + } }
