/*
 * 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;

import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import tachyon.Constants;
import tachyon.LocalTachyonClusterResource;
import tachyon.TachyonURI;
import tachyon.client.file.TachyonFileSystem;
import tachyon.collections.PrefixList;
import tachyon.conf.TachyonConf;
import tachyon.underfs.UnderFileSystem;
import tachyon.util.UnderFileSystemUtils;

/**
 * To test the utilities related to under filesystem, including loadufs and etc.
 */
public class UfsUtilsIntegrationTest {
  @Rule
  public LocalTachyonClusterResource mLocalTachyonClusterResource =
      new LocalTachyonClusterResource(10000, 1000, 128);
  private TachyonFileSystem mTachyonFileSystem = null;
  private String mUfsRoot = null;
  private UnderFileSystem mUfs = null;

  @Before
  public final void before() throws Exception {
    mTachyonFileSystem = mLocalTachyonClusterResource.get().getClient();

    TachyonConf masterConf = mLocalTachyonClusterResource.get().getMasterTachyonConf();
    mUfsRoot = masterConf.get(Constants.UNDERFS_ADDRESS);
    mUfs = UnderFileSystem.get(mUfsRoot + TachyonURI.SEPARATOR, masterConf);
  }

  @Test
  public void loadUnderFsTest() throws Exception {
    String[] exclusions = {"/tachyon", "/exclusions"};
    String[] inclusions = {"/inclusions/sub-1", "/inclusions/sub-2"};
    for (String exclusion : exclusions) {
      if (!mUfs.exists(mUfsRoot + exclusion)) {
        mUfs.mkdirs(mUfsRoot + exclusion, true);
      }
    }

    for (String inclusion : inclusions) {
      if (!mUfs.exists(mUfsRoot + inclusion)) {
        mUfs.mkdirs(mUfsRoot + inclusion, true);
      }
      UnderFileSystemUtils.touch(mUfsRoot + inclusion + "/1",
          mLocalTachyonClusterResource.get().getMasterTachyonConf());
    }

    UfsUtils.loadUfs(mTachyonFileSystem, new TachyonURI(TachyonURI.SEPARATOR), new TachyonURI(
        mUfsRoot + TachyonURI.SEPARATOR), new PrefixList("tachyon;exclusions", ";"),
        mLocalTachyonClusterResource.get().getMasterTachyonConf());

    List<String> paths;
    for (String exclusion : exclusions) {
      paths = TachyonFSTestUtils.listFiles(mTachyonFileSystem, exclusion);
      Assert.assertEquals(0, paths.size());
    }
    for (String inclusion : inclusions) {
      paths = TachyonFSTestUtils.listFiles(mTachyonFileSystem, inclusion);
      Assert.assertNotEquals(0, paths.size());
    }
  }
}

@@ -1228,16 +1228,50 @@
 emUtils;
+%0Aimport tachyon.util.io.PathUtils;
 %0A%0A/**%0A *
@@ -2246,32 +2246,50 @@
 %7B%0A      
-if (!mUfs.exists
+String path = PathUtils.concatPath
 (mUfsRoo
@@ -2281,34 +2281,33 @@
 catPath(mUfsRoot
- +
+,
  exclusion)) %7B%0A 
@@ -2293,32 +2293,62 @@
 Root, exclusion)
+;%0A      if (!mUfs.exists(path)
 ) %7B%0A        mUfs
@@ -2359,28 +2359,12 @@
 irs(
-mUfsRoot + exclusion
+path
 , tr
@@ -2431,32 +2431,50 @@
 %7B%0A      
-if (!mUfs.exists
+String path = PathUtils.concatPath
 (mUfsRoo
@@ -2466,34 +2466,33 @@
 catPath(mUfsRoot
- +
+,
  inclusion)) %7B%0A 
@@ -2486,16 +2486,46 @@
 clusion)
+;%0A      if (!mUfs.exists(path)
 ) %7B%0A    
@@ -2540,36 +2540,20 @@
 .mkdirs(
-mUfsRoot + inclusion
+path
 , true);

Use PathUtils to concatenate paths