<commit_before>/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.collect;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNotSame;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.MonthDay;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import org.joda.beans.Bean;
import org.joda.beans.BeanBuilder;
import org.joda.beans.ImmutableBean;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaBean;
import org.joda.beans.MetaProperty;
import org.joda.beans.impl.direct.DirectMetaProperty;
import org.joda.beans.test.BeanAssert;
import org.joda.convert.StringConvert;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;

/**
 * Test helper.
 * <p>
 * Provides additional classes to help with testing.
 */
public class TestHelper {

  //-------------------------------------------------------------------------
  /**
   * Creates a {@code LocalDate}, intended for static import.
   * 
   * @param year  the year
   * @param month  the month
   * @param dayOfMonth  the dayOfMonth
   * @return the date
   */
  public static LocalDate date(int year, int month, int dayOfMonth) {
    return LocalDate.of(year, month, dayOfMonth);
  }

  /**
   * Creates a {@code LocalDate}, intended for static import.
   * 
   * @param year  the year
   * @param month  the month
   * @param dayOfMonth  the dayOfMonth
   * @return the date
   */
  public static LocalDate date(int year, Month month, int dayOfMonth) {
    return LocalDate.of(year, month, dayOfMonth);
  }

  //-------------------------------------------------------------------------
  /**
   * Asserts that two beans are equal.
   * Provides better error messages than a normal {@code assertEquals} comparison.
   * 
   * @param actual  the actual bean under test
   * @param expected  the expected bean
   */
  public static void assertEqualsBean(Bean actual, Bean expected) {
    BeanAssert.assertBeanEquals(expected, actual);
  }

  /**
   * Asserts that two beans are equal.
   * Provides better error messages than a normal {@code assertEquals} comparison.
   * <p>
   * This provides extra detail used when debugging an issue.
   * Normal use should be to call {@link #assertEqualsBean(Bean, Bean)}.
   * 
   * @param actual  the actual bean under test
   * @param expected  the expected bean
   */
  public static void assertEqualsBeanDetailed(Bean actual, Bean expected) {
    BeanAssert.assertBeanEqualsFullDetail(expected, actual);
  }

