summaryrefslogtreecommitdiff
path: root/test/yacysearchitemTest.java
blob: 6fda4ce4845011dfc1dbd03e8af272c81919497b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

/**
 *  yacysearchitemTest
 *  Copyright 2016 by luccioman; https://github.com/luccioman
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program in the file lgpl21.txt
 *  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Dimension;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

import net.yacy.cora.document.id.DigestURL;
import net.yacy.kelondro.data.meta.URIMetadataNode;
import net.yacy.search.schema.CollectionConfiguration;
import net.yacy.search.schema.CollectionSchema;

/**
 * Unit tests for yacysearchitem class.
 * 
 * @author luc
 *
 */
public class yacysearchitemTest {

	/**
	 * Three standard icons with different sizes, one non-standard with a larger
	 * size
	 * 
	 * @throws MalformedURLException
	 */
	@Test
	public final void testGetFaviconURL() throws MalformedURLException {
		URIMetadataNode metadataNode = new URIMetadataNode(new DigestURL("http://somehost.org"));
		metadataNode
				.setField(CollectionSchema.icons_urlstub_sxt.getSolrFieldName(),
						new String[] { "someHost.org/static/images/icon16.png", "somehost.org/static/images/icon32.png",
								"somehost.org/static/images/icon64.png",
								"somehost.org/static/images/iconApple124.png" });
		List<String> protocols = CollectionConfiguration
				.protocolList2indexedList(Arrays.asList(new String[] { "http", "http", "http", "http" }));
		metadataNode.setField(CollectionSchema.icons_protocol_sxt.getSolrFieldName(), protocols);
		metadataNode.setField(CollectionSchema.icons_rel_sxt.getSolrFieldName(),
				new String[] { "icon", "icon", "icon", "apple-touch-icon" });
		metadataNode.setField(CollectionSchema.icons_sizes_sxt.getSolrFieldName(),
				new String[] { "16x16", "32x32", "64x64", "128x128" });

		/* Search for a size present in icons collection */
		DigestURL faviconURL = yacysearchitem.getFaviconURL(metadataNode, new Dimension(32, 32));
		Assert.assertNotNull(faviconURL);
		Assert.assertEquals("http://somehost.org/static/images/icon32.png", faviconURL.toNormalform(false));

		/* Search for a size not in icons collection */
		faviconURL = yacysearchitem.getFaviconURL(metadataNode, new Dimension(40, 40));
		Assert.assertNotNull(faviconURL);
		Assert.assertEquals("http://somehost.org/static/images/icon32.png", faviconURL.toNormalform(false));

		/*
		 * Search for a size equals to non-standard : standard icon is stil
		 * preffered
		 */
		faviconURL = yacysearchitem.getFaviconURL(metadataNode, new Dimension(128, 128));
		Assert.assertNotNull(faviconURL);
		Assert.assertEquals("http://somehost.org/static/images/icon64.png", faviconURL.toNormalform(false));
	}

	/**
	 * Only non-standard icons
	 * 
	 * @throws MalformedURLException
	 */
	@Test
	public final void testGetFaviconURLNonStandard() throws MalformedURLException {
		URIMetadataNode metadataNode = new URIMetadataNode(new DigestURL("http://somehost.org"));
		metadataNode
				.setField(CollectionSchema.icons_urlstub_sxt.getSolrFieldName(),
						new String[] { "somehost.org/static/images/mask32.png",
								"somehost.org/static/images/fluid.64.png",
								"somehost.org/static/images/iconApple124.png" });
		List<String> protocols = CollectionConfiguration
				.protocolList2indexedList(Arrays.asList(new String[] { "http", "http", "http" }));
		metadataNode.setField(CollectionSchema.icons_protocol_sxt.getSolrFieldName(), protocols);
		metadataNode.setField(CollectionSchema.icons_rel_sxt.getSolrFieldName(),
				new String[] { "mask-icon", "fluid-icon", "apple-touch-icon" });
		metadataNode.setField(CollectionSchema.icons_sizes_sxt.getSolrFieldName(),
				new String[] { "32x32", "64x64", "128x128" });

		/* Non standard icon is returned as fallback */
		DigestURL faviconURL = yacysearchitem.getFaviconURL(metadataNode, new Dimension(32, 32));
		Assert.assertNotNull(faviconURL);
		Assert.assertEquals("http://somehost.org/static/images/mask32.png", faviconURL.toNormalform(false));
	}

