@SuppressWarnings( "deprecation" )
@FixFor( "MODE-1680" )
@Test
public void testOrderByWithAliases() throws Exception {

    // fill the repository with test data
    Node src = session.getRootNode().addNode("src", "nt:folder");
    // add node f1 with child jcr:content
    try {
        Node f1 = src.addNode("f1", "nt:file");
        f1.addMixin("mix:simpleVersionable");
        Node content1 = f1.addNode("jcr:content", "nt:resource");
        content1.setProperty("jcr:data", session.getValueFactory().createBinary("Node f1".getBytes()));
        // save and slip a bit to have difference in time of node creation.
        session.save();
        Thread.sleep(100);
        // add node f2 with child jcr:content
        Node f2 = src.addNode("f2", "nt:file");
        f2.addMixin("mix:simpleVersionable");
        Node content2 = f2.addNode("jcr:content", "nt:resource");
        content2.setProperty("jcr:data", session.getValueFactory().createBinary("Node f2".getBytes()));
        session.save();
        // print = true;
        printMessage("-------------------- MyQueryTest---------------------");
        String descOrder = "SELECT [nt:file].[jcr:created] FROM [nt:file] INNER JOIN [nt:base] AS content ON ISCHILDNODE(content,[nt:file]) WHERE ([nt:file].[jcr:mixinTypes] = 'mix:simpleVersionable' AND NAME([nt:file]) LIKE 'f%') ORDER BY content.[jcr:lastModified] DESC";
        String ascOrder = "SELECT [nt:file].[jcr:created] FROM [nt:file] INNER JOIN [nt:base] AS content ON ISCHILDNODE(content,[nt:file]) WHERE ([nt:file].[jcr:mixinTypes] = 'mix:simpleVersionable' AND NAME([nt:file]) LIKE 'f%') ORDER BY content.[jcr:lastModified] ASC";
        QueryManager queryManager = session.getWorkspace().getQueryManager();
        Query query = queryManager.createQuery(descOrder, Query.JCR_SQL2);
        QueryResult result = query.execute();
        // checking first query
        RowIterator it = result.getRows();
        assertEquals(2, it.getSize());
        Node n1 = it.nextRow().getNode();
        Node n2 = it.nextRow().getNode();
        assertEquals("f2", n1.getName());
        assertEquals("f1", n2.getName());
        // the same request with other order
        query = queryManager.createQuery(ascOrder, Query.JCR_SQL2);
        result = query.execute();
        // checking second query
        it = result.getRows();
        assertEquals(2, it.getSize());
        n1 = it.nextRow().getNode();
        n2 = it.nextRow().getNode();
        assertEquals("f1", n1.getName());
        assertEquals("f2", n2.getName());
        // Try the XPath query ...
        String descOrderX = "/jcr:root//element(*,nt:file)[(@jcr:mixinTypes = 'mix:simpleVersionable')] order by jcr:content/@jcr:lastModified descending";
        String ascOrderX = "/jcr:root//element(*,nt:file)[(@jcr:mixinTypes = 'mix:simpleVersionable')] order by jcr:content/@jcr:lastModified ascending";
        query = queryManager.createQuery(descOrderX, Query.XPATH);
        result = query.execute();
        // checking first query
        it = result.getRows();
        assertEquals(2, it.getSize());
        n1 = it.nextRow().getNode();
        n2 = it.nextRow().getNode();
        assertEquals("f2", n1.getName());
        assertEquals("f1", n2.getName());
        // the same request with other order
        query = queryManager.createQuery(ascOrderX, Query.XPATH);
        result = query.execute();
        // checking second query
        it = result.getRows();
        assertEquals(2, it.getSize());
        n1 = it.nextRow().getNode();
        n2 = it.nextRow().getNode();
        assertEquals("f1", n1.getName());
        assertEquals("f2", n2.getName());
    } finally {
        src.remove();
        session.save();
    }
}