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

File CompareVersionsTest.java

 

Code metrics

4
115
6
1
287
191
9
0.08
19.17
6
1.5

Classes

Class Line # Actions
CompareVersionsTest 58 115 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 3 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.test.ui;
21   
22    import java.text.SimpleDateFormat;
23    import java.util.Arrays;
24    import java.util.Date;
25    import java.util.HashMap;
26    import java.util.List;
27    import java.util.Map;
28   
29    import org.junit.AfterClass;
30    import org.junit.Before;
31    import org.junit.Rule;
32    import org.junit.Test;
33    import org.xwiki.tag.test.po.AddTagsPane;
34    import org.xwiki.tag.test.po.TaggablePage;
35    import org.xwiki.test.ui.po.AttachmentsPane;
36    import org.xwiki.test.ui.po.ChangesPane;
37    import org.xwiki.test.ui.po.CommentsTab;
38    import org.xwiki.test.ui.po.FormElement;
39    import org.xwiki.test.ui.po.HistoryPane;
40    import org.xwiki.test.ui.po.ViewPage;
41    import org.xwiki.test.ui.po.diff.DocumentDiffSummary;
42    import org.xwiki.test.ui.po.diff.EntityDiff;
43    import org.xwiki.test.ui.po.editor.ClassEditPage;
44    import org.xwiki.test.ui.po.editor.ObjectEditPage;
45    import org.xwiki.test.ui.po.editor.WikiEditPage;
46   
47    import static org.junit.Assert.assertEquals;
48    import static org.junit.Assert.assertTrue;
49    import static org.junit.Assert.assertThat;
50    import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
51   
52    /**
53    * Tests the comparison of document versions.
54    *
55    * @version $Id: 6668d05fa5a60a4a442ab77578f47a9504b4be8f $
56    * @since 4.2M1
57    */
 
