Skip to content

Commit ce418d1

Browse files
Merge pull request eugenp#2 from eugenp/master
Merge
2 parents 1403edc + 275a4ac commit ce418d1

File tree

90 files changed

+1600
-311
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+1600
-311
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package com.baeldung.filechannel;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.FileInputStream;
8+
import java.io.FileOutputStream;
9+
import java.io.IOException;
10+
import java.io.RandomAccessFile;
11+
import java.nio.ByteBuffer;
12+
import java.nio.MappedByteBuffer;
13+
import java.nio.channels.FileChannel;
14+
import java.nio.channels.FileLock;
15+
import java.nio.charset.StandardCharsets;
16+
17+
import org.junit.Test;
18+
19+
public class FileChannelUnitTest {
20+
21+
@Test
22+
public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
23+
24+
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
25+
FileChannel channel = reader.getChannel();
26+
ByteArrayOutputStream out = new ByteArrayOutputStream();) {
27+
28+
int bufferSize = 1024;
29+
if (bufferSize > channel.size()) {
30+
bufferSize = (int) channel.size();
31+
}
32+
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
33+
34+
while (channel.read(buff) > 0) {
35+
out.write(buff.array(), 0, buff.position());
36+
buff.clear();
37+
}
38+
39+
String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8);
40+
41+
assertEquals("Hello world", fileContent);
42+
}
43+
}
44+
45+
@Test
46+
public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException {
47+
48+
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
49+
FileInputStream fin = new FileInputStream("src/test/resources/test_read.in");
50+
FileChannel channel = fin.getChannel();) {
51+
52+
int bufferSize = 1024;
53+
if (bufferSize > channel.size()) {
54+
bufferSize = (int) channel.size();
55+
}
56+
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
57+
58+
while (channel.read(buff) > 0) {
59+
out.write(buff.array(), 0, buff.position());
60+
buff.clear();
61+
}
62+
String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8);
63+
64+
assertEquals("Hello world", fileContent);
65+
}
66+
}
67+
68+
@Test
69+
public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException {
70+
71+
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
72+
FileChannel channel = reader.getChannel();
73+
ByteArrayOutputStream out = new ByteArrayOutputStream();) {
74+
75+
MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5);
76+
77+
if (buff.hasRemaining()) {
78+
byte[] data = new byte[buff.remaining()];
79+
buff.get(data);
80+
assertEquals("world", new String(data, StandardCharsets.UTF_8));
81+
}
82+
}
83+
}
84+
85+
@Test
86+
public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
87+
String file = "src/test/resources/test_write_using_filechannel.txt";
88+
try (RandomAccessFile writer = new RandomAccessFile(file, "rw");
89+
FileChannel channel = writer.getChannel();) {
90+
ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8));
91+
92+
channel.write(buff);
93+
94+
// now we verify whether the file was written correctly
95+
RandomAccessFile reader = new RandomAccessFile(file, "r");
96+
assertEquals("Hello world", reader.readLine());
97+
reader.close();
98+
}
99+
}
100+
101+
@Test
102+
public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException {
103+
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "rw");
104+
FileChannel channel = reader.getChannel();
105+
FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE);) {
106+
107+
assertNotNull(fileLock);
108+
}
109+
}
110+
111+
@Test
112+
public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException {
113+
114+
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
115+
RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
116+
FileChannel channel = reader.getChannel();) {
117+
118+
int bufferSize = 1024;
119+
if (bufferSize > channel.size()) {
120+
bufferSize = (int) channel.size();
121+
}
122+
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
123+
124+
while (channel.read(buff) > 0) {
125+
out.write(buff.array(), 0, buff.position());
126+
buff.clear();
127+
}
128+
129+
// the original file is 11 bytes long, so that's where the position pointer should be
130+
assertEquals(11, channel.position());
131+
132+
channel.position(4);
133+
assertEquals(4, channel.position());
134+
}
135+
}
136+
137+
@Test
138+
public void whenGetFileSize_thenCorrect() throws IOException {
139+
140+
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
141+
FileChannel channel = reader.getChannel();) {
142+
143+
// the original file is 11 bytes long, so that's where the position pointer should be
144+
assertEquals(11, channel.size());
145+
}
146+
}
147+
148+
@Test
149+
public void whenTruncateFile_thenCorrect() throws IOException {
150+
String input = "this is a test input";
151+
152+
FileOutputStream fout = new FileOutputStream("src/test/resources/test_truncate.txt");
153+
FileChannel channel = fout.getChannel();
154+
155+
ByteBuffer buff = ByteBuffer.wrap(input.getBytes());
156+
channel.write(buff);
157+
buff.flip();
158+
159+
channel = channel.truncate(5);
160+
assertEquals(5, channel.size());
161+
162+
fout.close();
163+
channel.close();
164+
}
165+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world

