1   /*******************************************************************************
2    * Copyright (c) 2009 Ordina and committers to Mod4j
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *     Ordina - initial implementation
10   *******************************************************************************/
11  package org.mod4j.dslcommon.openarchitectureware;
12  
13  import static org.junit.Assert.*;
14  
15  import java.io.File;
16  import java.io.IOException;
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import org.junit.Before;
21  import org.junit.Test;
22  
23  /**
24   * @author johan
25   * 
26   */
27  public class Mod4WorklowRunnerTest {
28  
29      private String wfFile;
30  
31      private Map<String, String> properties = new HashMap<String, String>();
32  
33      private static final String TESTDIR = "target/mod4j-tests";
34  
35      @Before
36      public void createTestDir() {
37          new File(TESTDIR).mkdirs();
38      }
39  
40      /**
41       * Test method for
42       * {@link org.mod4j.dslcommon.openarchitectureware.Mod4jWorkflowRunner#runWorkflow(java.lang.String, java.util.Map)}
43       * .
44       */
45      @Test
46      public final void testRunWorkflowCleanerSuccessful() {
47  
48          Mod4jWorkflowRunner workflowRunner = new Mod4jWorkflowRunner();
49          File testFile = new File(TESTDIR + "/dummyFile.txt");
50          properties.put("directories", TESTDIR);
51          wfFile = "org/mod4j/dslcommon/workflow/DirectoryClean.oaw";
52  
53          try {
54              if (testFile.exists() || testFile.createNewFile()) {
55                  workflowRunner.runWorkflow(wfFile, properties);
56              }
57              assertFalse("dummyFile expected to be cleaned [" + testFile.getAbsolutePath() + "]", testFile.exists());
58  
59          } catch (Mod4jWorkflowException e) {
60              e.printStackTrace();
61              fail("A Mod4jWorkflowException was thrown. Expected to run successful.");
62          } catch (IOException e) {
63              e.printStackTrace();
64              fail("Cannot ceate test file in the system temporal directory");
65          }
66      }
67  
68      /**
69       * Test method for
70       * {@link org.mod4j.dslcommon.openarchitectureware.Mod4jWorkflowRunner#runWorkflow(java.lang.String, java.util.Map)}
71       * . When the workflow fails it should throw a exception.
72       */
73      @Test
74      public final void testRunWorkflowCleanerFail() {
75  
76          Mod4jWorkflowRunner workflowRunner = new Mod4jWorkflowRunner();
77          wfFile = "src/test/resources/TestWorkflowCleaner.oaw";
78  
79          try {
80              workflowRunner.runWorkflow(wfFile, properties);
81              fail("A Mod4jWorkflowException was expected to be thrown.");
82          } catch (Mod4jWorkflowException e) {
83              // Ok this is what we expected to happen.
84              assertTrue("Caught a Mod4jWorkflowException", e.getMessage().startsWith(
85                      "ERROR(S) detected while running workflow"));
86          }
87      }
88  }