<commit_before>/*
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.setup;

import org.opencms.configuration.CmsConfigurationManager;
import org.opencms.configuration.CmsWorkplaceConfiguration;
import org.opencms.configuration.I_CmsXmlConfiguration;
import org.opencms.setup.xml.CmsSetupXmlHelper;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.util.CmsFileUtil;

import java.io.File;

import org.dom4j.Document;

/** 
 * Tests the setup xml helper class.<p>
 * 
 * @since 6.1.8
 */
public class TestCmsSetupXmlHelper extends OpenCmsTestCase {

    /**
     * Default JUnit constructor.<p>
     * 
     * @param arg0 JUnit parameters
     */
    public TestCmsSetupXmlHelper(String arg0) {

        super(arg0);
    }

    /**
     * Tests reading xml file.<p>
     * 
     * @throws Exception if something goes wrong
     */
    public void testXmlModification() throws Exception {

        String base = getTestDataPath(File.separator + "WEB-INF" + File.separator + "base");
        CmsSetupXmlHelper xmlHelper = new CmsSetupXmlHelper(base);

        // create test file
        String inputFile = "test.xml";
        CmsFileUtil.copy(base + CmsWorkplaceConfiguration.DEFAULT_XML_FILE_NAME, base + inputFile);

        // open test file
        System.out.println("Modifying xml from " + base + inputFile);
        Document ori = xmlHelper.getDocument(inputFile);

        String baseXp = "/" + CmsConfigurationManager.N_ROOT + "/" + CmsWorkplaceConfiguration.N_WORKPLACE + "/";
        // simple test
        String xPath = baseXp + CmsWorkplaceConfiguration.N_AUTOLOCK;
        String value = xmlHelper.getValue(inputFile, xPath);
        assertEquals("true", value);
        String expected = "false";
        xmlHelper.setValue(inputFile, xPath, expected);
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals(expected, value);

        // advanced test
        xPath = baseXp
            + CmsWorkplaceConfiguration.N_LOCALIZEDFOLDERS
            + "/"
            + I_CmsXmlConfiguration.N_RESOURCE
            + "[2]/@"
            + I_CmsXmlConfiguration.A_URI;
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals("/system/login/", value);
        expected = "/";
        xmlHelper.setValue(inputFile, xPath, expected);
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals(expected, value);

        // test adding a new node
        xPath = baseXp + "test1/test2/test3";
        expected = "test4";
        assertEquals(1, xmlHelper.setValue(inputFile, xPath, expected));
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals(expected, value);

        // test adding a new attribute
        xPath = baseXp
            + CmsWorkplaceConfiguration.N_LOCALIZEDFOLDERS
            + "/"
            + I_CmsXmlConfiguration.N_RESOURCE
            + "[2]/@test-attr";
        expected = "test-value";
        assertEquals(1, xmlHelper.setValue(inputFile, xPath, expected));
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals(expected, value);

        // test adding a new node in a list
        xPath = baseXp + "test1/test2[2]/test5";
        expected = "test6";
        assertEquals(1, xmlHelper.setValue(inputFile, xPath, expected));
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals(expected, value);

        // write modified file
        xmlHelper.write(inputFile);

        // restoring file
        xPath = baseXp
            + CmsWorkplaceConfiguration.N_LOCALIZEDFOLDERS
            + "/"
            + I_CmsXmlConfiguration.N_RESOURCE
            + "[2]/@"
            + I_CmsXmlConfiguration.A_URI;
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals("/", value);
        expected = "/system/login/";
        xmlHelper.setValue(inputFile, xPath, expected);
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals(expected, value);

        xPath = baseXp + CmsWorkplaceConfiguration.N_AUTOLOCK;
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals("false", value);
        expected = "true";
        xmlHelper.setValue(inputFile, xPath, expected);
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals(expected, value);

        // test removing a node
        xPath = baseXp + "test1";
        assertEquals(1, xmlHelper.setValue(inputFile, xPath, null));
        assertNull(xmlHelper.getValue(inputFile, xPath));

        // test removing an attribute
        xPath = baseXp
            + CmsWorkplaceConfiguration.N_LOCALIZEDFOLDERS
            + "/"
            + I_CmsXmlConfiguration.N_RESOURCE
            + "[2]/@test-attr";
        assertEquals(1, xmlHelper.setValue(inputFile, xPath, null));
        assertNull(xmlHelper.getValue(inputFile, xPath));
        assertNotNull(xmlHelper.getValue(inputFile, xPath.substring(0, xPath.lastIndexOf('/')) + "/@uri"));

        // test removing non existent node
        xPath = baseXp + "test1";
        assertEquals(0, xmlHelper.setValue(inputFile, xPath, null));

        // test removing non existent attribute
        xPath = baseXp
            + CmsWorkplaceConfiguration.N_LOCALIZEDFOLDERS
            + "/"
            + I_CmsXmlConfiguration.N_RESOURCE<commit_msg>Improved test case to produce valid XML structure.<commit_after>/*
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.setup;

import org.opencms.configuration.CmsConfigurationManager;
import org.opencms.configuration.CmsWorkplaceConfiguration;
import org.opencms.configuration.I_CmsXmlConfiguration;
import org.opencms.setup.xml.CmsSetupXmlHelper;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.util.CmsFileUtil;

import java.io.File;

import org.dom4j.Document;

/** 
 * Tests the setup xml helper class.<p>
 * 
 * @since 6.1.8
 */
public class TestCmsSetupXmlHelper extends OpenCmsTestCase {

    /**
     * Default JUnit constructor.<p>
     * 
     * @param arg0 JUnit parameters
     */
    public TestCmsSetupXmlHelper(String arg0) {

        super(arg0);
    }

    /**
     * Tests reading xml file.<p>
     * 
     * @throws Exception if something goes wrong
     */
    public void testXmlModification() throws Exception {

        String base = getTestDataPath(File.separator + "WEB-INF" + File.separator + "base");
        CmsSetupXmlHelper xmlHelper = new CmsSetupXmlHelper(base);

        // create test file
        String inputFile = "test.xml";
        CmsFileUtil.copy(base + CmsWorkplaceConfiguration.DEFAULT_XML_FILE_NAME, base + inputFile);

        // open test file
        System.out.println("Modifying xml from " + base + inputFile);
        Document ori = xmlHelper.getDocument(inputFile);

        String baseXp = "/" + CmsConfigurationManager.N_ROOT + "/" + CmsWorkplaceConfiguration.N_WORKPLACE + "/";
        // simple test
        String xPath = baseXp + CmsWorkplaceConfiguration.N_AUTOLOCK;
        String value = xmlHelper.getValue(inputFile, xPath);
        assertEquals("true", value);
        String expected = "false";
        xmlHelper.setValue(inputFile, xPath, expected);
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals(expected, value);

        // advanced test
        xPath = baseXp
            + CmsWorkplaceConfiguration.N_LOCALIZEDFOLDERS
            + "/"
            + I_CmsXmlConfiguration.N_RESOURCE
            + "[2]/@"
            + I_CmsXmlConfiguration.A_URI;
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals("/system/login/", value);
        expected = "/";
        xmlHelper.setValue(inputFile, xPath, expected);
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals(expected, value);

        // test adding a new node
        xPath = baseXp + CmsWorkplaceConfiguration.N_GALLERY_DEFAULT_SCOPE;
        expected = "root";
        assertEquals(1, xmlHelper.setValue(inputFile, xPath, expected));
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals(expected, value);

        //        // test adding a new attribute
        xPath = baseXp
            + CmsWorkplaceConfiguration.N_LOCALIZEDFOLDERS
            + "/"
            + I_CmsXmlConfiguration.N_RESOURCE
            + "[3]/@"
            + I_CmsXmlConfiguration.A_URI;
        expected = "/test-value/";
        assertEquals(1, xmlHelper.setValue(inputFile, xPath, expected));
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals(expected, value);

        // write modified file
        xmlHelper.write(inputFile);

        // restoring file
        xPath = baseXp
            + CmsWorkplaceConfiguration.N_LOCALIZEDFOLDERS
            + "/"
            + I_CmsXmlConfiguration.N_RESOURCE
            + "[2]/@"
            + I_CmsXmlConfiguration.A_URI;
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals("/", value);
        expected = "/system/login/";
        xmlHelper.setValue(inputFile, xPath, expected);
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals(expected, value);

        xPath = baseXp + CmsWorkplaceConfiguration.N_AUTOLOCK;
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals("false", value);
        expected = "true";
        xmlHelper.setValue(inputFile, xPath, expected);
        value = xmlHelper.getValue(inputFile, xPath);
        assertEquals(expected, value);

        // test removing a node
        xPath = baseXp + CmsWorkplaceConfiguration.N_GALLERY_DEFAULT_SCOPE;
        assertEquals(1, xmlHelper.setValue(inputFile, xPath, null));
        assertNull(xmlHelper.getValue(inputFile, xPath));

        // test removing an attribute
        xPath = baseXp
            + CmsWorkplaceConfiguration.N_LOCALIZEDFOLDERS
            + "/"
            + I_CmsXmlConfiguration.N_RESOURCE
            + "[3]/@"
            + I_CmsXmlConfiguration.A_URI;
        assertEquals(1, xmlHelper.setValue(inputFile, xPath, null));
        assertNull(xmlHelper.getValue(inputFile, xPath));
        xPath = baseXp + CmsWorkplaceConfiguration.N_LOCALIZEDFOLDERS + "/" + I_CmsXmlConfiguration.N_RESOURCE + "[3]";
        assertEquals(1, xmlHelper.setValue(inputFile, xPath, null));

        // test removing non existent node
        xPath = baseXp + "test1";
        assertEquals(0, xmlHelper.setValue(inputFile, xPath, null));

        // test removing non existent attribute
        xPath = baseXp
            + CmsWorkplaceConfiguration.N_LOCALIZEDFOLDERS
            + "/"
            + I_CmsXmlConfiguration.N_RESOURCE
{'fraction_non_alphanumeric': 0.0980546585807671, 'fraction_numerical': 0.006300700952981019, 'mean_word_length': 2.6827146171693736, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 62}, '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': '3625498', 'n_tokens_mistral': 3592, 'n_tokens_neox': 3519, 'n_words': 903}