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

File DocumentCacheTest.java

 

Code metrics

14
64
4
1
161
105
11
0.17
16
4
2.75

Classes

Class Line # Actions
DocumentCacheTest 38 64 0% 11 24
0.7073170570.7%
 

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.cluster;
21   
22    import org.junit.Assert;
23    import org.junit.Test;
24    import org.xwiki.model.reference.AttachmentReference;
25    import org.xwiki.model.reference.DocumentReference;
26    import org.xwiki.model.reference.LocalDocumentReference;
27    import org.xwiki.rest.model.jaxb.Attachment;
28    import org.xwiki.rest.model.jaxb.Attachments;
29    import org.xwiki.rest.model.jaxb.Page;
30    import org.xwiki.rest.resources.attachments.AttachmentsResource;
31    import org.xwiki.test.cluster.framework.AbstractClusterHttpTest;
32   
33    /**
34    * Verify the document cache update based on distributed events.
35    *
36    * @version $Id: eb38095b3aa8a19ad0187ef3303f4175b3a72894 $
37    */
 
38    public class DocumentCacheTest extends AbstractClusterHttpTest
39    {
40    private final static String TEST_SPACE = "Test";
41   
 
42  1 toggle @Test
43    public void testDocumentModifiedCacheSync() throws Exception
44    {
45  1 Page page = new Page();
46  1 page.setSpace(TEST_SPACE);
47  1 page.setName("CacheSync");
48   
49  1 LocalDocumentReference documentReference = new LocalDocumentReference(page.getSpace(), page.getName());
50   
51    // 1) Edit a page on XWiki 0
52  1 getUtil().switchExecutor(0);
53  1 page.setContent("content");
54  1 getUtil().rest().save(page);
55  1 Assert.assertEquals("content", getUtil().rest().<Page>get(documentReference).getContent());
56   
57    // 2) Modify content of the page on XWiki 1
58  1 getUtil().switchExecutor(1);
59  1 page.setContent("modified content");
60  1 getUtil().rest().save(page);
61  1 Assert.assertEquals("modified content", getUtil().rest().<Page>get(documentReference).getContent());
62   
63    // ASSERT) The content in XWiki 0 should be the one set than in XWiki 1
64    // Since it can take time for the Cluster to propagate the change, we need to wait and set up a timeout.
65  1 getUtil().switchExecutor(0);
66  1 long t1 = System.currentTimeMillis();
67  1 long t2;
68  1 String result;
69  ? while (!(result = getUtil().rest().<Page>get(documentReference).getContent())
70    .equalsIgnoreCase("modified content")) {
71  0 t2 = System.currentTimeMillis();
72  0 if (t2 - t1 > 10000L) {
73  0 Assert.fail("Content should have been [modified content] but was [" + result + "]");
74    }
75  0 Thread.sleep(100);
76    }
77    }
78   
 
79  1 toggle @Test
80    public void testDocumentDeletedCacheSync() throws Exception
81    {
82  1 Page page = new Page();
83  1 page.setSpace(TEST_SPACE);
84  1 page.setName("CacheSync");
85   
86  1 LocalDocumentReference documentReference = new LocalDocumentReference(page.getSpace(), page.getName());
87   
88    // 1) Make sure page exist on XWiki 0
89  1 getUtil().switchExecutor(0);
90  1 page.setContent("content");
91  1 getUtil().rest().save(page);
92  1 Assert.assertEquals("content", getUtil().rest().<Page>get(documentReference).getContent());
93   
94    // 2) Delete page on XWiki 1
95  1 getUtil().switchExecutor(1);
96    // Need superadmin to delete document
97  1 getUtil().loginAsSuperAdmin();
98  1 getUtil().rest().delete(documentReference);
99  1 Assert.assertFalse(getUtil().rest().exists(documentReference));
100   
101    // ASSERT) The document should be deleted on XWiki 0
102    // Since it can take time for the Cluster to propagate the change, we need to wait and set up a timeout.
103  1 getUtil().switchExecutor(0);
104  1 long t1 = System.currentTimeMillis();
105  1 long t2;
106  1 while (getUtil().rest().exists(documentReference)) {
107  0 t2 = System.currentTimeMillis();
108  0 if (t2 - t1 > 10000L) {
109  0 Assert.fail("Document shoud not exist anymore");
110    }
111  0 Thread.sleep(100);
112    }
113    }
114   
 
115  1 toggle @Test
116    public void testDocumentCacheSyncForAttachments() throws Exception
117    {
118  1 Page page = new Page();
119  1 page.setSpace(TEST_SPACE);
120  1 page.setName("AttachementCacheSync");
121   
122  1 AttachmentReference attachmentReference = new AttachmentReference("file.ext",
123    new DocumentReference(getUtil().getCurrentWiki(), page.getSpace(), page.getName()));
124   
125    // 1) Edit a page on XWiki 0
126  1 getUtil().switchExecutor(0);
127  1 page.setContent("content");
128  1 getUtil().rest().save(page);
129   
130    // 2) Add attachment to the page on XWiki 1
131  1 getUtil().switchExecutor(1);
132  1 getUtil().rest().attachFile(attachmentReference, "content".getBytes(), true);
133   
134    // ASSERT) The content in XWiki 0 should be the one set than in XWiki 1
135    // Since it can take time for the Cluster to propagate the change, we need to wait and set up a timeout.
136  1 getUtil().switchExecutor(0);
137  1 long t1 = System.currentTimeMillis();
138  1 long t2;
139  1 while (!hasAttachment(attachmentReference)) {
140  0 t2 = System.currentTimeMillis();
141  0 if (t2 - t1 > 10000L) {
142  0 Assert.fail("Failed to find attachment");
143    }
144  0 Thread.sleep(100);
145    }
146    }
147   
 
148  1 toggle private boolean hasAttachment(AttachmentReference attachmentReference) throws Exception
149    {
150  1 Attachments attachments =
151    getUtil().rest().get(AttachmentsResource.class, attachmentReference.getDocumentReference());
152  1 for (Attachment attachment : attachments.getAttachments()) {
153  1 System.out.println(attachment.getName());
154  1 if (attachment.getName().equals(attachmentReference.getName())) {
155  1 return true;
156    }
157    }
158   
159  0 return false;
160    }
161    }