Package br.com.objectos.way.etc

Source Code of br.com.objectos.way.etc.WayEtcResourcesTest

/*
* Copyright 2012 Objectos, Fábrica de Software LTDA.
*
* Licensed 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 br.com.objectos.way.etc;

import static br.com.objectos.way.base.testing.WayMatchers.equalTo;
import static br.com.objectos.way.base.testing.WayMatchers.hasContentsEqualTo;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.File;
import java.util.List;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import br.com.objectos.way.base.io.Directory;
import br.com.objectos.way.etc.model.FakeGlobals;
import br.com.objectos.way.etc.model.Global;

/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
@Test
public class WayEtcResourcesTest {

  private Directory dir;

  @BeforeClass
  public void setUp() {
    dir = Directory.at("/tmp/wetc/resources");
  }

  @BeforeMethod
  public void clean() {
    dir.deleteContents();
  }

  public void copy() {
    WayEtc.resourcesAt()
        .copyTo(dir);

    List<File> res = listFiles();
    assertThat(res.size(), equalTo(0));
  }

  public void copy_selection() {
    WayEtc.resourcesAt()
        .add("/tmpl/etc/user")
        .copyTo(dir);

    List<File> res = listFiles();
    assertThat(res.size(), equalTo(1));

    File r0 = res.get(0);
    assertThat(r0.getPath(), equalTo("/tmp/wetc/resources/tmpl/etc/user"));
    assertThat(r0, hasContentsEqualTo("/tmpl/etc/user"));
  }

  public void copy_file_list() {
    WayEtc.resourcesAt()
        .addFromListAt("/resources-file-list.txt")
        .copyTo(dir);

    List<File> res = listFiles();
    assertThat(res.size(), equalTo(2));

    assertThat(res.get(0).getPath(), equalTo("/tmp/wetc/resources/tmpl/etc/host"));
    assertThat(res.get(1).getPath(), equalTo("/tmp/wetc/resources/tmpl/etc/user"));
  }

  public void copy_selection_and_file_list() {
    WayEtc.resourcesAt()
        .addFromListAt("/resources-file-list.txt")
        .add("/tmpl/conf.d/service")
        .copyTo(dir);

    List<File> res = listFiles();
    assertThat(res.size(), equalTo(3));

    assertThat(res.get(0).getPath(), equalTo("/tmp/wetc/resources/tmpl/conf.d/service"));
    assertThat(res.get(1).getPath(), equalTo("/tmp/wetc/resources/tmpl/etc/host"));
    assertThat(res.get(2).getPath(), equalTo("/tmp/wetc/resources/tmpl/etc/user"));
  }

  public void copy_with_basedir() {
    WayEtc.resourcesAt("/tmpl")
        .add("/conf.d/service")
        .add("/etc/host")
        .add("/etc/user")
        .copyTo(dir);

    List<File> res = listFiles();
    assertThat(res.size(), equalTo(3));

    assertThat(res.get(0).getPath(), equalTo("/tmp/wetc/resources/conf.d/service"));
    assertThat(res.get(1).getPath(), equalTo("/tmp/wetc/resources/etc/host"));
    assertThat(res.get(2).getPath(), equalTo("/tmp/wetc/resources/etc/user"));
  }

  public void map() {
    WayEtc.resourcesAt("/tmpl")
        .map("/conf.d/service", "file0.txt")
        .map("/etc/host", "sub/host")
        .map("/etc/user", "sub/user")
        .copyTo(dir);

    List<File> res = listFiles();
    assertThat(res.size(), equalTo(3));

    assertThat(res.get(0).getPath(), equalTo("/tmp/wetc/resources/file0.txt"));
    assertThat(res.get(1).getPath(), equalTo("/tmp/wetc/resources/sub/host"));
    assertThat(res.get(2).getPath(), equalTo("/tmp/wetc/resources/sub/user"));
  }

  public void map_template() {
    WayEtc.resourcesAt("/tmpl")
        .map("/conf.d/service", "file%d.txt", 0)
        .map("/etc/host", "file%d.txt", 1)
        .map("/etc/user", "file%d.txt", 2)
        .copyTo(dir);

    List<File> res = listFiles();
    assertThat(res.size(), equalTo(3));

    assertThat(res.get(0).getPath(), equalTo("/tmp/wetc/resources/file0.txt"));
    assertThat(res.get(1).getPath(), equalTo("/tmp/wetc/resources/file1.txt"));
    assertThat(res.get(2).getPath(), equalTo("/tmp/wetc/resources/file2.txt"));
  }

  public void map_from_file() {
    WayEtc.resourcesAt("/tmpl")
        .mapFromListAt("/resources-map-list.txt")
        .copyTo(dir);

    List<File> res = listFiles();
    assertThat(res.size(), equalTo(3));

    assertThat(res.get(0).getPath(), equalTo("/tmp/wetc/resources/file0.txt"));
    assertThat(res.get(1).getPath(), equalTo("/tmp/wetc/resources/file1.txt"));
    assertThat(res.get(2).getPath(), equalTo("/tmp/wetc/resources/file2.txt"));
  }

  public void filter_contents() {
    Global global = FakeGlobals.GLOBAL_USER_A;

    WayEtc.resourcesAt()
        .add("/tmpl/etc/user")
        .evalWith(global)
        .copyTo(dir);

    List<File> res = listFiles();
    assertThat(res.size(), equalTo(1));

    File r0 = res.get(0);
    assertThat(r0.getPath(), equalTo("/tmp/wetc/resources/tmpl/etc/user"));
    assertThat(r0, hasContentsEqualTo("/eval/etc/user"));
  }

  public void filter_contents_with_list() {
    Global global = FakeGlobals.GLOBAL_USER_A;

    WayEtc.resourcesAt()
        .addFromListAt("/resources-file-list.txt")
        .evalWith(global)
        .only("/tmpl/etc/host")
        .copyTo(dir);

    List<File> res = listFiles();
    assertThat(res.size(), equalTo(2));

    assertThat(res.get(0), hasContentsEqualTo("/eval/etc/host"));
    assertThat(res.get(1), hasContentsEqualTo("/tmpl/etc/user"));
  }

  private List<File> listFiles() {
    return dir
        .find()
        .typef()
        .list();
  }

}
TOP

Related Classes of br.com.objectos.way.etc.WayEtcResourcesTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.