<commit_before>package org.basex.test.unresolved;

import static org.junit.Assert.*;
import static org.basex.util.Token.*;

import java.io.IOException;

import org.basex.core.BaseXException;
import org.basex.core.Context;
import org.basex.core.Prop;
import org.basex.core.cmd.CreateDB;
import org.basex.core.cmd.DropDB;
import org.basex.core.cmd.Set;
import org.basex.core.cmd.XQuery;
import org.basex.data.Data;
import org.basex.io.IOContent;
import org.basex.query.QueryException;
import org.basex.query.QueryProcessor;
import org.basex.query.func.FNSimple;
import org.basex.query.item.DBNode;
import org.basex.query.item.Nod;
import org.basex.query.iter.Iter;
import org.basex.query.iter.NodIter;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * This class tests namespaces.
 *
 * @author Workgroup DBIS, University of Konstanz 2005-10, ISC License
 * @author <NAME>
 */
public class NamespaceTest {
  /** Database context. */
  private static Context context;

  /** Test documents. */
  private static String[][] docs = {
    { "d1", "<a xmlns:ns0='A'><b><d xmlns:ns1='D'>" +
        "<g xmlns:ns2='G'/></d></b></a>" },
    { "d2", "<n/>" }
  };

  /**
   * Test query.
   * Detects malformed namespace hierarchy.
   */
  @Test
  public final void nsInAtt() {
    query("data(<a a='{namespace-uri-for-prefix('x', <b/>)}' xmlns:x='X'/>/@a)",
        "X");
  }

  /**
   * Test query.
   * Detects malformed namespace hierarchy.
   */
  @Test
  public final void nsInBraces() {
    query("<a xmlns:x='X'>{namespace-uri-for-prefix('x', <b/>)}</a>/text()",
        "X");
  }

  /** 
   * Test query.
   * Detects malformed namespace hierarchy.
   */
  @Test
  public final void newPrefix() {
    query("<a>{ attribute {QName('http://bla', 'att')} {} }</a>",
        "<a xmlns:ns1='http://bla' ns1:att=''/>");
  }

  /** 
   * Test query.
   * Detects malformed namespace hierarchy.
   */
  @Test
  public final void newPrefix2() {
    query("<a xmlns:ns1='ns1'><b xmlns='ns1'>" +
        "  <c>{attribute {QName('ns1', 'att1')} {}," +
        "    attribute {QName('ns2', 'att2')} {}}</c>" +
        "</b></a>",
        "<a xmlns:ns1='ns1'><b xmlns='ns1'>" +
        "  <c xmlns:ns2='ns2' ns1:att1='' ns2:att2=''/>" +
        "</b></a>");
  }

  /** 
   * Test query.
   * Detects malformed namespace hierarchy.
   */
  @Test
  public final void newPrefix3() {
    query("insert node attribute {QName('test', 'att')}{} into doc('d1')//g",
        "doc('d1')//g",
        "<g xmlns:ns2='G' xmlns:ns3='test' ns3:att=''/>");
  }

  /** 
   * Test query.
   * Detects malformed namespace hierarchy.
   */
  @Test
  public final void xuty0004() {
    try {
      new QueryProcessor("declare variable $input-context external;" +
          "let $source as node()* := (" +
          "    <status>on leave</status>," +
          "    <!-- for 6 months -->" +
          "  )," +
          "  $target := $input-context/works[1]/employee[1]" +
          "return insert nodes $source into $target", context).execute();
    } catch(QueryException e) {
      e.printStackTrace();
      assertEquals("XUTY0004", e.code());
    }
    fail("should throw XUTY0004");
  }
  
  /**
   * [LK] 
   * Test query.
   * Tests preserve, no-inherit for copy expression. Related to XQUTS
   * id-insert-expr-081-no-inherit.xq 
   */
  @Test
  public final void copyPreserveNoInherit() {
    query("declare copy-namespaces preserve,no-inherit;" +
    		"declare namespace my = 'ns';" +
    		"let $v :=" +
    		"(copy $c := <my:n><my:a/></my:n>" +
    		"modify insert node <new/> into $c " +
    		"return $c)" +
    		"return namespace-uri-for-prefix('my', $v/new)"
        , "");
  }
  
  /**
   * [LK] 
   * Test query.
   * Tests preserve, no-inherit for copy expression. Related to XQUTS
   * id-insert-expr-081-no-inherit.xq. Tests if no-inherit has a persistent
   * effect. Is it actually supposed to? Check with i.e. saxon.
   * The <new/> tag is inserted into a fragment f using no-inherit and copy.
   * The resulting fragment is inserted into a database. The
   * namespaces in scope with prefix 'ns' are finally checked for the 
   * inserted <new/> tag. If the result is non-empty we may have a problem -
   * being not able popagate the no-inherent flag to our table.   
   */
  @Test
  public final void copyPreserveNoInheritPersistent() {
    query("declare copy-namespaces preserve,no-inherit;" +
        "declare namespace my = 'ns';" +
        "let $v :=" +
        "(copy $c := <my:n><my:a/></my:n>" +
        "modify insert node <new/> into $c " +
        "return $c)" +
        "return insert node $v into doc('d2')/n",
        "namespace-uri-for-prefix('my', doc('d2')//*:new)",
        "");
  }
  
