<commit_before>/*
 * Kuali Coeus, a comprehensive research administration system for higher education.
 * 
 * Copyright 2005-2015 Kuali, Inc.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program 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 Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kuali.kra.test;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.*;
import org.kuali.rice.core.api.config.property.Config;
import org.kuali.rice.core.api.util.ClassLoaderUtils;
import org.kuali.rice.core.impl.config.property.JAXBConfigImpl;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Unit Tests for validating an OJB repository XML file. The objective is to validate without initializing OJB. If OJB starts up and
 * the repository.xml file is bad, then it will fast-fail. This is an undesirable effect. What is needed is to know that OJB will
 * fail beforehand.
 * 
 * NOTE: This class was originally written to handle only repository.xml. Starting with Release 2.0, the modules have their own
 * repository file(s). This class needs significant modification before it can be considered complete
 * 
 */
public class OjbRepositoryMappingTest {
    
    private static final Log LOG = LogFactory.getLog(OjbRepositoryMappingTest.class);
    
    private static final String INTERNAL_TEST_CONFIG_FILE_PATH = "classpath:META-INF/kc-test-config.xml";
    
    // For XML parsing and validation
    private static final String CLASS_DESCRIPTOR_NAME = "class-descriptor";
    private static final String FIELD_DESCRIPTOR_NAME = "field-descriptor";
    private static final String CLASS_ATTRIBUTE_NAME = "class";
    private static final String TABLE_ATTRIBUTE_NAME = "table";
    private static final String DEFAULT_ATTRIBUTE_NAME = "name";
    private static final String COLUMN_ATTRIBUTE_NAME = "column";
    private static final String COLLECTION_DESCRIPTOR_NAME = "collection-descriptor";
    private static final String REFERENCE_DESCRIPTOR_NAME = "reference-descriptor";
    private static final String CLASS_REF_ATTRIBUTE_NAME = "class-ref";
    private static final String EL_CLASS_REF_ATTRIBUTE_NAME = "element-class-ref";
    private static final String FIELD_REF_ATTRIBUTE_NAME = "field-ref";
    private static final String FIELD_ID_REF_ATTRIBUTE_NAME = "field-id-ref";
    private static final String ID_ATTRIBUTE_NAME = "id";
    private static final String FOREIGN_KEY_NAME = "foreignkey";
    private static final String INVERSE_FOREIGN_KEY_NAME = "inverse-foreignkey";
    private static final String COLLECTION_CLASS_NAME = "collection-class";
    private static final String DATASOURCE_URL_NAME = "datasource.url";
    private static final String DATASOURCE_USERNAME_NAME = "datasource.username";
    private static final String DATASOURCE_PASSWORD_NAME = "<PASSWORD>";
    private static final String DATASOURCE_DRIVER_NAME = "datasource.driver.name";
    private static final String EXTENT_CLASS = "extent-class";

    public static final String REPOSITORY_XML = "classpath:repository.xml";
    public static final String REPOSITORY_AWARD_XML = "classpath:org/kuali/kra/award/repository-award.xml";
    public static final String REPOSITORY_BUDGET_XML = "classpath:org/kuali/coeus/common/budget/impl/repository-budget.xml";
    public static final String REPOSITORY_COI_XML = "classpath:org/kuali/kra/coi/repository-coi.xml";
    public static final String REPOSITORY_COMMITTEE_XML = "classpath:org/kuali/kra/committee/repository-committee.xml";
    public static final String REPOSITORY_IACUC_XML = "classpath:org/kuali/kra/iacuc/repository-iacuc.xml";
    public static final String REPOSITORY_INSTITUTIONALPROPOSAL_XML = "classpath:org/kuali/kra/institutionalproposal/repository-institutionalproposal.xml";
    public static final String REPOSITORY_IRB_XML = "classpath:org/kuali/kra/irb/repository-irb.xml";
    public static final String REPOSITORY_NEGOTIATION_XML = "classpath:org/kuali/kra/negotiation/repository-negotiation.xml";
    public static final String REPOSITORY_PERSONMASSCHANGE_XML = "classpath:org/kuali/kra/personmasschange/repository-personmasschange.xml";
    public static final String REPOSITORY_PROPOSALDEVELOPMENT_XML = "classpath:org/kuali/coeus/propdev/impl/repository-proposaldevelopment.xml";
    public static final String REPOSITORY_QUESTIONNAIRE_XML = "classpath:org/kuali/coeus/common/questionnaire/impl/repository-questionnaire.xml";
    public static final String REPOSITORY_SUB_AWARD_XML = "classpath:org/kuali/kra/subaward/repository-subAward.xml";
    public static final String REPOSITORY_TIMEANDMONEY_XML = "classpath:org/kuali/kra/timeandmoney/repository-timeandmoney.xml";
    public static final String REPOSITORY_IACUC_COMMITTEE_XML = "classpath:org/kuali/kra/iacuc/repository-iacucCommittee.xml";
    public static final String REPOSITORY_COMMON_COMMITTEE_XML = "classpath:org/kuali/kra/common/committee/repository-commonCommittee.xml";