58    public class CompareVersionsTest extends AbstractTest
59    {
60    @Rule
61    public AdminAuthenticationRule adminAuthenticationRule = new AdminAuthenticationRule(getUtil());
62   
63    /**
64    * The test page.
65    */
66    private ViewPage testPage;
67   
 
68  3 toggle @Before
69    public void setUp() throws Exception
70    {
71  3 String pageName = "PageWithManyVersions";
72   
73    // Check if the test page exists.
74  3 testPage = getUtil().gotoPage(getTestClassName(), pageName);
75  3 if (testPage.exists()) {
76    // TODO: Remove when XWIKI-6688 (Possible race condition when clicking on a tab at the bottom of a page in
77    // view mode) is fixed.
78  2 testPage.waitForDocExtraPaneActive("comments");
79    // NOTE: We use the same page for all tests because tests don't modify the test page. They only compare two
80    // versions of the test page.
81  2 return;
82    }
83   
84    // Since this test also verifies diffs when the parent/child relationship is modified, use this mode (which
85    // isn't the default) for testing purpose. Also note that in the "reference" mode there's no diff to display
86    // when the document is moved to a different parent since it means changing the identity of the document for
87    // now and thus changing it means getting a new document.
88  1 getUtil().setHierarchyMode("parentchild");
89  1 getDriver().navigate().refresh();
90   
91    // Create the test page.
92  1 testPage = getUtil().createPage(getTestClassName(), pageName, "one\ntwo\nthree", "Test");
93   
94    // Change the content and the meta data.
95  1 WikiEditPage wikiEditPage = testPage.editWiki();
96  1 wikiEditPage.setContent("one\n**two**\nfour");
97  1 wikiEditPage.setTitle("Compare verSions test");
98  1 wikiEditPage.setParent("Sandbox.WebHome");
99  1 wikiEditPage.setEditComment("Changed content and meta data.");
100  1 wikiEditPage.clickSaveAndContinue();
101  1 wikiEditPage.setTitle("Compare versions test");
102  1 wikiEditPage.setMinorEdit(true);
103  1 wikiEditPage.setEditComment("Fix typo in title.");
104  1 wikiEditPage.clickSaveAndContinue();
105   
106    // Add objects.
107  1 ObjectEditPage objectEditPage = wikiEditPage.editObjects();
108  1 FormElement form = objectEditPage.addObject("XWiki.JavaScriptExtension");
109  1 Map<String, String> assignment = new HashMap<String, String>();
110  1 assignment.put("XWiki.JavaScriptExtension_0_name", "JavaScript code");
111  1 assignment.put("XWiki.JavaScriptExtension_0_code", "var tmp = alice;\nalice = bob;\nbob = tmp;");
112  1 assignment.put("XWiki.JavaScriptExtension_0_use", "onDemand");
113  1 form.fillFieldsByName(assignment);
114  1 objectEditPage.clickSaveAndContinue();
115  1 assignment.put("XWiki.JavaScriptExtension_0_name", "Code snippet");
116  1 assignment.put("XWiki.JavaScriptExtension_0_code", "var tmp = alice;\nalice = 2 * bob;\nbob = tmp;");
117  1 form.fillFieldsByName(assignment);
118  1 objectEditPage.clickSaveAndContinue();
119   
120    // Create class.
121  1 ClassEditPage classEditPage = objectEditPage.editClass();
122  1 classEditPage.addProperty("age", "Number");
123  1 classEditPage.addProperty("color", "String");
124  1 classEditPage.getNumberClassEditElement("age").setNumberType("integer");
125  1 classEditPage.clickSaveAndContinue();
126  1 classEditPage.deleteProperty("color");
127  1 testPage = classEditPage.clickSaveAndView();
128   
129    // Add tags.
130  1 TaggablePage taggablePage = new TaggablePage();
131  1 AddTagsPane addTagsPane = taggablePage.addTags();
132  1 addTagsPane.setTags("foo,bar");
133  1 addTagsPane.add();
134  1 taggablePage.removeTag("foo");
135   
136    // Attach files.
137  1 AttachmentsPane attachmentsPane = testPage.openAttachmentsDocExtraPane();
138    // TODO: Update this code when we (re)add support for uploading multiple files at once.
139  1 for (String fileName : new String[] {"SmallAttachment.txt", "SmallAttachment2.txt", "SmallAttachment.txt"}) {
140  3 attachmentsPane.setFileToUpload(this.getClass().getResource('/' + fileName).getPath());
141  3 attachmentsPane.waitForUploadToFinish(fileName);
142  3 attachmentsPane.clickHideProgress();
143    }
144  1 attachmentsPane.deleteAttachmentByFileByName("SmallAttachment2.txt");
145   
146    // Add comments.
147  1 getUtil().createUserAndLogin("Alice", "ecila");
148  1 testPage = getUtil().gotoPage(getTestClassName(), pageName);
149  1 CommentsTab commentsTab = testPage.openCommentsDocExtraPane();
150  1 commentsTab.postComment("first line\nsecond line", true);
151  1 commentsTab.editCommentByID(0, "first line\nline in between\nsecond line");
152  1 commentsTab.replyToCommentByID(0, "this is a reply");
153  1 commentsTab.deleteCommentByID(1);
154    }
155   
 
156  1 toggle @AfterClass
157    public static void tearDownClass() throws Exception
158    {
159    // Put back the default hierarchy mode
160  1 getUtil().setHierarchyMode("reference");
161  1 getDriver().navigate().refresh();
162    }
163   
164    /**
165    * Tests that all changes are displayed.
166    */
 
167  1 toggle @Test
168    public void testAllChanges()
169    {
170  1 HistoryPane historyTab = testPage.openHistoryDocExtraPane().showMinorEdits();
171  1 String currentVersion = historyTab.getCurrentVersion();
172   
173    // TODO: If the document has many versions, like in this case, the versions are paginated and currently there's
174    // no way to compare two versions from two different pagination pages using the UI. Thus we have to build the
175    // URL and load the compare page manually. Update the code when we remove this UI limitation.
176    // ChangesPane changesPane = historyTab.compare("1.1", currentVersion).getChangesPane();
177  1 String queryString = String.format("viewer=changes&rev1=1.1&rev2=%s", currentVersion);
178  1 getUtil().gotoPage(getTestClassName(), testPage.getMetaDataValue("page"), "view", queryString);
179  1 ChangesPane changesPane = new ChangesPane();
180   
181    // Version summary.
182  1 String today = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
183  1 assertTrue(changesPane.getFromVersionSummary().startsWith(
184    "From version 1.1\nedited by Administrator\non " + today));
185  1 assertTrue(changesPane.getToVersionSummary().startsWith(
186    "To version " + currentVersion + "\nedited by Alice\non " + today));
187  1 assertEquals("Change comment: Deleted object", changesPane.getChangeComment());
188   
189    // Diff summary.
190  1 DocumentDiffSummary diffSummary = changesPane.getDiffSummary();
191  1 assertThat(Arrays.asList("Page properties", "Attachments", "Objects", "Class properties"),
192    containsInAnyOrder(diffSummary.getItems().toArray()));
193  1 assertEquals("(5 modified, 0 added, 0 removed)", diffSummary.getPagePropertiesSummary());
194  1 assertEquals("(0 modified, 1 added, 0 removed)", diffSummary.getAttachmentsSummary());
195  1 assertEquals("(0 modified, 2 added, 0 removed)", diffSummary.getObjectsSummary());
196  1 assertEquals("(0 modified, 1 added, 0 removed)", diffSummary.getClassPropertiesSummary());
197  1 assertEquals(Arrays.asList("SmallAttachment.txt"), diffSummary.toggleAttachmentsDetails().getAddedAttachments());
198  1 assertEquals(Arrays.asList("XWiki.JavaScriptExtension[0]", "XWiki.XWikiComments[0]"), diffSummary
199    .toggleObjectsDetails().getAddedObjects());
200  1 assertEquals(Arrays.asList("age"), diffSummary.toggleClassPropertiesDetails().getAddedClassProperties());
201   
202    // Diff details.
203  1 assertThat(Arrays.asList("Page properties", "SmallAttachment.txt", "XWiki.JavaScriptExtension[0]",
204    "XWiki.XWikiComments[0]", "age"), containsInAnyOrder(changesPane.getChangedEntities().toArray()));
205   
206    // Page properties changes.
207  1 EntityDiff pageProperties = changesPane.getEntityDiff("Page properties");
208  1 assertThat(Arrays.asList("Title", "Parent", "Author", "Tags", "Content"),
209    containsInAnyOrder(pageProperties.getPropertyNames().toArray()));
210  1 assertDiff(pageProperties.getDiff("Title"), "-<del>T</del>est",
211    "+<ins>Compar</ins>e<ins> ver</ins>s<ins>ions </ins>t<ins>est</ins>");
212  1 assertDiff(pageProperties.getDiff("Parent"), "+Sandbox.WebHome");
213  1 assertDiff(pageProperties.getDiff("Author"), "-XWiki.A<del>dm</del>i<del>n</del>",
214    "+XWiki.A<ins>l</ins>i<ins>ce</ins>");
215  1 assertDiff(pageProperties.getDiff("Tags"), "+bar");
216  1 assertDiff(pageProperties.getDiff("Content"), "@@ -1,3 +1,3 @@", " one", "-two",
217    "-<del>th</del>r<del>ee</del>", "+<ins>**</ins>two<ins>**</ins>", "+<ins>fou</ins>r");
218   
219    // Attachment changes.
220  1 EntityDiff attachmentDiff = changesPane.getEntityDiff("SmallAttachment.txt");
221  1 assertThat(Arrays.asList("Author", "Size", "Content"),
222    containsInAnyOrder(attachmentDiff.getPropertyNames().toArray()));
223  1 assertDiff(attachmentDiff.getDiff("Author"), "+XWiki.Admin");
224  1 assertDiff(attachmentDiff.getDiff("Size"), "+27 bytes");
225  1 assertDiff(attachmentDiff.getDiff("Content"), "+This is a small attachment.");
226   
227    // Object changes.
228  1 EntityDiff jsxDiff = changesPane.getEntityDiff("XWiki.JavaScriptExtension[0]");
229  1 assertThat(Arrays.asList("Caching policy", "Name", "Use this extension", "Code"),
230    containsInAnyOrder(jsxDiff.getPropertyNames().toArray()));
231  1 assertDiff(jsxDiff.getDiff("Caching policy"), "+long");
232  1 assertDiff(jsxDiff.getDiff("Name"), "+Code snippet");
233  1 assertDiff(jsxDiff.getDiff("Use this extension"), "+onDemand");
234  1 assertDiff(jsxDiff.getDiff("Code"), "+var tmp = alice;", "+alice = 2 * bob;", "+bob = tmp;");
235   
236    // Comment changes.
237  1 EntityDiff commentDiff = changesPane.getEntityDiff("XWiki.XWikiComments[0]");
238  1 assertThat(Arrays.asList("Author", "Date", "Comment"),
239    containsInAnyOrder(commentDiff.getPropertyNames().toArray()));
240  1 assertDiff(commentDiff.getDiff("Author"), "+XWiki.Alice");
241  1 assertEquals(2, commentDiff.getDiff("Date").size());
242  1 assertDiff(commentDiff.getDiff("Comment"), "+first line", "+line in between", "+second line");
243   
244    // Class property changes.
245  1 EntityDiff ageDiff = changesPane.getEntityDiff("age");
246  1 assertThat(Arrays.asList("Name", "Number", "Pretty Name", "Size", "Number Type"),
247    containsInAnyOrder(ageDiff.getPropertyNames().toArray()));
248  1 assertDiff(ageDiff.getDiff("Name"), "+age");
249  1 assertDiff(ageDiff.getDiff("Number"), "+1");
250  1 assertDiff(ageDiff.getDiff("Pretty Name"), "+age");
251  1 assertDiff(ageDiff.getDiff("Size"), "+30");
252  1 assertDiff(ageDiff.getDiff("Number Type"), "+integer");
253    }
254   
255    /**
256    * Tests that a message is displayed when there are no changes.
257    */
 
258  1 toggle @Test
259    public void testNoChanges()
260    {
261  1 HistoryPane historyTab = testPage.openHistoryDocExtraPane();
262  1 String currentVersion = historyTab.getCurrentVersion();
263  1 assertTrue(historyTab.compare(currentVersion, currentVersion).getChangesPane().hasNoChanges());
264    }
265   
266    /**
267    * Tests that the unified diff (for multi-line text) shows the inline changes.
268    */
 
269  1 toggle @Test
270    public void testUnifiedDiffShowsInlineChanges()
271    {
272  1 ChangesPane changesPane =
273    testPage.openHistoryDocExtraPane().showMinorEdits().compare("2.2", "2.3").getChangesPane();
274  1 EntityDiff jsxDiff = changesPane.getEntityDiff("XWiki.JavaScriptExtension[0]");
275  1 assertDiff(jsxDiff.getDiff("Code"), "@@ -1,3 +1,3 @@", " var tmp = alice;", "-alice = bob;",
276    "+alice = <ins>2 * </ins>bob;", " bob = tmp;");
277    }
278   
 
279  20 toggle private void assertDiff(List<String> actualLines, String... expectedLines)
280    {
281  20 if (expectedLines.length > 0 && !expectedLines[0].startsWith("@@")) {
282  18 assertEquals(Arrays.asList(expectedLines), actualLines.subList(1, actualLines.size()));
283    } else {
284  2 assertEquals(Arrays.asList(expectedLines), actualLines);
285    }
286    }
287    }