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.console

File MgnlGroovyConsoleTest.java

 

Code metrics

0
19
5
1
122
57
5
0.26
3.8
5
1

Classes

Class Line # Actions
MgnlGroovyConsoleTest 59 19 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /**
2    * This file Copyright (c) 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.console;
35   
36    import static org.mockito.Matchers.*;
37    import static org.mockito.Mockito.*;
38   
39    import info.magnolia.context.Context;
40    import info.magnolia.context.MgnlContext;
41    import info.magnolia.context.SystemContext;
42    import info.magnolia.i18nsystem.SimpleTranslator;
43    import info.magnolia.module.groovy.console.MgnlGroovyConsole.ScriptCallback;
44    import info.magnolia.objectfactory.ComponentProvider;
45    import info.magnolia.test.ComponentsTestUtil;
46    import info.magnolia.ui.framework.message.MessagesManager;
47   
48    import javax.jcr.Session;
49   
50    import org.junit.After;
51    import org.junit.Before;
52    import org.junit.Test;
53   
54    import com.vaadin.event.UIEvents.PollListener;
55    import com.vaadin.ui.UI;
56   
57    import groovy.lang.Binding;
58   
 
59    public class MgnlGroovyConsoleTest {
60   
61    private MgnlGroovyConsole console;
62    private ScriptCallback callback;
63    private UI ui;
64   
 
65  3 toggle @Before
66    public void setUp() throws Exception {
67  3 MgnlContext.setInstance(mock(Context.class));
68  3 ComponentsTestUtil.setInstance(ComponentProvider.class, mock(ComponentProvider.class));
69   
70  3 Session session = mock(Session.class);
71  3 when(session.nodeExists(anyString())).thenReturn(false);
72   
73  3 SystemContext systemContext = mock(SystemContext.class);
74  3 when(systemContext.getJCRSession(anyString())).thenReturn(session);
75  3 ComponentsTestUtil.setInstance(SystemContext.class, systemContext);
76   
77  3 console = new MgnlGroovyConsole(new Binding(), mock(MessagesManager.class), mock(SimpleTranslator.class));
78  3 callback = mock(ScriptCallback.class);
79  3 ui = mock(UI.class);
80    }
81   
 
82  3 toggle @After
83    public void tearDown() throws Exception {
84  3 ComponentsTestUtil.clear();
85  3 MgnlContext.setInstance(null);
86    }
87   
 
88  1 toggle @Test
89    public void fastScriptReturnsWithoutStartingPolling() throws Exception {
90    // GIVEN
91   
92    // WHEN
93  1 console.runAsync("return 'hello'", ui, callback);
94   
95    // THEN
96  1 verify(ui, never()).addPollListener(any(PollListener.class));
97  1 verify(callback).onSuccess("hello");
98    }
99   
100   
 
101  1 toggle @Test
102    public void longRunningScriptStartsPolling() throws Exception {
103    // GIVEN
104   
105    // WHEN
106  1 console.runAsync("Thread.sleep(1000); return 'done'", ui, callback);
107   
108    // THEN
109  1 verify(ui, times(1)).addPollListener(any(PollListener.class));
110    }
111   
 
112  1 toggle @Test
113    public void scriptWithNoReturnStatementDoesNotPrintOutNull() throws Exception {
114    // GIVEN
115   
116    // WHEN
117  1 console.runAsync("for(i=0; i< 3; i++) {}", ui, callback);
118   
119    // THEN
120  1 verify(callback).onSuccess("");
121    }
122    }