de.egore911.versioning.deployer.performer.PerformRelacementTest.java Source code

Java tutorial

Introduction

Here is the source code for de.egore911.versioning.deployer.performer.PerformRelacementTest.java

Source

/*
 * Copyright 2013  Christoph Brill <egore911@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of 
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.egore911.versioning.deployer.performer;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Test;

import de.egore911.versioning.deployer.util.ReplacementPair;
import de.egore911.versioning.util.UrlUtil;

/**
 * @author Christoph Brill &lt;egore911@gmail.com&gt;
 */
public class PerformRelacementTest {

    private static final List<File> emptyList = Collections.emptyList();

    @Test
    public void testPerformFile() throws IOException {
        String tmpdir = System.getProperty("java.io.tmpdir");
        File propertiesFile = new File(UrlUtil.concatenateUrlWithSlashes(tmpdir, "myfile.properties"));

        File replacementProperties = new File(UrlUtil.concatenateUrlWithSlashes(tmpdir, "replacement.properties"));
        FileUtils.write(replacementProperties, "testvariable = testvalue");

        FileUtils.write(propertiesFile, "Test = ${versioning:testvariable}");
        PerformReplacement.perform(new File(tmpdir), Collections.singletonList("*.properties"),
                new ArrayList<ReplacementPair>(), Collections.singletonList(replacementProperties));
        Assert.assertEquals("Test = testvalue", FileUtils.readFileToString(propertiesFile));
    }

    @Test
    public void testPerformValid() throws IOException {
        String tmpdir = System.getProperty("java.io.tmpdir");
        File propertiesFile = new File(UrlUtil.concatenateUrlWithSlashes(tmpdir, "myfile.properties"));
        FileUtils.write(propertiesFile, "Test = ${versioning:testvariable}");
        PerformReplacement.perform(new File(tmpdir), Collections.singletonList("*.properties"),
                Collections.singletonList(new ReplacementPair("testvariable", "testvalue")), emptyList);
        Assert.assertEquals("Test = testvalue", FileUtils.readFileToString(propertiesFile));
    }

    @Test
    public void testPerformMultiline() throws IOException {
        String tmpdir = System.getProperty("java.io.tmpdir");
        File propertiesFile = new File(UrlUtil.concatenateUrlWithSlashes(tmpdir, "myfile.properties"));
        FileUtils.write(propertiesFile,
                "Something = Nothing\n" + "Test = ${versioning:testvariable}\n" + "OneWay = Or another");
        PerformReplacement.perform(new File(tmpdir), Collections.singletonList("*.properties"),
                Collections.singletonList(new ReplacementPair("testvariable", "testvalue")), emptyList);
        Assert.assertEquals("Something = Nothing\n" + "Test = testvalue\n" + "OneWay = Or another",
                FileUtils.readFileToString(propertiesFile));
    }

    @Test
    public void testPerformMultiple() throws IOException {
        String tmpdir = System.getProperty("java.io.tmpdir");
        File propertiesFile = new File(UrlUtil.concatenateUrlWithSlashes(tmpdir, "myfile.properties"));
        FileUtils.write(propertiesFile,
                "Test = ${versioning:testvariable}${versioning:testvariable}${versioning:testvariable}");
        PerformReplacement.perform(new File(tmpdir), Collections.singletonList("*.properties"),
                Collections.singletonList(new ReplacementPair("testvariable", "testvalue")), emptyList);
        Assert.assertEquals("Test = testvaluetestvaluetestvalue", FileUtils.readFileToString(propertiesFile));
    }

    @Test
    public void testPerformNoFiles() throws IOException {
        String tmpdir = System.getProperty("java.io.tmpdir");
        File propertiesFile = new File(UrlUtil.concatenateUrlWithSlashes(tmpdir, "myfile.properties"));
        FileUtils.write(propertiesFile, "Test = ${versioning:testvariable}");
        PerformReplacement.perform(new File(tmpdir), Collections.singletonList("*.conf"),
                Collections.singletonList(new ReplacementPair("testvariable", "testvalue")), emptyList);
        Assert.assertEquals("Test = ${versioning:testvariable}", FileUtils.readFileToString(propertiesFile));
    }

    @Test
    public void testPerformNoVariables() throws IOException {
        String tmpdir = System.getProperty("java.io.tmpdir");
        File propertiesFile = new File(UrlUtil.concatenateUrlWithSlashes(tmpdir, "myfile.properties"));
        FileUtils.write(propertiesFile, "Test = testvalue");
        PerformReplacement.perform(new File(tmpdir), Collections.singletonList("*.conf"),
                Collections.singletonList(new ReplacementPair("testvariable", "testvalue")), emptyList);
        Assert.assertEquals("Test = testvalue", FileUtils.readFileToString(propertiesFile));
    }

    @Test
    public void testPerformNoOtherVariables() throws IOException {
        String tmpdir = System.getProperty("java.io.tmpdir");
        File propertiesFile = new File(UrlUtil.concatenateUrlWithSlashes(tmpdir, "myfile.properties"));
        FileUtils.write(propertiesFile, "Test = ${versioning:testvariable}");
        PerformReplacement.perform(new File(tmpdir), Collections.singletonList("*.conf"),
                Collections.singletonList(new ReplacementPair("othervariable", "testvalue")), emptyList);
        Assert.assertEquals("Test = ${versioning:testvariable}", FileUtils.readFileToString(propertiesFile));
    }

}