<commit_before>/* 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.camunda.bpm.engine.test.variables.serializer;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;

import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.Scanner;

import org.camunda.bpm.engine.impl.core.variable.type.FileValueTypeImpl;
import org.camunda.bpm.engine.impl.core.variable.value.UntypedValueImpl;
import org.camunda.bpm.engine.impl.persistence.entity.ByteArrayEntity;
import org.camunda.bpm.engine.impl.variable.serializer.FileValueSerializer;
import org.camunda.bpm.engine.impl.variable.serializer.ValueFields;
import org.camunda.bpm.engine.variable.Variables;
import org.camunda.bpm.engine.variable.value.FileValue;
import org.camunda.bpm.engine.variable.value.TypedValue;
import org.junit.Before;
import org.junit.Test;

/**
 * @author <NAME>
 *
 */
public class FileValueSerializerTest {

  private FileValueSerializer serializer;

  @Before
  public void setUp() {
    serializer = new FileValueSerializer();
  }

  @Test
  public void testTypeIsFileValueType() {
    assertThat(serializer.getType(), is(instanceOf(FileValueTypeImpl.class)));
  }

  @Test
  public void testWriteFilenameOnlyValue() {
    String filename = "test.txt";
    FileValue fileValue = Variables.fileValue(filename).create();
    ValueFields valueFields = new MockValueFields();

    serializer.writeValue(fileValue, valueFields);

    assertThat(valueFields.getByteArrayValue().getBytes(), is(nullValue()));
    assertThat(valueFields.getTextValue(), is(filename));
    assertThat(valueFields.getTextValue2(), is(nullValue()));
  }

  @Test
  public void testWriteMimetypeAndFilenameValue() {
    String filename = "test.txt";
    String mimeType = "text/json";
    FileValue fileValue = Variables.fileValue(filename).mimeType(mimeType).create();
    ValueFields valueFields = new MockValueFields();

    serializer.writeValue(fileValue, valueFields);

    assertThat(valueFields.getByteArrayValue().getBytes(), is(nullValue()));
    assertThat(valueFields.getTextValue(), is(filename));
    assertThat(valueFields.getTextValue2(), is(mimeType));
  }

  @Test
  public void testWriteMimetypeFilenameAndBytesValue() {
    String filename = "test.txt";
    String mimeType = "text/json";
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("org/camunda/bpm/engine/test/variables/simpleFile.txt");
    FileValue fileValue = Variables.fileValue(filename).mimeType(mimeType).file(is).create();
    ValueFields valueFields = new MockValueFields();

    serializer.writeValue(fileValue, valueFields);

    assertThat(new String(valueFields.getByteArrayValue().getBytes()), is("text"));
    assertThat(valueFields.getTextValue(), is(filename));
    assertThat(valueFields.getTextValue2(), is(mimeType));
  }

  @Test
  public void testWriteMimetypeFilenameAndBytesValueWithShortcutMethod() throws URISyntaxException {
    File file = new File(this.getClass().getClassLoader().getResource("org/camunda/bpm/engine/test/variables/simpleFile.txt").toURI());
    FileValue fileValue = Variables.fileValue(file);
    ValueFields valueFields = new MockValueFields();

    serializer.writeValue(fileValue, valueFields);

    assertThat(new String(valueFields.getByteArrayValue().getBytes()), is("text"));<commit_msg>chore(engine): specify file encoding in test<commit_after>/* 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.camunda.bpm.engine.test.variables.serializer;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;

import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.Scanner;

import org.camunda.bpm.engine.impl.core.variable.type.FileValueTypeImpl;
import org.camunda.bpm.engine.impl.core.variable.value.UntypedValueImpl;
import org.camunda.bpm.engine.impl.persistence.entity.ByteArrayEntity;
import org.camunda.bpm.engine.impl.variable.serializer.FileValueSerializer;
import org.camunda.bpm.engine.impl.variable.serializer.ValueFields;
import org.camunda.bpm.engine.variable.Variables;
import org.camunda.bpm.engine.variable.value.FileValue;
import org.camunda.bpm.engine.variable.value.TypedValue;
import org.junit.Before;
import org.junit.Test;

/**
 * @author <NAME>
 *
 */
public class FileValueSerializerTest {

  private FileValueSerializer serializer;

  @Before
  public void setUp() {
    serializer = new FileValueSerializer();
  }

  @Test
  public void testTypeIsFileValueType() {
    assertThat(serializer.getType(), is(instanceOf(FileValueTypeImpl.class)));
  }

  @Test
  public void testWriteFilenameOnlyValue() {
    String filename = "test.txt";
    FileValue fileValue = Variables.fileValue(filename).create();
    ValueFields valueFields = new MockValueFields();

    serializer.writeValue(fileValue, valueFields);

    assertThat(valueFields.getByteArrayValue().getBytes(), is(nullValue()));
    assertThat(valueFields.getTextValue(), is(filename));
    assertThat(valueFields.getTextValue2(), is(nullValue()));
  }

  @Test
  public void testWriteMimetypeAndFilenameValue() {
    String filename = "test.txt";
    String mimeType = "text/json";
    FileValue fileValue = Variables.fileValue(filename).mimeType(mimeType).create();
    ValueFields valueFields = new MockValueFields();

    serializer.writeValue(fileValue, valueFields);

    assertThat(valueFields.getByteArrayValue().getBytes(), is(nullValue()));
    assertThat(valueFields.getTextValue(), is(filename));
    assertThat(valueFields.getTextValue2(), is(mimeType));
  }

  @Test
  public void testWriteMimetypeFilenameAndBytesValue() throws UnsupportedEncodingException {
    String filename = "test.txt";
    String mimeType = "text/json";
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("org/camunda/bpm/engine/test/variables/simpleFile.txt");
    FileValue fileValue = Variables.fileValue(filename).mimeType(mimeType).file(is).create();
    ValueFields valueFields = new MockValueFields();

    serializer.writeValue(fileValue, valueFields);

    assertThat(new String(valueFields.getByteArrayValue().getBytes(), "UTF-8"), is("text"));
    assertThat(valueFields.getTextValue(), is(filename));
    assertThat(valueFields.getTextValue2(), is(mimeType));
  }

  @Test
  public void testWriteMimetypeFilenameAndBytesValueWithShortcutMethod() throws URISyntaxException, UnsupportedEncodingException {
    File file = new File(this.getClass().getClassLoader().getResource("org/camunda/bpm/engine/test/variables/simpleFile.txt").toURI());
    FileValue fileValue = Variables.fileValue(file);
    ValueFields valueFields = new MockValueFields();

    serializer.writeValue(fileValue, valueFields);

    assertThat(new String(valueFields.getByteArrayValue().getBytes(), "UTF-8"), is("text"));
{'fraction_non_alphanumeric': 0.11491886655364494, 'fraction_numerical': 0.0019375151368370065, 'mean_word_length': 5.328735632183908, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '7665615', 'n_tokens_mistral': 2365, 'n_tokens_neox': 2343, 'n_words': 499}