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