| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.icon.internal; |
| 21 |
|
|
| 22 |
|
import java.io.IOException; |
| 23 |
|
import java.io.InputStreamReader; |
| 24 |
|
import java.io.Reader; |
| 25 |
|
import java.io.StringWriter; |
| 26 |
|
|
| 27 |
|
import org.apache.commons.io.IOUtils; |
| 28 |
|
import org.junit.Before; |
| 29 |
|
import org.junit.Rule; |
| 30 |
|
import org.junit.Test; |
| 31 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
| 32 |
|
import org.xwiki.bridge.DocumentModelBridge; |
| 33 |
|
import org.xwiki.icon.IconException; |
| 34 |
|
import org.xwiki.icon.IconSet; |
| 35 |
|
import org.xwiki.icon.IconType; |
| 36 |
|
import org.xwiki.model.reference.DocumentReference; |
| 37 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
| 38 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
| 39 |
|
|
| 40 |
|
import static org.junit.Assert.assertEquals; |
| 41 |
|
import static org.junit.Assert.assertNotNull; |
| 42 |
|
import static org.mockito.ArgumentMatchers.any; |
| 43 |
|
import static org.mockito.ArgumentMatchers.eq; |
| 44 |
|
import static org.mockito.Mockito.mock; |
| 45 |
|
import static org.mockito.Mockito.when; |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
@link |
| 49 |
|
|
| 50 |
|
@since |
| 51 |
|
@version |
| 52 |
|
|
| |
|
| 96.4% |
Uncovered Elements: 2 (56) |
Complexity: 10 |
Complexity Density: 0.22 |
|
| 53 |
|
public class DefaultIconSetLoaderTest |
| 54 |
|
{ |
| 55 |
|
@Rule |
| 56 |
|
public MockitoComponentMockingRule<DefaultIconSetLoader> mocker = |
| 57 |
|
new MockitoComponentMockingRule<>(DefaultIconSetLoader.class); |
| 58 |
|
|
| 59 |
|
private DocumentAccessBridge documentAccessBridge; |
| 60 |
|
|
| 61 |
|
private WikiDescriptorManager wikiDescriptorManager; |
| 62 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 63 |
4 |
@Before... |
| 64 |
|
public void setUp() throws Exception |
| 65 |
|
{ |
| 66 |
4 |
documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class); |
| 67 |
4 |
wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class); |
| 68 |
4 |
when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("wikiId"); |
| 69 |
|
} |
| 70 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
| 71 |
2 |
private void verifies(IconSet result) throws Exception... |
| 72 |
|
{ |
| 73 |
2 |
assertNotNull(result); |
| 74 |
2 |
assertEquals("http://url_to_css", result.getCss()); |
| 75 |
2 |
assertEquals("IconThemes.Default", result.getSsx()); |
| 76 |
2 |
assertEquals("IconThemes.JS", result.getJsx()); |
| 77 |
2 |
assertEquals("{{html clean=\"false\"}}<span class=\"fa fa-$icon\"></span>{{/html}}", result.getRenderWiki()); |
| 78 |
2 |
assertEquals("<span class=\"fa fa-$icon\"></span>", result.getRenderHTML()); |
| 79 |
2 |
assertEquals("anchor", result.getIcon("transmit").getValue()); |
| 80 |
2 |
assertEquals("globe", result.getIcon("earth").getValue()); |
| 81 |
2 |
assertEquals(IconType.FONT, result.getType()); |
| 82 |
|
} |
| 83 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 84 |
1 |
@Test... |
| 85 |
|
public void loadIconSet() throws Exception |
| 86 |
|
{ |
| 87 |
1 |
Reader content = new InputStreamReader(getClass().getResourceAsStream("/test.iconset")); |
| 88 |
|
|
| 89 |
|
|
| 90 |
1 |
IconSet result = mocker.getComponentUnderTest().loadIconSet(content, "FontAwesome"); |
| 91 |
|
|
| 92 |
|
|
| 93 |
1 |
verifies(result); |
| 94 |
1 |
assertEquals("FontAwesome", result.getName()); |
| 95 |
|
} |
| 96 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
| 97 |
1 |
@Test... |
| 98 |
|
public void loadIconSetFromWikiDocument() throws Exception |
| 99 |
|
{ |
| 100 |
1 |
DocumentReference iconSetRef = new DocumentReference("xwiki", "IconThemes", "Default"); |
| 101 |
1 |
DocumentReference iconClassRef = new DocumentReference("wikiId", "IconThemesCode", "IconThemeClass"); |
| 102 |
1 |
when(documentAccessBridge.getProperty(eq(iconSetRef), eq(iconClassRef), eq("name"))).thenReturn("MyIconTheme"); |
| 103 |
1 |
DocumentModelBridge doc = mock(DocumentModelBridge.class); |
| 104 |
1 |
when(documentAccessBridge.getDocument(iconSetRef)).thenReturn(doc); |
| 105 |
|
|
| 106 |
1 |
StringWriter content = new StringWriter(); |
| 107 |
1 |
IOUtils.copyLarge(new InputStreamReader(getClass().getResourceAsStream("/test.iconset")), content); |
| 108 |
1 |
when(doc.getContent()).thenReturn(content.toString()); |
| 109 |
|
|
| 110 |
|
|
| 111 |
1 |
IconSet result = mocker.getComponentUnderTest().loadIconSet(iconSetRef); |
| 112 |
|
|
| 113 |
|
|
| 114 |
1 |
verifies(result); |
| 115 |
1 |
assertEquals("MyIconTheme", result.getName()); |
| 116 |
|
} |
| 117 |
|
|
| |
|
| 91.7% |
Uncovered Elements: 1 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
1PASS
|
|
| 118 |
1 |
@Test... |
| 119 |
|
public void loadIconSetWithException() throws Exception |
| 120 |
|
{ |
| 121 |
1 |
Reader content = mock(Reader.class); |
| 122 |
1 |
IOException exception = new IOException("test"); |
| 123 |
1 |
when(content.read(any(char[].class))).thenThrow(exception); |
| 124 |
|
|
| 125 |
|
|
| 126 |
1 |
Exception caughException = null; |
| 127 |
1 |
try { |
| 128 |
1 |
mocker.getComponentUnderTest().loadIconSet(content, "FontAwesome"); |
| 129 |
|
} catch (IconException e) { |
| 130 |
1 |
caughException = e; |
| 131 |
|
} |
| 132 |
|
|
| 133 |
1 |
assertNotNull(caughException); |
| 134 |
1 |
assert(caughException instanceof IconException); |
| 135 |
1 |
assertEquals(exception, caughException.getCause()); |
| 136 |
1 |
assertEquals("Failed to load the IconSet [FontAwesome].", caughException.getMessage()); |
| 137 |
|
} |
| 138 |
|
|
| |
|
| 90.9% |
Uncovered Elements: 1 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
1PASS
|
|
| 139 |
1 |
@Test... |
| 140 |
|
public void loadIconSetFromWikiDocumentWithException() throws Exception |
| 141 |
|
{ |
| 142 |
1 |
Exception exception = new Exception("test"); |
| 143 |
1 |
when(documentAccessBridge.getDocument(any(DocumentReference.class))).thenThrow(exception); |
| 144 |
|
|
| 145 |
|
|
| 146 |
1 |
Exception caughException = null; |
| 147 |
1 |
try { |
| 148 |
1 |
mocker.getComponentUnderTest().loadIconSet(new DocumentReference("a", "b", "c")); |
| 149 |
|
} catch (IconException e) { |
| 150 |
1 |
caughException = e; |
| 151 |
|
} |
| 152 |
|
|
| 153 |
1 |
assertNotNull(caughException); |
| 154 |
1 |
assert(caughException instanceof IconException); |
| 155 |
1 |
assertEquals(exception, caughException.getCause()); |
| 156 |
1 |
assertEquals("Failed to load the IconSet [a:b.c].", caughException.getMessage()); |
| 157 |
|
} |
| 158 |
|
} |