<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.core;

import java.io.File;

import javax.jcr.Credentials;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyType;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;

import junit.framework.TestCase;

import org.apache.commons.io.FileUtils;
import org.apache.jackrabbit.core.RepositoryCopier;
import org.apache.jackrabbit.core.RepositoryImpl;
import org.apache.jackrabbit.core.config.RepositoryConfig;

public class RepositoryCopierTest extends TestCase {

    private static final Credentials CREDENTIALS =
        new SimpleCredentials("admin", "admin".toCharArray());

    private final File BASE = new File("target", "RepositoryCopierTest");

    private final File SOURCE = new File(BASE, "source");

    private final File TARGET = new File(BASE, "target");

    protected void setUp() {
        BASE.mkdirs();
    }

    protected void tearDown() throws Exception {
        FileUtils.deleteDirectory(BASE);
    }

    public void testRepositoryCopy() throws Exception {
        createSourceRepository();
        new RepositoryCopier(SOURCE, TARGET).copy();
        verifyTargetRepository();
    }

    private void createSourceRepository() throws Exception {
        RepositoryImpl repository = RepositoryImpl.create(
                RepositoryConfig.install(SOURCE));
        try {
            Session session = repository.login(CREDENTIALS);
            try {
                Node test = session.getRootNode().addNode("test");
                test.setProperty("foo", "bar");
                session.save();
            } finally {
                session.logout();
            }
        } finally {
            repository.shutdown();
        }
    }

    private void verifyTargetRepository() throws Exception {
        RepositoryImpl repository = RepositoryImpl.create(
                RepositoryConfig.create(TARGET));
        try {
            Session session = repository.login(CREDENTIALS);
            try {
                assertTrue(session.nodeExists("/test"));
                assertTrue(session.propertyExists("/test/foo"));

                Property foo = session.getProperty("/test/foo");
                assertEquals(PropertyType.STRING, foo.getType());
                assertEquals("bar", foo.getString());
            } finally {
                session.logout();
            }<commit_msg>JCR-442: Implement a backup tool<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.core;

import java.io.File;
import java.math.BigDecimal;
import java.util.Calendar;

import javax.jcr.Credentials;
import javax.jcr.NamespaceRegistry;
import javax.jcr.Node;
import javax.jcr.PropertyType;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.Value;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.NodeTypeManager;
import javax.jcr.nodetype.NodeTypeTemplate;

import junit.framework.TestCase;

import org.apache.commons.io.FileUtils;
import org.apache.jackrabbit.core.config.RepositoryConfig;

public class RepositoryCopierTest extends TestCase {

    private static final Credentials CREDENTIALS =
        new SimpleCredentials("admin", "admin".toCharArray());

    private final File BASE = new File("target", "RepositoryCopierTest");

    private final File SOURCE = new File(BASE, "source");

    private final File TARGET = new File(BASE, "target");

    private final Calendar DATE = Calendar.getInstance();

    private String identifier;

    protected void setUp() {
        BASE.mkdirs();
    }

    protected void tearDown() throws Exception {
        FileUtils.deleteDirectory(BASE);
    }

    public void testRepositoryCopy() throws Exception {
        createSourceRepository();
        new RepositoryCopier(SOURCE, TARGET).copy();
        verifyTargetRepository();
    }

    private void createSourceRepository() throws Exception {
        RepositoryImpl repository = RepositoryImpl.create(
                RepositoryConfig.install(SOURCE));
        try {
            Session session = repository.login(CREDENTIALS);
            try {
                NamespaceRegistry registry =
                    session.getWorkspace().getNamespaceRegistry();
                registry.registerNamespace("test", "http://www.example.org/");

                NodeTypeManager manager =
                    session.getWorkspace().getNodeTypeManager();
                NodeTypeTemplate template = manager.createNodeTypeTemplate();
                template.setName("test:unstructured");
                template.setDeclaredSuperTypeNames(
                        new String[] { "nt:unstructured" });
                manager.registerNodeType(template, false);

                Node root = session.getRootNode();

                Node referenceable =
                    root.addNode("referenceable", "test:unstructured");
                referenceable.addMixin(NodeType.MIX_REFERENCEABLE);
                session.save();
                identifier = referenceable.getIdentifier();

                Node properties = root.addNode("properties", "test:unstructured");
                properties.setProperty("boolean", true);
                properties.setProperty("date", DATE);
                properties.setProperty("decimal", new BigDecimal(123));
                properties.setProperty("double", Math.PI);
                properties.setProperty("long", 9876543210L);
                properties.setProperty("reference", referenceable);
                properties.setProperty("string", "test");
                properties.setProperty("multiple", "a,b,c".split(","));
                session.save();
            } finally {
                session.logout();
            }
        } finally {
            repository.shutdown();
        }
    }

    private void verifyTargetRepository() throws Exception {
        RepositoryImpl repository = RepositoryImpl.create(
                RepositoryConfig.create(TARGET));
        try {
            Session session = repository.login(CREDENTIALS);
            try {
                assertEquals(
                        "http://www.example.org/",
                        session.getNamespaceURI("test"));

                NodeTypeManager manager =
                    session.getWorkspace().getNodeTypeManager();
                assertTrue(manager.hasNodeType("test:unstructured"));
                NodeType type = manager.getNodeType("test:unstructured");
                assertFalse(type.isMixin());
                assertTrue(type.isNodeType("nt:unstructured"));

                assertTrue(session.nodeExists("/properties"));
                Node properties = session.getNode("/properties");
                assertEquals(
                        PropertyType.BOOLEAN,
                        properties.getProperty("boolean").getType());
                assertEquals(
                        true, properties.getProperty("boolean").getBoolean());
                assertEquals(
                        PropertyType.DATE,
                        properties.getProperty("date").getType());
                assertEquals(
                        DATE.getTimeInMillis(),
                        properties.getProperty("date").getDate().getTimeInMillis());
                assertEquals(
                        PropertyType.DECIMAL,
                        properties.getProperty("decimal").getType());
                assertEquals(
                        new BigDecimal(123),
                        properties.getProperty("decimal").getDecimal());
                assertEquals(
                        PropertyType.DOUBLE,
                        properties.getProperty("double").getType());
                assertEquals(
                        Math.PI, properties.getProperty("double").getDouble());
                assertEquals(
                        PropertyType.LONG,
                        properties.getProperty("long").getType());
                assertEquals(
                        9876543210L, properties.getProperty("long").getLong());
                assertEquals(
                        PropertyType.REFERENCE,
                        properties.getProperty("reference").getType());
                assertEquals(
                        identifier,
                        properties.getProperty("reference").getString());
                assertEquals(
                        "/referenceable",
                        properties.getProperty("reference").getNode().getPath());
                assertEquals(
                        PropertyType.STRING,
                        properties.getProperty("string").getType());
                assertEquals(
                        "test", properties.getProperty("string").getString());
                assertEquals(
                        PropertyType.STRING,
                        properties.getProperty("multiple").getType());
                Value[] values = properties.getProperty("multiple").getValues();
                assertEquals(3, values.length);
                assertEquals("a", values[0].getString());
                assertEquals("b", values[1].getString());
                assertEquals("c", values[2].getString());
            } finally {
                session.logout();
            }
{'fraction_non_alphanumeric': 0.09717607973421927, 'fraction_numerical': 0.003783684016242156, 'mean_word_length': 2.120357040023035, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, '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': '3024332', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2380, 'n_words': 616}