Search in sources :

Example 56 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project commons-text by apache.

the class PropertiesStringLookupTest method testInterpolator.

@Test
public void testInterpolator() {
    final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
    assertEquals("Hello World!", stringSubstitutor.replace("${properties:" + KEY_PATH + "}"));
}
Also used : StringSubstitutor(org.apache.commons.text.StringSubstitutor) Test(org.junit.jupiter.api.Test)

Example 57 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project commons-text by apache.

the class PropertiesStringLookupTest method testInterpolatorNestedColon.

@Test
public void testInterpolatorNestedColon() {
    final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
    // Need to handle "C:" in the sys prop user.dir.
    final String replaced = stringSubstitutor.replace("$${properties:${sys:user.dir}/" + KEY_PATH + "}");
    assertEquals("${properties:" + System.getProperty("user.dir") + "/src/test/resources/document.properties::mykey}", replaced);
    assertEquals("Hello World!", stringSubstitutor.replace(replaced));
}
Also used : StringSubstitutor(org.apache.commons.text.StringSubstitutor) Test(org.junit.jupiter.api.Test)

Example 58 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project commons-text by apache.

the class PropertiesStringLookupTest method testInterpolatorWithParameterizedKey2.

@Test
public void testInterpolatorWithParameterizedKey2() {
    final Map<String, String> map = new HashMap<>();
    map.put("KeyIsHere", KEY);
    final StringSubstitutor stringSubstitutor = new StringSubstitutor(StringLookupFactory.INSTANCE.interpolatorStringLookup(map));
    final String replaced = stringSubstitutor.replace("$${properties:${sys:user.dir}/" + PropertiesStringLookup.toPropertyKey(DOC_PATH, "${KeyIsHere}}"));
    assertEquals("${properties:" + System.getProperty("user.dir") + "/" + PropertiesStringLookup.toPropertyKey(DOC_PATH, "mykey}"), replaced);
    assertEquals("Hello World!", stringSubstitutor.replace(replaced));
}
Also used : HashMap(java.util.HashMap) StringSubstitutor(org.apache.commons.text.StringSubstitutor) Test(org.junit.jupiter.api.Test)

Example 59 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project anserini by castorini.

the class JDIQ2018EffectivenessDocsTest method main.

@Test
public void main() throws Exception {
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    URL yaml = JDIQ2018EffectivenessDocsTest.class.getResource("/jdiq2018/models.yaml");
    Model data = mapper.readValue(new File(yaml.toURI()), Model.class);
    Map<String, String> valuesMap = new HashMap<>();
    valuesMap.put("results", data.generateEffectiveness());
    StringSubstitutor sub = new StringSubstitutor(valuesMap);
    URL template = GenerateRegressionDocsTest.class.getResource("/jdiq2018/doc.template");
    Scanner scanner = new Scanner(new File(template.toURI()), "UTF-8");
    String text = scanner.useDelimiter("\\A").next();
    scanner.close();
    String resolvedString = sub.replace(text);
    FileUtils.writeStringToFile(new File("docs/experiments-jdiq2018.md"), resolvedString, "UTF-8");
}
Also used : Scanner(java.util.Scanner) HashMap(java.util.HashMap) StringSubstitutor(org.apache.commons.text.StringSubstitutor) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) URL(java.net.URL) Test(org.junit.Test)

Example 60 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project anserini by castorini.

the class GenerateRegressionDocsTest method main.

@Test
public void main() throws Exception {
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    URL templatesRoot = GenerateRegressionDocsTest.class.getResource("/docgen/templates/");
    for (final File fileEntry : new File(templatesRoot.toURI()).listFiles()) {
        String fileName = fileEntry.getName();
        // This is the name of the test, which can be different from the name of the collection,
        // e.g., multiple topics run on the same collection.
        String testName = fileEntry.getName().replaceAll(".template", "");
        URL yaml = GenerateRegressionDocsTest.class.getResource(String.format("/regression/%s.yaml", testName));
        DataModel data = mapper.readValue(new File(yaml.toURI()), DataModel.class);
        String corpus = data.getCorpus();
        Map<String, String> valuesMap = new HashMap<>();
        valuesMap.put("yaml", String.format("../src/main/resources/regression/%s.yaml", testName));
        valuesMap.put("template", String.format("../src/main/resources/docgen/templates/%s.template", testName));
        valuesMap.put("test_name", testName);
        valuesMap.put("corpus", corpus);
        valuesMap.put("index_cmds", data.generateIndexingCommand(corpus));
        valuesMap.put("ranking_cmds", data.generateRankingCommand(corpus));
        valuesMap.put("eval_cmds", data.generateEvalCommand(corpus));
        valuesMap.put("effectiveness", data.generateEffectiveness(corpus));
        StringSubstitutor sub = new StringSubstitutor(valuesMap);
        URL template = GenerateRegressionDocsTest.class.getResource(String.format("/docgen/templates/%s.template", testName));
        Scanner scanner = new Scanner(new File(template.toURI()), "UTF-8");
        String text = scanner.useDelimiter("\\A").next();
        scanner.close();
        String resolvedString = sub.replace(text);
        FileUtils.writeStringToFile(new File(String.format("docs/regressions-%s.md", testName)), resolvedString, "UTF-8");
    }
}
Also used : Scanner(java.util.Scanner) HashMap(java.util.HashMap) StringSubstitutor(org.apache.commons.text.StringSubstitutor) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) URL(java.net.URL) Test(org.junit.Test)

Aggregations

StringSubstitutor (org.apache.commons.text.StringSubstitutor)71 HashMap (java.util.HashMap)24 Test (org.junit.jupiter.api.Test)19 IOException (java.io.IOException)11 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)9 File (java.io.File)8 List (java.util.List)8 InputStream (java.io.InputStream)6 Collectors (java.util.stream.Collectors)6 StringLookup (org.apache.commons.text.lookup.StringLookup)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)5 SubstitutingSourceProvider (io.dropwizard.configuration.SubstitutingSourceProvider)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 MetricRegistry (com.codahale.metrics.MetricRegistry)4 JsonObject (com.google.gson.JsonObject)4 URL (java.net.URL)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Scanner (java.util.Scanner)4