See http://mantis.tokeek.de/view.php?id=629pull/39/head
parent
edef6cd0dc
commit
3cc5619d93
@ -0,0 +1,198 @@
|
||||
/**
|
||||
* IconEntry
|
||||
* Copyright 2011 by Michael Peter Christen
|
||||
* First released 28.04.2011 at http://yacy.net
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
package net.yacy.document.parser.html;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import net.yacy.cora.document.id.DigestURL;
|
||||
|
||||
/**
|
||||
* Represents an icon in a document.
|
||||
*
|
||||
* @author luc
|
||||
*
|
||||
*/
|
||||
public class IconEntry {
|
||||
|
||||
/** Patern to parse a HTML link sizes token attribute (ie. "16x16") */
|
||||
public static final Pattern SIZE_PATTERN = Pattern.compile("([1-9][0-9]*)[xX]([1-9][0-9]*)");
|
||||
|
||||
/** Icon URL */
|
||||
private final DigestURL url;
|
||||
/**
|
||||
* Icon links relations (one url may be used as multiple icon relations in
|
||||
* the same document)
|
||||
*/
|
||||
private final Set<String> rel;
|
||||
/** Icon sizes */
|
||||
private final Set<Dimension> sizes;
|
||||
|
||||
/**
|
||||
* Constructs instance from parameters.
|
||||
*
|
||||
* @param url
|
||||
* must not be null.
|
||||
* @param rel
|
||||
* must not be null and contain at least one item.
|
||||
* @param sizes
|
||||
* optional.
|
||||
*/
|
||||
public IconEntry(final DigestURL url, Set<String> rel, Set<Dimension> sizes) {
|
||||
if (url == null) {
|
||||
throw new IllegalArgumentException("url must not be null.");
|
||||
}
|
||||
if (rel == null || rel.isEmpty()) {
|
||||
throw new IllegalArgumentException("rel must be specified");
|
||||
}
|
||||
this.url = url;
|
||||
this.rel = rel;
|
||||
if (sizes != null) {
|
||||
this.sizes = sizes;
|
||||
} else {
|
||||
this.sizes = new HashSet<>();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true when rel property contains a standard IANA registered icon
|
||||
* link relation
|
||||
*/
|
||||
public boolean isStandardIcon() {
|
||||
boolean standard = false;
|
||||
for (String relation : this.rel) {
|
||||
if (IconLinkRelations.isStandardIconRel(relation)) {
|
||||
standard = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return standard;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param size1
|
||||
* @param size2
|
||||
* @return distance between two sizes, or Double.MAX_VALUE when one size is null
|
||||
*/
|
||||
public static double getDistance(Dimension size1, Dimension size2) {
|
||||
double result = Double.MAX_VALUE;
|
||||
if(size1 != null && size2 != null) {
|
||||
result = (Math.abs(size1.width - size2.width) + Math.abs(size1.height - size2.height)) / 2.0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param preferredSize
|
||||
* @return the size among sizes property which is the closest to
|
||||
* preferredSize, or null when sizes is empty or preferredSize is null.
|
||||
*/
|
||||
public Dimension getClosestSize(Dimension preferredSize) {
|
||||
Dimension closest = null;
|
||||
if (preferredSize != null) {
|
||||
double closestDistance = Double.MAX_VALUE;
|
||||
for (Dimension size : this.sizes) {
|
||||
double currentDistance = getDistance(size, preferredSize);
|
||||
if (closest == null) {
|
||||
closest = size;
|
||||
closestDistance = currentDistance;
|
||||
} else {
|
||||
if (currentDistance < closestDistance) {
|
||||
closest = size;
|
||||
closestDistance = currentDistance;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return closest;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder res = new StringBuilder();
|
||||
res.append("<link");
|
||||
res.append(" href=\"").append(this.url.toNormalform(false)).append("\"");
|
||||
res.append(" rel=\"");
|
||||
res.append(relToString());
|
||||
res.append("\"");
|
||||
if (!this.sizes.isEmpty()) {
|
||||
res.append(" sizes=\"");
|
||||
res.append(sizesToString());
|
||||
res.append("\"");
|
||||
}
|
||||
res.append(">");
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return icon URL
|
||||
*/
|
||||
public DigestURL getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return icons link relations
|
||||
*/
|
||||
public Set<String> getRel() {
|
||||
return rel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return icon eventual sizes
|
||||
*/
|
||||
public Set<Dimension> getSizes() {
|
||||
return sizes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a string representation of sizes property, in the form of a valid
|
||||
* HTML link tag sizes attribute (e.g. "16x16 64x64")
|
||||
*/
|
||||
public String sizesToString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (Dimension size : this.sizes) {
|
||||
if (builder.length() > 0) {
|
||||
builder.append(" ");
|
||||
}
|
||||
builder.append(size.width).append("x").append(size.height);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a string representation of rel property, int the form of a valid
|
||||
* HTML link tag rel attribute (e.g. "icon apple-touch-icon")
|
||||
*/
|
||||
public String relToString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (String relation : this.rel) {
|
||||
if (builder.length() > 0) {
|
||||
builder.append(" ");
|
||||
}
|
||||
builder.append(relation);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
/**
|
||||
* IconLinkRelations
|
||||
* Copyright 2011 by Michael Peter Christen
|
||||
* First released 28.04.2011 at http://yacy.net
|
||||
*
|
||||
* $LastChangedDate: 2011-03-08 02:51:51 +0100 (Di, 08 Mrz 2011) $
|
||||
* $LastChangedRevision: 7567 $
|
||||
* $LastChangedBy: low012 $
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
package net.yacy.document.parser.html;
|
||||
|
||||
/**
|
||||
* Enumeration of HTML link relationships (rel attribute) designating icon.
|
||||
* @author luc
|
||||
*
|
||||
*/
|
||||
public enum IconLinkRelations {
|
||||
/** Standard IANA registered icon link relation (see https://www.iana.org/assignments/link-relations/link-relations.xhtml) */
|
||||
ICON("icon", "Standard favicon"),
|
||||
/** Icon for IOS app shortcut */
|
||||
APPLE_TOUCH_ICON("apple-touch-icon", "IOS app shortcut icon"),
|
||||
/** Icon for IOS app shortcut (deprecated but still used by major websites in 2015) */
|
||||
APPLE_TOUCH_ICON_PRECOMPOSED("apple-touch-icon-precomposed", "Deprecated IOS app shortcut icon"),
|
||||
/** icon for Safari pinned tab */
|
||||
MASK_ICON("mask-icon", "Safari browser pinned tab icon"),
|
||||
/** Icon for Fluid web app */
|
||||
FLUID_ICON("fluid-icon", "Fluid app icon");
|
||||
|
||||
/** HTML rel attribute value */
|
||||
private String relValue;
|
||||
|
||||
/** Human readable description */
|
||||
private String description;
|
||||
|
||||
private IconLinkRelations(String relValue, String description) {
|
||||
this.relValue = relValue;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HTML rel attribute value
|
||||
*/
|
||||
public String getRelValue() {
|
||||
return relValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Human readable description of icon rel attribute
|
||||
*/
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param relToken HTML rel attribute token
|
||||
* @return true when relToken is an icon relationship (standard or non-standard)
|
||||
*/
|
||||
public static boolean isIconRel(String relToken) {
|
||||
boolean res = false;
|
||||
for(IconLinkRelations iconRel : IconLinkRelations.values()) {
|
||||
if(iconRel.getRelValue().equalsIgnoreCase(relToken)) {
|
||||
res = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param relToken HTML rel attribute token
|
||||
* @return true when relToken is Standard IANA registered icon link relation
|
||||
*/
|
||||
public static boolean isStandardIconRel(String relToken) {
|
||||
return ICON.getRelValue().equalsIgnoreCase(relToken);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,125 @@
|
||||
/**
|
||||
* ContentScraperTest
|
||||
* Copyright 2011 by Michael Peter Christen
|
||||
* First released 28.04.2011 at http://yacy.net
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
package net.yacy.document.parser.html;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for ContentScrapper class.
|
||||
* @author luc
|
||||
*
|
||||
*/
|
||||
public class ContentScraperTest {
|
||||
|
||||
@Test
|
||||
public final void testParseSizes() {
|
||||
/* Normal case */
|
||||
Set<Dimension> sizes = ContentScraper.parseSizes("96x128");
|
||||
Assert.assertEquals(1, sizes.size());
|
||||
Assert.assertTrue(sizes.contains(new Dimension(96, 128)));
|
||||
|
||||
/* "any" keyword */
|
||||
sizes = ContentScraper.parseSizes("any");
|
||||
Assert.assertEquals(0, sizes.size());
|
||||
|
||||
/* Multiple valid sizes, lower and upper case separator */
|
||||
sizes = ContentScraper.parseSizes("96x128 16X16 1X2 1024x768");
|
||||
Assert.assertEquals(4, sizes.size());
|
||||
Assert.assertTrue(sizes.contains(new Dimension(96, 128)));
|
||||
Assert.assertTrue(sizes.contains(new Dimension(16, 16)));
|
||||
Assert.assertTrue(sizes.contains(new Dimension(1, 2)));
|
||||
Assert.assertTrue(sizes.contains(new Dimension(1024, 768)));
|
||||
|
||||
/* Duplicate entries */
|
||||
sizes = ContentScraper.parseSizes("96x128 96X128 1X2 96x128");
|
||||
Assert.assertEquals(2, sizes.size());
|
||||
Assert.assertTrue(sizes.contains(new Dimension(96, 128)));
|
||||
Assert.assertTrue(sizes.contains(new Dimension(1, 2)));
|
||||
|
||||
/* Mutiple inner and trailing spaces */
|
||||
sizes = ContentScraper.parseSizes(" 96x128 16X16 ");
|
||||
Assert.assertEquals(2, sizes.size());
|
||||
Assert.assertTrue(sizes.contains(new Dimension(96, 128)));
|
||||
Assert.assertTrue(sizes.contains(new Dimension(16, 16)));
|
||||
|
||||
/* Empty string */
|
||||
sizes = ContentScraper.parseSizes("");
|
||||
Assert.assertEquals(0, sizes.size());
|
||||
|
||||
/* null string */
|
||||
sizes = ContentScraper.parseSizes(null);
|
||||
Assert.assertEquals(0, sizes.size());
|
||||
|
||||
/* Invalid sizes */
|
||||
sizes = ContentScraper.parseSizes("096x0128 -16x-16 0x0 x768 78x axb 1242");
|
||||
Assert.assertEquals(0, sizes.size());
|
||||
|
||||
/* Mix of valid and invalid sizes */
|
||||
sizes = ContentScraper.parseSizes("96x128 16X16 axb 123 78x32");
|
||||
Assert.assertEquals(3, sizes.size());
|
||||
Assert.assertTrue(sizes.contains(new Dimension(96, 128)));
|
||||
Assert.assertTrue(sizes.contains(new Dimension(16, 16)));
|
||||
Assert.assertTrue(sizes.contains(new Dimension(78, 32)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void testParseSpaceSeparatedTokens() {
|
||||
/* Normal case */
|
||||
Set<String> tokens = ContentScraper.parseSpaceSeparatedTokens("abc de");
|
||||
Assert.assertEquals(2, tokens.size());
|
||||
Assert.assertTrue(tokens.contains("abc"));
|
||||
Assert.assertTrue(tokens.contains("de"));
|
||||
|
||||
/* One item only */
|
||||
tokens = ContentScraper.parseSpaceSeparatedTokens("abc");
|
||||
Assert.assertEquals(1, tokens.size());
|
||||
Assert.assertTrue(tokens.contains("abc"));
|
||||
|
||||
/* Mutiple inner and trailing spaces */
|
||||
tokens = ContentScraper.parseSpaceSeparatedTokens(" abc d efff fgj ");
|
||||
Assert.assertEquals(4, tokens.size());
|
||||
Assert.assertTrue(tokens.contains("abc"));
|
||||
Assert.assertTrue(tokens.contains("d"));
|
||||
Assert.assertTrue(tokens.contains("efff"));
|
||||
Assert.assertTrue(tokens.contains("fgj"));
|
||||
|
||||
/* Duplicate entries */
|
||||
tokens = ContentScraper.parseSpaceSeparatedTokens("abc bb abc abc ABC");
|
||||
Assert.assertEquals(3, tokens.size());
|
||||
Assert.assertTrue(tokens.contains("abc"));
|
||||
/* ignoring case is not the purpose of this function */
|
||||
Assert.assertTrue(tokens.contains("ABC"));
|
||||
Assert.assertTrue(tokens.contains("bb"));
|
||||
|
||||
/* Empty string */
|
||||
tokens = ContentScraper.parseSpaceSeparatedTokens("");
|
||||
Assert.assertEquals(0, tokens.size());
|
||||
|
||||
/* Null string */
|
||||
tokens = ContentScraper.parseSpaceSeparatedTokens(null);
|
||||
Assert.assertEquals(0, tokens.size());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,192 @@
|
||||
/**
|
||||
* IconEntryTest
|
||||
* Copyright 2011 by Michael Peter Christen
|
||||
* First released 28.04.2011 at http://yacy.net
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
package net.yacy.document.parser.html;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.yacy.cora.document.id.DigestURL;
|
||||
|
||||
/**
|
||||
* Unit tests for IconEntry class.
|
||||
* @author luc
|
||||
*
|
||||
*/
|
||||
public class IconEntryTest {
|
||||
|
||||
@Test
|
||||
public final void testGetDistance() {
|
||||
/* Normal case : one size has both width and height greater */
|
||||
Dimension size1 = new Dimension(5, 8);
|
||||
Dimension size2 = new Dimension(7, 12);
|
||||
Assert.assertEquals(3.0, IconEntry.getDistance(size1, size2), 0.0);
|
||||
/* Check inverted parameters should produces same result */
|
||||
Assert.assertEquals(3.0, IconEntry.getDistance(size2, size1), 0.0);
|
||||
/* Equal sizes */
|
||||
size2 = new Dimension(5, 8);
|
||||
Assert.assertEquals(0.0, IconEntry.getDistance(size1, size2), 0.0);
|
||||
/* Equal sizes */
|
||||
size2 = new Dimension(5, 8);
|
||||
Assert.assertEquals(0.0, IconEntry.getDistance(size1, size2), 0.0);
|
||||
/* Only one dimension differs */
|
||||
size2 = new Dimension(5, 12);
|
||||
Assert.assertEquals(2.0, IconEntry.getDistance(size1, size2), 0.0);
|
||||
size2 = new Dimension(10, 8);
|
||||
Assert.assertEquals(2.5, IconEntry.getDistance(size1, size2), 0.0);
|
||||
/* width lower, height upper */
|
||||
size2 = new Dimension(3, 12);
|
||||
Assert.assertEquals(3.0, IconEntry.getDistance(size1, size2), 0.0);
|
||||
/* negative values */
|
||||
size1 = new Dimension(-5, -8);
|
||||
size2 = new Dimension(-7, -12);
|
||||
Assert.assertEquals(3.0, IconEntry.getDistance(size1, size2), 0.0);
|
||||
/* one null */
|
||||
size1 = null;
|
||||
size2 = new Dimension(-7, -12);
|
||||
Assert.assertEquals(Double.MAX_VALUE, IconEntry.getDistance(size1, size2), 0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void testGetClosestSize() throws MalformedURLException {
|
||||
/* Preferred size in sizes set */
|
||||
Set<String> rels = new HashSet<>();
|
||||
rels.add(IconLinkRelations.ICON.getRelValue());
|
||||
|
||||
Set<Dimension> sizes = new HashSet<>();
|
||||
sizes.add(new Dimension(128,128));
|
||||
sizes.add(new Dimension(256,512));
|
||||
sizes.add(new Dimension(16,16));
|
||||
|
||||
Dimension preferredSize = new Dimension(16, 16);
|
||||
IconEntry icon = new IconEntry(new DigestURL("http://yacy.net"), rels, sizes);
|
||||
Dimension result = icon.getClosestSize(preferredSize);
|
||||
Assert.assertEquals(preferredSize, result);
|
||||
|
||||
/* Preferred size lower than all sizes in set */
|
||||
preferredSize = new Dimension(12, 12);
|
||||
result = icon.getClosestSize(preferredSize);
|
||||
Assert.assertEquals(new Dimension(16,16), result);
|
||||
|
||||
/* Preferred size over than all sizes in set */
|
||||
preferredSize = new Dimension(1992, 1024);
|
||||
result = icon.getClosestSize(preferredSize);
|
||||
Assert.assertEquals(new Dimension(256, 512), result);
|
||||
|
||||
/* Preferred size between sizes in set */
|
||||
preferredSize = new Dimension(17, 18);
|
||||
result = icon.getClosestSize(preferredSize);
|
||||
Assert.assertEquals(new Dimension(16, 16), result);
|
||||
|
||||
/* Sizes set contains only one item */
|
||||
sizes = new HashSet<>();
|
||||
sizes.add(new Dimension(128,128));
|
||||
icon = new IconEntry(new DigestURL("http://yacy.net"), rels, sizes);
|
||||
preferredSize = new Dimension(1992, 1024);
|
||||
result = icon.getClosestSize(preferredSize);
|
||||
Assert.assertEquals(new Dimension(128, 128), result);
|
||||
|
||||
/* Empty sizes set */
|
||||
sizes = new HashSet<>();
|
||||
icon = new IconEntry(new DigestURL("http://yacy.net"), rels, sizes);
|
||||
preferredSize = new Dimension(16, 16);
|
||||
result = icon.getClosestSize(preferredSize);
|
||||
Assert.assertNull(result);
|
||||
|
||||
/* Null preferred size */
|
||||
sizes = new HashSet<>();
|
||||
sizes.add(new Dimension(128,128));
|
||||
sizes.add(new Dimension(256,512));
|
||||
sizes.add(new Dimension(16,16));
|
||||
icon = new IconEntry(new DigestURL("http://yacy.net"), rels, sizes);
|
||||
preferredSize = null;
|
||||
result = icon.getClosestSize(preferredSize);
|
||||
Assert.assertNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void testSizesToString() throws MalformedURLException {
|
||||
/* Multiple values in sizes set */
|
||||
Set<String> rels = new HashSet<>();
|
||||
rels.add(IconLinkRelations.ICON.getRelValue());
|
||||
|
||||
Set<Dimension> sizes = new HashSet<>();
|
||||
sizes.add(new Dimension(128,128));
|
||||
sizes.add(new Dimension(256,512));
|
||||
sizes.add(new Dimension(16,16));
|
||||
|
||||
IconEntry icon = new IconEntry(new DigestURL("http://yacy.net"), rels, sizes);
|
||||
String sizesStr = icon.sizesToString();
|
||||
/* The set is not ordered, only check result contains what we expect */
|
||||
Assert.assertTrue(sizesStr.contains("128x128"));
|
||||
Assert.assertTrue(sizesStr.contains("256x512"));
|
||||
Assert.assertTrue(sizesStr.contains("16x16"));
|
||||
Assert.assertTrue(sizesStr.contains(" "));
|
||||
|
||||
/* One value in sizes set */
|
||||
sizes = new HashSet<>();
|
||||
sizes.add(new Dimension(128,128));
|
||||
|
||||
icon = new IconEntry(new DigestURL("http://yacy.net"), rels, sizes);
|
||||
sizesStr = icon.sizesToString();
|
||||
Assert.assertEquals("128x128", sizesStr);
|
||||
|
||||
/* Empty sizes set */
|
||||
sizes = new HashSet<>();
|
||||
|
||||
icon = new IconEntry(new DigestURL("http://yacy.net"), rels, sizes);
|
||||
sizesStr = icon.sizesToString();
|
||||
Assert.assertTrue(sizesStr.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void testRelToString() throws MalformedURLException {
|
||||
/* Multiple values in rel set */
|
||||
Set<String> rels = new HashSet<>();
|
||||
rels.add(IconLinkRelations.ICON.getRelValue());
|
||||
rels.add(IconLinkRelations.APPLE_TOUCH_ICON.getRelValue());
|
||||
rels.add(IconLinkRelations.MASK_ICON.getRelValue());
|
||||
|
||||
Set<Dimension> sizes = new HashSet<>();
|
||||
sizes.add(new Dimension(128,128));
|
||||
|
||||
IconEntry icon = new IconEntry(new DigestURL("http://yacy.net"), rels, sizes);
|
||||
String relStr = icon.relToString();
|
||||
/* The set is not ordered, only check result contains what we expect */
|
||||
Assert.assertTrue(relStr.contains(IconLinkRelations.ICON.getRelValue()));
|
||||
Assert.assertTrue(relStr.contains(IconLinkRelations.APPLE_TOUCH_ICON.getRelValue()));
|
||||
Assert.assertTrue(relStr.contains(IconLinkRelations.MASK_ICON.getRelValue()));
|
||||
Assert.assertTrue(relStr.contains(" "));
|
||||
|
||||
/* One value in rel set */
|
||||
rels = new HashSet<>();
|
||||
rels.add(IconLinkRelations.ICON.getRelValue());
|
||||
|
||||
icon = new IconEntry(new DigestURL("http://yacy.net"), rels, sizes);
|
||||
relStr = icon.relToString();
|
||||
Assert.assertEquals(IconLinkRelations.ICON.getRelValue(), relStr);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,157 @@
|
||||
/**
|
||||
* URIMetadataNodeTest
|
||||
* Copyright 2011 by Michael Peter Christen
|
||||
* First released 28.04.2011 at http://yacy.net
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
package net.yacy.kelondro.data.meta;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.yacy.cora.document.id.DigestURL;
|
||||
import net.yacy.document.parser.html.IconEntry;
|
||||
import net.yacy.search.schema.CollectionConfiguration;
|
||||
import net.yacy.search.schema.CollectionSchema;
|
||||
|
||||
/**
|
||||
* Unit tests for URIMetadataNode class.
|
||||
*
|
||||
* @author luc
|
||||
*
|
||||
*/
|
||||
public class URIMetadataNodeTest {
|
||||
|
||||
/**
|
||||
* Three standard icons with diferrent sizes, one non-standard with a larger
|
||||
* size
|
||||
*/
|
||||
@Test
|
||||
public final void testGetIcons4Items() 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/iconApple128.png" });
|
||||
List<String> protocols = CollectionConfiguration
|
||||
.protocolList2indexedList(Arrays.asList(new String[] { "http", "https", "https", "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[] { "16x24", "32x32", "58x64", "128x128" });
|
||||
|
||||
Collection<IconEntry> icons = metadataNode.getIcons();
|
||||
int nb = 0;
|
||||
/* Check results consistency */
|
||||
for(IconEntry icon : icons) {
|
||||
if("http://somehost.org/static/images/icon16.png".equals(icon.getUrl().toNormalform(false))) {
|
||||
Assert.assertEquals(1, icon.getSizes().size());
|
||||
Dimension size = icon.getSizes().iterator().next();
|
||||
Assert.assertEquals(16, size.width);
|
||||
Assert.assertEquals(24, size.height);
|
||||
Assert.assertEquals(1, icon.getRel().size());
|
||||
Assert.assertEquals("icon", icon.getRel().iterator().next());
|
||||
nb++;
|
||||
} else if("https://somehost.org/static/images/icon32.png".equals(icon.getUrl().toNormalform(false))) {
|
||||
Assert.assertEquals(1, icon.getSizes().size());
|
||||
Dimension size = icon.getSizes().iterator().next();
|
||||
Assert.assertEquals(32, size.width);
|
||||
Assert.assertEquals(32, size.height);
|
||||
Assert.assertEquals(1, icon.getRel().size());
|
||||
Assert.assertEquals("icon", icon.getRel().iterator().next());
|
||||
nb++;
|
||||
} else if("https://somehost.org/static/images/icon64.png".equals(icon.getUrl().toNormalform(false))) {
|
||||
Assert.assertEquals(1, icon.getSizes().size());
|
||||
Dimension size = icon.getSizes().iterator().next();
|
||||
Assert.assertEquals(58, size.width);
|
||||
Assert.assertEquals(64, size.height);
|
||||
Assert.assertEquals(1, icon.getRel().size());
|
||||
Assert.assertEquals("icon", icon.getRel().iterator().next());
|
||||
nb++;
|
||||
} else if("http://somehost.org/static/images/iconApple128.png".equals(icon.getUrl().toNormalform(false))) {
|
||||
Assert.assertEquals(1, icon.getSizes().size());
|
||||
Dimension size = icon.getSizes().iterator().next();
|
||||
Assert.assertEquals(128, size.width);
|
||||
Assert.assertEquals(128, size.height);
|
||||
Assert.assertEquals(1, icon.getRel().size());
|
||||
Assert.assertEquals("apple-touch-icon", icon.getRel().iterator().next());
|
||||
nb++;
|
||||
}
|
||||
}
|
||||
Assert.assertEquals(4, nb);
|
||||
}
|
||||
|
||||
/**
|
||||
* Only icons_urlstub_sxt field valued
|
||||
*/
|
||||
@Test
|
||||
public final void testGetIconsOnlyIconsUrlstubSxt() 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" });
|
||||
|
||||
Collection<IconEntry> icons = metadataNode.getIcons();
|
||||
Assert.assertEquals(4, icons.size());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Only one standard icon
|
||||
*/
|
||||
@Test
|
||||
public final void testGetIcons1Item() 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" });
|
||||
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" });
|
||||
|
||||
Collection<IconEntry> icons = metadataNode.getIcons();
|
||||
Assert.assertEquals(1, icons.size());
|
||||
IconEntry icon = icons.iterator().next();
|
||||
Assert.assertEquals(1, icon.getSizes().size());
|
||||
Dimension size = icon.getSizes().iterator().next();
|
||||
Assert.assertEquals(16.0, size.getWidth(), 0.0);
|
||||
Assert.assertEquals(16.0, size.getHeight(), 0.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* No Icon
|
||||
*/
|
||||
@Test
|
||||
public final void testGetIconsNoIcon() throws MalformedURLException {
|
||||
URIMetadataNode metadataNode = new URIMetadataNode(new DigestURL("http://somehost.org"));
|
||||
|
||||
Collection<IconEntry> icons = metadataNode.getIcons();
|
||||
Assert.assertEquals(0, icons.size());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,187 @@
|
||||
|
||||
/**
|
||||
* URIMetadataNodeTest
|
||||
* Copyright 2011 by Michael Peter Christen
|
||||
* First released 28.04.2011 at http://yacy.net
|
||||
*
|
||||
* 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.cora.protocol.RequestHeader;
|
||||
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 diferrent 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(false, RequestHeader.FileType.HTML, 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(false, RequestHeader.FileType.HTML, 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(false, RequestHeader.FileType.HTML, 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(false, RequestHeader.FileType.HTML, 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(false, RequestHeader.FileType.HTML, 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(false, RequestHeader.FileType.HTML, 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(false, RequestHeader.FileType.HTML, metadataNode,
|
||||
new Dimension(32, 32));
|
||||
Assert.assertNotNull(faviconURL);
|
||||
Assert.assertEquals("http://somehost.org/static/images/favicon.ico", 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(false, RequestHeader.FileType.HTML, metadataNode,
|
||||
new Dimension(32, 32));
|
||||
Assert.assertEquals("http://somehost.org/favicon.ico", faviconURL.toNormalform(false));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue