1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.internal.objects.classes

File XClassMigratorListenerTest.java

 

Code metrics

0
29
4
1
147
92
4
0.14
7.25
4
1

Classes

Class Line # Actions
XClassMigratorListenerTest 60 29 0% 4 0
1.0100%
 

Contributing tests

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