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

File XWikiMigrationManagerTest.java

 

Code metrics

0
45
21
3
220
162
21
0.47
2.14
7
1

Classes

Class Line # Actions
XWikiMigrationManagerTest 44 29 0% 6 0
1.0100%
XWikiMigrationManagerTest.TestDataMigrationManager 50 12 0% 10 2
0.9090909490.9%
XWikiMigrationManagerTest.TestForceMigration 174 4 0% 5 3
0.666666766.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 com.xpn.xwiki.store.migration;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.Collection;
25    import java.util.List;
26   
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29   
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.component.annotation.ComponentAnnotationLoader;
32    import org.xwiki.component.descriptor.ComponentDescriptor;
33   
34    import com.xpn.xwiki.XWiki;
35    import com.xpn.xwiki.XWikiContext;
36    import com.xpn.xwiki.XWikiException;
37    import com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase;
38   
39    /**
40    * Test for {@link AbstractDataMigrationManager}
41    *
42    * @version $Id: 0fe9316c59dbec1c886cf6111d16eab4204fdadc $
43    */
 
44    public class XWikiMigrationManagerTest extends AbstractBridgedXWikiComponentTestCase
45    {
46    /** mocked migration manager */
47    @Component(staticRegistration = false)
48    @Named("TestDataMigration")
49    @Singleton
 
50    public static class TestDataMigrationManager extends AbstractDataMigrationManager
51    {
 
52  8 toggle private DataMigration createMigrator(final int ver)
53    {
54  8 return new DataMigration()
55    {
 
56  2 toggle @Override
57    public String getName()
58    {
59  2 return "Test";
60    }
61   
 
62  2 toggle @Override
63    public String getDescription()
64    {
65  2 return "Test";
66    }
67   
 
68  28 toggle @Override
69    public XWikiDBVersion getVersion()
70    {
71  28 return new XWikiDBVersion(ver);
72    }
73   
 
74  2 toggle @Override
75    public boolean shouldExecute(XWikiDBVersion startupVersion)
76    {
77  2 return true;
78    }
79   
 
80  0 toggle @Override
81    public void migrate()
82    {
83    }
84    };
85    }
86   
 
87  2 toggle @Override
88    protected List<DataMigration> getAllMigrations()
89    {
90  2 List<DataMigration> lst = new ArrayList<DataMigration>();
91  2 lst.add(createMigrator(345));
92  2 lst.add(createMigrator(123));
93  2 lst.add(createMigrator(456));
94  2 lst.add(createMigrator(234));
95   
96  2 return lst;
97    }
98   
99    XWikiDBVersion curversion;
100   
 
101  0 toggle @Override
102    protected void initializeEmptyDB() throws DataMigrationException
103    {
104    }
105   
 
106  1 toggle @Override
107    protected void setDBVersionToDatabase(XWikiDBVersion version)
108    {
109  1 this.curversion = version;
110    }
111   
 
112  1 toggle @Override
113    protected void updateSchema(Collection<XWikiMigration> migrations)
114    {
115    }
116    }
117   
 
118  4 toggle private void registerComponent(Class<?> klass) throws Exception
119    {
120  4 ComponentAnnotationLoader loader = new ComponentAnnotationLoader();
121  4 List<ComponentDescriptor> descriptors = loader.getComponentsDescriptors(klass);
122   
123  4 for (ComponentDescriptor<?> descriptor : descriptors) {
124  4 getComponentManager().registerComponent(descriptor);
125    }
126    }
127   
 
128  3 toggle @Override
129    protected void setUp() throws Exception
130    {
131  3 super.setUp();
132  3 getContext().setWiki(new XWiki() {
 
133  1 toggle @Override
134    public List<String> getVirtualWikisDatabaseNames(XWikiContext context) throws XWikiException
135    {
136  1 return Arrays.asList("xwiki");
137    }
138    });
139   
140  3 registerComponent(TestDataMigrationManager.class);
141    }
142   
143    /** test migration if there are no data version */
 
144  1 toggle public void testMigrationWhenNoVersion() throws Exception
145    {
146  1 TestDataMigrationManager mm = (TestDataMigrationManager) getComponentManager().getInstance(
147    DataMigrationManager.class,"TestDataMigration");
148  1 Collection neededMigration = mm.getNeededMigrations();
149  1 assertEquals(0, neededMigration.size());
150  1 mm.startMigrations();
151  1 assertEquals(456, mm.curversion.getVersion());
152    }
153   
154    /**
155    * test parameters "xwiki.store.migration.version", "xwiki.store.migration.ignored" and migrations order
156    */
 
157  1 toggle public void testMigrationOrderAndIgnore() throws Exception
158    {
159  1 getConfigurationSource().setProperty("xwiki.store.migration.version", "123");
160  1 getConfigurationSource().setProperty("xwiki.store.migration.ignored", "345");
161  1 TestDataMigrationManager mm = getComponentManager().getInstance(
162    DataMigrationManager.class,"TestDataMigration");
163  1 Collection neededMigration = mm.getNeededMigrations();
164  1 assertEquals(2, neededMigration.size());
165  1 AbstractDataMigrationManager.XWikiMigration[] actual = new AbstractDataMigrationManager.XWikiMigration[2];
166  1 neededMigration.toArray(actual);
167  1 assertEquals(234, actual[0].dataMigration.getVersion().getVersion());
168  1 assertEquals(456, actual[1].dataMigration.getVersion().getVersion());
169    }
170   
171    @Component(staticRegistration = false)
172    @Named("TestForcedMigration")
173    @Singleton
 
174    public static class TestForceMigration implements DataMigration
175    {
 
176  1 toggle @Override
177    public String getName()
178    {
179  1 return "Test";
180    }
181   
 
182  1 toggle @Override
183    public String getDescription()
184    {
185  1 return "Test";
186    }
187   
 
188  2 toggle @Override
189    public XWikiDBVersion getVersion()
190    {
191  2 return new XWikiDBVersion(567);
192    }
193   
 
194  0 toggle @Override
195    public boolean shouldExecute(XWikiDBVersion startupVersion)
196    {
197  0 return true;
198    }
199   
 
200  0 toggle @Override
201    public void migrate() throws DataMigrationException
202    {
203    }
204    }
205   
206    /** test "xwiki.store.migration.force" parameter */
 
207  1 toggle public void testMigrationForce() throws Exception
208    {
209  1 getConfigurationSource().setProperty("xwiki.store.migration.version", "234");
210  1 getConfigurationSource().setProperty("xwiki.store.migration.force", "TestForcedMigration");
211  1 registerComponent(TestForceMigration.class);
212   
213  1 TestDataMigrationManager mm = getComponentManager().getInstance(
214    DataMigrationManager.class,"TestDataMigration");
215  1 Collection neededMigration = mm.getNeededMigrations();
216  1 assertEquals(1, neededMigration.size());
217  1 assertEquals(567, ((AbstractDataMigrationManager.XWikiMigration) neededMigration.toArray()[0])
218    .dataMigration.getVersion().getVersion());
219    }
220    }