simplequerytest.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 528 行 · 第 1/2 页

JAVA
528
字号
        sql = "SELECT * FROM nt:base WHERE jcr:path LIKE '" + testRoot + "/%' AND value LIKE 'foo%bar'";  // matches all nodes        q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);        result = q.execute();        checkResult(result, 4);        sql = "SELECT * FROM nt:base WHERE jcr:path LIKE '" + testRoot + "/%' AND value LIKE 'foo\\\\\\_bar' ESCAPE '\\'";  // matches node1        q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);        result = q.execute();        checkResult(result, 1);        sql = "SELECT * FROM nt:base WHERE jcr:path LIKE '" + testRoot + "/%' AND value LIKE 'foo\\_bar'";  // matches node1        q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);        result = q.execute();        checkResult(result, 1);    }    public void testLikeWithLineTerminator() throws Exception {        Node n = testRootNode.addNode("node1");        n.setProperty("value", new String[]{"foo\nbar"});        testRootNode.save();        String sql = "SELECT * FROM nt:base WHERE jcr:path LIKE '" + testRoot + "/%' AND value LIKE 'foo%bar'";        Query q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);        QueryResult result = q.execute();        checkResult(result, 1);        sql = "SELECT * FROM nt:base WHERE jcr:path LIKE '" + testRoot + "/%' AND value LIKE 'foo_bar'";        q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);        result = q.execute();        checkResult(result, 1);    }    public void testNotEqual() throws Exception {        Node n = testRootNode.addNode("node1");        n.setProperty("value", new String[]{"foo"});        n = testRootNode.addNode("node2");        n.setProperty("value", new String[]{"bar"});        n = testRootNode.addNode("node3");        n.setProperty("value", new String[]{"foobar"});        testRootNode.save();        String jcrql = "SELECT * FROM nt:base WHERE jcr:path LIKE '" + testRoot + "/%' AND value <> 'bar'";        Query q = superuser.getWorkspace().getQueryManager().createQuery(jcrql, Query.SQL);        QueryResult result = q.execute();        checkResult(result, 2);    }    public void testIsNull() throws Exception {        Node foo = testRootNode.addNode("foo");        foo.setProperty("mytext", "the quick brown fox jumps over the lazy dog.");        Node bar = testRootNode.addNode("bar");        bar.setProperty("text", "the quick brown fox jumps over the lazy dog.");        testRootNode.save();        String sql = "SELECT * FROM nt:unstructured WHERE mytext is null and jcr:path LIKE '"                + testRoot + "/%'";        Query q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);        QueryResult result = q.execute();        checkResult(result, 1);        String xpath = "/" + testRoot + "/*[@jcr:primaryType='nt:unstructured' and fn:not(@mytext)]";        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);        result = q.execute();        checkResult(result, 1);        xpath = "/jcr:root" + testRoot + "/*[@jcr:primaryType='nt:unstructured' and fn:not(@mytext)]";        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);        result = q.execute();        checkResult(result, 1);    }    public void testIsNotNull() throws Exception {        Node foo = testRootNode.addNode("foo");        foo.setProperty("mytext", "the quick brown fox jumps over the lazy dog.");        Node bar = testRootNode.addNode("bar");        bar.setProperty("text", "the quick brown fox jumps over the lazy dog.");        // documents which field name is not exactly "mytext" should not match (JCR-1051)        bar.setProperty("mytextwhichstartswithmytext", "the quick brown fox jumps over the lazy dog.");                testRootNode.save();        String sql = "SELECT * FROM nt:unstructured WHERE mytext is not null";        Query q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);        QueryResult result = q.execute();        checkResult(result, 1);        String xpath = "//*[@jcr:primaryType='nt:unstructured' and @mytext]";        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);        result = q.execute();        checkResult(result, 1);    }    public void testNegativeNumber() throws Exception {        Node foo = testRootNode.addNode("foo");        foo.setProperty("number", -10);        Node bar = testRootNode.addNode("bar");        bar.setProperty("number", -20);        testRootNode.save();        String sql = "SELECT * FROM nt:unstructured WHERE number = -10";        Query q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);        QueryResult result = q.execute();        checkResult(result, 1);        String xpath = "//*[@jcr:primaryType='nt:unstructured' and @number = -10]";        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);        result = q.execute();        checkResult(result, 1);        sql = "SELECT * FROM nt:unstructured WHERE number <= -10";        q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);        result = q.execute();        checkResult(result, 2);        xpath = "//*[@jcr:primaryType='nt:unstructured' and @number <= -10]";        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);        result = q.execute();        checkResult(result, 2);    }    public void testQuotes() throws Exception {        Node foo = testRootNode.addNode("foo");        foo.setProperty("foo", "bar'bar");        testRootNode.save();        String sql = "SELECT * FROM nt:unstructured WHERE foo = 'bar''bar'";        Query q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);        QueryResult result = q.execute();        checkResult(result, 1);        String xpath = "//*[@jcr:primaryType='nt:unstructured' and @foo ='bar''bar']";        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);        result = q.execute();        checkResult(result, 1);        xpath = "//*[@jcr:primaryType='nt:unstructured' and jcr:like(@foo,'%ar''ba%')]";        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);        result = q.execute();        checkResult(result, 1);        xpath = "//*[@jcr:primaryType='nt:unstructured' and jcr:like(@foo,\"%ar'ba%\")]";        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);        result = q.execute();        checkResult(result, 1);    }    public void testGeneralComparison() throws Exception {        Node foo = testRootNode.addNode("foo");        foo.setProperty("text", new String[]{"foo", "bar"}); // mvp        Node bar = testRootNode.addNode("bar");        bar.setProperty("text", new String[]{"foo"}); // mvp with one value        Node bla = testRootNode.addNode("bla");        bla.setProperty("text", "foo"); // svp        Node blu = testRootNode.addNode("blu");        blu.setProperty("text", "bar"); // svp        testRootNode.save();        String sql = "SELECT * FROM nt:unstructured WHERE 'foo' IN text " +                "and jcr:path LIKE '" + testRoot + "/%'";        Query q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);        QueryResult result = q.execute();        checkResult(result, new Node[]{foo, bar, bla});        String xpath = "/" + testRoot + "/*[@jcr:primaryType='nt:unstructured' and @text = 'foo']";        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);        result = q.execute();        checkResult(result, new Node[]{foo, bar, bla});        xpath = "/jcr:root" + testRoot + "/*[@jcr:primaryType='nt:unstructured' and @text = 'foo']";        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);        result = q.execute();        checkResult(result, new Node[]{foo, bar, bla});        //----------------------------------------------------------------------        // the text = 'foo' is now also a general comparison        sql = "SELECT * FROM nt:unstructured WHERE text = 'foo' " +                "and jcr:path LIKE '" + testRoot + "/%'";        q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);        result = q.execute();        checkResult(result, new Node[]{foo, bar, bla});        xpath = "/" + testRoot + "/*[@jcr:primaryType='nt:unstructured' and @text eq 'foo']";        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);        result = q.execute();        checkResult(result, new Node[]{bar, bla});        xpath = "/jcr:root" + testRoot + "/*[@jcr:primaryType='nt:unstructured' and @text eq 'foo']";        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);        result = q.execute();        checkResult(result, new Node[]{bar, bla});        //----------------------------------------------------------------------        sql = "SELECT * FROM nt:unstructured WHERE 'bar' NOT IN text " +                "and jcr:path LIKE '" + testRoot + "/%'";        q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);        result = q.execute();        checkResult(result, new Node[]{foo, bar, bla});        xpath = "/" + testRoot + "/*[@jcr:primaryType='nt:unstructured' and @text != 'bar']";        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);        result = q.execute();        checkResult(result, new Node[]{foo, bar, bla});        xpath = "/jcr:root" + testRoot + "/*[@jcr:primaryType='nt:unstructured' and @text != 'bar']";        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);        result = q.execute();        checkResult(result, new Node[]{foo, bar, bla});        //----------------------------------------------------------------------        sql = "SELECT * FROM nt:unstructured WHERE 'foo' NOT IN text " +                "and jcr:path LIKE '" + testRoot + "/%'";        q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);        result = q.execute();        checkResult(result, new Node[]{foo, blu});        xpath = "/" + testRoot + "/*[@jcr:primaryType='nt:unstructured' and @text != 'foo']";        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);        result = q.execute();        checkResult(result, new Node[]{foo, blu});        xpath = "/jcr:root" + testRoot + "/*[@jcr:primaryType='nt:unstructured' and @text != 'foo']";        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);        result = q.execute();        checkResult(result, new Node[]{foo, blu});    }    public void testLogicalExpression() throws Exception {        Node foo = testRootNode.addNode("foo");        foo.setProperty("a", 1);        foo.setProperty("b", 2);        foo.setProperty("c", 3);        Node bar = testRootNode.addNode("bar");        bar.setProperty("a", 0);        bar.setProperty("b", 2);        bar.setProperty("c", 0);        Node bla = testRootNode.addNode("bla");        bla.setProperty("a", 1);        bla.setProperty("b", 0);        bla.setProperty("c", 3);        testRootNode.save();        String sql = "SELECT * FROM nt:unstructured WHERE a=1 and b=2 or c=3";        Query q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);        QueryResult result = q.execute();        checkResult(result, new Node[]{foo, bla});        String xpath = "//*[@a=1 and @b=2 or @c=3] ";        q = superuser.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH);        result = q.execute();        checkResult(result, new Node[]{foo, bla});    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?