Clover icon

magnolia-module-groovy 2.4.7

  1. Project Clover database Thu Dec 1 2016 10:48:40 CET
  2. Package info.magnolia.module.groovy.task

File ExecuteGroovyFileTaskTest.java

 

Code metrics

0
43
8
1
171
98
11
0.26
5.38
8
1.38

Classes

Class Line # Actions
ExecuteGroovyFileTaskTest 66 43 0% 11 3
0.941176594.1%
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    /**
2    * This file Copyright (c) 2011-2016 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.module.groovy.task;
35   
36    import static info.magnolia.module.groovy.GroovyModule.SCRIPTS_REPOSITORY_NAME;
37    import static org.junit.Assert.*;
38    import static org.mockito.BDDMockito.given;
39    import static org.mockito.Mockito.mock;
40   
41    import info.magnolia.context.MgnlContext;
42    import info.magnolia.context.SystemContext;
43    import info.magnolia.i18nsystem.SimpleTranslator;
44    import info.magnolia.module.InstallContext;
45    import info.magnolia.module.InstallContext.Message;
46    import info.magnolia.module.InstallContext.MessagePriority;
47    import info.magnolia.module.InstallContextImpl;
48    import info.magnolia.module.ModuleRegistryImpl;
49    import info.magnolia.module.delta.TaskExecutionException;
50    import info.magnolia.test.ComponentsTestUtil;
51    import info.magnolia.ui.framework.message.MessagesManager;
52   
53    import java.io.IOException;
54    import java.util.List;
55    import java.util.Map;
56   
57    import javax.jcr.Session;
58   
59    import org.junit.After;
60    import org.junit.Before;
61    import org.junit.Test;
62   
63    import groovy.lang.GroovyRuntimeException;
64   
65   
 
66    public class ExecuteGroovyFileTaskTest {
67   
 
68  5 toggle @Before
69    public void setUp() throws Exception {
70  5 SystemContext context = mock(SystemContext.class);
71  5 Session session = mock(Session.class);
72  5 given(context.getJCRSession(SCRIPTS_REPOSITORY_NAME)).willReturn(session);
73  5 ComponentsTestUtil.setInstance(SystemContext.class, context);
74  5 ComponentsTestUtil.setInstance(MessagesManager.class, mock(MessagesManager.class));
75  5 ComponentsTestUtil.setInstance(SimpleTranslator.class, mock(SimpleTranslator.class));
76  5 MgnlContext.setInstance(context);
77    }
78   
 
79  5 toggle @After
80    public void tearDown() throws Exception {
81  5 ComponentsTestUtil.setInstance(SystemContext.class, null);
82  5 MgnlContext.setInstance(null);
83    }
84   
 
85  1 toggle @Test
86    public void inputFileNotExisting() throws Exception {
87    // GIVEN
88  1 String inputFilename = "info/magnolia/module/groovy/test/notExisting.groovy";
89  1 try {
90    // WHEN
91  1 doTestExecuteTask(inputFilename);
92    // THEN
93  0 fail("Should have faced an exception of type IOException");
94    } catch (TaskExecutionException tee) {
95  1 Throwable throwable = tee.getCause();
96    // THEN
97  1 assertTrue("Exception should be of a subtype of IOException ", throwable instanceof IOException);
98    }
99    }
100   
 
101  1 toggle @Test
102    public void inputFileNull() throws Exception {
103    // GIVEN
104  1 String inputFilename = null;
105  1 try {
106    // WHEN
107  1 doTestExecuteTask(inputFilename);
108    // THEN
109  0 fail("Should have faced an exception of type IOException");
110    } catch (TaskExecutionException tee) {
111  1 Throwable throwable = tee.getCause();
112    // THEN
113  1 assertTrue("Exception should be of a subtype of IOException ", throwable instanceof IOException);
114    }
115    }
116   
 
117  1 toggle @Test
118    public void inputFileEmpty() throws Exception {
119    // GIVEN
120  1 String inputFilename = "info/magnolia/module/groovy/test/task/Empty.groovy";
121   
122    // WHEN
123   
124  1 InstallContext installContext = doTestExecuteTask(inputFilename);
125   
126    // THEN
127  1 Map<String, List<Message>> messages = installContext.getMessages();
128  1 assertNotNull("Should have messages", messages);
129  1 assertEquals("Message should be of priority Info", messages.get("General messages").get(0).getPriority(), MessagePriority.info);
130  1 assertEquals("This message should be empty ", messages.get("General messages").get(0).getMessage().length(), 0);
131    }
132   
 
133  1 toggle @Test
134    public void inputFileGeneratingGroovyException() throws Exception {
135    // GIVEN
136  1 String inputFilename = "info/magnolia/module/groovy/test/Buggy.groovy";
137  1 try {
138    // WHEN
139  1 doTestExecuteTask(inputFilename);
140    // THEN
141  0 fail("Should have faced an exception of type GroovyRuntimeException");
142    } catch (TaskExecutionException tee) {
143  1 Throwable throwable = tee.getCause();
144    // THEN
145  1 assertTrue("Exception should be of a subtype of GroovyRuntimeException ", throwable instanceof GroovyRuntimeException);
146    }
147    }
148   
 
149  1 toggle @Test
150    public void inputFileHappyFlow() throws Exception {
151    // GIVEN
152  1 String inputFilename = "info/magnolia/module/groovy/test/task/Dummy.groovy";
153   
154    // WHEN
155  1 InstallContext installContext = doTestExecuteTask(inputFilename);
156    // THEN
157  1 Map<String, List<Message>> messages = installContext.getMessages();
158  1 assertNotNull("Should have messages", messages);
159  1 assertEquals("Message should be of priority Info", messages.get("General messages").get(0).getPriority(), MessagePriority.info);
160  1 assertEquals("This message should be 'Init dialog map' ", messages.get("General messages").get(0).getMessage(), "Init dialog map\n");
161    }
162   
 
163  5 toggle private InstallContext doTestExecuteTask(String fileName) throws TaskExecutionException {
164  5 final InstallContext installCtx = new InstallContextImpl(new ModuleRegistryImpl());
165   
166  5 final ExecuteGroovyFileTask task = new ExecuteGroovyFileTask("Test", "Description", fileName);
167  5 task.execute(installCtx);
168   
169  2 return installCtx;
170    }
171    }