<commit_before>/*
 * Created on 20 juil. 2004
$Id$
 */
package org.exist.xmldb;

import java.util.ArrayList;

import org.exist.xquery.util.URIUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.Service;
import org.xmldb.api.base.XMLDBException;

import static org.junit.Assert.*;

/** A test case for accessing collections remotely
 * @author jmv
 * @author <NAME> <<EMAIL>>
 */
public class RemoteCollectionTest extends RemoteDBTest {

	private final static String XML_CONTENT = "<xml/>";
	private final static String BINARY_CONTENT = "TEXT";

	@Before
	public void setUp() throws ClassNotFoundException, InstantiationException, XMLDBException, IllegalAccessException {
		setUpRemoteDatabase();
	}

    @After
  	public void tearDown() {
        removeCollection();
	}

    @Ignore
    @Test
    public void indexQueryService() {
		// TODO .............
	}

    @Test
	public void getServices() throws XMLDBException {
        Service[] services = getCollection().getServices();
        assertEquals(6, services.length);
        assertEquals(RemoteXPathQueryService.class, services[0].getClass());
        assertEquals(RemoteCollectionManagementService.class, services[1].getClass());
        assertEquals(RemoteUserManagementService.class, services[2].getClass());
        assertEquals(RemoteDatabaseInstanceManager.class, services[3].getClass());
        assertEquals(RemoteIndexQueryService.class, services[4].getClass());
        assertEquals(RemoteXUpdateQueryService.class, services[5].getClass());
	}

    @Test
	public void isRemoteCollection() throws XMLDBException {
        assertTrue(getCollection().isRemoteCollection());
	}

    @Test
	public void getPath() throws XMLDBException {
        assertEquals(XmldbURI.ROOT_COLLECTION + "/" + getTestCollectionName(), URIUtils.urlDecodeUtf8(getCollection().getPath()));
	}

    @Test
	public void createResource() throws XMLDBException {
        Collection collection = getCollection();
        { // XML resource:
            Resource resource = collection.createResource("testresource", "XMLResource");
            assertNotNull(resource);
            assertEquals(collection, resource.getParentCollection());
            resource.setContent("<?xml version='1.0'?><xml/>");
            collection.storeResource(resource);
        }
        { // binary resource:
            Resource resource = collection.createResource("testresource", "BinaryResource");
            assertNotNull(resource);
            assertEquals(collection, resource.getParentCollection());
            resource.setContent("some random binary data here :-)");
            collection.storeResource(resource);
        }
	}

    @Test
	public void getNonExistentResource() throws XMLDBException {
        Collection collection = getCollection();
        Resource resource = collection.getResource("unknown.xml");
        assertNull(resource);
	}

    @Test
	public void listResources() throws XMLDBException {
        ArrayList<String> xmlNames = new ArrayList<>();
        xmlNames.add("xml1");
        xmlNames.add("xml2");
        xmlNames.add("xml3");
        createResources(xmlNames, "XMLResource");

        ArrayList<String> binaryNames = new ArrayList<>();
        binaryNames.add("b1");<commit_msg>Test to prove that #1874 is fixed.<commit_after>/*
 * Created on 20 juil. 2004
$Id$
 */
package org.exist.xmldb;

import org.exist.util.io.FastByteArrayInputStream;
import org.exist.xquery.util.URIUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.Service;
import org.xmldb.api.base.XMLDBException;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;

import static org.junit.Assert.*;

/** A test case for accessing collections remotely
 * @author jmv
 * @author <NAME> <<EMAIL>>
 */
public class RemoteCollectionTest extends RemoteDBTest {

    private final static String XML_CONTENT = "<xml/>";
	private final static String BINARY_CONTENT = "TEXT";

	@Before
	public void setUp() throws ClassNotFoundException, InstantiationException, XMLDBException, IllegalAccessException {
		setUpRemoteDatabase();
	}

    @After
  	public void tearDown() {
        removeCollection();
	}

    @Ignore
    @Test
    public void indexQueryService() {
		// TODO .............
	}

    @Test
	public void getServices() throws XMLDBException {
        Service[] services = getCollection().getServices();
        assertEquals(6, services.length);
        assertEquals(RemoteXPathQueryService.class, services[0].getClass());
        assertEquals(RemoteCollectionManagementService.class, services[1].getClass());
        assertEquals(RemoteUserManagementService.class, services[2].getClass());
        assertEquals(RemoteDatabaseInstanceManager.class, services[3].getClass());
        assertEquals(RemoteIndexQueryService.class, services[4].getClass());
        assertEquals(RemoteXUpdateQueryService.class, services[5].getClass());
	}

    @Test
	public void isRemoteCollection() throws XMLDBException {
        assertTrue(getCollection().isRemoteCollection());
	}

    @Test
	public void getPath() throws XMLDBException {
        assertEquals(XmldbURI.ROOT_COLLECTION + "/" + getTestCollectionName(), URIUtils.urlDecodeUtf8(getCollection().getPath()));
	}

    @Test
	public void createStringResource() throws XMLDBException {
        Collection collection = getCollection();
        { // XML resource:
            Resource resource = collection.createResource("testresource", "XMLResource");
            assertNotNull(resource);
            assertEquals(collection, resource.getParentCollection());
            resource.setContent("<?xml version='1.0'?><xml/>");
            collection.storeResource(resource);
        }
        { // binary resource:
            Resource resource = collection.createResource("testresource", "BinaryResource");
            assertNotNull(resource);
            assertEquals(collection, resource.getParentCollection());
            resource.setContent("some random binary data here :-)");
            collection.storeResource(resource);
        }
	}

    @Test /* issue 1874 */
    public void createXMLFileResource() throws XMLDBException, IOException {
        Collection collection = getCollection();
        final Resource resource = collection.createResource("testresource", "XMLResource");
        assertNotNull(resource);
        assertEquals(collection, resource.getParentCollection());

        final String sometxt = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        final Path path = Paths.get("tmp.xml");
        try {
            final StringBuilder sb = new StringBuilder();
            sb.append("<?xml version='1.0'?><xml>");
            for (int i = 0; i < 5000; i++) {
                sb.append("<element>").append(sometxt).append("</element>");
            }
            sb.append("</xml>");
            Files.copy(new FastByteArrayInputStream(sb.toString().getBytes()), path, StandardCopyOption.REPLACE_EXISTING);
            resource.setContent(path);
            collection.storeResource(resource);

        } finally {
            Files.delete(path);
        }
    }

    @Test
	public void getNonExistentResource() throws XMLDBException {
        Collection collection = getCollection();
        Resource resource = collection.getResource("unknown.xml");
        assertNull(resource);
	}

    @Test
	public void listResources() throws XMLDBException {
        ArrayList<String> xmlNames = new ArrayList<>();
        xmlNames.add("xml1");
        xmlNames.add("xml2");
        xmlNames.add("xml3");
        createResources(xmlNames, "XMLResource");

        ArrayList<String> binaryNames = new ArrayList<>();
        binaryNames.add("b1");
{'fraction_non_alphanumeric': 0.11992665036674817, 'fraction_numerical': 0.006723716381418093, 'mean_word_length': 3.4437805540467137, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 3, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 40}, 'pii_count': 0, 'substrings_counts': 1, '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': '524176', 'n_tokens_mistral': 2155, 'n_tokens_neox': 2104, 'n_words': 454}