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

File XClassMigratorListenerTest.java

 

Code metrics

0
23
4
1
150
105
4
0.17
5.75
4
1

Classes

Class Line # Actions
XClassMigratorListenerTest 84 23 0% 4 0
1.0100%
 

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.internal.objects.classes;
21   
22    import java.util.Arrays;
23   
24    import org.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.model.internal.DefaultModelConfiguration;
28    import org.xwiki.model.internal.reference.DefaultEntityReferenceProvider;
29    import org.xwiki.model.internal.reference.DefaultStringDocumentReferenceResolver;
30    import org.xwiki.model.internal.reference.DefaultStringEntityReferenceResolver;
31    import org.xwiki.model.internal.reference.DefaultStringEntityReferenceSerializer;
32    import org.xwiki.model.internal.reference.DefaultSymbolScheme;
33    import org.xwiki.model.internal.reference.LocalStringEntityReferenceSerializer;
34    import org.xwiki.model.internal.reference.LocalUidStringEntityReferenceSerializer;
35    import org.xwiki.model.reference.DocumentReference;
36    import org.xwiki.observation.internal.DefaultObservationManager;
37    import org.xwiki.query.Query;
38    import org.xwiki.query.QueryManager;
39    import org.xwiki.rendering.syntax.Syntax;
40    import org.xwiki.test.annotation.AfterComponent;
41    import org.xwiki.test.annotation.ComponentList;
42   
43    import com.xpn.xwiki.XWikiException;
44    import com.xpn.xwiki.doc.XWikiDocument;
45    import com.xpn.xwiki.internal.event.XClassPropertyEventGeneratorListener;
46    import com.xpn.xwiki.internal.model.reference.CurrentEntityReferenceProvider;
47    import com.xpn.xwiki.internal.model.reference.CurrentReferenceDocumentReferenceResolver;
48    import com.xpn.xwiki.internal.model.reference.CurrentReferenceEntityReferenceResolver;
49    import com.xpn.xwiki.internal.model.reference.CurrentStringDocumentReferenceResolver;
50    import com.xpn.xwiki.internal.model.reference.CurrentStringEntityReferenceResolver;
51    import com.xpn.xwiki.internal.store.PropertyConverter;
52    import com.xpn.xwiki.objects.BaseObject;
53    import com.xpn.xwiki.objects.BaseProperty;
54    import com.xpn.xwiki.test.MockitoOldcoreRule;
55   
56    import static org.junit.Assert.assertEquals;
57    import static org.mockito.Mockito.mock;
58    import static org.mockito.Mockito.when;
59   
60    /**
61    * Validate {@link XClassMigratorListener}.
62    *
63    * @version $Id: 2ee1e6776b33c7f950f4cb7a3f779ba3bbd60d10 $
64    */
65    @ComponentList({
66    XClassMigratorListener.class,
67    DefaultObservationManager.class,
68    LocalStringEntityReferenceSerializer.class,
69    DefaultStringDocumentReferenceResolver.class,
70    PropertyConverter.class,
71    CurrentReferenceDocumentReferenceResolver.class,
72    CurrentReferenceEntityReferenceResolver.class,
73    CurrentEntityReferenceProvider.class,
74    DefaultModelConfiguration.class,
75    DefaultStringEntityReferenceResolver.class,
76    DefaultEntityReferenceProvider.class,
77    XClassPropertyEventGeneratorListener.class,
78    CurrentStringDocumentReferenceResolver.class,
79    CurrentStringEntityReferenceResolver.class,
80    LocalUidStringEntityReferenceSerializer.class,
81    DefaultStringEntityReferenceSerializer.class,
82    DefaultSymbolScheme.class
83    })
 
84    public class XClassMigratorListenerTest
85    {
86    @Rule
87    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
88   
89    private XWikiDocument xobjectDocument;
90   
91    private XWikiDocument xclassDocument;
92   
93    private QueryManager mockQueryManager;
94   
 
95  1 toggle @AfterComponent
96    public void afterComponent() throws Exception
97    {
98  1 this.mockQueryManager = this.oldcore.getMocker().registerMockComponent(QueryManager.class);
99    }
100   
 
101  1 toggle @Before
102    public void before() throws Exception
103    {
104  1 this.xclassDocument = new XWikiDocument(new DocumentReference("wiki", "Space", "Class"));
105  1 this.xclassDocument.setSyntax(Syntax.PLAIN_1_0);
106   
107  1 this.xobjectDocument = new XWikiDocument(new DocumentReference("wiki", "Space", "Page"));
108  1 this.xobjectDocument.setSyntax(Syntax.PLAIN_1_0);
109  1 BaseObject xobject = new BaseObject();
110  1 xobject.setXClassReference(this.xclassDocument.getDocumentReference());
111  1 this.xobjectDocument.addXObject(xobject);
112   
113  1 Query query = mock(Query.class);
114  1 when(this.mockQueryManager.createQuery("from doc.object(Space.Class) as obj", Query.XWQL)).thenReturn(query);
115  1 when(query.<String>execute()).thenReturn(Arrays.asList("Space.Page"));
116   
117    // We need document modification notifications
118  1 this.oldcore.notifyDocumentUpdatedEvent(true);
119    }
120   
 
121  1 toggle @Test
122    public void migrateProperty() throws Exception
123    {
124  1 this.xclassDocument.getXClass().addTextField("property", "property", 30);
125  1 saveXClassDocument();
126  1 this.xobjectDocument.setStringValue(this.xclassDocument.getDocumentReference(), "property", "42");
127  1 this.oldcore.getSpyXWiki().saveDocument(this.xobjectDocument, this.oldcore.getXWikiContext());
128   
129    // Modify the class
130  1 this.xclassDocument.getXClass().removeField("property");
131  1 this.xclassDocument.getXClass().addNumberField("property", "property", 30, "integer");
132  1 saveXClassDocument();
133   
134    // Verify the document has been modified
135  1 this.xobjectDocument =
136    this.oldcore.getSpyXWiki().getDocument(this.xobjectDocument.getDocumentReference(),
137    this.oldcore.getXWikiContext());
138   
139  1 assertEquals(42, ((BaseProperty) this.xobjectDocument.getXObject(this.xclassDocument.getDocumentReference())
140    .get("property")).getValue());
141    }
142   
 
143  2 toggle private void saveXClassDocument() throws XWikiException
144    {
145  2 this.oldcore.getSpyXWiki().saveDocument(this.xclassDocument, this.oldcore.getXWikiContext());
146  2 this.xclassDocument =
147    this.oldcore.getSpyXWiki().getDocument(this.xclassDocument.getDocumentReference(),
148    this.oldcore.getXWikiContext());
149    }
150    }