guava-collections-set/.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
/neoDb*
6+
/data
7+
/src/main/webapp/WEB-INF/classes
8+
*/META-INF/*
9+
10+
# Packaged files #
11+
*.jar
12+
*.war
13+
*.ear

guava-collections-set/pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>guava-collections-set</artifactId>
6+
<version>0.1.0-SNAPSHOT</version>
7+
<name>guava-collections-set</name>
8+
9+
<parent>
10+
<groupId>com.baeldung</groupId>
11+
<artifactId>parent-java</artifactId>
12+
<version>0.0.1-SNAPSHOT</version>
13+
<relativePath>../parent-java</relativePath>
14+
</parent>
15+
16+
<dependencies>
17+
<!-- test scoped -->
18+
<dependency>
19+
<groupId>org.assertj</groupId>
20+
<artifactId>assertj-core</artifactId>
21+
<version>${assertj.version}</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<finalName>guava-collections-set</finalName>
28+
</build>
29+
30+
<properties>
31+
<!-- util -->
32+
<guava.version>27.1-jre</guava.version>
33+
<!-- testing -->
34+
<assertj.version>3.6.1</assertj.version>
35+
</properties>
36+
37+
</project>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.baeldung.guava;
2+
3+
import com.google.common.collect.HashMultiset;
4+
import com.google.common.collect.Multiset;
5+
import org.junit.Test;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
12+
13+
public class GuavaMultiSetUnitTest {
14+
15+
@Test
16+
public void givenMultiSet_whenAddingValues_shouldReturnCorrectCount() {
17+
Multiset<String> bookStore = HashMultiset.create();
18+
bookStore.add("Potter");
19+
bookStore.add("Potter");
20+
bookStore.add("Potter");
21+
22+
assertThat(bookStore.contains("Potter")).isTrue();
23+
assertThat(bookStore.count("Potter")).isEqualTo(3);
24+
}
25+
26+
@Test
27+
public void givenMultiSet_whenRemovingValues_shouldReturnCorrectCount() {
28+
Multiset<String> bookStore = HashMultiset.create();
29+
bookStore.add("Potter");
30+
bookStore.add("Potter");
31+
32+
bookStore.remove("Potter");
33+
assertThat(bookStore.contains("Potter")).isTrue();
34+
assertThat(bookStore.count("Potter")).isEqualTo(1);
35+
}
36+
37+
@Test
38+
public void givenMultiSet_whenSetCount_shouldReturnCorrectCount() {
39+
Multiset<String> bookStore = HashMultiset.create();
40+
bookStore.setCount("Potter", 50);
41+
assertThat(bookStore.count("Potter")).isEqualTo(50);
42+
}
43+
44+
@Test
45+
public void givenMultiSet_whenSettingNegativeCount_shouldThrowException() {
46+
Multiset<String> bookStore = HashMultiset.create();
47+
assertThatThrownBy(() -> bookStore.setCount("Potter", -1))
48+
.isInstanceOf(IllegalArgumentException.class);
49+
}
50+
51+
@Test
52+
public void givenMultiSet_whenSettingCountWithEmptySet_shouldBeSuccessful() {
53+
Multiset<String> bookStore = HashMultiset.create();
54+
assertThat(bookStore.setCount("Potter", 0, 2)).isTrue();
55+
}
56+
57+
@Test
58+
public void givenMultiSet_whenSettingCountWithCorrectValue_shouldBeSuccessful() {
59+
Multiset<String> bookStore = HashMultiset.create();
60+
bookStore.add("Potter");
61+
bookStore.add("Potter");
62+
63+
assertThat(bookStore.setCount("Potter", 2, 52)).isTrue();
64+
}
65+
66+
@Test
67+
public void givenMultiSet_whenSettingCountWithIncorrectValue_shouldFail() {
68+
Multiset<String> bookStore = HashMultiset.create();
69+
bookStore.add("Potter");
70+
bookStore.add("Potter");
71+
72+
assertThat(bookStore.setCount("Potter", 5, 52)).isFalse();
73+
}
74+
75+
@Test
76+
public void givenMap_compareMultiSetOperations() {
77+
Map<String, Integer> bookStore = new HashMap<>();
78+
bookStore.put("Potter", 3);
79+
80+
assertThat(bookStore.containsKey("Potter")).isTrue();
81+
assertThat(bookStore.get("Potter")).isEqualTo(3);
82+
83+
bookStore.put("Potter", 2);
84+
assertThat(bookStore.get("Potter")).isEqualTo(2);
85+
86+
bookStore.put("Potter", null);
87+
assertThat(bookStore.containsKey("Potter")).isTrue();
88+
89+
bookStore.put("Potter", -1);
90+
assertThat(bookStore.containsKey("Potter")).isTrue();
91+
}
92+
}

jackson-simple/.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
/neoDb*
6+
/data
7+
/src/main/webapp/WEB-INF/classes
8+
*/META-INF/*
9+
10+
# Packaged files #
11+
*.jar
12+
*.war
13+
*.ear

jackson-simple/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=========
2+
### Jackson Articles that are also part of the e-book
3+
4+
###The Course
5+
The "REST With Spring" Classes: http://bit.ly/restwithspring
6+
7+
### Relevant Articles:
8+
- [Jackson Ignore Properties on Marshalling](http://www.baeldung.com/jackson-ignore-properties-on-serialization)
9+
- [Jackson Unmarshalling json with Unknown Properties](http://www.baeldung.com/jackson-deserialize-json-unknown-properties)
10+
- [Jackson Annotation Examples](http://www.baeldung.com/jackson-annotations)
11+
- [Intro to the Jackson ObjectMapper](http://www.baeldung.com/jackson-object-mapper-tutorial)
12+
- [Ignore Null Fields with Jackson](http://www.baeldung.com/jackson-ignore-null-fields)
13+
- [Jackson – Change Name of Field](http://www.baeldung.com/jackson-name-of-property)

0 commit comments

Comments
 (0)