  /**
   * Extension to the JUnit methods to test for deep equality of XML fragments.
   * @param exp expected fragment<commit_msg>some more namespace tests moved to main package<commit_after>package org.basex.test.unresolved;

import static org.junit.Assert.*;
import static org.basex.util.Token.*;

import java.io.IOException;

import org.basex.core.BaseXException;
import org.basex.core.Context;
import org.basex.core.Prop;
import org.basex.core.cmd.CreateDB;
import org.basex.core.cmd.DropDB;
import org.basex.core.cmd.Set;
import org.basex.core.cmd.XQuery;
import org.basex.data.Data;
import org.basex.io.IOContent;
import org.basex.query.QueryException;
import org.basex.query.QueryProcessor;
import org.basex.query.func.FNSimple;
import org.basex.query.item.DBNode;
import org.basex.query.item.Nod;
import org.basex.query.iter.Iter;
import org.basex.query.iter.NodIter;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * This class tests namespaces.
 *
 * @author Workgroup DBIS, University of Konstanz 2005-10, ISC License
 * @author <NAME>
 */
public class NamespaceTest {
  /** Database context. */
  private static Context context;

  /** Test documents. */
  private static String[][] docs = {
    { "d1", "<a xmlns:ns0='A'><b><d xmlns:ns1='D'>" +
        "<g xmlns:ns2='G'/></d></b></a>" },
    { "d2", "<n/>" }
  };

  /**
   * Test query.
   * Detects malformed namespace hierarchy.
   */
  @Test
  public final void nsInAtt() {
    query("data(<a a='{namespace-uri-for-prefix('x', <b/>)}' xmlns:x='X'/>/@a)",
        "X");
  }

  /**
   * Test query.
   * Detects malformed namespace hierarchy.
   */
  @Test
  public final void nsInBraces() {
    query("<a xmlns:x='X'>{namespace-uri-for-prefix('x', <b/>)}</a>/text()",
        "X");
  }

  /** 
   * Test query.
   * Detects malformed namespace hierarchy.
   */
  @Test
  public final void newPrefix() {
    query("<a>{ attribute {QName('http://bla', 'att')} {} }</a>",
        "<a xmlns:ns1='http://bla' ns1:att=''/>");
  }

  /** 
   * Test query.
   * Detects malformed namespace hierarchy.
   */
  @Test
  public final void newPrefix2() {
    query("<a xmlns:ns1='ns1'><b xmlns='ns1'>" +
        "  <c>{attribute {QName('ns1', 'att1')} {}," +
        "    attribute {QName('ns2', 'att2')} {}}</c>" +
        "</b></a>",
        "<a xmlns:ns1='ns1'><b xmlns='ns1'>" +
        "  <c xmlns:ns2='ns2' ns1:att1='' ns2:att2=''/>" +
        "</b></a>");
  }

  /** 
   * Test query.
   * Detects malformed namespace hierarchy.
   */
  @Test
  public final void newPrefix3() {
    query("insert node attribute {QName('test', 'att')}{} into doc('d1')//g",
        "doc('d1')//g",
        "<g xmlns:ns2='G' xmlns:ns3='test' ns3:att=''/>");
  }

  /** 
   * Test query.
   * Detects malformed namespace hierarchy.
   */
  @Test
  public final void xuty0004() {
    try {
      new QueryProcessor("declare variable $input-context external;" +
          "let $source as node()* := (" +
          "    <status>on leave</status>," +
          "    <!-- for 6 months -->" +
          "  )," +
          "  $target := $input-context/works[1]/employee[1]" +
          "return insert nodes $source into $target", context).execute();
    } catch(QueryException e) {
      e.printStackTrace();
      assertEquals("XUTY0004", e.code());
    }
    fail("should throw XUTY0004");
  }
  
  /**
   * [LK] 
   * Test query.
   * Tests preserve, no-inherit for copy expression. Related to XQUTS
   * id-insert-expr-081-no-inherit.xq. Tests if no-inherit has a persistent
   * effect. Is it actually supposed to?
   * The <new/> tag is inserted into a fragment f using no-inherit and copy.
   * The resulting fragment is inserted into a database. The
   * namespaces in scope with prefix 'ns' are finally checked for the 
   * inserted <new/> tag. If the result is non-empty we may have a problem -
   * being not able popagate the no-inherit flag to our table.   
   */
  @Test
  public final void copyPreserveNoInheritPersistent() {
    query("declare copy-namespaces preserve,no-inherit;" +
        "declare namespace my = 'ns';" +
        "let $v :=" +
        "(copy $c := <my:n><my:a/></my:n>" +
        "modify insert node <new/> into $c " +
        "return $c)" +
        "return insert node $v into doc('d2')/n",
        "namespace-uri-for-prefix('my', doc('d2')//*:new)",
        "");
  }
  
  /**
   * Extension to the JUnit methods to test for deep equality of XML fragments.
   * @param exp expected fragment
{'fraction_non_alphanumeric': 0.17685683530678148, 'fraction_numerical': 0.012378902045209902, 'mean_word_length': 2.71937550040032, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 26}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7389343', 'n_tokens_mistral': 3297, 'n_tokens_neox': 3185, 'n_words': 781}