Best Mockito code snippet using org.mockito.internal.util.io.IOUtil.IOUtil
Source:BaseByteArrayWriterTest.java
1package com.fr.swift.cloud.cube.io.impl;2import com.fr.swift.cloud.cube.io.input.LongReader;3import com.fr.swift.cloud.cube.io.output.ByteWriter;4import com.fr.swift.cloud.cube.io.output.IntWriter;5import com.fr.swift.cloud.cube.io.output.LongWriter;6import com.fr.swift.cloud.util.IoUtil;7import org.junit.Test;8import org.junit.runner.RunWith;9import org.mockito.Mock;10import org.mockito.junit.MockitoJUnitRunner;11import org.powermock.core.classloader.annotations.PrepareForTest;12import org.powermock.modules.junit4.PowerMockRunner;13import org.powermock.modules.junit4.PowerMockRunnerDelegate;14import org.powermock.reflect.Whitebox;15import java.io.IOException;16import java.io.OutputStream;17import static org.junit.Assert.assertEquals;18import static org.mockito.Mockito.times;19import static org.mockito.Mockito.verify;20import static org.mockito.Mockito.verifyNoMoreInteractions;21import static org.powermock.api.mockito.PowerMockito.doThrow;22import static org.powermock.api.mockito.PowerMockito.mockStatic;23import static org.powermock.api.mockito.PowerMockito.verifyStatic;24import static org.powermock.api.mockito.PowerMockito.verifyZeroInteractions;25import static org.powermock.api.mockito.PowerMockito.when;26/**27 * @author anchore28 * @date 2019/3/2729 */30@RunWith(PowerMockRunner.class)31@PowerMockRunnerDelegate(MockitoJUnitRunner.class)32@PrepareForTest({IoUtil.class})33public class BaseByteArrayWriterTest {34 @Mock35 private ByteWriter dataWriter;36 @Mock37 private LongWriter posWriter;38 @Mock39 private IntWriter lenWriter;40 @Mock41 private LongWriter lastPosWriter;42 @Mock43 private LongReader lastPosReader;44 @Test45 public void put() {46 BaseByteArrayWriter byteArrayWriter = new BaseByteArrayWriter(dataWriter, posWriter, lenWriter, true, lastPosWriter, lastPosReader);47 byteArrayWriter.put(0, new byte[]{1, 2, 3});48 verifyZeroInteractions(lastPosWriter, lastPosReader);49 verify(posWriter).put(0, 0);50 verify(lenWriter).put(0, 3);51 verify(dataWriter).put(0, (byte) 1);52 verify(dataWriter).put(1, (byte) 2);53 verify(dataWriter).put(2, (byte) 3);54 assertEquals(3L, (long) Whitebox.getInternalState(byteArrayWriter, "curPos"));55 byteArrayWriter.put(1, null);56 verify(posWriter).put(1, 3);57 verify(lenWriter).put(1, 0);58 verifyNoMoreInteractions(dataWriter);59 assertEquals(3L, (long) Whitebox.getInternalState(byteArrayWriter, "curPos"));60 when(lastPosReader.isReadable()).thenReturn(true);61 when(lastPosReader.get(0)).thenReturn(3L);62 byteArrayWriter = new BaseByteArrayWriter(dataWriter, posWriter, lenWriter, false, lastPosWriter, lastPosReader);63 assertEquals(3L, (long) Whitebox.getInternalState(byteArrayWriter, "curPos"));64 }65 @Test66 public void putStream() throws IOException {67 BaseByteArrayWriter byteArrayWriter = new BaseByteArrayWriter(dataWriter, posWriter, lenWriter, true, lastPosWriter, lastPosReader);68 OutputStream output = byteArrayWriter.putStream(0);69 output.write(new byte[]{1, 2, 3});70 output.close();71 verifyZeroInteractions(lastPosWriter, lastPosReader);72 verify(posWriter).put(0, 0);73 verify(lenWriter).put(0, 3);74 verify(dataWriter).put(0, (byte) 1);75 verify(dataWriter).put(1, (byte) 2);76 verify(dataWriter).put(2, (byte) 3);77 assertEquals(3L, (long) Whitebox.getInternalState(byteArrayWriter, "curPos"));78 output = byteArrayWriter.putStream(1);79 output.close();80 verify(posWriter).put(1, 3);81 verify(lenWriter).put(1, 0);82 verifyNoMoreInteractions(dataWriter);83 assertEquals(3L, (long) Whitebox.getInternalState(byteArrayWriter, "curPos"));84 when(lastPosReader.isReadable()).thenReturn(true);85 when(lastPosReader.get(0)).thenReturn(3L);86 byteArrayWriter = new BaseByteArrayWriter(dataWriter, posWriter, lenWriter, false, lastPosWriter, lastPosReader);87 assertEquals(3L, (long) Whitebox.getInternalState(byteArrayWriter, "curPos"));88 }89 @Test90 public void resetContentPosition() {91 BaseByteArrayWriter byteArrayWriter = new BaseByteArrayWriter(dataWriter, posWriter, lenWriter, true, lastPosWriter, lastPosReader);92 Whitebox.setInternalState(byteArrayWriter, "curPos", 1L);93 byteArrayWriter.resetContentPosition();94 assertEquals(0L, (long) Whitebox.getInternalState(byteArrayWriter, "curPos"));95 }96 @Test97 public void release() {98 mockStatic(IoUtil.class);99 new BaseByteArrayWriter(dataWriter, posWriter, lenWriter, true, lastPosWriter, lastPosReader).release();100 verifyZeroInteractions(lastPosWriter);101 verifyStatic(IoUtil.class);102 IoUtil.release(lastPosWriter, dataWriter, posWriter, lenWriter);103 new BaseByteArrayWriter(dataWriter, posWriter, lenWriter, false, lastPosWriter, lastPosReader).release();104 verify(lastPosWriter).put(0, 0);105 verifyStatic(IoUtil.class, times(2));106 IoUtil.release(lastPosWriter, dataWriter, posWriter, lenWriter);107 doThrow(new RuntimeException()).when(lastPosWriter).put(0, 0);108 try {109 new BaseByteArrayWriter(dataWriter, posWriter, lenWriter, false, lastPosWriter, lastPosReader).release();110 } catch (Exception ignore) {111 }112 verify(lastPosWriter, times(2)).put(0, 0);113 verifyStatic(IoUtil.class, times(3));114 IoUtil.release(lastPosWriter, dataWriter, posWriter, lenWriter);115 }116}...
Source:PluginFinderTest.java
...7import org.junit.Test;8import org.junit.rules.TemporaryFolder;9import org.mockito.InjectMocks;10import org.mockito.Mock;11import org.mockito.internal.util.io.IOUtil;12import org.mockito.plugins.PluginSwitch;13import org.mockitoutil.TestBase;14import java.io.File;15import java.net.URL;16import java.util.Collections;17import static java.util.Arrays.asList;18import static org.junit.Assert.*;19import static org.assertj.core.api.Assertions.assertThat;20import static org.mockito.Matchers.anyString;21import static org.mockito.Mockito.when;22public class PluginFinderTest extends TestBase {23 @Mock24 PluginSwitch switcher;25 @InjectMocks PluginFinder finder;26 public @Rule TemporaryFolder tmp = new TemporaryFolder();27 @Test public void empty_resources() {28 assertNull(finder.findPluginClass(Collections.<URL>emptyList()));29 }30 @Test public void no_valid_impl() throws Exception {31 File f = tmp.newFile();32 //when33 IOUtil.writeText(" \n ", f);34 //then35 assertNull(finder.findPluginClass(asList(f.toURI().toURL())));36 }37 @Test public void single_implementation() throws Exception {38 File f = tmp.newFile();39 when(switcher.isEnabled("foo.Foo")).thenReturn(true);40 //when41 IOUtil.writeText(" foo.Foo ", f);42 //then43 assertEquals("foo.Foo", finder.findPluginClass(asList(f.toURI().toURL())));44 }45 @Test public void single_implementation_disabled() throws Exception {46 File f = tmp.newFile();47 when(switcher.isEnabled("foo.Foo")).thenReturn(false);48 //when49 IOUtil.writeText(" foo.Foo ", f);50 //then51 assertEquals(null, finder.findPluginClass(asList(f.toURI().toURL())));52 }53 @Test public void multiple_implementations_only_one_enabled() throws Exception {54 File f1 = tmp.newFile(); File f2 = tmp.newFile();55 when(switcher.isEnabled("Bar")).thenReturn(true);56 //when57 IOUtil.writeText("Foo", f1); IOUtil.writeText("Bar", f2);58 //then59 assertEquals("Bar", finder.findPluginClass(asList(f1.toURI().toURL(), f2.toURI().toURL())));60 }61 @Test public void multiple_implementations_only_one_useful() throws Exception {62 File f1 = tmp.newFile(); File f2 = tmp.newFile();63 when(switcher.isEnabled(anyString())).thenReturn(true);64 //when65 IOUtil.writeText(" ", f1); IOUtil.writeText("X", f2);66 //then67 assertEquals("X", finder.findPluginClass(asList(f1.toURI().toURL(), f2.toURI().toURL())));68 }69 @Test public void multiple_empty_implementations() throws Exception {70 File f1 = tmp.newFile(); File f2 = tmp.newFile();71 when(switcher.isEnabled(anyString())).thenReturn(true);72 //when73 IOUtil.writeText(" ", f1); IOUtil.writeText("\n", f2);74 //then75 assertEquals(null, finder.findPluginClass(asList(f1.toURI().toURL(), f2.toURI().toURL())));76 }77 @Test public void problems_loading_impl() throws Exception {78 when(switcher.isEnabled(anyString())).thenThrow(new RuntimeException("Boo!"));79 try {80 //when81 finder.findPluginClass(asList(new File("xxx").toURI().toURL()));82 //then83 fail();84 } catch(Exception e) {85 assertThat(e).hasMessageContaining("xxx");86 e.getCause().getMessage().equals("Boo!");87 }...
Source:BaseRoaringBitMapTest.java
1package com.fr.swift.cloud.bitmap.impl;2import com.fr.swift.cloud.bitmap.BitMapType;3import com.fr.swift.cloud.bitmap.roaringbitmap.buffer.MutableRoaringBitmap;4import com.fr.swift.cloud.util.IoUtil;5import org.junit.Test;6import org.junit.runner.RunWith;7import org.powermock.core.classloader.annotations.PrepareForTest;8import org.powermock.modules.junit4.PowerMockRunner;9import org.powermock.reflect.Whitebox;10import java.io.DataOutputStream;11import java.io.OutputStream;12import static org.mockito.Mockito.CALLS_REAL_METHODS;13import static org.mockito.Mockito.verify;14import static org.powermock.api.mockito.PowerMockito.mock;15import static org.powermock.api.mockito.PowerMockito.mockStatic;16import static org.powermock.api.mockito.PowerMockito.verifyStatic;17import static org.powermock.api.mockito.PowerMockito.when;18import static org.powermock.api.mockito.PowerMockito.whenNew;19/**20 * @author anchore21 * @date 2019/4/322 */23@RunWith(PowerMockRunner.class)24@PrepareForTest({IoUtil.class, BaseRoaringBitMap.class})25public class BaseRoaringBitMapTest {26 @Test27 public void writeBytes() throws Exception {28 BaseRoaringBitMap baseRoaringBitMap = mock(BaseRoaringBitMap.class, CALLS_REAL_METHODS);29 MutableRoaringBitmap mutableRoaringBitmap = mock(MutableRoaringBitmap.class);30 Whitebox.setInternalState(baseRoaringBitMap, "bitmap", mutableRoaringBitmap);31 OutputStream output = mock(OutputStream.class);32 DataOutputStream dataOutput = mock(DataOutputStream.class);33 whenNew(DataOutputStream.class).withArguments(output).thenReturn(dataOutput);34 when(baseRoaringBitMap.getType()).thenReturn(BitMapType.ROARING_MUTABLE);35 mockStatic(IoUtil.class);36 baseRoaringBitMap.writeBytes(output);37 verify(dataOutput).write(BitMapType.ROARING_MUTABLE.getHead());38 verify(mutableRoaringBitmap).serialize(dataOutput);39 verifyStatic(IoUtil.class);40 IoUtil.close(dataOutput);41 }42}...
Source:IOUtilTest.java
...10import java.io.File;11import java.io.FileInputStream;12import static java.util.Arrays.asList;13import static org.junit.Assert.assertEquals;14public class IOUtilTest {15 @Rule public TemporaryFolder tmp = new TemporaryFolder();16 @Test17 public void closes_streams() throws Exception {18 IOUtil.closeQuietly(null);19 IOUtil.closeQuietly(new ByteArrayOutputStream());20 IOUtil.close(null);21 IOUtil.close(new ByteArrayOutputStream());22 }23 @Test24 public void writes_reads_files() throws Exception {25 File file = tmp.newFile();26 IOUtil.writeText("foo\n\nbar", file);27 assertEquals(asList("foo", "", "bar"), IOUtil.readLines(new FileInputStream(file)));28 }29}...
IOUtil
Using AI Code Generation
1package org.mockito.internal.util.io;2import java.io.IOException;3import java.io.InputStream;4import java.io.InputStreamReader;5import java.io.Reader;6public class IOUtil {7 public static String readFully(Reader reader) throws IOException {8 StringBuilder sb = new StringBuilder();9 char[] buffer = new char[1024];10 int read;11 while ((read = reader.read(buffer)) != -1) {12 sb.append(buffer, 0, read);13 }14 return sb.toString();15 }16 public static String readFully(InputStream inputStream) throws IOException {17 return readFully(new InputStreamReader(inputStream));18 }19}20package org.mockito.internal.util.io;21import java.io.IOException;22import java.io.InputStream;23public class IOUtil {24 public static String readFully(InputStream inputStream) throws IOException {25 return readFully(new InputStreamReader(inputStream));26 }27}28package org.mockito.internal.util.io;29import java.io.IOException;30import java.io.InputStream;31import java.io.InputStreamReader;32import java.io.Reader;33public class IOUtil {34 public static String readFully(InputStream inputStream) throws IOException {35 return readFully(new InputStreamReader(inputStream));36 }37}38package org.mockito.internal.util.io;39import java.io.IOException;40import java.io.InputStream;41import java.io.InputStreamReader;42import java.io.Reader;43public class IOUtil {44 public static String readFully(Reader reader) throws IOException {45 StringBuilder sb = new StringBuilder();46 char[] buffer = new char[1024];47 int read;48 while ((read = reader.read(buffer)) != -1) {49 sb.append(buffer, 0, read);50 }51 return sb.toString();52 }53}54package org.mockito.internal.util.io;55import java.io.IOException;56import java.io.InputStream;57import java.io.InputStreamReader;58import java.io.Reader;59public class IOUtil {60 public static String readFully(Reader reader) throws IOException {61 StringBuilder sb = new StringBuilder();62 char[] buffer = new char[1024];63 int read;64 while ((read = reader.read(buffer)) != -
IOUtil
Using AI Code Generation
1import org.mockito.internal.util.io.IOUtil;2import java.io.IOException;3import java.io.InputStream;4import java.io.OutputStream;5public class MyClass {6 public static void main(String[] args) throws IOException {7 InputStream in = null;8 OutputStream out = null;9 try {10 IOUtil.copy(in, out);11 } finally {12 IOUtil.closeQuietly(in);13 IOUtil.closeQuietly(out);14 }15 }16}17import org.mockito.internal.util.io.IOUtil;18import java.io.IOException;19import java.io.InputStream;20import java.io.OutputStream;21public class MyClass {22 public static void main(String[] args) throws IOException {23 InputStream in = null;24 OutputStream out = null;25 try {26 IOUtil.copy(in, out);27 } finally {28 IOUtil.closeQuietly(in);29 IOUtil.closeQuietly(out);30 }31 }32}33import org.mockito.internal.util.io.IOUtil;34import java.io.IOException;35import java.io.InputStream;36import java.io.OutputStream;37public class MyClass {38 public static void main(String[] args) throws IOException {39 InputStream in = null;40 OutputStream out = null;41 try {42 IOUtil.copy(in, out);43 } finally {44 IOUtil.closeQuietly(in);45 IOUtil.closeQuietly(out);46 }47 }48}49import org.mockito.internal.util.io.IOUtil;50import java.io.IOException;51import java.io.InputStream;52import java.io.OutputStream;53public class MyClass {54 public static void main(String[] args) throws IOException {55 InputStream in = null;56 OutputStream out = null;57 try {58 IOUtil.copy(in, out);59 } finally {60 IOUtil.closeQuietly(in);61 IOUtil.closeQuietly(out);62 }63 }64}65import org.mockito.internal.util.io.IOUtil;66import java.io.IOException;67import java.io.InputStream;68import java.io.OutputStream;69public class MyClass {70 public static void main(String[] args) throws IOException {71 InputStream in = null;
IOUtil
Using AI Code Generation
1package org.mockito.internal.util.io;2import java.io.*;3public class IOUtil {4 public static byte[] toByteArray(InputStream is) throws IOException {5 ByteArrayOutputStream baos = new ByteArrayOutputStream();6 try {7 copy(is, baos);8 } finally {9 is.close();10 }11 return baos.toByteArray();12 }13 public static void copy(InputStream is, OutputStream os) throws IOException {14 byte[] buffer = new byte[1024];15 int len;16 while ((len = is.read(buffer)) != -1) {17 os.write(buffer, 0, len);18 }19 }20}21package org.mockito.internal.util.io;22import java.io.*;23public class IOUtil {24 public static byte[] toByteArray(InputStream is) throws IOException {25 ByteArrayOutputStream baos = new ByteArrayOutputStream();26 try {27 copy(is, baos);28 } finally {29 is.close();30 }31 return baos.toByteArray();32 }33 public static void copy(InputStream is, OutputStream os) throws IOException {34 byte[] buffer = new byte[1024];35 int len;36 while ((len = is.read(buffer)) != -1) {37 os.write(buffer, 0, len);38 }39 }40}41package org.mockito.internal.util.io;42import java.io.*;43public class IOUtil {44 public static byte[] toByteArray(InputStream is) throws IOException {45 ByteArrayOutputStream baos = new ByteArrayOutputStream();46 try {47 copy(is, baos);48 } finally {49 is.close();50 }51 return baos.toByteArray();52 }53 public static void copy(InputStream is, OutputStream os) throws IOException {54 byte[] buffer = new byte[1024];55 int len;56 while ((len = is.read(buffer)) != -1) {57 os.write(buffer, 0, len);58 }59 }60}61package org.mockito.internal.util.io;62import java.io.*;63public class IOUtil {64 public static byte[] toByteArray(InputStream is) throws IOException {65 ByteArrayOutputStream baos = new ByteArrayOutputStream();66 try {67 copy(is, baos);68 } finally {69 is.close();70 }71 return baos.toByteArray();72 }73 public static void copy(InputStream is, OutputStream os) throws IOException {74 byte[] buffer = new byte[1024];75 int len;76 while ((len = is.read(buffer)) != -1) {
IOUtil
Using AI Code Generation
1public class IOUtilTest {2 public static void main(String[] args) throws IOException {3 byte[] data = IOUtil.readFile("test.txt");4 System.out.println(new String(data));5 }6}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!