1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.annotation

File AnnotationsMockSetup.java

 

Coverage histogram

../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

4
37
10
1
192
99
12
0.32
3.7
10
1.2

Classes

Class Line # Actions
AnnotationsMockSetup 45 37 0% 12 11
0.7843137478.4%
 

Contributing tests

This file is covered by 49 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.annotation;
21   
22    import java.io.IOException;
23    import java.util.Collection;
24   
25    import org.hamcrest.Description;
26    import org.jmock.Expectations;
27    import org.jmock.Mockery;
28    import org.jmock.api.Action;
29    import org.jmock.api.Invocation;
30    import org.jmock.integration.junit4.JUnit4Mockery;
31    import org.xwiki.annotation.io.IOService;
32    import org.xwiki.annotation.io.IOServiceException;
33    import org.xwiki.annotation.io.IOTargetService;
34    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
35    import org.xwiki.component.manager.ComponentManager;
36    import org.xwiki.component.manager.ComponentRepositoryException;
37   
38    /**
39    * Mock setup for the annotations tests, mocking the {@link IOService} and {@link IOTargetService} to provide documents
40    * functions for the data in the test files.
41    *
42    * @version $Id: 35d96bf2dd859364436be549838814ec9a021fbf $
43    * @since 2.3M1
44    */
 
45    public class AnnotationsMockSetup
46    {
47    /**
48    * Mockery to setup IO services in this test, setup as a JUnit4 mockery so that tests fail when expectations are not
49    * met so that we test components through invocation expectations.
50    */
51    protected Mockery mockery = new JUnit4Mockery();
52   
53    /**
54    * IOTargetService used by this test.
55    */
56    protected IOTargetService ioTargetService;
57   
58    /**
59    * IOService used in this test.
60    */
61    protected IOService ioService;
62   
63    /**
64    * The document factory used to load documents from the test files.
65    */
66    protected TestDocumentFactory docFactory;
67   
68    /**
69    * Builds an annotation mock setup registering mocked {@link IOService} and {@link IOTargetService} to manipulate
70    * documents from the test description files.
71    *
72    * @param componentManager the component manager to register the services with
73    * @param docFactory the document factory used to load documents from the test files
74    * @throws ComponentRepositoryException if the components cannot be registered
75    */
 
76  49 toggle public AnnotationsMockSetup(ComponentManager componentManager, TestDocumentFactory docFactory)
77    throws ComponentRepositoryException
78    {
79    // IOTargetService mockup
80  49 ioTargetService = mockery.mock(IOTargetService.class);
81  49 DefaultComponentDescriptor<IOTargetService> iotsDesc = new DefaultComponentDescriptor<IOTargetService>();
82  49 iotsDesc.setRole(IOTargetService.class);
83  49 componentManager.registerComponent(iotsDesc, ioTargetService);
84   
85    // IOService mockup
86  49 ioService = mockery.mock(IOService.class);
87  49 DefaultComponentDescriptor<IOService> ioDesc = new DefaultComponentDescriptor<IOService>();
88  49 ioDesc.setRole(IOService.class);
89  49 componentManager.registerComponent(ioDesc, ioService);
90   
91  49 this.docFactory = docFactory;
92    }
93   
94    /**
95    * Sets up the expectations for the {@link IOService} and {@link IOTargetService} to return correctly the values in
96    * the test files for {@code docName}. Call this function when operating with mocked documents to provide all the
97    * information in the test file (document source, rendered contents, annotations).
98    *
99    * @param docName the name of the document to setup expectations for
100    * @throws IOServiceException if something wrong happens while mocking the documents access
101    * @throws IOException if something wrong happens while mocking the documents access
102    */
 
103  49 toggle public void setupExpectations(final String docName) throws IOServiceException, IOException
104    {
105  49 mockery.checking(new Expectations()
106    {
 
107  49 toggle {
108  49 MockDocument mDoc = docFactory.getDocument(docName);
109   
110  49 allowing(ioService).getAnnotations(with(docName));
111  49 will(returnValue(mDoc.getAnnotations()));
112   
113  49 allowing(ioService).updateAnnotations(with(docName), with(any(Collection.class)));
114    // update the list of document annotations
115  49 will(new Action()
116    {
 
117  0 toggle @Override
118    public void describeTo(Description description)
119    {
120  0 description.appendText("Updates the annotations");
121    }
122   
 
123  49 toggle @Override
124    public Object invoke(Invocation invocation) throws Throwable
125    {
126  49 String documentName = (String) invocation.getParameter(0);
127  49 MockDocument document = docFactory.getDocument(documentName);
128  49 Collection<Annotation> annList = (Collection<Annotation>) invocation.getParameter(1);
129  49 for (Annotation ann : annList) {
130  35 Annotation toUpdate = getAnnotation(ann.getId(), document.getAnnotations());
131    // remove toUpdate and add ann
132  35 if (toUpdate != null) {
133  35 document.getAnnotations().remove(toUpdate);
134    }
135  35 document.getAnnotations().add(ann);
136    }
137  49 return null;
138    }
139   
 
140  35 toggle private Annotation getAnnotation(String annId, Collection<Annotation> list)
141    {
142  35 for (Annotation ann : list) {
143  35 if (ann.getId().equals(annId)) {
144  35 return ann;
145    }
146    }
147   
148  0 return null;
149    }
150    });
151   
152  49 allowing(ioTargetService).getSource(with(docName));
153  49 will(returnValue(mDoc.getSource()));
154   
155  49 allowing(ioTargetService).getSourceSyntax(with(docName));
156  49 will(returnValue(mDoc.getSyntax()));
157    }
158    });
159    }
160   
161    /**
162    * @return the mockery
163    */
 
164  0 toggle public Mockery getMockery()
165    {
166  0 return mockery;
167    }
168   
169    /**
170    * @return the ioTargetService
171    */
 
172  0 toggle public IOTargetService getIoTargetService()
173    {
174  0 return ioTargetService;
175    }
176   
177    /**
178    * @return the ioService
179    */
 
180  0 toggle public IOService getIoService()
181    {
182  0 return ioService;
183    }
184   
185    /**
186    * @return the docFactory
187    */
 
188  49 toggle public TestDocumentFactory getDocFactory()
189    {
190  49 return docFactory;
191    }
192    }