| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package com.xpn.xwiki.web; |
| 21 |
|
|
| 22 |
|
import java.util.Locale; |
| 23 |
|
|
| 24 |
|
import org.junit.Assert; |
| 25 |
|
import org.junit.Before; |
| 26 |
|
import org.junit.Rule; |
| 27 |
|
import org.junit.Test; |
| 28 |
|
import org.xwiki.component.manager.ComponentLookupException; |
| 29 |
|
import org.xwiki.environment.Environment; |
| 30 |
|
import org.xwiki.extension.repository.internal.core.CoreExtensionScanner; |
| 31 |
|
import org.xwiki.localization.ContextualLocalizationManager; |
| 32 |
|
import org.xwiki.model.reference.DocumentReference; |
| 33 |
|
import org.xwiki.rendering.syntax.Syntax; |
| 34 |
|
import org.xwiki.test.annotation.AfterComponent; |
| 35 |
|
import org.xwiki.test.annotation.AllComponents; |
| 36 |
|
|
| 37 |
|
import com.xpn.xwiki.XWikiException; |
| 38 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 39 |
|
import com.xpn.xwiki.objects.BaseObject; |
| 40 |
|
import com.xpn.xwiki.test.MockitoOldcoreRule; |
| 41 |
|
|
| 42 |
|
@AllComponents |
| |
|
| 99.2% |
Uncovered Elements: 1 (119) |
Complexity: 19 |
Complexity Density: 0.2 |
|
| 43 |
|
public class XWikiMessageToolBridgeTest |
| 44 |
|
{ |
| 45 |
|
@Rule |
| 46 |
|
public MockitoOldcoreRule oldcore = new MockitoOldcoreRule(); |
| 47 |
|
|
| 48 |
|
private XWikiMessageTool tool; |
| 49 |
|
|
| 50 |
|
private DocumentReference preferencesDocumentReference; |
| 51 |
|
|
| 52 |
|
private DocumentReference defaultWikiTranslationReference; |
| 53 |
|
|
| 54 |
|
private static final String TRANSLATION_FULLNAME = "XWiki.Translations"; |
| 55 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 56 |
10 |
public XWikiMessageToolBridgeTest()... |
| 57 |
|
{ |
| 58 |
10 |
this.oldcore.notifyDocumentCreatedEvent(true); |
| 59 |
10 |
this.oldcore.notifyDocumentUpdatedEvent(true); |
| 60 |
10 |
this.oldcore.notifyDocumentDeletedEvent(true); |
| 61 |
|
} |
| 62 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 63 |
10 |
@AfterComponent... |
| 64 |
|
public void afterComponent() throws Exception |
| 65 |
|
{ |
| 66 |
|
|
| 67 |
10 |
this.oldcore.getMocker().registerMockComponent(Environment.class); |
| 68 |
|
|
| 69 |
10 |
this.oldcore.getMocker().registerMockComponent(CoreExtensionScanner.class); |
| 70 |
|
} |
| 71 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
|
| 72 |
10 |
@Before... |
| 73 |
|
public void before() throws Exception |
| 74 |
|
{ |
| 75 |
10 |
Locale.setDefault(Locale.ROOT); |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
10 |
this.preferencesDocumentReference = |
| 80 |
|
new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "XWiki", "XWikiPreferences"); |
| 81 |
|
|
| 82 |
10 |
XWikiDocument preferencesDocument = new XWikiDocument(this.preferencesDocumentReference); |
| 83 |
10 |
BaseObject preferencesObject = new BaseObject(); |
| 84 |
10 |
preferencesObject.setXClassReference(new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "XWiki", |
| 85 |
|
"XWikiPreferences")); |
| 86 |
10 |
preferencesDocument.addXObject(preferencesObject); |
| 87 |
10 |
preferencesDocument.setSyntax(Syntax.PLAIN_1_0); |
| 88 |
10 |
this.oldcore.getSpyXWiki().saveDocument(preferencesDocument, "", this.oldcore.getXWikiContext()); |
| 89 |
|
|
| 90 |
10 |
this.defaultWikiTranslationReference = |
| 91 |
|
new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "XWiki", "Translations"); |
| 92 |
|
|
| 93 |
10 |
XWikiDocument defaultWikiTranslation = new XWikiDocument(this.defaultWikiTranslationReference); |
| 94 |
10 |
defaultWikiTranslation.setSyntax(Syntax.PLAIN_1_0); |
| 95 |
10 |
this.oldcore.getSpyXWiki().saveDocument(defaultWikiTranslation, "", this.oldcore.getXWikiContext()); |
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
10 |
this.tool = |
| 100 |
|
new XWikiMessageTool(this.oldcore.getMocker().<ContextualLocalizationManager>getInstance( |
| 101 |
|
ContextualLocalizationManager.class)); |
| 102 |
|
} |
| 103 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 104 |
13 |
private void setBundles(String bundles) throws XWikiException... |
| 105 |
|
{ |
| 106 |
13 |
XWikiDocument preferencesDocument = |
| 107 |
|
this.oldcore.getSpyXWiki().getDocument(this.preferencesDocumentReference, this.oldcore.getXWikiContext()); |
| 108 |
13 |
BaseObject preferencesObject = preferencesDocument.getXObject(); |
| 109 |
|
|
| 110 |
13 |
if (!bundles.equals(preferencesObject.getStringValue("documentBundles"))) { |
| 111 |
9 |
preferencesObject.setStringValue("documentBundles", bundles); |
| 112 |
|
|
| 113 |
9 |
this.oldcore.getSpyXWiki().saveDocument(preferencesDocument, "", this.oldcore.getXWikiContext()); |
| 114 |
|
} |
| 115 |
|
} |
| 116 |
|
|
| |
|
| 95% |
Uncovered Elements: 1 (20) |
Complexity: 3 |
Complexity Density: 0.19 |
|
| 117 |
11 |
private void addWikiTranslation(String key, String message, Locale locale) throws XWikiException... |
| 118 |
|
{ |
| 119 |
11 |
XWikiDocument document = |
| 120 |
|
this.oldcore.getSpyXWiki().getDocument(this.defaultWikiTranslationReference, |
| 121 |
|
this.oldcore.getXWikiContext()); |
| 122 |
|
|
| 123 |
11 |
if (!locale.equals(Locale.ROOT)) { |
| 124 |
3 |
XWikiDocument translatedDocument = document.getTranslatedDocument(locale, this.oldcore.getXWikiContext()); |
| 125 |
3 |
if (translatedDocument == document) { |
| 126 |
3 |
translatedDocument = new XWikiDocument(document.getDocumentReference(), locale); |
| 127 |
3 |
translatedDocument.setDefaultLocale(document.getDefaultLocale()); |
| 128 |
|
} |
| 129 |
3 |
document = translatedDocument; |
| 130 |
|
} |
| 131 |
|
|
| 132 |
11 |
document.setSyntax(Syntax.PLAIN_1_0); |
| 133 |
|
|
| 134 |
11 |
StringBuilder builder = new StringBuilder(document.getContent()); |
| 135 |
|
|
| 136 |
11 |
builder.append('\n'); |
| 137 |
11 |
builder.append(key); |
| 138 |
11 |
builder.append('='); |
| 139 |
11 |
builder.append(message); |
| 140 |
|
|
| 141 |
11 |
document.setContent(builder.toString()); |
| 142 |
|
|
| 143 |
11 |
this.oldcore.getSpyXWiki().saveDocument(document, "", this.oldcore.getXWikiContext()); |
| 144 |
|
|
| 145 |
11 |
setBundles(document.getFullName()); |
| 146 |
|
} |
| 147 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 148 |
1 |
private void deleteWikiTranslation() throws XWikiException... |
| 149 |
|
{ |
| 150 |
1 |
this.oldcore.getSpyXWiki().deleteAllDocuments( |
| 151 |
|
this.oldcore.getSpyXWiki().getDocument(this.defaultWikiTranslationReference, |
| 152 |
|
this.oldcore.getXWikiContext()), this.oldcore.getXWikiContext()); |
| 153 |
|
} |
| 154 |
|
|
| 155 |
|
|
| 156 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 157 |
1 |
@Test... |
| 158 |
|
public void getInvalidTranslation() |
| 159 |
|
{ |
| 160 |
1 |
Assert.assertEquals("doesnotexists.translation", this.tool.get("doesnotexists.translation")); |
| 161 |
|
} |
| 162 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 163 |
1 |
@Test... |
| 164 |
|
public void getResourceTranslation() |
| 165 |
|
{ |
| 166 |
1 |
Assert.assertEquals("Language", this.tool.get("language")); |
| 167 |
|
} |
| 168 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 169 |
1 |
@Test... |
| 170 |
|
public void getWikiTranslation() throws XWikiException, ComponentLookupException |
| 171 |
|
{ |
| 172 |
1 |
addWikiTranslation("wiki.translation", "Wiki translation", Locale.ROOT); |
| 173 |
|
|
| 174 |
1 |
Assert.assertEquals("Wiki translation", this.tool.get("wiki.translation")); |
| 175 |
|
} |
| 176 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 177 |
1 |
@Test... |
| 178 |
|
public void getTranslatedWikiTranslation() throws XWikiException, ComponentLookupException |
| 179 |
|
{ |
| 180 |
1 |
addWikiTranslation("wiki.translation", "English translation", Locale.ENGLISH); |
| 181 |
1 |
addWikiTranslation("wiki.translation", "French translation", Locale.FRENCH); |
| 182 |
|
|
| 183 |
1 |
this.oldcore.getXWikiContext().setLocale(Locale.FRENCH); |
| 184 |
|
|
| 185 |
1 |
Assert.assertEquals("French translation", this.tool.get("wiki.translation")); |
| 186 |
|
|
| 187 |
1 |
this.oldcore.getXWikiContext().setLocale(Locale.ENGLISH); |
| 188 |
|
|
| 189 |
1 |
Assert.assertEquals("English translation", this.tool.get("wiki.translation")); |
| 190 |
|
} |
| 191 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
| 192 |
1 |
@Test... |
| 193 |
|
public void fallackOnParentLocale() throws XWikiException, ComponentLookupException |
| 194 |
|
{ |
| 195 |
1 |
addWikiTranslation("wiki.defaulttranslation", "Default translation", Locale.ROOT); |
| 196 |
|
|
| 197 |
1 |
Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation")); |
| 198 |
|
|
| 199 |
1 |
this.oldcore.getXWikiContext().setLocale(Locale.ENGLISH); |
| 200 |
|
|
| 201 |
1 |
Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation")); |
| 202 |
|
|
| 203 |
1 |
this.oldcore.getXWikiContext().setLocale(Locale.US); |
| 204 |
|
|
| 205 |
1 |
Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation")); |
| 206 |
|
|
| 207 |
1 |
this.oldcore.getXWikiContext().setLocale(Locale.FRENCH); |
| 208 |
1 |
addWikiTranslation("wiki.frenchtranslation", "French translation", Locale.FRENCH); |
| 209 |
|
|
| 210 |
1 |
Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation")); |
| 211 |
1 |
Assert.assertEquals("French translation", this.tool.get("wiki.frenchtranslation")); |
| 212 |
|
|
| 213 |
1 |
this.oldcore.getXWikiContext().setLocale(Locale.FRANCE); |
| 214 |
|
|
| 215 |
1 |
Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation")); |
| 216 |
1 |
Assert.assertEquals("French translation", this.tool.get("wiki.frenchtranslation")); |
| 217 |
|
} |
| 218 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
| 219 |
1 |
@Test... |
| 220 |
|
public void updateWikiTranslationCache() throws XWikiException, ComponentLookupException |
| 221 |
|
{ |
| 222 |
1 |
setBundles(TRANSLATION_FULLNAME); |
| 223 |
|
|
| 224 |
1 |
Assert.assertEquals("wiki.defaulttranslation", this.tool.get("wiki.defaulttranslation")); |
| 225 |
|
|
| 226 |
1 |
addWikiTranslation("wiki.defaulttranslation", "Default translation", Locale.ROOT); |
| 227 |
|
|
| 228 |
1 |
Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation")); |
| 229 |
|
|
| 230 |
1 |
addWikiTranslation("wiki.anothertranslation", "Another translation", Locale.ROOT); |
| 231 |
|
|
| 232 |
1 |
Assert.assertEquals("Another translation", this.tool.get("wiki.anothertranslation")); |
| 233 |
|
|
| 234 |
1 |
deleteWikiTranslation(); |
| 235 |
|
|
| 236 |
1 |
Assert.assertEquals("wiki.defaulttranslation", this.tool.get("wiki.defaulttranslation")); |
| 237 |
1 |
Assert.assertEquals("wiki.anothertranslation", this.tool.get("wiki.anothertranslation")); |
| 238 |
|
} |
| 239 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
| 240 |
1 |
@Test... |
| 241 |
|
public void updateWikiPreferencesCache() throws XWikiException, ComponentLookupException |
| 242 |
|
{ |
| 243 |
1 |
addWikiTranslation("wiki.defaulttranslation", "Default translation", Locale.ROOT); |
| 244 |
|
|
| 245 |
1 |
Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation")); |
| 246 |
|
|
| 247 |
1 |
XWikiDocument otherWikiTranslation = |
| 248 |
|
new XWikiDocument(new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "XWiki", |
| 249 |
|
"OtherTranslations")); |
| 250 |
1 |
otherWikiTranslation.setSyntax(Syntax.PLAIN_1_0); |
| 251 |
1 |
otherWikiTranslation.setContent("wiki.othertranslation=Other translation"); |
| 252 |
1 |
this.oldcore.getSpyXWiki().saveDocument(otherWikiTranslation, "", this.oldcore.getXWikiContext()); |
| 253 |
|
|
| 254 |
1 |
setBundles(" " + otherWikiTranslation.getFullName()); |
| 255 |
|
|
| 256 |
1 |
Assert.assertEquals("Other translation", this.tool.get("wiki.othertranslation")); |
| 257 |
|
|
| 258 |
1 |
Assert.assertEquals("wiki.defaulttranslation", this.tool.get("wiki.defaulttranslation")); |
| 259 |
|
} |
| 260 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 261 |
1 |
@Test... |
| 262 |
|
public void getTranslationWithParameters() throws XWikiException, ComponentLookupException |
| 263 |
|
{ |
| 264 |
1 |
addWikiTranslation("wiki.translation", "{1} {0}", Locale.ROOT); |
| 265 |
|
|
| 266 |
1 |
Assert.assertEquals("Parameter translation", this.tool.get("wiki.translation", "translation", "Parameter")); |
| 267 |
|
} |
| 268 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 269 |
1 |
@Test... |
| 270 |
|
public void getEmptyWikiTranslation() throws XWikiException, ComponentLookupException |
| 271 |
|
{ |
| 272 |
1 |
addWikiTranslation("wiki.translation", "", Locale.ROOT); |
| 273 |
|
|
| 274 |
1 |
Assert.assertEquals("", this.tool.get("wiki.translation")); |
| 275 |
|
} |
| 276 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
| 277 |
1 |
@Test... |
| 278 |
|
public void fallbackOnResource() throws XWikiException, ComponentLookupException |
| 279 |
|
{ |
| 280 |
1 |
Assert.assertEquals("Language", this.tool.get("language")); |
| 281 |
|
|
| 282 |
1 |
XWikiDocument defaultWikiTranslation = |
| 283 |
|
this.oldcore.getSpyXWiki().getDocument(this.defaultWikiTranslationReference, |
| 284 |
|
this.oldcore.getXWikiContext()); |
| 285 |
1 |
defaultWikiTranslation.setDefaultLocale(Locale.FRENCH); |
| 286 |
|
|
| 287 |
1 |
addWikiTranslation("language", "Overwritten language", Locale.ROOT); |
| 288 |
|
|
| 289 |
|
|
| 290 |
1 |
this.oldcore.getXWikiContext().setLocale(Locale.ROOT); |
| 291 |
1 |
Assert.assertEquals("Overwritten language", this.tool.get("language")); |
| 292 |
|
|
| 293 |
|
|
| 294 |
1 |
this.oldcore.getXWikiContext().setLocale(Locale.FRENCH); |
| 295 |
1 |
Assert.assertEquals("Overwritten language", this.tool.get("language")); |
| 296 |
|
|
| 297 |
|
|
| 298 |
1 |
this.oldcore.getXWikiContext().setLocale(Locale.GERMAN); |
| 299 |
1 |
Assert.assertEquals("Sprache", this.tool.get("language")); |
| 300 |
|
|
| 301 |
|
|
| 302 |
1 |
this.oldcore.getXWikiContext().setLocale(Locale.ENGLISH); |
| 303 |
1 |
Assert.assertEquals("Overwritten language", this.tool.get("language")); |
| 304 |
|
} |
| 305 |
|
} |