  //-------------------------------------------------------------------------
  /**
   * Asserts that the object can be serialized and deserialized to an equal form.
   * 
   * @param base  the object to be tested
   */
  public static void assertSerialization(Object base) {
    assertNotNull(base);
    try {
      try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
          oos.writeObject(base);
          oos.close();
          try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) {
            try (ObjectInputStream ois = new ObjectInputStream(bais)) {
              assertEquals(ois.readObject(), base);
            }
          }
        }
      }
    } catch (IOException | ClassNotFoundException ex) {
      throw new RuntimeException(ex);
    }
  }

  //-------------------------------------------------------------------------
  /**
   * Asserts that the object can be serialized and deserialized via a string using Joda-Convert.
   * 
   * @param cls  the effective type
   * @param base  the object to be tested
   */
  public static <T> void assertJodaConvert(Class<T> cls, Object base) {
    assertNotNull(base);
    StringConvert convert = StringConvert.create();
    String str = convert.convertToString(base);
    T result = convert.convertFromString(cls, str);
    assertEquals(result, base);
  }

  //-------------------------------------------------------------------------
  /**
   * Asserts that the lambda-based code throws the specified exception.
   * <p>
   * For example:
   * <pre>
   *  assertThrows(() -> bean.property(""), NoSuchElementException.class);
   * </pre>
   * 
   * @param runner  the lambda containing the code to test
   * @param expected  the expected exception
   */
  public static void assertThrows(AssertRunnable runner, Class<? extends Throwable> expected) {
    assertNotNull(runner, "assertThrows() called with null AssertRunnable");
    assertNotNull(expected, "assertThrows() called with null expected Class");
    try {
      runner.run();
      fail("Expected " + expected.getSimpleName() + " but code succeeded normally");
    } catch (Throwable ex) {
      if (expected.isInstance(ex)) {
        return;<commit_msg>PLT-344 - Enhance TestHelper<commit_after>/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.collect;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNotSame;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.MonthDay;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import org.joda.beans.Bean;
import org.joda.beans.BeanBuilder;
import org.joda.beans.ImmutableBean;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaBean;
import org.joda.beans.MetaProperty;
import org.joda.beans.impl.direct.DirectMetaProperty;
import org.joda.beans.test.BeanAssert;
import org.joda.convert.StringConvert;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;

/**
 * Test helper.
 * <p>
 * Provides additional classes to help with testing.
 */
public class TestHelper {

  //-------------------------------------------------------------------------
  /**
   * Creates a {@code LocalDate}, intended for static import.
   * 
   * @param year  the year
   * @param month  the month
   * @param dayOfMonth  the dayOfMonth
   * @return the date
   */
  public static LocalDate date(int year, int month, int dayOfMonth) {
    return LocalDate.of(year, month, dayOfMonth);
  }

  /**
   * Creates a {@code LocalDate}, intended for static import.
   * 
   * @param year  the year
   * @param month  the month
   * @param dayOfMonth  the dayOfMonth
   * @return the date
   */
  public static LocalDate date(int year, Month month, int dayOfMonth) {
    return LocalDate.of(year, month, dayOfMonth);
  }

  //-------------------------------------------------------------------------
  /**
   * Asserts that two beans are equal.
   * Provides better error messages than a normal {@code assertEquals} comparison.
   * 
   * @param actual  the actual bean under test
   * @param expected  the expected bean
   */
  public static void assertEqualsBean(Bean actual, Bean expected) {
    BeanAssert.assertBeanEquals(expected, actual);
  }

  /**
   * Asserts that two beans are equal.
   * Provides better error messages than a normal {@code assertEquals} comparison.
   * <p>
   * This provides extra detail used when debugging an issue.
   * Normal use should be to call {@link #assertEqualsBean(Bean, Bean)}.
   * 
   * @param actual  the actual bean under test
   * @param expected  the expected bean
   */
  public static void assertEqualsBeanDetailed(Bean actual, Bean expected) {
    BeanAssert.assertBeanEqualsFullDetail(expected, actual);
  }

  //-------------------------------------------------------------------------
  /**
   * Asserts that the object can be serialized and deserialized to an equal form.
   * 
   * @param base  the object to be tested
   */
  public static void assertSerialization(Object base) {
    assertNotNull(base);
    try {
      try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
          oos.writeObject(base);
          oos.close();
          try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) {
            try (ObjectInputStream ois = new ObjectInputStream(bais)) {
              assertEquals(ois.readObject(), base);
            }
          }
        }
      }
    } catch (IOException | ClassNotFoundException ex) {
      throw new RuntimeException(ex);
    }
  }

  //-------------------------------------------------------------------------
  /**
   * Asserts that the object can be serialized and deserialized via a string using Joda-Convert.
   * 
   * @param cls  the effective type
   * @param base  the object to be tested
   */
  public static <T> void assertJodaConvert(Class<T> cls, Object base) {
    assertNotNull(base);
    StringConvert convert = StringConvert.create();
    String str = convert.convertToString(base);
    T result = convert.convertFromString(cls, str);
    assertEquals(result, base);
  }

  //-------------------------------------------------------------------------
  /**
   * Asserts that the lambda-based code throws the specified exception.
   * <p>
   * For example:
   * <pre>
   *  assertThrows(() -> bean.property(""), NoSuchElementException.class);
   * </pre>
   * 
   * @param runner  the lambda containing the code to test
   * @param expected  the expected exception
   */
  public static void assertThrows(AssertRunnable runner, Class<? extends Throwable> expected) {
    assertNotNull(runner, "assertThrows() called with null AssertRunnable");
    assertNotNull(expected, "assertThrows() called with null expected Class");
    try {
      runner.run();
      fail("Expected " + expected.getSimpleName() + " but code succeeded normally");
    } catch (AssertionError ex) {
      throw ex;
    } catch (Throwable ex) {
      if (expected.isInstance(ex)) {
        return;
{'fraction_non_alphanumeric': 0.15362925964714413, 'fraction_numerical': 0.0008861677273825828, 'mean_word_length': 2.799816345270891, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5031135', 'n_tokens_mistral': 3436, 'n_tokens_neox': 3389, 'n_words': 986}