1+ package org .dbpedia .extraction .mappings
2+
3+ import org .dbpedia .extraction .wikiparser ._
4+ import org .dbpedia .extraction .sources .{WikiPage , XMLSource }
5+ import org .dbpedia .extraction .destinations .{QuadBuilder , Quad }
6+ import org .dbpedia .extraction .destinations .formatters .TerseFormatter
7+ import org .dbpedia .extraction .ontology .io .OntologyReader
8+ import io .Source
9+ import org .dbpedia .extraction .util .Language
10+ import java .io .{FilenameFilter , File }
11+ import scala .collection .mutable .ArrayBuffer
12+ import org .junit .Test
13+ import org .dbpedia .extraction .ontology .{OntologyProperty , Ontology }
14+ import org .scalatest .FlatSpec
15+ import org .scalatest .matchers .ShouldMatchers
16+ import org .scalatest .junit .JUnitRunner
17+ import org .junit .runner .RunWith
18+ import org .dbpedia .extraction .dataparser .BooleanParser
19+
20+ /**
21+ *
22+ */
23+ @ RunWith (classOf [JUnitRunner ])
24+ class HomepageExtractorTest extends FlatSpec with ShouldMatchers
25+ {
26+ // Tests
27+ " HomepageExtractor" should " return http://example.com from ExternalLink in External links section" in {
28+
29+ val lang = Language .English
30+ val quad : Seq [Quad ] = HomepageExtractorTest .datasets.toList.map(dataset =>
31+ new Quad (lang, dataset, " TestPage" , HomepageExtractorTest .homepageProperty, " http://example.com" , null , null )
32+ )
33+
34+ parse(
35+ """
36+ |==External links==
37+ |
38+ |* [http://example.com Official website]
39+ |
40+ """ .stripMargin, " TestPage" , lang) should equal (quad)
41+ }
42+
43+ " HomepageExtractor" should """ return http://example.com from Template 'Official website' in External links section""" in {
44+
45+ val lang = Language .English
46+ val quad : Seq [Quad ] = HomepageExtractorTest .datasets.toList.map(dataset =>
47+ new Quad (lang, dataset, " TestPage" , HomepageExtractorTest .homepageProperty, " http://example.com" , null , null )
48+ )
49+
50+ parse(
51+ """
52+ |==External links==
53+ |
54+ |* {{Official website|example.com}}
55+ |
56+ """ .stripMargin, " TestPage" , lang) should equal (quad)
57+ }
58+
59+ it should """ return http://correct.example.com from Template 'Official website' in External links section""" in {
60+
61+ val lang = Language .English
62+ val quad : Seq [Quad ] = HomepageExtractorTest .datasets.toList.map(dataset =>
63+ new Quad (lang, dataset, " TestPage" , HomepageExtractorTest .homepageProperty, " http://correct.example.com" , null , null )
64+ )
65+
66+ parse(
67+ """
68+ |==External links==
69+ |
70+ |* {{Official website|2=example.com}}
71+ |* {{Official website|correct.example.com}}
72+ |
73+ """ .stripMargin, " TestPage" , lang) should equal (quad)
74+ }
75+
76+ " HomepageExtractor" should """ return http://example.com from Template 'Official' in External links section""" in {
77+
78+ val lang = Language .English
79+ val quad : Seq [Quad ] = HomepageExtractorTest .datasets.toList.map(dataset =>
80+ new Quad (lang, dataset, " TestPage" , HomepageExtractorTest .homepageProperty, " http://example.com" , null , null )
81+ )
82+
83+ parse(
84+ """
85+ |==External links==
86+ |
87+ |* {{Official|http://example.com}}
88+ |
89+ """ .stripMargin, " TestPage" , lang) should equal (quad)
90+ }
91+
92+ " HomepageExtractor" should """ return http://example.com from Template property 'website = example.com'""" in {
93+
94+ val lang = Language .English
95+ val quad : Seq [Quad ] = HomepageExtractorTest .datasets.toList.map(dataset =>
96+ new Quad (lang, dataset, " TestPage" , HomepageExtractorTest .homepageProperty, " http://example.com" , null , null )
97+ )
98+
99+ parse(""" {{Infobox | website = example.com}}""" , " TestPage" , lang) should equal (quad)
100+ }
101+
102+ " HomepageExtractor" should """ return http://example.com from Template property 'website = http://example.com'""" in {
103+
104+ val lang = Language .English
105+ val quad : Seq [Quad ] = HomepageExtractorTest .datasets.toList.map(dataset =>
106+ new Quad (lang, dataset, " TestPage" , HomepageExtractorTest .homepageProperty, " http://example.com" , null , null )
107+ )
108+
109+ parse(""" {{Infobox | website = http://example.com}}""" , " TestPage" , lang) should equal (quad)
110+ }
111+
112+ " HomepageExtractor" should """ return http://example.com from Template property 'website = http://example.com or http://or.com'""" in {
113+
114+ val lang = Language .English
115+ val quad : Seq [Quad ] = HomepageExtractorTest .datasets.toList.map(dataset =>
116+ new Quad (lang, dataset, " TestPage" , HomepageExtractorTest .homepageProperty, " http://example.com" , null , null )
117+ )
118+
119+ parse(""" {{Infobox | website = http://example.com or http://or.com}}""" , " TestPage" , lang) should equal (quad)
120+ }
121+
122+ it should """ return Seq.empty from Template property 'website = N/A'""" in {
123+
124+ val lang = Language .English
125+ val quad : Seq [Quad ] = HomepageExtractorTest .datasets.toList.map(dataset =>
126+ new Quad (lang, dataset, " TestPage" , HomepageExtractorTest .homepageProperty, " http://example.com" , null , null )
127+ )
128+
129+ parse(""" {{Infobox | website = N/A}}""" , " TestPage" , lang) should equal (Seq .empty)
130+ }
131+
132+ // end of tests
133+
134+ private val parser = WikiParser .getInstance()
135+
136+ private def parse (input : String , title : String = " TestPage" , lang : Language = Language .English ) : Seq [Quad ] =
137+ {
138+ val page = new WikiPage (WikiTitle .parse(title, lang), input)
139+ val context = new {
140+ def ontology = HomepageExtractorTest .ontology;
141+ def language = lang;
142+ def redirects = new Redirects (Map (" Official" -> " Official website" ))
143+ }
144+
145+ val extractor = new HomepageExtractor (context)
146+
147+ extractor.extract(parser(page)," TestPage" , new PageContext ())
148+ }
149+ }
150+
151+ object HomepageExtractorTest {
152+
153+ // We need the OntologyProperty for "foaf:homepage"
154+ private val homepageProperty = new OntologyProperty (" Foaf:homepage" , Map (Language .English -> " homepage" ), Map (), null , null , false , Set ())
155+
156+ /**
157+ * val classes : Map[String, OntologyClass],
158+ val properties : Map[String, OntologyProperty],
159+ val datatypes : Map[String, Datatype],
160+ val specializations : Map[(OntologyClass, OntologyProperty), UnitDatatype],
161+ val equivalentPropertiesMap : Map[OntologyProperty,Set[OntologyProperty]],
162+ val equivalentClassesMap : Map[OntologyProperty,Set[OntologyProperty]]
163+
164+ name: String,
165+ labels: Map[Language, String],
166+ comments: Map[Language, String],
167+ val domain: OntologyClass,
168+ val range: OntologyType,
169+ val isFunctional: Boolean,
170+ val equivalentProperties: Set[OntologyProperty]
171+ */
172+ private val ontology = new Ontology (
173+ Map (),
174+ Map (" foaf:homepage" -> homepageProperty),
175+ Map (),
176+ Map (),
177+ Map (),
178+ Map ()
179+ )
180+
181+ private val datasets = new HomepageExtractor (new {
182+ def ontology = HomepageExtractorTest .ontology;
183+ def language = Language .English ;
184+ def redirects = null
185+ }).datasets
186+ }
0 commit comments