<commit_before>/*
 * Copyright 2003-2005 The Apache Software Foundation
 *
 * Licensed 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.commons.cli2.validation;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.commons.cli2.resource.ResourceConstants;
import org.apache.commons.cli2.resource.ResourceHelper;

/**
 * JUnit test case for DateValidator.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class DateValidatorTest
    extends TestCase {
    private static final ResourceHelper resources = ResourceHelper.getResourceHelper();
    public static final DateFormat D_M_YY = new SimpleDateFormat("d/M/yy");
    public static final DateFormat YYYY_MM_YY = new SimpleDateFormat("yyyy-MM-dd");
    private List formats = Arrays.asList(new Object[] { D_M_YY, YYYY_MM_YY });

    public void testSingleFormatValidate()
        throws InvalidArgumentException {
        final Object[] array = new Object[] { "23/12/03" };
        final List list = Arrays.asList(array);
        final Validator validator = new DateValidator(D_M_YY);

        validator.validate(list);

        final Iterator i = list.iterator();
        assertEquals("2003-12-23", YYYY_MM_YY.format((Date) i.next()));
        assertFalse(i.hasNext());
    }

    public void testDefaultDateFormatValidate()
        throws InvalidArgumentException {
        final Object[] array = new Object[] { "23-Dec-2003" };
        final List list = Arrays.asList(array);
        final Validator validator = DateValidator.getDateInstance();

        validator.validate(list);

        final Iterator i = list.iterator();
        assertEquals("2003-12-23", YYYY_MM_YY.format((Date) i.next()));
        assertFalse(i.hasNext());
    }

    public void testDefaultTimeFormatValidate()
        throws InvalidArgumentException {
        final Object[] array = new Object[] { "18:00:00" };
        final List list = Arrays.asList(array);
        final Validator validator = DateValidator.getTimeInstance();

        validator.validate(list);

        final Iterator i = list.iterator();
        final DateFormat df = new SimpleDateFormat("HH:mm:ss");
        assertEquals("18:00:00", df.format((Date) i.next()));
        assertFalse(i.hasNext());
    }

    public void testDefaultDateTimeFormatValidate()
        throws InvalidArgumentException {
        final Object[] array = new Object[] { "23-Jan-2003 18:00:00" };
        final List list = Arrays.asList(array);
        final Validator validator = DateValidator.getDateTimeInstance();

        validator.validate(list);

        final Iterator i = list.iterator();
        final DateFormat df = new SimpleDateFormat("yyyy/M/dd HH:mm:ss");
        assertEquals("2003/1/23 18:00:00", df.format((Date) i.next()));
        assertFalse(i.hasNext());
    }

    public void testDefaultValidator()
        throws InvalidArgumentException {<commit_msg>As #38845 states, three tests in this class fail. This is because they make assumptions about the default datetime formatting instances. I've made them non-default, which should allow tests to pass. Two do pass for me, but the third oddly enough doesn't - somehow it turns 23-Dec-2003 into 2003-12-22. Very odd. <commit_after>/*
 * Copyright 2003-2005 The Apache Software Foundation
 *
 * Licensed 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.commons.cli2.validation;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.commons.cli2.resource.ResourceConstants;
import org.apache.commons.cli2.resource.ResourceHelper;

/**
 * JUnit test case for DateValidator.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class DateValidatorTest
    extends TestCase {
    private static final ResourceHelper resources = ResourceHelper.getResourceHelper();
    public static final DateFormat D_M_YY = new SimpleDateFormat("d/M/yy");
    public static final DateFormat YYYY_MM_YY = new SimpleDateFormat("yyyy-MM-dd");
    private List formats = Arrays.asList(new Object[] { D_M_YY, YYYY_MM_YY });

    public void testSingleFormatValidate()
        throws InvalidArgumentException {
        final Object[] array = new Object[] { "23/12/03" };
        final List list = Arrays.asList(array);
        final Validator validator = new DateValidator(D_M_YY);

        validator.validate(list);

        final Iterator i = list.iterator();
        assertEquals("2003-12-23", YYYY_MM_YY.format((Date) i.next()));
        assertFalse(i.hasNext());
    }

    public void testDefaultDateFormatValidate()
        throws InvalidArgumentException {
        final Object[] array = new Object[] { "23-Dec-2003" };
        final List list = Arrays.asList(array);
        final Validator validator = new DateValidator( new SimpleDateFormat("dd-MMM-yyyy") );

        validator.validate(list);

        final Iterator i = list.iterator();
        assertEquals("2003-12-23", YYYY_MM_YY.format((Date) i.next()));
        assertFalse(i.hasNext());
    }

    public void testDefaultTimeFormatValidate()
        throws InvalidArgumentException {
        final Object[] array = new Object[] { "18:00:00" };
        final List list = Arrays.asList(array);
        final Validator validator = new DateValidator( new SimpleDateFormat("HH:mm:ss") );

        validator.validate(list);

        final Iterator i = list.iterator();
        final DateFormat df = new SimpleDateFormat("HH:mm:ss");
        assertEquals("18:00:00", df.format((Date) i.next()));
        assertFalse(i.hasNext());
    }

    public void testDefaultDateTimeFormatValidate()
        throws InvalidArgumentException {
        final Object[] array = new Object[] { "23-Jan-2003 18:00:00" };
        final List list = Arrays.asList(array);
        final Validator validator = new DateValidator( new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss") );

        validator.validate(list);

        final Iterator i = list.iterator();
        final DateFormat df = new SimpleDateFormat("yyyy/M/dd HH:mm:ss");
        assertEquals("2003/1/23 18:00:00", df.format((Date) i.next()));
        assertFalse(i.hasNext());
    }

    public void testDefaultValidator()
        throws InvalidArgumentException {
{'fraction_non_alphanumeric': 0.10737726437385141, 'fraction_numerical': 0.023496980834864793, 'mean_word_length': 3.4425655976676386, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1890496', 'n_tokens_mistral': 2210, 'n_tokens_neox': 2064, 'n_words': 626}