package org.exist.xquery;

import org.exist.test.ExistXmldbEmbeddedServer;
import org.junit.ClassRule;
import org.junit.Test;
import static org.junit.Assert.*;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.XQueryService;

import static org.junit.Assert.assertEquals;

public class UnknownAtomicTypeTest {
    @ClassRule
    public static final ExistXmldbEmbeddedServer existEmbeddedServer = new ExistXmldbEmbeddedServer(true, true, true);

    @Test
    public void letVariable() throws XMLDBException {
        final String query = "let $x as y := 0 return $x";
        final String error = "err:XPST0051 Unknown simple type y [at line 1, column 11]";
        assertCompilationError(query, error);
    }

    @Test
    public void functionReturnType() throws XMLDBException {
        final String query = "function () as y { () }";
        final String error = "err:XPST0051 Unknown simple type y [at line 1, column 16]";
        assertCompilationError(query, error);
    }

    @Test
    public void functionParameterType() throws XMLDBException {
        final String query = "function ($x as y) { $x }";
        final String error = "err:XPST0051 Unknown simple type y [at line 1, column 17]";
        assertCompilationError(query, error);
    }

    @Test
    public void instanceOf() throws XMLDBException {
        final String query = "1 instance of y";
        final String error = "err:XPST0051 Unknown simple type y [at line 1, column 15]";
        assertCompilationError(query, error);
    }

    @Test
    public void treatAs() throws XMLDBException {
        final String query = "1 treat as y";
        final String error = "err:XPST0051 Unknown simple type y [at line 1, column 12]";
        assertCompilationError(query, error);
    }

    @Test
    public void castAs() throws XMLDBException {
        final String query = "1 cast as y";
        final String error = "err:XPST0051 Unknown simple type y [at line 1, column 11]";
        assertCompilationError(query, error);
    }

    @Test
    public void castableAs() throws XMLDBException {
        final String query = "1 castable as y";
        final String error = "err:XPST0051 Unknown simple type y [at line 1, column 15]";
        assertCompilationError(query, error);
    }

    private void assertCompilationError(final String query, final String error) throws XMLDBException {
        final XQueryService service = (XQueryService)existEmbeddedServer.getRoot().getService("XQueryService", "1.0");

        try {
            service.compile(query);
            fail("no XMLDBException was thrown during compilation.");
        } catch (XMLDBException ex) {
            assertEquals( error, ex.getMessage() );
        }
    }
}

@@ -1,8 +1,897 @@
+/*%0A * eXist-db Open Source Native XML Database%0A * Copyright (C) 2001 The eXist-db Authors%0A *%0A * info@exist-db.org%0A * http://www.exist-db.org%0A *%0A * This library is free software; you can redistribute it and/or%0A * modify it under the terms of the GNU Lesser General Public%0A * License as published by the Free Software Foundation; either%0A * version 2.1 of the License, or (at your option) any later version.%0A *%0A * This library is distributed in the hope that it will be useful,%0A * but WITHOUT ANY WARRANTY; without even the implied warranty of%0A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU%0A * Lesser General Public License for more details.%0A *%0A * You should have received a copy of the GNU Lesser General Public%0A * License along with this library; if not, write to the Free Software%0A * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA%0A */%0A
 package 
@@ -1012,42 +1012,8 @@
 st;%0A
-import static org.junit.Assert.*;%0A
 impo
@@ -1130,22 +1130,218 @@
 ert.
-assertEquals;%0A
+*;%0A%0A/**%0A * Ensure tests for unknown atomic types are reported with location information%0A * https://github.com/eXist-db/exist/issues/3518%0A *%0A * @author %3Ca href=%22mailto:juri@existsolutions.com%22%3EJuri Leino%3C/a%3E%0A */
 %0Apub

add license, docblock, nicer imports