Author: proyal
Date: Mon Sep 3 20:54:45 2007
New Revision: 572504
URL: http://svn.apache.org/viewvc?rev=572504&view=rev
Log:
porting r572024 from 1.1 branch
Modified:
mina/trunk/core/src/test/java/org/apache/mina/filter/stream/StreamWriteFilterTest.java
Modified:
mina/trunk/core/src/test/java/org/apache/mina/filter/stream/StreamWriteFilterTest.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/test/java/org/apache/mina/filter/stream/StreamWriteFilterTest.java?rev=572504&r1=572503&r2=572504&view=diff
==============================================================================
---
mina/trunk/core/src/test/java/org/apache/mina/filter/stream/StreamWriteFilterTest.java
(original)
+++
mina/trunk/core/src/test/java/org/apache/mina/filter/stream/StreamWriteFilterTest.java
Mon Sep 3 20:54:45 2007
@@ -6,16 +6,16 @@
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
- * under the License.
- *
+ * under the License.
+ *
*/
package org.apache.mina.filter.stream;
@@ -28,6 +28,7 @@
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
+import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
@@ -36,12 +37,12 @@
import org.apache.mina.common.DefaultWriteRequest;
import org.apache.mina.common.DummySession;
import org.apache.mina.common.IdleStatus;
+import org.apache.mina.common.IoFilter.NextFilter;
import org.apache.mina.common.IoFutureListener;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.WriteFuture;
import org.apache.mina.common.WriteRequest;
-import org.apache.mina.common.IoFilter.NextFilter;
import org.apache.mina.transport.socket.nio.SocketAcceptor;
import org.apache.mina.transport.socket.nio.SocketConnector;
import org.apache.mina.util.AvailablePortFinder;
@@ -50,7 +51,7 @@
/**
* Tests [EMAIL PROTECTED] StreamWriteFilter}.
- *
+ *
* @author The Apache MINA Project ([EMAIL PROTECTED])
* @version $Rev$, $Date$
*/
@@ -60,6 +61,7 @@
private IoSession session;
private NextFilter nextFilter;
+ private static final byte[] BUF = new byte[0];
@Override
protected void setUp() throws Exception {
@@ -80,12 +82,13 @@
.containsAttribute(StreamWriteFilter.CURRENT_WRITE_REQUEST));
assertFalse(session
.containsAttribute(StreamWriteFilter.WRITE_REQUEST_QUEUE));
+ super.tearDown();
}
public void testWriteEmptyStream() throws Exception {
StreamWriteFilter filter = new StreamWriteFilter();
- InputStream stream = new ByteArrayInputStream(new byte[0]);
+ InputStream stream = new ByteArrayInputStream(BUF);
WriteRequest writeRequest = new DefaultWriteRequest(stream,
new DummyWriteFuture());
@@ -112,6 +115,7 @@
/**
* Tests that the filter just passes objects which aren't InputStreams
* through to the next filter.
+ *
* @throws Exception when something goes wrong
*/
public void testWriteNonStreamMessage() throws Exception {
@@ -143,6 +147,7 @@
/**
* Tests when the contents of the stream fits into one write buffer.
+ *
* @throws Exception when something goes wrong
*/
public void testWriteSingleBufferStream() throws Exception {
@@ -180,6 +185,7 @@
/**
* Tests when the contents of the stream doesn't fit into one write buffer.
+ *
* @throws Exception when something goes wrong
*/
public void testWriteSeveralBuffersStream() throws Exception {
@@ -272,7 +278,7 @@
queue.offer(wrs[0]);
queue.offer(wrs[1]);
queue.offer(wrs[2]);
- InputStream stream = new ByteArrayInputStream(new byte[0]);
+ InputStream stream = new ByteArrayInputStream(BUF);
/*
* Make up the situation.
@@ -352,14 +358,9 @@
acceptor.bind();
- synchronized (sender.lock) {
- synchronized (receiver.lock) {
- connector.connect(address);
-
- sender.lock.wait();
- receiver.lock.wait();
- }
- }
+ connector.connect(address);
+ sender.latch.await();
+ receiver.latch.await();
acceptor.unbind();
@@ -382,7 +383,7 @@
MessageDigest digest;
- public FixedRandomInputStream(long size) throws Exception {
+ private FixedRandomInputStream(long size) throws Exception {
this.size = size;
digest = MessageDigest.getInstance("MD5");
}
@@ -412,13 +413,13 @@
}
private static class SenderHandler extends IoHandlerAdapter {
- final Object lock = new Object();
+ final CountDownLatch latch = new CountDownLatch(1);
InputStream inputStream;
StreamWriteFilter streamWriteFilter = new StreamWriteFilter();
- public SenderHandler(InputStream inputStream) {
+ private SenderHandler(InputStream inputStream) {
this.inputStream = inputStream;
}
@@ -436,39 +437,31 @@
@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
- synchronized (lock) {
- lock.notifyAll();
- }
+ latch.countDown();
}
@Override
public void sessionClosed(IoSession session) throws Exception {
- synchronized (lock) {
- lock.notifyAll();
- }
+ latch.countDown();
}
@Override
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
- synchronized (lock) {
- lock.notifyAll();
- }
+ latch.countDown();
}
@Override
public void messageSent(IoSession session, Object message)
throws Exception {
if (message == inputStream) {
- synchronized (lock) {
- lock.notifyAll();
- }
+ latch.countDown();
}
}
}
private static class ReceiverHandler extends IoHandlerAdapter {
- final Object lock = new Object();
+ final CountDownLatch latch = new CountDownLatch(1);
long bytesRead = 0;
@@ -476,7 +469,7 @@
MessageDigest digest;
- public ReceiverHandler(long size) throws Exception {
+ private ReceiverHandler(long size) throws Exception {
this.size = size;
digest = MessageDigest.getInstance("MD5");
}
@@ -497,16 +490,12 @@
@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
- synchronized (lock) {
- lock.notifyAll();
- }
+ latch.countDown();
}
@Override
public void sessionClosed(IoSession session) throws Exception {
- synchronized (lock) {
- lock.notifyAll();
- }
+ latch.countDown();
}
@Override
@@ -533,7 +522,7 @@
return w1.getMessage().equals(w2.getMessage())
&& w1.getFuture().isWritten() == w2.getFuture()
- .isWritten();
+ .isWritten();
}
return super.argumentMatches(expected, actual);
}