	/**
	 * One standard icon with multiple sizes
	 * 
	 * @throws MalformedURLException
	 */
	@Test
	public final void testGetFaviconURLMultiSizes() throws MalformedURLException {
		URIMetadataNode metadataNode = new URIMetadataNode(new DigestURL("http://somehost.org"));
		metadataNode.setField(CollectionSchema.icons_urlstub_sxt.getSolrFieldName(),
				new String[] { "somehost.org/static/images/favicon.ico" });
		List<String> protocols = CollectionConfiguration
				.protocolList2indexedList(Arrays.asList(new String[] { "http" }));
		metadataNode.setField(CollectionSchema.icons_protocol_sxt.getSolrFieldName(), protocols);
		metadataNode.setField(CollectionSchema.icons_rel_sxt.getSolrFieldName(), new String[] { "icon" });
		metadataNode.setField(CollectionSchema.icons_sizes_sxt.getSolrFieldName(),
				new String[] { "16x16 32x32 64x64", });

		/* Search for a size in sizes set */
		DigestURL faviconURL = yacysearchitem.getFaviconURL(metadataNode, new Dimension(32, 32));
		Assert.assertNotNull(faviconURL);
		Assert.assertEquals("http://somehost.org/static/images/favicon.ico", faviconURL.toNormalform(false));

		/* Search for a size not in sizes set */
		faviconURL = yacysearchitem.getFaviconURL(metadataNode, new Dimension(40, 40));
		Assert.assertNotNull(faviconURL);
		Assert.assertEquals("http://somehost.org/static/images/favicon.ico", faviconURL.toNormalform(false));
	}

	/**
	 * One standard icon with no size
	 * 
	 * @throws MalformedURLException
	 */
	@Test
	public final void testGetFaviconURLNoSize() throws MalformedURLException {
		URIMetadataNode metadataNode = new URIMetadataNode(new DigestURL("http://somehost.org"));
		metadataNode.setField(CollectionSchema.icons_urlstub_sxt.getSolrFieldName(),
				new String[] { "somehost.org/static/images/favicon.ico" });
		List<String> protocols = CollectionConfiguration
				.protocolList2indexedList(Arrays.asList(new String[] { "http" }));
		metadataNode.setField(CollectionSchema.icons_protocol_sxt.getSolrFieldName(), protocols);
		metadataNode.setField(CollectionSchema.icons_rel_sxt.getSolrFieldName(), new String[] { "icon" });

		DigestURL faviconURL = yacysearchitem.getFaviconURL(metadataNode, new Dimension(32, 32));
		Assert.assertNotNull(faviconURL);
		Assert.assertEquals("http://somehost.org/static/images/favicon.ico", faviconURL.toNormalform(false));
	}
	
	/**
	 * One non-standard icon with no size
	 * 
	 * @throws MalformedURLException
	 */
	@Test
	public final void testGetFaviconURLNonStandardNoSize() throws MalformedURLException {
		URIMetadataNode metadataNode = new URIMetadataNode(new DigestURL("http://somehost.org"));
		metadataNode.setField(CollectionSchema.icons_urlstub_sxt.getSolrFieldName(),
				new String[] { "somehost.org/static/images/favicon.png" });
		List<String> protocols = CollectionConfiguration
				.protocolList2indexedList(Arrays.asList(new String[] { "http" }));
		metadataNode.setField(CollectionSchema.icons_protocol_sxt.getSolrFieldName(), protocols);
		metadataNode.setField(CollectionSchema.icons_rel_sxt.getSolrFieldName(), new String[] { "appel-touch-icon" });

		DigestURL faviconURL = yacysearchitem.getFaviconURL(metadataNode, new Dimension(32, 32));
		Assert.assertNotNull(faviconURL);
		Assert.assertEquals("http://somehost.org/static/images/favicon.png", faviconURL.toNormalform(false));
	}

	/**
	 * No icon in document
	 * 
	 * @throws MalformedURLException
	 */
	@Test
	public final void testGetFaviconURLNoIcon() throws MalformedURLException {
		URIMetadataNode metadataNode = new URIMetadataNode(new DigestURL("http://someHost.org"));

		/* Default fallback favicon URL should be generated */
		DigestURL faviconURL = yacysearchitem.getFaviconURL(metadataNode, new Dimension(32, 32));
		Assert.assertEquals("http://somehost.org/favicon.ico", faviconURL.toNormalform(false));
	}

}