Search in sources :

Example 16 with Resource

use of org.bndtools.templating.Resource in project bndtools by bndtools.

the class ReposTemplateLoaderTest method testProcessTemplate.

public void testProcessTemplate() throws Exception {
    List<Template> templates = loader.findTemplates("test1", new ProgressMonitorReporter(new NullProgressMonitor(), "")).getValue();
    assertEquals(1, templates.size());
    Template template = templates.get(0);
    Map<String, List<Object>> parameters = new HashMap<>();
    parameters.put("projectName", Collections.<Object>singletonList("org.example.foo"));
    parameters.put("srcDir", Collections.<Object>singletonList("src/main/java"));
    parameters.put("basePackageDir", Collections.<Object>singletonList("org/example/foo"));
    ResourceMap outputs = template.generateOutputs(parameters);
    assertEquals(5, outputs.size());
    Entry<String, Resource> entry;
    Iterator<Entry<String, Resource>> iter = outputs.entries().iterator();
    entry = iter.next();
    assertEquals("src/main/java/org/example/foo/Activator.java", entry.getKey());
    assertEquals("package org.example.foo; public class Activator {}", IO.collect(entry.getValue().getContent()));
    entry = iter.next();
    assertEquals("pic.jpg", entry.getKey());
    // Check the digest of the pic to ensure it didn't get damaged by the templating engine
    DigestInputStream digestStream = new DigestInputStream(entry.getValue().getContent(), MessageDigest.getInstance("SHA-256"));
    IO.drain(digestStream);
    byte[] digest = digestStream.getMessageDigest().digest();
    assertEquals("ea5d770bc2deddb1f9a20df3ad337bdc1490ba7b35fa41c33aa4e9a534e82ada", Hex.toHexString(digest).toLowerCase());
    entry = iter.next();
    assertEquals("src/main/java/", entry.getKey());
    entry = iter.next();
    assertEquals("src/main/java/org/example/foo/", entry.getKey());
    entry = iter.next();
    assertEquals("bnd.bnd", entry.getKey());
    assertEquals("Bundle-SymbolicName: org.example.foo", IO.collect(entry.getValue().getContent()));
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) DigestInputStream(java.security.DigestInputStream) ProgressMonitorReporter(org.bndtools.utils.progress.ProgressMonitorReporter) HashMap(java.util.HashMap) Resource(org.bndtools.templating.Resource) Template(org.bndtools.templating.Template) ResourceMap(org.bndtools.templating.ResourceMap) Entry(java.util.Map.Entry) List(java.util.List)

Example 17 with Resource

use of org.bndtools.templating.Resource in project bndtools by bndtools.

the class ReposTemplateLoaderTest method testAlternateDelimiters.

public void testAlternateDelimiters() throws Exception {
    List<Template> templates = loader.findTemplates("test2", new ProgressMonitorReporter(new NullProgressMonitor(), "")).getValue();
    assertEquals(1, templates.size());
    Template template = templates.get(0);
    Map<String, List<Object>> parameters = new HashMap<>();
    parameters.put("projectName", Collections.<Object>singletonList("org.example.foo"));
    parameters.put("srcDir", Collections.<Object>singletonList("src/main/java"));
    parameters.put("basePackageDir", Collections.<Object>singletonList("org/example/foo"));
    ResourceMap outputs = template.generateOutputs(parameters);
    assertEquals(5, outputs.size());
    Iterator<Entry<String, Resource>> iter = outputs.entries().iterator();
    Entry<String, Resource> entry;
    entry = iter.next();
    assertEquals("src/main/java/org/example/foo/Activator.java", entry.getKey());
    assertEquals("package org.example.foo; public class Activator {}", IO.collect(entry.getValue().getContent()));
    entry = iter.next();
    assertEquals("pic.jpg", entry.getKey());
    // Check the digest of the pic to ensure it didn't get damaged by the templating engine
    DigestInputStream digestStream = new DigestInputStream(entry.getValue().getContent(), MessageDigest.getInstance("SHA-256"));
    IO.drain(digestStream);
    byte[] digest = digestStream.getMessageDigest().digest();
    assertEquals("ea5d770bc2deddb1f9a20df3ad337bdc1490ba7b35fa41c33aa4e9a534e82ada", Hex.toHexString(digest).toLowerCase());
    entry = iter.next();
    assertEquals("src/main/java/", entry.getKey());
    entry = iter.next();
    assertEquals("src/main/java/org/example/foo/", entry.getKey());
    entry = iter.next();
    assertEquals("bnd.bnd", entry.getKey());
    assertEquals("Bundle-SymbolicName: org.example.foo", IO.collect(entry.getValue().getContent()));
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) DigestInputStream(java.security.DigestInputStream) ProgressMonitorReporter(org.bndtools.utils.progress.ProgressMonitorReporter) HashMap(java.util.HashMap) Resource(org.bndtools.templating.Resource) Template(org.bndtools.templating.Template) ResourceMap(org.bndtools.templating.ResourceMap) Entry(java.util.Map.Entry) List(java.util.List)

Aggregations

Resource (org.bndtools.templating.Resource)17 ResourceMap (org.bndtools.templating.ResourceMap)10 StringResource (org.bndtools.templating.StringResource)10 HashMap (java.util.HashMap)8 StringReader (java.io.StringReader)7 Properties (java.util.Properties)7 List (java.util.List)6 Template (org.bndtools.templating.Template)6 InputStreamReader (java.io.InputStreamReader)5 Entry (java.util.Map.Entry)5 ProgressMonitorReporter (org.bndtools.utils.progress.ProgressMonitorReporter)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 Reader (java.io.Reader)3 StringWriter (java.io.StringWriter)3 DigestInputStream (java.security.DigestInputStream)3 DefaultMustacheFactory (com.github.mustachejava.DefaultMustacheFactory)2 File (java.io.File)2 InputStream (java.io.InputStream)2 Enumeration (java.util.Enumeration)2 BuiltInTemplate (org.bndtools.core.ui.wizards.shared.BuiltInTemplate)2