421794 - Iterator from InputStreamProvider is not implemented properly.

Now the iterator contract is better respected.
This commit is contained in:
Simone Bordet 2013-12-13 16:32:14 +01:00
parent 6473bbc2b6
commit b0c275837b
2 changed files with 207 additions and 5 deletions

View File

@ -141,20 +141,21 @@ public class InputStreamContentProvider implements ContentProvider
LOG.debug("Read {} bytes from {}", read, stream);
if (read > 0)
{
buffer = onRead(bytes, 0, read);
hasNext = Boolean.TRUE;
buffer = onRead(bytes, 0, read);
return true;
}
else if (read < 0)
{
hasNext = Boolean.FALSE;
buffer = null;
close();
return false;
}
else
{
buffer = BufferUtil.EMPTY_BUFFER;
hasNext = Boolean.TRUE;
buffer = BufferUtil.EMPTY_BUFFER;
return true;
}
}
@ -167,6 +168,7 @@ public class InputStreamContentProvider implements ContentProvider
// Signal we have more content to cause a call to
// next() which will throw NoSuchElementException.
hasNext = Boolean.TRUE;
buffer = null;
close();
return true;
}
@ -178,13 +180,28 @@ public class InputStreamContentProvider implements ContentProvider
public ByteBuffer next()
{
if (failure != null)
{
// Consume the failure so that calls to hasNext() will return false.
hasNext = Boolean.FALSE;
buffer = null;
throw (NoSuchElementException)new NoSuchElementException().initCause(failure);
}
if (!hasNext())
throw new NoSuchElementException();
ByteBuffer result = buffer;
if (result == null)
{
hasNext = Boolean.FALSE;
buffer = null;
throw new NoSuchElementException();
buffer = null;
hasNext = null;
return result;
}
else
{
hasNext = null;
buffer = null;
return result;
}
}
@Override

View File

@ -0,0 +1,185 @@
//
// ========================================================================
// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.client.util;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Assert;
import org.junit.Test;
public class InputStreamContentProviderTest
{
@Test
public void testHasNextFalseThenNext()
{
final AtomicBoolean closed = new AtomicBoolean();
InputStream stream = new InputStream()
{
@Override
public int read() throws IOException
{
return -1;
}
@Override
public void close() throws IOException
{
super.close();
closed.compareAndSet(false, true);
}
};
InputStreamContentProvider provider = new InputStreamContentProvider(stream);
Iterator<ByteBuffer> iterator = provider.iterator();
Assert.assertNotNull(iterator);
Assert.assertFalse(iterator.hasNext());
try
{
iterator.next();
Assert.fail();
}
catch (NoSuchElementException expected)
{
}
Assert.assertFalse(iterator.hasNext());
Assert.assertTrue(closed.get());
}
@Test
public void testStreamWithContentThenNextThenNext()
{
final AtomicBoolean closed = new AtomicBoolean();
ByteArrayInputStream stream = new ByteArrayInputStream(new byte[]{1})
{
@Override
public void close() throws IOException
{
super.close();
closed.compareAndSet(false, true);
}
};
InputStreamContentProvider provider = new InputStreamContentProvider(stream);
Iterator<ByteBuffer> iterator = provider.iterator();
Assert.assertNotNull(iterator);
ByteBuffer buffer = iterator.next();
Assert.assertNotNull(buffer);
try
{
iterator.next();
Assert.fail();
}
catch (NoSuchElementException expected)
{
}
Assert.assertFalse(iterator.hasNext());
Assert.assertTrue(closed.get());
}
@Test
public void testStreamWithExceptionThenNext()
{
final AtomicBoolean closed = new AtomicBoolean();
InputStream stream = new InputStream()
{
@Override
public int read() throws IOException
{
throw new IOException();
}
@Override
public void close() throws IOException
{
super.close();
closed.compareAndSet(false, true);
}
};
InputStreamContentProvider provider = new InputStreamContentProvider(stream);
Iterator<ByteBuffer> iterator = provider.iterator();
Assert.assertNotNull(iterator);
try
{
iterator.next();
Assert.fail();
}
catch (NoSuchElementException expected)
{
}
Assert.assertFalse(iterator.hasNext());
Assert.assertTrue(closed.get());
}
@Test
public void testHasNextWithExceptionThenNext()
{
final AtomicBoolean closed = new AtomicBoolean();
InputStream stream = new InputStream()
{
@Override
public int read() throws IOException
{
throw new IOException();
}
@Override
public void close() throws IOException
{
super.close();
closed.compareAndSet(false, true);
}
};
InputStreamContentProvider provider = new InputStreamContentProvider(stream);
Iterator<ByteBuffer> iterator = provider.iterator();
Assert.assertNotNull(iterator);
Assert.assertTrue(iterator.hasNext());
try
{
iterator.next();
Assert.fail();
}
catch (NoSuchElementException expected)
{
}
Assert.assertFalse(iterator.hasNext());
Assert.assertTrue(closed.get());
}
}