1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.api

File ContextTest.java

 

Code metrics

0
42
7
2
153
100
7
0.17
6
3.5
1

Classes

Class Line # Actions
ContextTest 53 39 0% 4 2
0.9534883595.3%
ContextTest.XWikiDocumentMatcher 55 3 0% 3 6
0.00%
 

Contributing tests

This file is covered by 1 test. .

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 com.xpn.xwiki.api;
21   
22    import java.util.Locale;
23   
24    import org.apache.velocity.VelocityContext;
25    import org.hamcrest.Description;
26    import org.hamcrest.Factory;
27    import org.hamcrest.Matcher;
28    import org.hamcrest.TypeSafeMatcher;
29    import org.jmock.Expectations;
30    import org.jmock.lib.legacy.ClassImposteriser;
31    import org.junit.Assert;
32    import org.junit.Before;
33    import org.junit.Test;
34    import org.xwiki.context.Execution;
35    import org.xwiki.model.reference.DocumentReference;
36    import org.xwiki.rendering.syntax.Syntax;
37    import org.xwiki.test.jmock.AbstractComponentTestCase;
38    import org.xwiki.velocity.VelocityManager;
39   
40    import com.xpn.xwiki.CoreConfiguration;
41    import com.xpn.xwiki.XWikiContext;
42    import com.xpn.xwiki.doc.XWikiDocument;
43    import com.xpn.xwiki.objects.BaseObject;
44    import com.xpn.xwiki.objects.classes.BaseClass;
45    import com.xpn.xwiki.web.Utils;
46   
47    /**
48    * Unit tests for {@link Context}.
49    *
50    * @version $Id: d7090064fbff5698ca7f90473a08c980e3f2de20 $
51    * @since 4.4RC1
52    */
 
53    public class ContextTest extends AbstractComponentTestCase
54    {
 
55    public static class XWikiDocumentMatcher extends TypeSafeMatcher<XWikiDocument>
56    {
57    private DocumentReference documentReference;
58   
 
59  0 toggle public XWikiDocumentMatcher(DocumentReference documentReference)
60    {
61  0 this.documentReference = documentReference;
62    }
63   
 
64  0 toggle @Override
65    protected boolean matchesSafely(XWikiDocument document)
66    {
67  0 return documentReference.equals(document.getDocumentReference());
68    }
69   
 
70  0 toggle @Override
71    public void describeTo(Description description)
72    {
73  0 description.appendText("a document with a reference equal to [")
74    .appendValue(this.documentReference)
75    .appendText("]");
76    }
77    }
78   
 
79  0 toggle @Factory
80    public static Matcher<XWikiDocument> anXWikiDocumentWithReference(DocumentReference documentReference)
81    {
82  0 return new XWikiDocumentMatcher(documentReference);
83    }
84   
 
85  1 toggle @Before
86    public void configure() throws Exception
87    {
88  1 getMockery().setImposteriser(ClassImposteriser.INSTANCE);
89  1 Utils.setComponentManager(getComponentManager());
90    }
91   
92    /**
93    * Tests that pages can override the default property display mode using {@code $xcontext.setDisplayMode}.
94    *
95    * @see "XWIKI-2436."
96    */
 
97  1 toggle @Test
98    public void setDisplayMode() throws Exception
99    {
100    // Setup Context and XWiki objects
101  1 final XWikiContext xcontext = new XWikiContext();
102  1 xcontext.setMainXWiki("testwiki");
103  1 xcontext.setWikiId("testwiki");
104   
105  1 final com.xpn.xwiki.XWiki xwiki = getMockery().mock(com.xpn.xwiki.XWiki.class);
106  1 xcontext.setWiki(xwiki);
107   
108  1 final CoreConfiguration coreConfiguration = registerMockComponent(CoreConfiguration.class);
109  1 final VelocityManager velocityManager = registerMockComponent(VelocityManager.class);
110   
111  1 final DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
112  1 final XWikiDocument document = new XWikiDocument(documentReference);
113  1 final BaseClass baseClass = document.getXClass();
114   
115  1 getMockery().checking(new Expectations()
 
116  1 toggle {{
117  1 allowing(xwiki).getCurrentContentSyntaxId("xwiki/2.1", xcontext);
118  1 will(returnValue("xwiki/2.1"));
119  1 allowing(coreConfiguration).getDefaultDocumentSyntax();
120  1 will(returnValue(Syntax.XWIKI_2_1));
121  1 allowing(velocityManager).getVelocityContext();
122  1 will(returnValue(new VelocityContext()));
123  1 allowing(xwiki).getLanguagePreference(xcontext);
124  1 will(returnValue("en"));
125    // Translated document
126  1 allowing(xwiki).getDocument(with(equal(new DocumentReference(documentReference, Locale.ENGLISH))), with(same(xcontext)));
127  1 will(returnValue(document));
128  1 allowing(xwiki).getXClass(documentReference, xcontext);
129  1 will(returnValue(baseClass));
130    // Decide that there's no custom Displayer for the String field
131  1 allowing(xwiki).exists(new DocumentReference("testwiki", "XWiki", "StringDisplayer"), xcontext);
132  1 will(returnValue(false));
133  1 allowing(xwiki).evaluateTemplate("displayer_string.vm", xcontext);
134  1 will(returnValue(""));
135    }});
136   
137  1 baseClass.addTextField("prop", "prop", 5);
138  1 BaseObject obj = (BaseObject) document.getXClass().newObject(xcontext);
139  1 obj.setStringValue("prop", "value");
140  1 document.addXObject(obj);
141   
142    // Tie together Execution Context and old XWiki Context
143  1 Execution execution = getComponentManager().getInstance(Execution.class);
144  1 execution.getContext().setProperty("xwikicontext", xcontext);
145   
146  1 Context context = new Context(xcontext);
147  1 context.setDisplayMode("edit");
148   
149    // We verify that the result contains a form input
150  1 Assert.assertEquals("<input size='5' id='space.page_0_prop' value='value' name='space.page_0_prop' "
151    + "type='text'/>", document.display("prop", xcontext));
152    }
153    }