Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id DAD8B200BC1 for ; Wed, 16 Nov 2016 11:55:40 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id D95B7160B1C; Wed, 16 Nov 2016 10:55:40 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 8F126160B17 for ; Wed, 16 Nov 2016 11:55:38 +0100 (CET) Received: (qmail 14158 invoked by uid 500); 16 Nov 2016 10:55:37 -0000 Mailing-List: contact commits-help@commonsrdf.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commonsrdf.incubator.apache.org Delivered-To: mailing list commits@commonsrdf.incubator.apache.org Received: (qmail 14149 invoked by uid 99); 16 Nov 2016 10:55:37 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 16 Nov 2016 10:55:37 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 2B30FC1E82 for ; Wed, 16 Nov 2016 10:55:37 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.001 X-Spam-Level: X-Spam-Status: No, score=0.001 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, KAM_LINEPADDING=1.2, RP_MATCHES_RCVD=-2.999] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id HAfjVv2YrRUZ for ; Wed, 16 Nov 2016 10:55:31 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTP id 75CEF5FD92 for ; Wed, 16 Nov 2016 10:55:24 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id EED65E8EB0 for ; Wed, 16 Nov 2016 10:55:20 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id E5C083A1B6A for ; Wed, 16 Nov 2016 10:55:20 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: svn commit: r1001191 [27/29] - in /websites/production/commonsrdf/content: ./ apidocs/ apidocs/org/apache/commons/rdf/api/ apidocs/org/apache/commons/rdf/api/class-use/ apidocs/org/apache/commons/rdf/experimental/ apidocs/org/apache/commons/rdf/experim... Date: Wed, 16 Nov 2016 10:55:19 -0000 To: commits@commonsrdf.incubator.apache.org From: stain@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20161116105520.E5C083A1B6A@svn01-us-west.apache.org> archived-at: Wed, 16 Nov 2016 10:55:41 -0000 Added: websites/production/commonsrdf/content/testapidocs/src-html/org/apache/commons/rdf/api/AbstractGraphTest.html ============================================================================== --- websites/production/commonsrdf/content/testapidocs/src-html/org/apache/commons/rdf/api/AbstractGraphTest.html (added) +++ websites/production/commonsrdf/content/testapidocs/src-html/org/apache/commons/rdf/api/AbstractGraphTest.html Wed Nov 16 10:55:17 2016 @@ -0,0 +1,612 @@ + + + +Source code + + + +
+
001/**
+002 * Licensed to the Apache Software Foundation (ASF) under one
+003 * or more contributor license agreements. See the NOTICE file
+004 * distributed with this work for additional information
+005 * regarding copyright ownership. The ASF licenses this file
+006 * to you under the Apache License, Version 2.0 (the
+007 * "License"); you may not use this file except in compliance
+008 * with the License.  You may obtain a copy of the License at
+009 *
+010 *     http://www.apache.org/licenses/LICENSE-2.0
+011 *
+012 * Unless required by applicable law or agreed to in writing, software
+013 * distributed under the License is distributed on an "AS IS" BASIS,
+014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+015 * See the License for the specific language governing permissions and
+016 * limitations under the License.
+017 */
+018package org.apache.commons.rdf.api;
+019
+020import static org.junit.Assert.assertEquals;
+021import static org.junit.Assert.assertFalse;
+022import static org.junit.Assert.assertNotNull;
+023import static org.junit.Assert.assertTrue;
+024import static org.junit.Assert.fail;
+025
+026import java.util.ArrayList;
+027import java.util.HashSet;
+028import java.util.Iterator;
+029import java.util.List;
+030import java.util.Map;
+031import java.util.Optional;
+032import java.util.concurrent.ConcurrentHashMap;
+033import java.util.stream.Stream;
+034
+035import org.junit.Assume;
+036import org.junit.Before;
+037import org.junit.Test;
+038
+039/**
+040 * Test Graph implementation
+041 * <p>
+042 * To add to your implementation's tests, create a subclass with a name ending
+043 * in <code>Test</code> and provide {@link #createFactory()} which minimally
+044 * must support {@link RDF#createGraph()} and {@link RDF#createIRI(String)}, but
+045 * ideally support all operations.
+046 * <p>
+047 * This test uses try-with-resources blocks for calls to {@link Graph#stream()}
+048 * and {@link Graph#iterate()}.
+049 * 
+050 * @see Graph
+051 * @see RDF
+052 */
+053public abstract class AbstractGraphTest {
+054
+055    protected RDF factory;
+056    protected Graph graph;
+057    protected IRI alice;
+058    protected IRI bob;
+059    protected IRI name;
+060    protected IRI knows;
+061    protected IRI member;
+062    protected BlankNode bnode1;
+063    protected BlankNode bnode2;
+064    protected Literal aliceName;
+065    protected Literal bobName;
+066    protected Literal secretClubName;
+067    protected Literal companyName;
+068    protected Triple bobNameTriple;
+069
+070    /**
+071     * 
+072     * This method must be overridden by the implementing test to provide a
+073     * factory for the test to create {@link Graph}, {@link IRI} etc.
+074     * 
+075     * @return {@link RDF} instance to be tested.
+076     */
+077    protected abstract RDF createFactory();
+078
+079    @Before
+080    public void createGraphAndAdd() {
+081        factory = createFactory();
+082        graph = factory.createGraph();
+083        assertEquals(0, graph.size());
+084
+085        alice = factory.createIRI("http://example.com/alice");
+086        bob = factory.createIRI("http://example.com/bob");
+087        name = factory.createIRI("http://xmlns.com/foaf/0.1/name");
+088        knows = factory.createIRI("http://xmlns.com/foaf/0.1/knows");
+089        member = factory.createIRI("http://xmlns.com/foaf/0.1/member");
+090        try {
+091            bnode1 = factory.createBlankNode("org1");
+092            bnode2 = factory.createBlankNode("org2");
+093        } catch (UnsupportedOperationException ex) {
+094            // leave as null
+095        }
+096
+097        try {
+098            secretClubName = factory.createLiteral("The Secret Club");
+099            companyName = factory.createLiteral("A company");
+100            aliceName = factory.createLiteral("Alice");
+101            bobName = factory.createLiteral("Bob", "en-US");
+102        } catch (UnsupportedOperationException ex) {
+103            // leave as null
+104        }
+105
+106        if (aliceName != null) {
+107            graph.add(alice, name, aliceName);
+108        }
+109        graph.add(alice, knows, bob);
+110
+111        if (bnode1 != null) {
+112            graph.add(alice, member, bnode1);
+113        }
+114
+115        if (bobName != null) {
+116            try {
+117                bobNameTriple = factory.createTriple(bob, name, bobName);
+118            } catch (UnsupportedOperationException ex) {
+119                // leave as null
+120            }
+121            if (bobNameTriple != null) {
+122                graph.add(bobNameTriple);
+123            }
+124        }
+125        if (bnode1 != null) {
+126            graph.add(factory.createTriple(bob, member, bnode1));
+127            graph.add(factory.createTriple(bob, member, bnode2));
+128            if (secretClubName != null) {
+129                graph.add(bnode1, name, secretClubName);
+130                graph.add(bnode2, name, companyName);
+131            }
+132        }
+133    }
+134
+135    @Test
+136    public void size() throws Exception {
+137        assertTrue(graph.size() > 0);
+138        Assume.assumeNotNull(bnode1, bnode2, aliceName, bobName, secretClubName, companyName, bobNameTriple);
+139        // Can only reliably predict size if we could create all triples
+140        assertEquals(8, graph.size());
+141    }
+142
+143    @Test
+144    public void iterate() throws Exception {
+145
+146        Assume.assumeTrue(graph.size() > 0);
+147
+148        List<Triple> triples = new ArrayList<>();
+149        for (Triple t : graph.iterate()) {
+150            triples.add(t);
+151        }
+152        assertEquals(graph.size(), triples.size());
+153        if (bobNameTriple != null) {
+154            assertTrue(triples.contains(bobNameTriple));
+155        }
+156
+157        // aborted iteration
+158        Iterable<Triple> iterate = graph.iterate();
+159        Iterator<Triple> it = iterate.iterator();
+160
+161        assertTrue(it.hasNext());
+162        it.next();
+163        closeIterable(iterate);
+164
+165        // second iteration - should start from fresh and
+166        // get the same count
+167        long count = 0;
+168        Iterable<Triple> iterable = graph.iterate();
+169        for (@SuppressWarnings("unused")
+170        Triple t : iterable) {
+171            count++;
+172        }
+173        assertEquals(graph.size(), count);
+174    }
+175
+176    /**
+177     * Special triple closing for RDF4J.
+178     */
+179    private void closeIterable(Iterable<Triple> iterate) throws Exception {
+180        if (iterate instanceof AutoCloseable) {
+181            ((AutoCloseable) iterate).close();
+182        }
+183    }
+184
+185    @Test
+186    public void iterateFilter() throws Exception {
+187        List<RDFTerm> friends = new ArrayList<>();
+188        IRI alice = factory.createIRI("http://example.com/alice");
+189        IRI knows = factory.createIRI("http://xmlns.com/foaf/0.1/knows");
+190        for (Triple t : graph.iterate(alice, knows, null)) {
+191            friends.add(t.getObject());
+192        }
+193        assertEquals(1, friends.size());
+194        assertEquals(bob, friends.get(0));
+195
+196        // .. can we iterate over zero hits?
+197        Iterable<Triple> iterate = graph.iterate(bob, knows, alice);
+198        for (Triple unexpected : iterate) {
+199            fail("Unexpected triple " + unexpected);
+200        }
+201        // closeIterable(iterate);
+202    }
+203
+204    @Test
+205    public void contains() throws Exception {
+206        assertFalse(graph.contains(bob, knows, alice)); // or so he claims..
+207
+208        assertTrue(graph.contains(alice, knows, bob));
+209
+210        try (Stream<? extends Triple> stream = graph.stream()) {
+211            Optional<? extends Triple> first = stream.skip(4).findFirst();
+212            Assume.assumeTrue(first.isPresent());
+213            Triple existingTriple = first.get();
+214            assertTrue(graph.contains(existingTriple));
+215        }
+216
+217        Triple nonExistingTriple = factory.createTriple(bob, knows, alice);
+218        assertFalse(graph.contains(nonExistingTriple));
+219
+220        Triple triple = null;
+221        try {
+222            triple = factory.createTriple(alice, knows, bob);
+223        } catch (UnsupportedOperationException ex) {
+224        }
+225        if (triple != null) {
+226            // FIXME: Should not this always be true?
+227            // assertTrue(graph.contains(triple));
+228        }
+229    }
+230
+231    @Test
+232    public void remove() throws Exception {
+233        long fullSize = graph.size();
+234        graph.remove(alice, knows, bob);
+235        long shrunkSize = graph.size();
+236        assertEquals(1, fullSize - shrunkSize);
+237
+238        graph.remove(alice, knows, bob);
+239        assertEquals(shrunkSize, graph.size()); // unchanged
+240
+241        graph.add(alice, knows, bob);
+242        graph.add(alice, knows, bob);
+243        graph.add(alice, knows, bob);
+244        // Undetermined size at this point -- but at least it
+245        // should be bigger
+246        assertTrue(graph.size() > shrunkSize);
+247
+248        // and after a single remove they should all be gone
+249        graph.remove(alice, knows, bob);
+250        assertEquals(shrunkSize, graph.size());
+251
+252        Triple otherTriple;
+253        try (Stream<? extends Triple> stream = graph.stream()) {
+254            Optional<? extends Triple> anyTriple = stream.findAny();
+255            Assume.assumeTrue(anyTriple.isPresent());
+256            otherTriple = anyTriple.get();
+257        }
+258
+259        graph.remove(otherTriple);
+260        assertEquals(shrunkSize - 1, graph.size());
+261        graph.remove(otherTriple);
+262        assertEquals(shrunkSize - 1, graph.size()); // no change
+263
+264        // for some reason in rdf4j this causes duplicates!
+265        graph.add(otherTriple);
+266        // graph.stream().forEach(System.out::println);
+267        // should have increased
+268        assertTrue(graph.size() >= shrunkSize);
+269    }
+270
+271    @Test
+272    public void clear() throws Exception {
+273        graph.clear();
+274        assertFalse(graph.contains(alice, knows, bob));
+275        assertEquals(0, graph.size());
+276        graph.clear(); // no-op
+277        assertEquals(0, graph.size());
+278    }
+279
+280    @Test
+281    public void getTriples() throws Exception {
+282        long tripleCount;
+283        try (Stream<? extends Triple> stream = graph.stream()) {
+284            tripleCount = stream.count();
+285        }
+286        assertTrue(tripleCount > 0);
+287
+288        try (Stream<? extends Triple> stream = graph.stream()) {
+289            assertTrue(stream.allMatch(t -> graph.contains(t)));
+290        }
+291
+292        // Check exact count
+293        Assume.assumeNotNull(bnode1, bnode2, aliceName, bobName, secretClubName, companyName, bobNameTriple);
+294        assertEquals(8, tripleCount);
+295    }
+296
+297    @Test
+298    public void getTriplesQuery() throws Exception {
+299
+300        try (Stream<? extends Triple> stream = graph.stream(alice, null, null)) {
+301            long aliceCount = stream.count();
+302            assertTrue(aliceCount > 0);
+303            Assume.assumeNotNull(aliceName);
+304            assertEquals(3, aliceCount);
+305        }
+306
+307        Assume.assumeNotNull(bnode1, bnode2, bobName, companyName, secretClubName);
+308        try (Stream<? extends Triple> stream = graph.stream(null, name, null)) {
+309            assertEquals(4, stream.count());
+310        }
+311        Assume.assumeNotNull(bnode1);
+312        try (Stream<? extends Triple> stream = graph.stream(null, member, null)) {
+313            assertEquals(3, stream.count());
+314        }
+315    }
+316
+317    @Test
+318    public void addBlankNodesFromMultipleGraphs() {
+319
+320        try {
+321            // Create two separate Graph instances
+322            Graph g1 = createGraph1();
+323            Graph g2 = createGraph2();
+324
+325            // and add them to a new Graph g3
+326            Graph g3 = factory.createGraph();
+327            addAllTriples(g1, g3);
+328            addAllTriples(g2, g3);
+329
+330            // Let's make a map to find all those blank nodes after insertion
+331            // (The Graph implementation is not currently required to
+332            // keep supporting those BlankNodes with contains() - see
+333            // COMMONSRDF-15)
+334
+335            final Map<String, BlankNodeOrIRI> whoIsWho = new ConcurrentHashMap<>();
+336            // ConcurrentHashMap as we will try parallel forEach below,
+337            // which should not give inconsistent results (it does with a
+338            // HashMap!)
+339
+340            // look up BlankNodes by name
+341            IRI name = factory.createIRI("http://xmlns.com/foaf/0.1/name");
+342            try (Stream<? extends Triple> stream = g3.stream(null, name, null)) {
+343                stream.parallel().forEach(t -> whoIsWho.put(t.getObject().ntriplesString(), t.getSubject()));
+344            }
+345
+346            assertEquals(4, whoIsWho.size());
+347            // and contains 4 unique values
+348            assertEquals(4, new HashSet<BlankNodeOrIRI>(whoIsWho.values()).size());
+349
+350            BlankNodeOrIRI b1Alice = whoIsWho.get("\"Alice\"");
+351            assertNotNull(b1Alice);
+352            BlankNodeOrIRI b2Bob = whoIsWho.get("\"Bob\"");
+353            assertNotNull(b2Bob);
+354            BlankNodeOrIRI b1Charlie = whoIsWho.get("\"Charlie\"");
+355            assertNotNull(b1Charlie);
+356            BlankNodeOrIRI b2Dave = whoIsWho.get("\"Dave\"");
+357            assertNotNull(b2Dave);
+358
+359            // All blank nodes should differ
+360            notEquals(b1Alice, b2Bob);
+361            notEquals(b1Alice, b1Charlie);
+362            notEquals(b1Alice, b2Dave);
+363            notEquals(b2Bob, b1Charlie);
+364            notEquals(b2Bob, b2Dave);
+365            notEquals(b1Charlie, b2Dave);
+366
+367            // And we should be able to query with them again
+368            // as we got them back from g3
+369            IRI hasChild = factory.createIRI("http://example.com/hasChild");
+370            assertTrue(g3.contains(b1Alice, hasChild, b2Bob));
+371            assertTrue(g3.contains(b2Dave, hasChild, b1Charlie));
+372            // But not
+373            assertFalse(g3.contains(b1Alice, hasChild, b1Alice));
+374            assertFalse(g3.contains(b1Alice, hasChild, b1Charlie));
+375            assertFalse(g3.contains(b1Alice, hasChild, b2Dave));
+376            // nor
+377            assertFalse(g3.contains(b2Dave, hasChild, b1Alice));
+378            assertFalse(g3.contains(b2Dave, hasChild, b1Alice));
+379
+380            // and these don't have any children (as far as we know)
+381            assertFalse(g3.contains(b2Bob, hasChild, null));
+382            assertFalse(g3.contains(b1Charlie, hasChild, null));
+383        } catch (UnsupportedOperationException ex) {
+384            Assume.assumeNoException(ex);
+385        }
+386    }
+387
+388    private void notEquals(BlankNodeOrIRI node1, BlankNodeOrIRI node2) {
+389        assertFalse(node1.equals(node2));
+390        // in which case we should be able to assume
+391        // (as they are in the same graph)
+392        assertFalse(node1.ntriplesString().equals(node2.ntriplesString()));
+393    }
+394
+395    /**
+396     * Add all triples from the source to the target.
+397     * <p>
+398     * The triples may be copied in any order. No special conversion or
+399     * adaptation of {@link BlankNode}s are performed.
+400     *
+401     * @param source
+402     *            Source Graph to copy triples from
+403     * @param target
+404     *            Target Graph where triples will be added
+405     */
+406    private void addAllTriples(Graph source, Graph target) {
+407
+408        // unordered() as we don't need to preserve triple order
+409        // sequential() as we don't (currently) require target Graph to be
+410        // thread-safe
+411
+412        try (Stream<? extends Triple> stream = source.stream()) {
+413            stream.unordered().sequential().forEach(t -> target.add(t));
+414        }
+415    }
+416
+417    /**
+418     * Make a new graph with two BlankNodes - each with a different
+419     * uniqueReference
+420     */
+421    private Graph createGraph1() {
+422        RDF factory1 = createFactory();
+423
+424        IRI name = factory1.createIRI("http://xmlns.com/foaf/0.1/name");
+425        Graph g1 = factory1.createGraph();
+426        BlankNode b1 = createOwnBlankNode("b1", "0240eaaa-d33e-4fc0-a4f1-169d6ced3680");
+427        g1.add(b1, name, factory1.createLiteral("Alice"));
+428
+429        BlankNode b2 = createOwnBlankNode("b2", "9de7db45-0ce7-4b0f-a1ce-c9680ffcfd9f");
+430        g1.add(b2, name, factory1.createLiteral("Bob"));
+431
+432        IRI hasChild = factory1.createIRI("http://example.com/hasChild");
+433        g1.add(b1, hasChild, b2);
+434
+435        return g1;
+436    }
+437
+438    /**
+439     * Create a different implementation of BlankNode to be tested with
+440     * graph.add(a,b,c); (the implementation may or may not then choose to
+441     * translate such to its own instances)
+442     * 
+443     * @param name
+444     * @return
+445     */
+446    private BlankNode createOwnBlankNode(String name, String uuid) {
+447        return new BlankNode() {
+448            @Override
+449            public String ntriplesString() {
+450                return "_: " + name;
+451            }
+452
+453            @Override
+454            public String uniqueReference() {
+455                return uuid;
+456            }
+457
+458            @Override
+459            public int hashCode() {
+460                return uuid.hashCode();
+461            }
+462
+463            @Override
+464            public boolean equals(Object obj) {
+465                if (!(obj instanceof BlankNode)) {
+466                    return false;
+467                }
+468                BlankNode other = (BlankNode) obj;
+469                return uuid.equals(other.uniqueReference());
+470            }
+471        };
+472    }
+473
+474    private Graph createGraph2() {
+475        RDF factory2 = createFactory();
+476        IRI name = factory2.createIRI("http://xmlns.com/foaf/0.1/name");
+477
+478        Graph g2 = factory2.createGraph();
+479
+480        BlankNode b1 = createOwnBlankNode("b1", "bc8d3e45-a08f-421d-85b3-c25b373abf87");
+481        g2.add(b1, name, factory2.createLiteral("Charlie"));
+482
+483        BlankNode b2 = createOwnBlankNode("b2", "2209097a-5078-4b03-801a-6a2d2f50d739");
+484        g2.add(b2, name, factory2.createLiteral("Dave"));
+485
+486        IRI hasChild = factory2.createIRI("http://example.com/hasChild");
+487        // NOTE: Opposite direction of loadGraph1
+488        g2.add(b2, hasChild, b1);
+489        return g2;
+490    }
+491
+492    /**
+493     * An attempt to use the Java 8 streams to look up a more complicated query.
+494     * <p>
+495     * FYI, the equivalent SPARQL version (untested):
+496     * 
+497     * <pre>
+498     *     SELECT ?orgName WHERE {
+499     *             ?org foaf:name ?orgName .
+500     *             ?alice foaf:member ?org .
+501     *             ?bob foaf:member ?org .
+502     *             ?alice foaf:knows ?bob .
+503     *           FILTER NOT EXIST { ?bob foaf:knows ?alice }
+504     *    }
+505     * </pre>
+506     *
+507     * @throws Exception If test fails
+508     */
+509    @Test
+510    public void whyJavaStreamsMightNotTakeOverFromSparql() throws Exception {
+511        Assume.assumeNotNull(bnode1, bnode2, secretClubName);
+512        // Find a secret organizations
+513        try (Stream<? extends Triple> stream = graph.stream(null, knows, null)) {
+514            assertEquals("\"The Secret Club\"",
+515                    // Find One-way "knows"
+516                    stream.filter(t -> !graph.contains((BlankNodeOrIRI) t.getObject(), knows, t.getSubject()))
+517                            .map(knowsTriple -> {
+518                                try (Stream<? extends Triple> memberOf = graph
+519                                        // and those they know, what are they
+520                                        // member of?
+521                                        .stream((BlankNodeOrIRI) knowsTriple.getObject(), member, null)) {
+522                                    return memberOf
+523                                            // keep those which first-guy is a
+524                                            // member of
+525                                            .filter(memberTriple -> graph.contains(knowsTriple.getSubject(), member,
+526                                                    // First hit is good enough
+527                                                    memberTriple.getObject()))
+528                                            .findFirst().get().getObject();
+529                                }
+530                            })
+531                            // then look up the name of that org
+532                            .map(org -> {
+533                                try (Stream<? extends Triple> orgName = graph.stream((BlankNodeOrIRI) org, name,
+534                                        null)) {
+535                                    return orgName.findFirst().get().getObject().ntriplesString();
+536                                }
+537                            }).findFirst().get());
+538        }
+539    }
+540}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ + \ No newline at end of file Added: websites/production/commonsrdf/content/testapidocs/src-html/org/apache/commons/rdf/api/AbstractRDFTest.html ============================================================================== --- websites/production/commonsrdf/content/testapidocs/src-html/org/apache/commons/rdf/api/AbstractRDFTest.html (added) +++ websites/production/commonsrdf/content/testapidocs/src-html/org/apache/commons/rdf/api/AbstractRDFTest.html Wed Nov 16 10:55:17 2016 @@ -0,0 +1,382 @@ + + + +Source code + + + +
+
001/**
+002 * Licensed to the Apache Software Foundation (ASF) under one
+003 * or more contributor license agreements. See the NOTICE file
+004 * distributed with this work for additional information
+005 * regarding copyright ownership. The ASF licenses this file
+006 * to you under the Apache License, Version 2.0 (the
+007 * "License"); you may not use this file except in compliance
+008 * with the License.  You may obtain a copy of the License at
+009 *
+010 *     http://www.apache.org/licenses/LICENSE-2.0
+011 *
+012 * Unless required by applicable law or agreed to in writing, software
+013 * distributed under the License is distributed on an "AS IS" BASIS,
+014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+015 * See the License for the specific language governing permissions and
+016 * limitations under the License.
+017 */
+018package org.apache.commons.rdf.api;
+019
+020import static org.junit.Assert.assertEquals;
+021import static org.junit.Assert.assertFalse;
+022import static org.junit.Assert.assertNotEquals;
+023import static org.junit.Assert.assertNotSame;
+024
+025import java.util.Objects;
+026
+027import org.junit.Before;
+028import org.junit.Test;
+029
+030/**
+031 * Test RDF implementation (and thus its RDFTerm implementations)
+032 * <p>
+033 * To add to your implementation's tests, create a subclass with a name ending
+034 * in <code>Test</code> and provide {@link #createFactory()} which minimally
+035 * supports one of the operations, but ideally supports all operations.
+036 *
+037 * @see RDF
+038 */
+039public abstract class AbstractRDFTest {
+040
+041    private RDF factory;
+042
+043    /**
+044     * 
+045     * This method must be overridden by the implementing test to provide a
+046     * factory for the test to create {@link Literal}, {@link IRI} etc.
+047     * 
+048     * @return {@link RDF} instance to be tested.
+049     */
+050    protected abstract RDF createFactory();
+051
+052    @Before
+053    public void setUp() {
+054        factory = createFactory();
+055    }
+056
+057    @Test
+058    public void testCreateBlankNode() throws Exception {
+059        BlankNode bnode = factory.createBlankNode();
+060
+061        BlankNode bnode2 = factory.createBlankNode();
+062        assertNotEquals("Second blank node has not got a unique internal identifier", bnode.uniqueReference(),
+063                bnode2.uniqueReference());
+064    }
+065
+066    @Test
+067    public void testCreateBlankNodeIdentifierEmpty() throws Exception {
+068        try {
+069            factory.createBlankNode("");
+070        } catch (IllegalArgumentException e) {
+071            // Expected exception
+072        }
+073    }
+074
+075    @Test
+076    public void testCreateBlankNodeIdentifier() throws Exception {
+077        factory.createBlankNode("example1");
+078    }
+079
+080    @Test
+081    public void testCreateBlankNodeIdentifierTwice() throws Exception {
+082        BlankNode bnode1, bnode2, bnode3;
+083        bnode1 = factory.createBlankNode("example1");
+084        bnode2 = factory.createBlankNode("example1");
+085        bnode3 = factory.createBlankNode("differ");
+086        // We don't know what the identifier is, but it MUST be the same
+087        assertEquals(bnode1.uniqueReference(), bnode2.uniqueReference());
+088        // We don't know what the ntriplesString is, but it MUST be the same
+089        assertEquals(bnode1.ntriplesString(), bnode2.ntriplesString());
+090        // and here it MUST differ
+091        assertNotEquals(bnode1.uniqueReference(), bnode3.uniqueReference());
+092        assertNotEquals(bnode1.ntriplesString(), bnode3.ntriplesString());
+093    }
+094
+095    @Test
+096    public void testCreateBlankNodeIdentifierTwiceDifferentFactories() throws Exception {
+097        BlankNode bnode1, differentFactory;
+098        bnode1 = factory.createBlankNode();
+099        // it MUST differ from a second factory
+100        differentFactory = createFactory().createBlankNode();
+101
+102        // NOTE: We can't make similar assumption if we provide a
+103        // name to createBlankNode(String) as its documentation
+104        // only says:
+105        //
+106        // * BlankNodes created using this method with the same parameter, for
+107        // * different instances of RDFFactory, SHOULD NOT be equivalent.
+108        //
+109        // https://github.com/apache/incubator-commonsrdf/pull/7#issuecomment-92312779
+110        assertNotEquals(bnode1, differentFactory);
+111        assertNotEquals(bnode1.uniqueReference(), differentFactory.uniqueReference());
+112        // but we can't require:
+113        // assertNotEquals(bnode1.ntriplesString(),
+114        // differentFactory.ntriplesString());
+115    }
+116
+117    @Test
+118    public void testCreateGraph() {
+119        Graph graph = factory.createGraph();
+120
+121        assertEquals("Graph was not empty", 0, graph.size());
+122        graph.add(factory.createBlankNode(), factory.createIRI("http://example.com/"), factory.createBlankNode());
+123
+124        Graph graph2 = factory.createGraph();
+125        assertNotSame(graph, graph2);
+126        assertEquals("Graph was empty after adding", 1, graph.size());
+127        assertEquals("New graph was not empty", 0, graph2.size());
+128    }
+129
+130    @Test
+131    public void testCreateIRI() throws Exception {
+132        IRI example = factory.createIRI("http://example.com/");
+133
+134        assertEquals("http://example.com/", example.getIRIString());
+135        assertEquals("<http://example.com/>", example.ntriplesString());
+136
+137        IRI term = factory.createIRI("http://example.com/vocab#term");
+138        assertEquals("http://example.com/vocab#term", term.getIRIString());
+139        assertEquals("<http://example.com/vocab#term>", term.ntriplesString());
+140
+141        // and now for the international fun!
+142
+143        IRI latin1 = factory.createIRI("http://accént.example.com/première");
+144        assertEquals("http://accént.example.com/première", latin1.getIRIString());
+145        assertEquals("<http://accént.example.com/première>", latin1.ntriplesString());
+146
+147        IRI cyrillic = factory.createIRI("http://example.испытание/Кириллица");
+148        assertEquals("http://example.испытание/Кириллица", cyrillic.getIRIString());
+149        assertEquals("<http://example.испытание/Кириллица>", cyrillic.ntriplesString());
+150
+151        IRI deseret = factory.createIRI("http://𐐀.example.com/𐐀");
+152        assertEquals("http://𐐀.example.com/𐐀", deseret.getIRIString());
+153        assertEquals("<http://𐐀.example.com/𐐀>", deseret.ntriplesString());
+154    }
+155
+156    @Test
+157    public void testCreateLiteral() throws Exception {
+158        Literal example = factory.createLiteral("Example");
+159        assertEquals("Example", example.getLexicalForm());
+160        assertFalse(example.getLanguageTag().isPresent());
+161        assertEquals("http://www.w3.org/2001/XMLSchema#string", example.getDatatype().getIRIString());
+162        // http://lists.w3.org/Archives/Public/public-rdf-comments/2014Dec/0004.html
+163        assertEquals("\"Example\"", example.ntriplesString());
+164    }
+165
+166    @Test
+167    public void testCreateLiteralDateTime() throws Exception {
+168        Literal dateTime = factory.createLiteral("2014-12-27T00:50:00T-0600",
+169                factory.createIRI("http://www.w3.org/2001/XMLSchema#dateTime"));
+170        assertEquals("2014-12-27T00:50:00T-0600", dateTime.getLexicalForm());
+171        assertFalse(dateTime.getLanguageTag().isPresent());
+172        assertEquals("http://www.w3.org/2001/XMLSchema#dateTime", dateTime.getDatatype().getIRIString());
+173        assertEquals("\"2014-12-27T00:50:00T-0600\"^^<http://www.w3.org/2001/XMLSchema#dateTime>",
+174                dateTime.ntriplesString());
+175    }
+176
+177    @Test
+178    public void testCreateLiteralLang() throws Exception {
+179        Literal example = factory.createLiteral("Example", "en");
+180
+181        assertEquals("Example", example.getLexicalForm());
+182        assertEquals("en", example.getLanguageTag().get());
+183        assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", example.getDatatype().getIRIString());
+184        assertEquals("\"Example\"@en", example.ntriplesString());
+185    }
+186
+187    @Test
+188    public void testCreateLiteralLangISO693_3() throws Exception {
+189        // see https://issues.apache.org/jira/browse/JENA-827
+190        Literal vls = factory.createLiteral("Herbert Van de Sompel", "vls"); // JENA-827
+191
+192        assertEquals("vls", vls.getLanguageTag().get());
+193        assertEquals("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", vls.getDatatype().getIRIString());
+194        assertEquals("\"Herbert Van de Sompel\"@vls", vls.ntriplesString());
+195    }
+196
+197    @Test
+198    public void testCreateLiteralString() throws Exception {
+199        Literal example = factory.createLiteral("Example",
+200                factory.createIRI("http://www.w3.org/2001/XMLSchema#string"));
+201        assertEquals("Example", example.getLexicalForm());
+202        assertFalse(example.getLanguageTag().isPresent());
+203        assertEquals("http://www.w3.org/2001/XMLSchema#string", example.getDatatype().getIRIString());
+204        // http://lists.w3.org/Archives/Public/public-rdf-comments/2014Dec/0004.html
+205        assertEquals("\"Example\"", example.ntriplesString());
+206    }
+207
+208    @Test
+209    public void testCreateTripleBnodeBnode() {
+210        BlankNode subject = factory.createBlankNode("b1");
+211        IRI predicate = factory.createIRI("http://example.com/pred");
+212        BlankNode object = factory.createBlankNode("b2");
+213        Triple triple = factory.createTriple(subject, predicate, object);
+214
+215        // bnode equivalence should be OK as we used the same
+216        // factory and have not yet inserted Triple into a Graph
+217        assertEquals(subject, triple.getSubject());
+218        assertEquals(predicate, triple.getPredicate());
+219        assertEquals(object, triple.getObject());
+220    }
+221
+222    @Test
+223    public void testCreateTripleBnodeIRI() {
+224        BlankNode subject = factory.createBlankNode("b1");
+225        IRI predicate = factory.createIRI("http://example.com/pred");
+226        IRI object = factory.createIRI("http://example.com/obj");
+227        Triple triple = factory.createTriple(subject, predicate, object);
+228
+229        // bnode equivalence should be OK as we used the same
+230        // factory and have not yet inserted Triple into a Graph
+231        assertEquals(subject, triple.getSubject());
+232        assertEquals(predicate, triple.getPredicate());
+233        assertEquals(object, triple.getObject());
+234    }
+235
+236    @Test
+237    public void testCreateTripleBnodeTriple() {
+238        BlankNode subject = factory.createBlankNode();
+239        IRI predicate = factory.createIRI("http://example.com/pred");
+240        Literal object = factory.createLiteral("Example", "en");
+241        Triple triple = factory.createTriple(subject, predicate, object);
+242
+243        // bnode equivalence should be OK as we used the same
+244        // factory and have not yet inserted Triple into a Graph
+245        assertEquals(subject, triple.getSubject());
+246        assertEquals(predicate, triple.getPredicate());
+247        assertEquals(object, triple.getObject());
+248    }
+249
+250    @Test
+251    public void testPossiblyInvalidBlankNode() throws Exception {
+252        BlankNode withColon;
+253        try {
+254            withColon = factory.createBlankNode("with:colon");
+255        } catch (IllegalArgumentException ex) {
+256            // Good!
+257            return;
+258        }
+259        // Factory allows :colon, which is OK as long as it's not causing an
+260        // invalid ntriplesString
+261        assertFalse(withColon.ntriplesString().contains("with:colon"));
+262
+263        // and creating it twice gets the same ntriplesString
+264        assertEquals(withColon.ntriplesString(), factory.createBlankNode("with:colon").ntriplesString());
+265    }
+266
+267    @Test(expected = IllegalArgumentException.class)
+268    public void testInvalidIRI() throws Exception {
+269        factory.createIRI("<no_brackets>");
+270    }
+271
+272    @Test(expected = IllegalArgumentException.class)
+273    public void testInvalidLiteralLang() throws Exception {
+274        factory.createLiteral("Example", "with space");
+275    }
+276
+277    @Test(expected = Exception.class)
+278    public void testInvalidTriplePredicate() {
+279        BlankNode subject = factory.createBlankNode("b1");
+280        BlankNode predicate = factory.createBlankNode("b2");
+281        BlankNode object = factory.createBlankNode("b3");
+282        factory.createTriple(subject, (IRI) predicate, object);
+283    }
+284
+285    @Test
+286    public void hashCodeBlankNode() throws Exception {
+287        BlankNode bnode1 = factory.createBlankNode();
+288        assertEquals(bnode1.uniqueReference().hashCode(), bnode1.hashCode());
+289    }
+290
+291    @Test
+292    public void hashCodeIRI() throws Exception {
+293        IRI iri = factory.createIRI("http://example.com/");
+294        assertEquals(iri.getIRIString().hashCode(), iri.hashCode());
+295    }
+296
+297    @Test
+298    public void hashCodeLiteral() throws Exception {
+299        Literal literal = factory.createLiteral("Hello");
+300        assertEquals(Objects.hash(literal.getLexicalForm(), literal.getDatatype(), literal.getLanguageTag()),
+301                literal.hashCode());
+302    }
+303
+304    @Test
+305    public void hashCodeTriple() throws Exception {
+306        IRI iri = factory.createIRI("http://example.com/");
+307        Triple triple = factory.createTriple(iri, iri, iri);
+308        assertEquals(Objects.hash(iri, iri, iri), triple.hashCode());
+309    }
+310}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ + \ No newline at end of file