    private static Map<String, String> configFileParms;
    
    private String dsUrl;
    private String dsUser;
    private String dsPass;
    private String dsSchema;
    private String dsDriver;
    
    @SuppressWarnings("unchecked")
    @BeforeClass
    public static void loadParms() throws Exception {
        Config config = new JAXBConfigImpl(INTERNAL_TEST_CONFIG_FILE_PATH);
        config.parseConfig();
        configFileParms = new HashMap(config.getProperties());
    }
    
    @AfterClass
    public static void unloadParms() throws Exception {
        if(configFileParms != null) {
            configFileParms.clear();
            configFileParms = null;
        }
    }
    
    @Before
    public void setUp() throws Exception {
        dsUrl = configFileParms.get(DATASOURCE_URL_NAME);
        dsUser = configFileParms.get(DATASOURCE_USERNAME_NAME);
        dsPass =  configFileParms.get(DATASOURCE_PASSWORD_NAME);
        dsDriver = configFileParms.get(DATASOURCE_DRIVER_NAME);
        dsSchema = StringUtils.upperCase(dsUser);
        
        LOG.debug(String.format("dsUrl = %s\n", dsUrl));
        LOG.debug(String.format("dsUser = %s\n", dsUser));
        LOG.debug(String.format("dsDriver = %s\n", dsDriver));
        LOG.debug(String.format("dsSchema = %s\n", dsSchema));
    }

    /**
     * Tear down
     */
    @After
    public void tearDown() {
        dsUrl = null;
        dsUser = null;
        dsPass =  null;
        dsSchema = null;
        dsDriver = null;
    }

    @Test
    public void xmlValidationRepositoryXml() throws Exception {
        validateXml(REPOSITORY_XML);
    }
    @Test
    public void xmlValidationAwardXml() throws Exception {
        validateXml(REPOSITORY_AWARD_XML);
    }
    @Test<commit_msg>RESOPS-24:OJB mapping test configurable by system prop In order to support configuring Jenkins CI test runs through Jenkins itself we need to be able to configure the OJB mapping test through system props<commit_after>/*
 * Kuali Coeus, a comprehensive research administration system for higher education.
 * 
 * Copyright 2005-2015 Kuali, Inc.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program 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 Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kuali.kra.test;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.*;
import org.kuali.rice.core.api.config.property.Config;
import org.kuali.rice.core.api.util.ClassLoaderUtils;
import org.kuali.rice.core.impl.config.property.JAXBConfigImpl;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Unit Tests for validating an OJB repository XML file. The objective is to validate without initializing OJB. If OJB starts up and
 * the repository.xml file is bad, then it will fast-fail. This is an undesirable effect. What is needed is to know that OJB will
 * fail beforehand.
 * 
 * NOTE: This class was originally written to handle only repository.xml. Starting with Release 2.0, the modules have their own
 * repository file(s). This class needs significant modification before it can be considered complete
 * 
 */
public class OjbRepositoryMappingTest {
    
    private static final Log LOG = LogFactory.getLog(OjbRepositoryMappingTest.class);
    
    private static final String INTERNAL_TEST_CONFIG_FILE_PATH = "classpath:META-INF/kc-test-config.xml";
    
    // For XML parsing and validation
    private static final String CLASS_DESCRIPTOR_NAME = "class-descriptor";
    private static final String FIELD_DESCRIPTOR_NAME = "field-descriptor";
    private static final String CLASS_ATTRIBUTE_NAME = "class";
    private static final String TABLE_ATTRIBUTE_NAME = "table";
    private static final String DEFAULT_ATTRIBUTE_NAME = "name";
    private static final String COLUMN_ATTRIBUTE_NAME = "column";
    private static final String COLLECTION_DESCRIPTOR_NAME = "collection-descriptor";
    private static final String REFERENCE_DESCRIPTOR_NAME = "reference-descriptor";
    private static final String CLASS_REF_ATTRIBUTE_NAME = "class-ref";
    private static final String EL_CLASS_REF_ATTRIBUTE_NAME = "element-class-ref";
    private static final String FIELD_REF_ATTRIBUTE_NAME = "field-ref";
    private static final String FIELD_ID_REF_ATTRIBUTE_NAME = "field-id-ref";
    private static final String ID_ATTRIBUTE_NAME = "id";
    private static final String FOREIGN_KEY_NAME = "foreignkey";
    private static final String INVERSE_FOREIGN_KEY_NAME = "inverse-foreignkey";
    private static final String COLLECTION_CLASS_NAME = "collection-class";
    private static final String DATASOURCE_URL_NAME = "datasource.url";
    private static final String DATASOURCE_USERNAME_NAME = "datasource.username";
    private static final String DATASOURCE_PASSWORD_NAME = "<PASSWORD>";
    private static final String DATASOURCE_DRIVER_NAME = "datasource.driver.name";
    private static final String EXTENT_CLASS = "extent-class";

