<commit_before>/*!
 * PENTAHO CORPORATION PROPRIETARY AND CONFIDENTIAL
 *
 * Copyright 2002 - 2014 Pentaho Corporation (Pentaho). All rights reserved.
 *
 * NOTICE: All information including source code contained herein is, and
 * remains the sole property of Pentaho and its licensors. The intellectual
 * and technical concepts contained herein are proprietary and confidential
 * to, and are trade secrets of Pentaho and may be covered by U.S. and foreign
 * patents, or patents in process, and are protected by trade secret and
 * copyright laws. The receipt or possession of this source code and/or related
 * information does not convey or imply any rights to reproduce, disclose or
 * distribute its contents, or to manufacture, use, or sell anything that it
 * may describe, in whole or in part. Any reproduction, modification, distribution,
 * or public display of this information without the express written authorization
 * from Pentaho is strictly prohibited and in violation of applicable laws and
 * international treaties. Access to the source code contained herein is strictly
 * prohibited to anyone except those individuals and entities who have executed
 * confidentiality and non-disclosure agreements or other agreements with Pentaho,
 * explicitly covering such access.
 */
package org.pentaho.mondrian.tck;

import com.google.common.base.Optional;
import org.olap4j.CellSet;
import org.olap4j.layout.TraditionalCellSetFormatter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class MondrianExpectation {
  private final String query;
  private final List<String> expectedSqls;
  private final Optional<String> result;

  private MondrianExpectation(
    final String query, final String result, final List<String> expectedSqls ) {
    this.query = query;
    this.expectedSqls = expectedSqls;
    this.result = Optional.fromNullable( result );
  }

  public String getQuery() {
    return query;
  }

  public void verify( CellSet cellSet, List<String> sqls ) {
    if ( result.isPresent() ) {
      assertEquals( result.get(), cellSetToString( cellSet ) );
    }
    for ( String expectedSql : this.expectedSqls ) {
      assertTrue( "Expected sql was not executed: \n" + expectedSql, sqls.contains( expectedSql ) );
    }
  }

  private String cellSetToString( CellSet cellSet ) {
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    try {
      final PrintWriter pw = new PrintWriter( stream );
      new TraditionalCellSetFormatter().format( cellSet, pw );
      pw.flush();
    } finally {
      try {
        stream.close();
      } catch ( IOException e ) {
        e.printStackTrace();
      }
    }
    return stream.toString().replaceAll( "\r\n", "\n" );
  }

  public static Builder newBuilder() {
    return new Builder();
  }

  public static class Builder {
    private String result;
    private List<String> sqls = new ArrayList<>();
    private String query;

    private Builder() {
    }

    public Builder query( String query ) {
      this.query = query;
      return this;
    }<commit_msg>[TEST] - fix verifying sql with CR symbol for Windows or Unix<commit_after>/*!
 * PENTAHO CORPORATION PROPRIETARY AND CONFIDENTIAL
 *
 * Copyright 2002 - 2014 Pentaho Corporation (Pentaho). All rights reserved.
 *
 * NOTICE: All information including source code contained herein is, and
 * remains the sole property of Pentaho and its licensors. The intellectual
 * and technical concepts contained herein are proprietary and confidential
 * to, and are trade secrets of Pentaho and may be covered by U.S. and foreign
 * patents, or patents in process, and are protected by trade secret and
 * copyright laws. The receipt or possession of this source code and/or related
 * information does not convey or imply any rights to reproduce, disclose or
 * distribute its contents, or to manufacture, use, or sell anything that it
 * may describe, in whole or in part. Any reproduction, modification, distribution,
 * or public display of this information without the express written authorization
 * from Pentaho is strictly prohibited and in violation of applicable laws and
 * international treaties. Access to the source code contained herein is strictly
 * prohibited to anyone except those individuals and entities who have executed
 * confidentiality and non-disclosure agreements or other agreements with Pentaho,
 * explicitly covering such access.
 */
package org.pentaho.mondrian.tck;

import com.google.common.base.Optional;
import org.olap4j.CellSet;
import org.olap4j.layout.TraditionalCellSetFormatter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class MondrianExpectation {
  private final String query;
  private final List<String> expectedSqls;
  private final Optional<String> result;

  private MondrianExpectation(
    final String query, final String result, final List<String> expectedSqls ) {
    this.query = query;
    this.expectedSqls = expectedSqls;
    this.result = Optional.fromNullable( result );
  }

  public String getQuery() {
    return query;
  }

  public void verify( CellSet cellSet, List<String> sqls ) {
    if ( result.isPresent() ) {
      assertEquals( result.get(), cellSetToString( cellSet ) );
    }

    List<String> cleanSqls = new ArrayList<>();
    for ( String sql : sqls ) {
      cleanSqls.add( cleanLineEndings( sql ) );
    }

    for ( String expectedSql : this.expectedSqls ) {
      assertTrue( "Expected sql was not executed: \n" + expectedSql, cleanSqls.contains( cleanLineEndings( expectedSql ) ) );
    }
  }

  private String cellSetToString( CellSet cellSet ) {
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    try {
      final PrintWriter pw = new PrintWriter( stream );
      new TraditionalCellSetFormatter().format( cellSet, pw );
      pw.flush();
    } finally {
      try {
        stream.close();
      } catch ( IOException e ) {
        e.printStackTrace();
      }
    }
    return stream.toString().replaceAll( "\r\n", "\n" );
  }

  private static String cleanLineEndings( String string ) {
    return string.replaceAll( "\r\n", "\n" );
  }

  public static Builder newBuilder() {
    return new Builder();
  }

  public static class Builder {
    private String result;
    private List<String> sqls = new ArrayList<>();
    private String query;

    private Builder() {
    }

    public Builder query( String query ) {
      this.query = query;
      return this;
    }
{'fraction_non_alphanumeric': 0.08025776215582894, 'fraction_numerical': 0.0029291154071470417, 'mean_word_length': 3.6582537517053204, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '7149998', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1756, 'n_words': 696}