Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create tempory file for tests in ram #3986 #3995

Merged
merged 1 commit into from
May 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.vfs2.FileObject;
import org.apache.hop.core.IRowSet;
Expand All @@ -53,7 +51,6 @@
import org.apache.hop.pipeline.transforms.mock.TransformMockHelper;
import org.apache.hop.utils.TestUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -81,12 +78,6 @@ public static void setUpBeforeClass() throws Exception {
PluginRegistry.init();
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
FileUtils.deleteQuietly(
Paths.get(TEXT_FILE_OUTPUT_PREFIX + TEXT_FILE_OUTPUT_EXTENSION).toFile());
}

public class TextFileOutputTestHandler
extends TextFileOutput<TextFileOutputMeta, TextFileOutputData> {
public List<Throwable> errors = new ArrayList<>();
Expand Down Expand Up @@ -592,6 +583,7 @@ private void assertNotInvokedTwice(TextFileField field) {
*/
@Test
public void testProcessRule_2() throws Exception {
String filename = createTemplateFile().toString();

TextFileField tfFieldMock = Mockito.mock(TextFileField.class);
TextFileField[] textFileFields = {tfFieldMock};
Expand All @@ -604,8 +596,7 @@ public void testProcessRule_2() throws Exception {
.thenCallRealMethod();

Mockito.when(transformMockHelper.iTransformMeta.getEndedLine()).thenReturn(EMPTY_STRING);
Mockito.when(transformMockHelper.iTransformMeta.getFileName())
.thenReturn(TEXT_FILE_OUTPUT_PREFIX + TEXT_FILE_OUTPUT_EXTENSION);
Mockito.when(transformMockHelper.iTransformMeta.getFileName()).thenReturn(filename);
Mockito.when(transformMockHelper.iTransformMeta.isFileAppended()).thenReturn(true);
Mockito.when(transformMockHelper.iTransformMeta.isHeaderEnabled()).thenReturn(true);
Mockito.when(transformMockHelper.iTransformMeta.getOutputFields()).thenReturn(textFileFields);
Expand All @@ -626,28 +617,21 @@ public void testProcessRule_2() throws Exception {
IRowMeta inputRowMeta = Mockito.mock(IRowMeta.class);

IValueMeta iValueMeta = Mockito.mock(IValueMeta.class);
Mockito.when(iValueMeta.getString(Mockito.any()))
.thenReturn(TEXT_FILE_OUTPUT_PREFIX + TEXT_FILE_OUTPUT_EXTENSION);
Mockito.when(iValueMeta.getString(Mockito.any())).thenReturn(filename);
Mockito.when(inputRowMeta.getValueMeta(Mockito.anyInt())).thenReturn(iValueMeta);
Mockito.when(inputRowMeta.clone()).thenReturn(inputRowMeta);

textFileOutput.setInputRowMeta(inputRowMeta);

TextFileOutput textFileOutputSpy = Mockito.spy(textFileOutput);
Mockito.doCallRealMethod()
.when(textFileOutputSpy)
.initFileStreamWriter(TEXT_FILE_OUTPUT_PREFIX + TEXT_FILE_OUTPUT_EXTENSION);
Mockito.doCallRealMethod().when(textFileOutputSpy).initFileStreamWriter(filename);
Mockito.doNothing().when(textFileOutputSpy).writeRow(inputRowMeta, rowData);
Mockito.doReturn(false)
.when(textFileOutputSpy)
.isFileExists(TEXT_FILE_OUTPUT_PREFIX + TEXT_FILE_OUTPUT_EXTENSION);
Mockito.doReturn(true)
.when(textFileOutputSpy)
.isWriteHeader(TEXT_FILE_OUTPUT_PREFIX + TEXT_FILE_OUTPUT_EXTENSION);
Mockito.doReturn(false).when(textFileOutputSpy).isFileExists(filename);
Mockito.doReturn(true).when(textFileOutputSpy).isWriteHeader(filename);
textFileOutputSpy.init();
Mockito.when(
transformMockHelper.iTransformMeta.buildFilename(
TEXT_FILE_OUTPUT_PREFIX + TEXT_FILE_OUTPUT_EXTENSION,
filename,
null,
textFileOutputSpy,
0,
Expand All @@ -658,7 +642,7 @@ public void testProcessRule_2() throws Exception {
0,
true,
transformMockHelper.iTransformMeta))
.thenReturn(TEXT_FILE_OUTPUT_PREFIX + TEXT_FILE_OUTPUT_EXTENSION);
.thenReturn(filename);

textFileOutputSpy.processRow();
Mockito.verify(textFileOutputSpy, Mockito.times(1)).writeHeader();
Expand All @@ -673,6 +657,8 @@ public void testProcessRule_2() throws Exception {
@Test
public void testProcessRule_2FileNameInField() throws Exception {

String filename = createTemplateFile().toString();

TextFileField tfFieldMock = Mockito.mock(TextFileField.class);
TextFileField[] textFileFields = {tfFieldMock};

Expand All @@ -684,8 +670,7 @@ public void testProcessRule_2FileNameInField() throws Exception {
.thenCallRealMethod();

Mockito.when(transformMockHelper.iTransformMeta.getEndedLine()).thenReturn(EMPTY_STRING);
Mockito.when(transformMockHelper.iTransformMeta.getFileName())
.thenReturn(TEXT_FILE_OUTPUT_PREFIX + TEXT_FILE_OUTPUT_EXTENSION);
Mockito.when(transformMockHelper.iTransformMeta.getFileName()).thenReturn(filename);
Mockito.when(transformMockHelper.iTransformMeta.isFileAppended()).thenReturn(true);
Mockito.when(transformMockHelper.iTransformMeta.isHeaderEnabled()).thenReturn(true);
Mockito.when(transformMockHelper.iTransformMeta.getOutputFields()).thenReturn(textFileFields);
Expand All @@ -706,28 +691,21 @@ public void testProcessRule_2FileNameInField() throws Exception {
IRowMeta inputRowMeta = Mockito.mock(IRowMeta.class);

IValueMeta iValueMeta = Mockito.mock(IValueMeta.class);
Mockito.when(iValueMeta.getString(Mockito.any()))
.thenReturn(TEXT_FILE_OUTPUT_PREFIX + TEXT_FILE_OUTPUT_EXTENSION);
Mockito.when(iValueMeta.getString(Mockito.any())).thenReturn(filename);
Mockito.when(inputRowMeta.getValueMeta(Mockito.anyInt())).thenReturn(iValueMeta);
Mockito.when(inputRowMeta.clone()).thenReturn(inputRowMeta);

textFileOutput.setInputRowMeta(inputRowMeta);

TextFileOutput textFileOutputSpy = Mockito.spy(textFileOutput);
Mockito.doCallRealMethod()
.when(textFileOutputSpy)
.initFileStreamWriter(TEXT_FILE_OUTPUT_PREFIX + TEXT_FILE_OUTPUT_EXTENSION);
Mockito.doReturn(false)
.when(textFileOutputSpy)
.isFileExists(TEXT_FILE_OUTPUT_PREFIX + TEXT_FILE_OUTPUT_EXTENSION);
Mockito.doReturn(true)
.when(textFileOutputSpy)
.isWriteHeader(TEXT_FILE_OUTPUT_PREFIX + TEXT_FILE_OUTPUT_EXTENSION);
Mockito.doCallRealMethod().when(textFileOutputSpy).initFileStreamWriter(filename);
Mockito.doReturn(false).when(textFileOutputSpy).isFileExists(filename);
Mockito.doReturn(true).when(textFileOutputSpy).isWriteHeader(filename);
Mockito.doNothing().when(textFileOutputSpy).writeRow(inputRowMeta, rowData);
textFileOutputSpy.init();
Mockito.when(
transformMockHelper.iTransformMeta.buildFilename(
TEXT_FILE_OUTPUT_PREFIX + TEXT_FILE_OUTPUT_EXTENSION,
filename,
null,
textFileOutputSpy,
0,
Expand Down
Loading