View Javadoc
1   /*
2    * Copyright (C) 2022-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.phplib.templateengine;
5   
6   
7   import static org.junit.jupiter.api.Assertions.assertEquals;
8   
9   import java.io.File;
10  import java.io.IOException;
11  
12  import io.cucumber.java.en.Given;
13  import io.cucumber.java.en.Then;
14  import io.cucumber.java.en.When;
15  
16  
17  /**
18   * Cucumber step definitions.
19   */
20  @SuppressWarnings({"PMD.MethodNamingConventions", "checkstyle:MethodName"})
21  public class StepDefinitions
22   {
23    /**
24     * Template file 1 path constant.
25     */
26    private static final String TEMPLATE1_TMPL = "target/test-classes/templates/template1.tmpl"; //$NON-NLS-1$
27  
28    /**
29     * Template file 2 path constant.
30     */
31    private static final String TEMPLATE2_TMPL = "target/test-classes/templates/template2.tmpl"; //$NON-NLS-1$
32  
33    /**
34     * Template block 1 block name constant.
35     */
36    private static final String BLK1_BLK = "BLK1_BLK"; //$NON-NLS-1$
37  
38    /**
39     * Template block 1 name constant.
40     */
41    private static final String BLK1 = "BLK1"; //$NON-NLS-1$
42  
43    /**
44     * Template name constant.
45     */
46    private static final String TEMPLATE = "template"; //$NON-NLS-1$
47  
48    /**
49     * Template variable 1 name constant.
50     */
51    private static final String VARIABLE1 = "variable1"; //$NON-NLS-1$
52  
53    /**
54     * Hello world constant.
55     */
56    private static final String HELLO_WORLD = "hello world"; //$NON-NLS-1$
57  
58    /**
59     * Result not as expected constant.
60     */
61    private static final String RESULT_NOT_AS_EXPECTED = "Result not as expected"; //$NON-NLS-1$
62  
63  
64  
65    /**
66     * Template engine.
67     */
68    private TemplateEngine engine;
69  
70    /**
71     * Result from variable template.
72     */
73    private String resultVar = ""; //$NON-NLS-1$
74  
75    /**
76     * Result from block template.
77     */
78    private String resultBlock = ""; //$NON-NLS-1$
79  
80  
81    /**
82     * Default constructor.
83     */
84    public StepDefinitions()
85     {
86      super();
87     }
88  
89  
90    /**
91     * Given.
92     *
93     * @throws IOException IO exception
94     */
95    @Given("A template with a variable")
96    public void a_template_with_a_variable() throws IOException
97     {
98      this.engine = TemplateEngine.newInstance(new File(StepDefinitions.TEMPLATE1_TMPL));
99     }
100 
101 
102   /**
103    * When.
104    *
105    * @throws IOException IO exception
106    */
107   @When("The template is processed to replace the variable")
108   public void the_template_is_processed_to_replace_the_variable() throws IOException
109    {
110     this.engine.setVar(StepDefinitions.VARIABLE1, StepDefinitions.HELLO_WORLD);
111     this.resultVar = this.engine.subst(StepDefinitions.TEMPLATE);
112    }
113 
114 
115   /**
116    * Then.
117    */
118   @Then("The variable is replaced with the value hello world")
119   public void the_variable_is_replaced_with_the_value_hello_world()
120    {
121     assertEquals("123\nhello world\n456\n", this.resultVar, StepDefinitions.RESULT_NOT_AS_EXPECTED); //$NON-NLS-1$
122    }
123 
124 
125   /**
126    * Given.
127    *
128    * @throws IOException IO exception
129    */
130   @Given("A template with a block")
131   public void a_template_with_a_block() throws IOException
132    {
133     this.engine = TemplateEngine.newInstance(new File(StepDefinitions.TEMPLATE2_TMPL));
134    }
135 
136 
137   /**
138    * When.
139    *
140    * @throws IOException IO exception
141    */
142   @When("The template is processed to replace the block")
143   public void the_template_is_processed_to_replace_the_block() throws IOException
144    {
145     /* final boolean successBlock = */ this.engine.setBlock(StepDefinitions.TEMPLATE, StepDefinitions.BLK1, StepDefinitions.BLK1_BLK);
146     this.engine.setVar(StepDefinitions.BLK1, StepDefinitions.HELLO_WORLD);
147     this.engine.setVar(StepDefinitions.VARIABLE1, ""); //$NON-NLS-1$
148     this.engine.setVar("variable3", ""); //$NON-NLS-1$ //$NON-NLS-2$
149     /* String parseResult = */ this.engine.parse(StepDefinitions.BLK1_BLK, StepDefinitions.BLK1, false);
150     this.resultBlock = this.engine.subst(StepDefinitions.TEMPLATE);
151    }
152 
153 
154   /**
155    * Then.
156    */
157   @Then("The block is replaced with the value hello world")
158   public void the_block_is_replaced_with_the_value_hello_world()
159    {
160     assertEquals("123\n\n456\nhello world\ndef\n\nghi\n", this.resultBlock, StepDefinitions.RESULT_NOT_AS_EXPECTED); //$NON-NLS-1$
161    }
162 
163  }