Skip to content

Commit 03ee525

Browse files
committed
Merge branch 'paasio-exercise'
2 parents b8ba510 + e07be74 commit 03ee525

2 files changed

Lines changed: 23 additions & 73 deletions

File tree

exercises/practice/paasio/src/main/java/Paasio.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class Paasio implements Closeable {
1010
private final InputStream inputStream;
1111
private final OutputStream outputStream;
1212

13-
public Paasio(InputStream inputStream, OutputStream outputStream) throws FileNotFoundException {
13+
public Paasio(InputStream inputStream, OutputStream outputStream) {
1414
this.inputStream = inputStream;
1515
this.outputStream = outputStream;
1616
}
@@ -61,7 +61,6 @@ public byte[] readAllBytes() throws IOException {
6161
}
6262

6363
public byte[] readNBytes(int len) throws IOException {
64-
// return new byte[0];
6564

6665
byte[] allData = this.inputStream.readNBytes(len);
6766
if (allData.length > 0) {

exercises/practice/paasio/src/test/java/PaasioTest.java

Lines changed: 22 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
import org.junit.jupiter.api.*;
22

33
import java.io.*;
4-
import java.net.Socket;
54
import java.nio.charset.StandardCharsets;
5+
import java.util.Arrays;
66

77
import static org.assertj.core.api.Assertions.*;
88

99
public class PaasioTest {
1010

1111
private ByteArrayInputStream dataInputStream;
1212
private ByteArrayOutputStream dataOutputStream;
13+
private static final String MESSAGECONSTANT = "This is additional Content.";
1314

1415
@BeforeEach
15-
public void setUPTest(TestInfo testInfo) throws IOException {
16+
public void setUPTest(TestInfo testInfo) {
1617

1718
this.dataInputStream = new ByteArrayInputStream("This is data ".getBytes());
1819
this.dataOutputStream = new ByteArrayOutputStream();
1920
}
2021

21-
// @AfterEach
22-
// public void cleanFile(TestInfo testInfo) {
23-
//
24-
// if (testInfo.getTags().contains("fileOperation") && tmpFile.exists()) {
25-
// tmpFile.delete();
26-
// }
27-
// }
28-
2922
@Test
3023
void checkReadOperationCount() {
3124

@@ -50,7 +43,7 @@ void checkAmountOfDataReadInBytes() {
5043
fileOperations.read();
5144
fileOperations.read();
5245

53-
assertThat(4).isEqualTo(fileOperations.getBytesRead());
46+
assertThat(fileOperations.getBytesRead()).isEqualTo(4);
5447

5548
} catch (IOException ioException) {
5649
ioException.printStackTrace();
@@ -68,9 +61,9 @@ void checkAmountOfDataReadInBytesForMultipleOperations() {
6861
fileOperations.read();
6962
fileOperations.read();
7063

71-
byte readByteData[] = new byte[5];
64+
byte[] readByteData = new byte[5];
7265
fileOperations.read(readByteData);
73-
assertThat(9).isEqualTo(fileOperations.getBytesRead());
66+
assertThat(fileOperations.getBytesRead()).isEqualTo(9);
7467

7568
} catch (IOException ioException) {
7669
ioException.printStackTrace();
@@ -83,10 +76,10 @@ void checkByteValueReadFromTheFile() {
8376

8477
try (Paasio fileOperations = new Paasio(this.dataInputStream, this.dataOutputStream)) {
8578

86-
byte fileData[] = new byte[100];
79+
byte[] fileData = new byte[100];
8780
int bytesRead = fileOperations.read(fileData);
8881
String dataInFile = new String(fileData, 0, bytesRead, StandardCharsets.UTF_8);
89-
assertThat("This is data ").isEqualTo(dataInFile);
82+
assertThat(dataInFile).isEqualTo("This is data ");
9083

9184
} catch (IOException ioException) {
9285
ioException.printStackTrace();
@@ -102,11 +95,6 @@ void checkIfNullPointerExceptionIsThrownWhenNullIsPassedInsteadOfByteArray() {
10295
fileOperations.read(null);
10396
}).isInstanceOf(NullPointerException.class);
10497

105-
// Assertions.assertThrows(NullPointerException.class, () -> {
106-
// FileOperations customFileReader = new FileOperations(tmpFile, true);
107-
// customFileReader.read(null);
108-
// });
109-
11098
}
11199

112100
@Test
@@ -115,18 +103,14 @@ void checkIfDataFollowsTheOffsetWhileWritingInByteArray() {
115103
try (Paasio fileOperations = new Paasio(this.dataInputStream, this.dataOutputStream)) {
116104

117105
byte[] initialMessage = "Hello! ".getBytes();
118-
byte[] messageArray = new byte[50];
119-
120-
for (int i = 0; i < initialMessage.length; i++) {
121-
messageArray[i] = initialMessage[i];
122-
}
106+
byte[] messageArray = Arrays.copyOf(initialMessage, 50);
123107

124108
int length = messageArray.length - initialMessage.length - 2;
125109
//It will read data into the messageArray and will begin writing it from its initialMessage.length index
126110
int bytesRead = fileOperations.read(messageArray, initialMessage.length, length);
127111

128112
String dataInFile = new String(messageArray, 0, initialMessage.length + bytesRead, StandardCharsets.UTF_8);
129-
assertThat("Hello! This is data ").isEqualTo(dataInFile);
113+
assertThat(dataInFile).isEqualTo("Hello! This is data ");
130114

131115
} catch (IOException ioException) {
132116
ioException.printStackTrace();
@@ -139,7 +123,7 @@ void validateBytesOfDataReadWhileReadingItFromOffset() {
139123

140124
try (Paasio fileOperations = new Paasio(this.dataInputStream, this.dataOutputStream)) {
141125

142-
byte byteData[] = new byte[50];
126+
byte[] byteData = new byte[50];
143127
int bytesRead = fileOperations.read(byteData, 0, 10);
144128

145129
assertThat(bytesRead).isEqualTo(10);
@@ -172,7 +156,7 @@ void validateAllBytesReadOperationCount() {
172156

173157
fileOperations.read();
174158
fileOperations.read();
175-
byte[] bytes = fileOperations.readAllBytes();
159+
fileOperations.readAllBytes();
176160
assertThat(fileOperations.getReadOperationCount()).isEqualTo(3);
177161

178162
} catch (IOException ioException) {
@@ -203,13 +187,8 @@ void validateReadNBytesReadOperationCount() {
203187
void validateReadNBytesDataReadInBytesAndVerifyReadCount() {
204188

205189
try (Paasio fileOperations = new Paasio(this.dataInputStream, this.dataOutputStream)) {
206-
207-
byte dataRead[] = new byte[100];
208190
byte[] helloArray = "Hello! ".getBytes();
209-
210-
for (int i = 0; i < helloArray.length; i++) {
211-
dataRead[i] = helloArray[i];
212-
}
191+
byte[] dataRead = Arrays.copyOf(helloArray,100);
213192

214193
int bytesRead = fileOperations.read(dataRead, helloArray.length, 40);
215194
String finalVAlue = new String(dataRead, 0, helloArray.length + bytesRead, StandardCharsets.UTF_8);
@@ -218,7 +197,7 @@ void validateReadNBytesDataReadInBytesAndVerifyReadCount() {
218197
assertThat(1).isEqualTo(fileOperations.getReadOperationCount());
219198

220199
} catch (IOException ioException) {
221-
200+
ioException.printStackTrace();
222201
}
223202

224203
}
@@ -232,32 +211,23 @@ void verifyNumberOfWriteOperations() {
232211
fileOperations.write(25);
233212
fileOperations.write(25);
234213

235-
assertThat(4).isEqualTo(fileOperations.getWriteOperationCount());
214+
assertThat(fileOperations.getWriteOperationCount()).isEqualTo(4);
236215

237216
} catch (IOException ioException) {
238217
ioException.printStackTrace();
239218
}
240219

241220
}
242221

243-
// @Test
244-
// @Tag("fileOperation")
245-
// void verifyIfExceptionIsThrownIfFileOperationIsNotInWriteMode() {
246-
// assertThatThrownBy(() -> {
247-
// Paasio fileOperations = new Paasio(new FileInputStream(tmpFile), new FileOutputStream(tmpFile,true));
248-
// fileOperations.write(null);
249-
// }).isInstanceOf(UnsupportedOperationException.class);
250-
// }
251-
252222
@Test
253223
void verifyContentAfterWritingByteArrayIntoTheOutputStream() {
254224
try (Paasio fileOperations = new Paasio(this.dataInputStream, this.dataOutputStream)) {
255225

256-
byte writeFileContent[] = "This is additional Content.".getBytes();
226+
byte[] writeFileContent = MESSAGECONSTANT.getBytes();
257227
fileOperations.write(writeFileContent);
258228
String dataWritten = this.dataOutputStream.toString(StandardCharsets.UTF_8);
259229

260-
assertThat(dataWritten).isEqualTo("This is additional Content.");
230+
assertThat(dataWritten).isEqualTo(MESSAGECONSTANT);
261231

262232
} catch (IOException ioException) {
263233
ioException.printStackTrace();
@@ -269,7 +239,7 @@ void verifyWriteOperationCountAfterWritingByteArrayIntoTheOutputStream() {
269239

270240
try (Paasio fileOperations = new Paasio(this.dataInputStream, this.dataOutputStream)) {
271241

272-
byte writeFileContent[] = "This is additional Content.".getBytes();
242+
byte[] writeFileContent = MESSAGECONSTANT.getBytes();
273243
fileOperations.write(writeFileContent);
274244
fileOperations.write(writeFileContent);
275245
fileOperations.write(43);
@@ -287,7 +257,7 @@ void verifyBytesOfDataWrittenInTheOutputStream() {
287257

288258
try (Paasio fileOperations = new Paasio(this.dataInputStream, this.dataOutputStream)) {
289259

290-
byte writeFileContent[] = "This is additional Content.".getBytes();
260+
byte[] writeFileContent = MESSAGECONSTANT.getBytes();
291261
fileOperations.write(writeFileContent);
292262
fileOperations.write(writeFileContent);
293263
fileOperations.write('s');
@@ -312,7 +282,7 @@ void verifyBytesWrittenFromOffsetIntoTheOutputStreamAlongWithTheCount() {
312282

313283
String fileConvertedToString = this.dataOutputStream.toString(StandardCharsets.UTF_8);
314284

315-
assertThat(fileContentToBeWritten.trim()).isEqualTo(fileConvertedToString);
285+
assertThat(fileConvertedToString).isEqualTo(fileContentToBeWritten.trim());
316286
assertThat(fileOperations.getWriteOperationCount()).isEqualTo(1);
317287

318288
} catch (IOException ioException) {
@@ -334,26 +304,15 @@ void verifyReadOperationStats() {
334304
fileOperations.read();
335305
fileOperations.read(dataRead, 0, 5);
336306

337-
assertThat(4).isEqualTo(fileOperations.getReadOperationCount());
338-
assertThat(8).isEqualTo(fileOperations.getBytesRead());
307+
assertThat(fileOperations.getReadOperationCount()).isEqualTo(4);
308+
assertThat(fileOperations.getBytesRead()).isEqualTo(8);
339309

340310
} catch (IOException ioException) {
341311
ioException.printStackTrace();
342312
}
343313

344314
}
345315

346-
347-
// @Test
348-
// @Tag("fileOperation")
349-
// void verifyIfMethodThrowsExceptionIfObjectIsNull(){
350-
//
351-
// assertThatThrownBy(()->{
352-
// Paasio fileOperations = new Paasio(new FileInputStream(tmpFile), new FileOutputStream(tmpFile,true));
353-
// }).isInstanceOf(RuntimeException.class);
354-
// }
355-
356-
357316
@Test
358317
public void checkBytesOfDataWrittenAlongWithWriteOperationCount() {
359318

@@ -375,12 +334,4 @@ public void checkBytesOfDataWrittenAlongWithWriteOperationCount() {
375334
}
376335

377336

378-
// @Test
379-
// @Tag("socketOperation")
380-
// void verifyIfMethodThrowsExceptionIfSocketObjectIsNull(){
381-
//
382-
// assertThatThrownBy(()->{
383-
// SocketOperations fileOperations = new SocketOperations(null);
384-
// }).isInstanceOf(RuntimeException.class);
385-
// }
386337
}

0 commit comments

Comments
 (0)