/*
* 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.Directories;
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 File dir;
@BeforeClass
public void setUp() {
dir = new File("/tmp/wetc/resources");
}
@BeforeMethod
public void clean() {
dir.mkdirs();
Directories.rm_rf(dir);
}
public void copy() {
WayEtc.resourcesAt()
.copyTo(dir);
List<File> res = Directories.listFiles(dir);
assertThat(res.size(), equalTo(0));
}
public void copy_selection() {
WayEtc.resourcesAt()
.add("/tmpl/etc/user")
.copyTo(dir);
List<File> res = Directories.listFiles(dir);
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 = Directories.listFiles(dir);
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 = Directories.listFiles(dir);
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 = Directories.listFiles(dir);
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 filter_contents() {
Global global = FakeGlobals.GLOBAL_USER_A;
WayEtc.resourcesAt()
.add("/tmpl/etc/user")
.evalWith(global)
.copyTo(dir);
List<File> res = Directories.listFiles(dir);
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 = Directories.listFiles(dir);
assertThat(res.size(), equalTo(2));
assertThat(res.get(0), hasContentsEqualTo("/eval/etc/host"));
assertThat(res.get(1), hasContentsEqualTo("/tmpl/etc/user"));
}
}