    public static final String REPOSITORY_XML = "classpath:repository.xml";
    public static final String REPOSITORY_AWARD_XML = "classpath:org/kuali/kra/award/repository-award.xml";
    public static final String REPOSITORY_BUDGET_XML = "classpath:org/kuali/coeus/common/budget/impl/repository-budget.xml";
    public static final String REPOSITORY_COI_XML = "classpath:org/kuali/kra/coi/repository-coi.xml";
    public static final String REPOSITORY_COMMITTEE_XML = "classpath:org/kuali/kra/committee/repository-committee.xml";
    public static final String REPOSITORY_IACUC_XML = "classpath:org/kuali/kra/iacuc/repository-iacuc.xml";
    public static final String REPOSITORY_INSTITUTIONALPROPOSAL_XML = "classpath:org/kuali/kra/institutionalproposal/repository-institutionalproposal.xml";
    public static final String REPOSITORY_IRB_XML = "classpath:org/kuali/kra/irb/repository-irb.xml";
    public static final String REPOSITORY_NEGOTIATION_XML = "classpath:org/kuali/kra/negotiation/repository-negotiation.xml";
    public static final String REPOSITORY_PERSONMASSCHANGE_XML = "classpath:org/kuali/kra/personmasschange/repository-personmasschange.xml";
    public static final String REPOSITORY_PROPOSALDEVELOPMENT_XML = "classpath:org/kuali/coeus/propdev/impl/repository-proposaldevelopment.xml";
    public static final String REPOSITORY_QUESTIONNAIRE_XML = "classpath:org/kuali/coeus/common/questionnaire/impl/repository-questionnaire.xml";
    public static final String REPOSITORY_SUB_AWARD_XML = "classpath:org/kuali/kra/subaward/repository-subAward.xml";
    public static final String REPOSITORY_TIMEANDMONEY_XML = "classpath:org/kuali/kra/timeandmoney/repository-timeandmoney.xml";
    public static final String REPOSITORY_IACUC_COMMITTEE_XML = "classpath:org/kuali/kra/iacuc/repository-iacucCommittee.xml";
    public static final String REPOSITORY_COMMON_COMMITTEE_XML = "classpath:org/kuali/kra/common/committee/repository-commonCommittee.xml";

    private static Properties configFileParms;
    
    private String dsUrl;
    private String dsUser;
    private String dsPass;
    private String dsSchema;
    private String dsDriver;
    
    @SuppressWarnings("unchecked")
    @BeforeClass
    public static void loadParms() throws Exception {
        Config config = new JAXBConfigImpl(INTERNAL_TEST_CONFIG_FILE_PATH);
        config.parseConfig();
        configFileParms = new Properties();
        configFileParms.putAll(config.getProperties());
        configFileParms.putAll(System.getProperties());
    }
    
    @AfterClass
    public static void unloadParms() throws Exception {
        if(configFileParms != null) {
            configFileParms.clear();
            configFileParms = null;
        }
    }
    
    @Before
    public void setUp() throws Exception {
        dsUrl = (String) configFileParms.get(DATASOURCE_URL_NAME);
        dsUser = (String) configFileParms.get(DATASOURCE_USERNAME_NAME);
        dsPass =  (String) configFileParms.get(DATASOURCE_PASSWORD_NAME);
        dsDriver = (String) configFileParms.get(DATASOURCE_DRIVER_NAME);
        dsSchema = StringUtils.upperCase(dsUser);
        
        LOG.debug(String.format("dsUrl = %s\n", dsUrl));
        LOG.debug(String.format("dsUser = %s\n", dsUser));
        LOG.debug(String.format("dsDriver = %s\n", dsDriver));
        LOG.debug(String.format("dsSchema = %s\n", dsSchema));
    }

    /**
     * Tear down
     */
    @After
    public void tearDown() {
        dsUrl = null;
        dsUser = null;
        dsPass =  null;
        dsSchema = null;
        dsDriver = null;
    }

    @Test
    public void xmlValidationRepositoryXml() throws Exception {
        validateXml(REPOSITORY_XML);
    }
    @Test
    public void xmlValidationAwardXml() throws Exception {
        validateXml(REPOSITORY_AWARD_XML);
    }
    @Test
{'fraction_non_alphanumeric': 0.08358484755158609, 'fraction_numerical': 0.0016014782876501387, 'mean_word_length': 4.222257960759086, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 58}, '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': '1464991', 'n_tokens_mistral': 4916, 'n_tokens_neox': 4796, 'n_words': 1240}