<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">
package fedora.services.oaiprovider;

import java.util.Date;
import java.util.Properties;

import junit.framework.TestCase;

import proai.error.RepositoryException;

/**
 * @author Edwin Shin
 * @author cwilper@cs.cornell.edu
 */
public class TestITQLQueryFactory
        extends TestCase {

    public static void main(String[] args) {
        junit.textui.TestRunner.run(TestITQLQueryFactory.class);
    }

    // this test factory is used by multiple tests
    private ITQLQueryFactory getTestQueryFactory() throws Exception {
        Properties props = new Properties();
        props.put(FedoraOAIDriver.PROP_ITEMID,
                  "http://www.openarchives.org/OAI/2.0/itemID");
        props.put(FedoraOAIDriver.PROP_SETSPEC,
                  "http://www.openarchives.org/OAI/2.0/setSpec");
        props.put(FedoraOAIDriver.PROP_SETSPEC_NAME,
                  "http://www.openarchives.org/OAI/2.0/setName");
        props
                .put(FedoraOAIDriver.PROP_ITEM_SETSPEC_PATH,
                     "$item &lt;fedora-rels-ext:isMemberOf&gt; $set $set &lt;http://www.openarchives.org/OAI/2.0/setSpec&gt; $setSpec");

        ITQLQueryFactory factory = new ITQLQueryFactory();
        factory.init(null, null, props);
        return factory;
    }

    public void testListRecordsPrimaryQuery() throws Exception {

        ITQLQueryFactory factory = getTestQueryFactory();

        Date from = new Date(0L); // the epoch (1970-01-01T00:00:00 GMT)
        Date until = new Date(1000000000000L); // the billenium (2001-09-09T:01:46:40 UTC)

        String afterUTC = factory.getExclusiveDateString(from, false);
        String beforeUTC = factory.getExclusiveDateString(until, true);

        String dissTypeURI = "info:fedora/*/oai_dc";

        String query =
                factory
                        .getListRecordsPrimaryQuery(afterUTC,
                                                    beforeUTC,
                                                    InvocationSpec
                                                            .getInstance(dissTypeURI));

        String q =
                "select $item $itemID $date $state\n"
                        + "from   &lt;#ri&gt;\n"
                        + "where  $item           &lt;http://www.openarchives.org/OAI/2.0/itemID&gt; $itemID\n"
                        + "and    $item           &lt;info:fedora/fedora-system:def/view#disseminates&gt; $recordDiss\n"
                        + "and    $recordDiss     &lt;info:fedora/fedora-system:def/model#state&gt; $state\n"
                        + "and    $recordDiss     &lt;info:fedora/fedora-system:def/view#disseminationType&gt; &lt;info:fedora/*/oai_dc&gt;\n"
                        + "and    $recordDiss     &lt;info:fedora/fedora-system:def/view#lastModifiedDate&gt; $date\n"
                        + "and    $date           &lt;http://tucana.org/tucana#after&gt; '1969-12-31T23:59:59.999Z'^^&lt;http://www.w3.org/2001/XMLSchema#dateTime&gt; in &lt;#xsd&gt;\n"
                        + "and    $date           &lt;http://tucana.org/tucana#before&gt; '2001-09-09T01:46:40.001Z'^^&lt;http://www.w3.org/2001/XMLSchema#dateTime&gt; in &lt;#xsd&gt;\n"
                        + "order  by $itemID asc";

        assertEquals(q, query);
    }

    public void testListRecordsSetMembershipQuery() throws Exception {

        ITQLQueryFactory factory = getTestQueryFactory();

        Date from = new Date(0L); // the epoch (1970-01-01T00:00:00 GMT)
        Date until = new Date(1000000000000L); // the billenium (2001-09-09T:01:46:40 UTC)

        String afterUTC = factory.getExclusiveDateString(from, false);
        String beforeUTC = factory.getExclusiveDateString(until, true);

        String dissTypeURI = "info:fedora/*/oai_dc";

        String query =
                factory
                        .getListRecordsSetMembershipQuery(afterUTC,
                                                          beforeUTC,
                                                          InvocationSpec
                                                                  .getInstance(dissTypeURI));

        String q =
                "select $itemID $setSpec\n"
                        + "from   &lt;#ri&gt;\n"
                        + "where  $item           &lt;http://www.openarchives.org/OAI/2.0/itemID&gt; $itemID\n"
                        + "and    $item           &lt;info:fedora/fedora-system:def/view#disseminates&gt; $recordDiss\n"
                        + "and    $recordDiss     &lt;info:fedora/fedora-system:def/view#disseminationType&gt; &lt;info:fedora/*/oai_dc&gt;\n"
                        + "and    $recordDiss     &lt;info:fedora/fedora-system:def/view#lastModifiedDate&gt; $date\n"
                        + "and    $date           &lt;http://tucana.org/tucana#after&gt; '1969-12-31T23:59:59.999Z'^^&lt;http://www.w3.org/2001/XMLSchema#dateTime&gt; in &lt;#xsd&gt;\n"
                        + "and    $date           &lt;http://tucana.org/tucana#before&gt; '2001-09-09T01:46:40.001Z'^^&lt;http://www.w3.org/2001/XMLSchema#dateTime&gt; in &lt;#xsd&gt;\n"
                        + "and    $item &lt;fedora-rels-ext:isMemberOf&gt; $set and $set &lt;http://www.openarchives.org/OAI/2.0/setSpec&gt; $setSpec\n"
                        + "order  by $itemID asc";

        assertEquals(q, query);
    }

    public void testListRecordsAboutQuery() throws Exception {

        ITQLQueryFactory factory = getTestQueryFactory();

        Date from = new Date(0L); // the epoch (1970-01-01T00:00:00 GMT)
        Date until = new Date(1000000000000L); // the billenium (2001-09-09T:01:46:40 UTC)

        String afterUTC = factory.getExclusiveDateString(from, false);
        String beforeUTC = factory.getExclusiveDateString(until, true);

        String dissTypeURI = "info:fedora/*/oai_dc";
        String aboutDissTypeURI = "info:fedora/*/about_oai_dc";

        FedoraMetadataFormat format =
                new FedoraMetadataFormat("oai_dc", "", "", InvocationSpec
                        .getInstance(dissTypeURI), InvocationSpec
                        .getInstance(aboutDissTypeURI));

        String query =
                factory.getListRecordsAboutQuery(afterUTC, beforeUTC, format);

        String q =
                "select $itemID\n"
                        + "from   &lt;#ri&gt;\n"
                        + "where  $item           &lt;http://www.openarchives.org/OAI/2.0/itemID&gt; $itemID\n"
                        + "and    $item           &lt;info:fedora/fedora-system:def/view#disseminates&gt; $recordDiss\n"
                        + "and    $recordDiss     &lt;info:fedora/fedora-system:def/view#disseminationType&gt; &lt;info:fedora/*/oai_dc&gt;\n"
                        + "and    $recordDiss     &lt;info:fedora/fedora-system:def/view#lastModifiedDate&gt; $date\n"
                        + "and    $date           &lt;http://mulgara.org/mulgara#after&gt; '1969-12-31T23:59:59.999Z'^^&lt;http://www.w3.org/2001/XMLSchema#dateTime&gt; in &lt;#xsd&gt;\n"
                        + "and    $date           &lt;http://mulgare.org/mulgara#before&gt; '2001-09-09T01:46:40.001Z'^^&lt;http://www.w3.org/2001/XMLSchema#dateTime&gt; in &lt;#xsd&gt;\n"
                        + "and    $item           &lt;info:fedora/fedora-system:def/view#disseminates&gt; $aboutDiss\n"
                        + "and    $aboutDiss      &lt;info:fedora/fedora-system:def/view#disseminationType&gt; &lt;info:fedora/*/about_oai_dc&gt;\n"
                        + "order  by $itemID asc";

        assertEquals(q, query);
    }

    public void testParseItemSetSpecPath() {
        Properties props = new Properties();
        props.put(FedoraOAIDriver.PROP_ITEMID, "urn:baz");
        String ssp = "$item &lt;urn:foo&gt; $set $set &lt;urn:bar&gt; $setSpec";
        props.put(FedoraOAIDriver.PROP_ITEM_SETSPEC_PATH, ssp);
        ITQLQueryFactory iqf = new ITQLQueryFactory();
        QueryFactory qf = (QueryFactory) iqf;
        qf.init(null, null, props);

        assertEquals("$item &lt;urn:foo&gt; $set and $set &lt;urn:bar&gt; $setSpec", iqf
                .parseItemSetSpecPath(ssp));

        String[] badSetPaths =
                {"foo", "$item &lt;predicate:foo&gt; $set",
                        "$foo &lt;predicate:bar&gt; $setSpec",
                        "$item $predicate $setSpec",
                        "$item &lt;urn:foo&gt; $set and $set &lt;urn:bar&gt; $setSpec",
                        "$item &lt;urn:foo&gt; $set $set &lt;urn:bar&gt;",
                        "$item urn:baz $setSpec"};

        for (int i = 0; i &lt; badSetPaths.length; i++) {
            try {
                iqf.parseItemSetSpecPath(badSetPaths[i]);
                fail("Should have failed with a RepositoryException");
            } catch (RepositoryException e) {
                assertTrue(e.getMessage()
                        .indexOf("Required property, itemSetSpecPath, ") != -1);
            }
        }

    }
}
</pre></body></html>