/*
 * Licensed to the University of California, Berkeley under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file 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.
 */

package tachyon.client.next;

import java.io.IOException;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import tachyon.Constants;
import tachyon.TachyonURI;
import tachyon.client.WriteType;
import tachyon.client.next.file.FileInStream;
import tachyon.client.next.file.FileOutStream;
import tachyon.client.next.file.TachyonFS;
import tachyon.client.next.file.TachyonFile;
import tachyon.conf.TachyonConf;
import tachyon.master.next.LocalTachyonCluster;
import tachyon.util.io.BufferUtils;
import tachyon.util.io.PathUtils;

/**
 * Integration tests for <code>tachyon.client.BlockInStream</code>.
 */
public class BlockInStreamIntegrationTest {
  private static final int MIN_LEN = 0;
  private static final int MAX_LEN = 255;
  private static final int MEAN = (MIN_LEN + MAX_LEN) / 2;
  private static final int DELTA = 33;

  private static LocalTachyonCluster sLocalTachyonCluster = null;
  private static TachyonFS sTfs = null;

  @AfterClass
  public static final void afterClass() throws Exception {
    sLocalTachyonCluster.stop();
    System.clearProperty("tachyon.user.quota.unit.bytes");
  }

  @BeforeClass
  public static final void beforeClass() throws Exception {
    sLocalTachyonCluster = new LocalTachyonCluster(10000000, 1000, Constants.GB);
    sLocalTachyonCluster.start();
    sTfs = sLocalTachyonCluster.getClient();
  }

  /**
   * Test <code>void read()</code>.
   */
  @Test
  public void newReadTest1() throws IOException {
    String uniqPath = PathUtils.uniqPath();
    for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) {
      for (WriteType op : WriteType.values()) {
        TachyonURI path = new TachyonURI(uniqPath + "/file_" + k + "_" + op);
        ClientOptions options = new ClientOptions.Builder(new TachyonConf()).build();
        FileOutStream os = sTfs.getOutStream(path, options);
        for (int j = 0; j < k; j ++) {
          os.write((byte) j);
        }
        os.close();

        TachyonFile f = sTfs.open(path);

        FileInStream is = sTfs.getInStream(f, options);
        byte[] ret = new byte[k];
        int value = is.read();
        int cnt = 0;
        while (value != -1) {
          Assert.assertTrue(value >= 0);
          Assert.assertTrue(value < 256);
          ret[cnt ++] = (byte) value;
          value = is.read();
        }
        Assert.assertEquals(cnt, k);
        Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k, ret));
        is.close();

        is = sTfs.getInStream(f, options);
        ret = new byte[k];
        value = is.read();
        cnt = 0;
        while (value != -1) {
          Assert.assertTrue(value >= 0);
          Assert.assertTrue(value < 256);
          ret[cnt ++] = (byte) value;
          value = is.read();
        }
        Assert.assertEquals(cnt, k);
        Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k, ret));
        is.close();
      }
    }
  }
}

@@ -1224,41 +1224,8 @@
 le;%0A
-import tachyon.conf.TachyonConf;%0A
 impo
@@ -2519,20 +2519,16 @@
 ptions =
- new
  ClientO
@@ -2538,40 +2538,16 @@
 ons.
-Builder(new TachyonConf()).build
+defaults
 ();%0A
@@ -2761,470 +2761,61 @@
     
-FileInStream is = sTfs.getInStream(f, options);%0A        byte%5B%5D ret = new byte%5Bk%5D;%0A        int value = is.read();%0A        int cnt = 0;%0A        while (value != -1) %7B%0A          Assert.assertTrue(value %3E= 0);%0A          Assert.assertTrue(value %3C 256);%0A          ret%5Bcnt ++%5D = (byte) value;%0A          value = is.read();%0A        %7D%0A        Assert.assertEquals(cnt, k);%0A        Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k, ret));%0A        is.close();%0A%0A       
+for (int i = 0; i %3C 2; i ++) %7B%0A          FileInStream
  is 
@@ -2849,24 +2849,33 @@
 ns);%0A       
+   byte%5B%5D
  ret = new b
@@ -2882,32 +2882,38 @@
 yte%5Bk%5D;%0A        
+  int 
 value = is.read(
@@ -2923,16 +2923,22 @@
         
+  int 
 cnt = 0;
@@ -2946,16 +2946,18 @@
         
+  
 while (v
@@ -2966,24 +2966,26 @@
 ue != -1) %7B%0A
+  
           As
@@ -3015,32 +3015,34 @@
 = 0);%0A          
+  
 Assert.assertTru
@@ -3057,16 +3057,18 @@
 %3C 256);%0A
+  
         
@@ -3076,17 +3076,16 @@
  ret%5Bcnt
- 
 ++%5D = (b
@@ -3096,16 +3096,18 @@
  value;%0A
+  
         
@@ -3127,34 +3127,38 @@
 read();%0A        
-%7D%0A
+  %7D%0A  
         Assert.a
@@ -3178,32 +3178,34 @@
 nt, k);%0A        
+  
 Assert.assertTru
@@ -3261,16 +3261,18 @@
         
+  
 is.close
@@ -3271,24 +3271,34 @@
 is.close();%0A
+        %7D%0A
       %7D%0A    

Remove redundant code