View Javadoc

1   /*
2    * $Id: LazyTransformedInputStreamTestCase.java 22865 2011-09-05 17:23:45Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.pgp;
12  
13  import java.io.ByteArrayInputStream;
14  import java.io.InputStream;
15  import java.io.OutputStream;
16  import java.util.concurrent.atomic.AtomicLong;
17  
18  import junit.framework.TestCase;
19  
20  import org.apache.commons.io.IOUtils;
21  
22  public class LazyTransformedInputStreamTestCase extends TestCase
23  {
24      private static String message = "abcdefghij";
25      private ByteArrayInputStream inputStream;
26      private LazyTransformedInputStream transformedInputStream;
27      private AddOneStreamTransformer simpleTransformer;
28  
29      @Override
30      protected void setUp() throws Exception
31      {
32          super.setUp();
33          inputStream = new ByteArrayInputStream(message.getBytes());
34          simpleTransformer = new AddOneStreamTransformer(inputStream);
35      }
36  
37      @Override
38      protected void tearDown() throws Exception
39      {
40          super.tearDown();
41          IOUtils.closeQuietly(inputStream);
42      }
43  
44      public void testTransformPerRequestPolicy() throws Exception
45      {
46          transformedInputStream = new LazyTransformedInputStream(new TransformPerRequestPolicy(),
47              simpleTransformer);
48  
49          for (int i = 0; i < message.length(); i++)
50          {
51              int read = transformedInputStream.read();
52              assertEquals(message.charAt(i) + 1, read);
53              // only one byte more should be consumed at this point
54              assertEquals(i + 1, simpleTransformer.bytesRead);
55              Thread.sleep(50);
56              assertEquals(i + 1, simpleTransformer.bytesRead);
57          }
58      }
59  
60      public void testTransformPerRequestInChunksPolicy() throws Exception
61      {
62          int chunkSize = 4;
63          LazyTransformedInputStream transformedInputStream = new LazyTransformedInputStream(
64              new TransformPerRequestInChunksPolicy(chunkSize), simpleTransformer);
65  
66          for (int i = 0; i < message.length(); i++)
67          {
68              int read = transformedInputStream.read();
69              assertEquals(message.charAt(i) + 1, read);
70              // only one byte more should be consumed at this point
71              int shouldBeTransformed = (int) Math.min(message.length(),
72                  Math.ceil((double) simpleTransformer.bytesRead / (double) chunkSize) * chunkSize);
73              assertEquals(shouldBeTransformed, simpleTransformer.bytesRead);
74              Thread.sleep(50);
75              assertEquals(shouldBeTransformed, simpleTransformer.bytesRead);
76          }
77      }
78  
79      public void testTransformContinuouslyPolicy() throws Exception
80      {
81          LazyTransformedInputStream transformedInputStream = new LazyTransformedInputStream(
82              new TransformContinuouslyPolicy(), simpleTransformer);
83  
84          int i = 0;
85          int read = transformedInputStream.read();
86          Thread.sleep(100);
87          // all input stream should be consumed at this point
88          assertEquals(message.length(), simpleTransformer.bytesRead);
89          do
90          {
91              assertEquals(message.charAt(i) + 1, read);
92              read = transformedInputStream.read();
93              i++;
94          }
95          while (i < message.length());
96      }
97  
98      private class AddOneStreamTransformer implements StreamTransformer
99      {
100 
101         private InputStream inputStream;
102         private int bytesRead;
103         private boolean finished;
104 
105         public AddOneStreamTransformer(InputStream inputStream)
106         {
107             this.inputStream = inputStream;
108             this.bytesRead = 0;
109             this.finished = false;
110         }
111 
112         @Override
113         public boolean write(OutputStream out, AtomicLong bytesRequested) throws Exception
114         {
115 
116             while (!this.finished && this.bytesRead + 1 <= bytesRequested.get())
117             {
118                 int byteRead = this.inputStream.read();
119                 if (byteRead == -1)
120                 {
121                     finished = true;
122                     out.write(-1);
123                 }
124                 else
125                 {
126                     out.write(byteRead + 1);
127                     this.bytesRead++;
128                 }
129             }
130             return finished;
131         }
132 
133         @Override
134         public void initialize(OutputStream out) throws Exception
135         {
136             // nothing to do here
137         }
138     }
139 }