Skip to content

Commit 43d9bf5

Browse files
committed
Merge branch 'master' into BAEL-1105
2 parents 9308bae + bcc122b commit 43d9bf5

28 files changed

Lines changed: 632 additions & 72 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.concurrent.volatilekeyword;
2+
3+
4+
public class SharedObject {
5+
private volatile int count=0;
6+
7+
public void increamentCount(){
8+
count++;
9+
}
10+
public int getCount(){
11+
return count;
12+
}
13+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.baeldung.concurrent.volatilekeyword;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import static junit.framework.Assert.assertEquals;
8+
9+
public class SharedObjectManualTest {
10+
11+
SharedObject sharedObject;
12+
int valueReadByThread2;
13+
int valueReadByThread3;
14+
15+
@Before
16+
public void setUp(){
17+
sharedObject = new SharedObject();
18+
}
19+
20+
@Test
21+
public void whenOneThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
22+
Thread writer = new Thread(){
23+
public void run(){
24+
sharedObject.increamentCount();
25+
}
26+
};
27+
writer.start();
28+
29+
30+
Thread readerOne = new Thread(){
31+
public void run(){
32+
try {
33+
Thread.sleep(1000);
34+
} catch (InterruptedException e) {
35+
e.printStackTrace();
36+
}
37+
valueReadByThread2= sharedObject.getCount();
38+
}
39+
};
40+
readerOne.start();
41+
42+
Thread readerTwo = new Thread(){
43+
public void run(){
44+
try {
45+
Thread.sleep(1000);
46+
} catch (InterruptedException e) {
47+
e.printStackTrace();
48+
}
49+
valueReadByThread3=sharedObject.getCount();
50+
}
51+
};
52+
readerTwo.start();
53+
54+
assertEquals(1,valueReadByThread2);
55+
assertEquals(1,valueReadByThread3);
56+
57+
}
58+
59+
@Test
60+
public void whenTwoThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
61+
Thread writerOne = new Thread(){
62+
public void run(){
63+
sharedObject.increamentCount();
64+
}
65+
};
66+
writerOne.start();
67+
Thread.sleep(100);
68+
69+
Thread writerTwo = new Thread(){
70+
public void run(){
71+
sharedObject.increamentCount();
72+
}
73+
};
74+
writerTwo.start();
75+
Thread.sleep(100);
76+
77+
Thread readerOne = new Thread(){
78+
public void run(){
79+
valueReadByThread2= sharedObject.getCount();
80+
}
81+
};
82+
readerOne.start();
83+
84+
Thread readerTwo = new Thread(){
85+
public void run(){
86+
valueReadByThread3=sharedObject.getCount();
87+
}
88+
};
89+
readerTwo.start();
90+
91+
assertEquals(2,valueReadByThread2);
92+
assertEquals(2,valueReadByThread3);
93+
94+
}
95+
@After
96+
public void cleanup(){
97+
sharedObject = null;
98+
}
99+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.baeldung.javanetworking.uriurl;
2+
3+
import java.io.IOException;
4+
import java.net.MalformedURLException;
5+
import java.net.URI;
6+
import java.net.URISyntaxException;
7+
import java.net.URL;
8+
import org.apache.commons.io.IOUtils;
9+
import static org.junit.Assert.*;
10+
import org.junit.Test;
11+
12+
public class URIvsURLUnitTest {
13+
14+
@Test
15+
public void whenCreatingURIs_thenSameInfo() throws URISyntaxException {
16+
URI firstURI = new URI("somescheme://theuser:thepassword@someauthority:80/some/path?thequery#somefragment");
17+
URI secondURI = new URI("somescheme", "theuser:thepassword", "someuthority", 80, "/some/path", "thequery", "somefragment");
18+
19+
assertEquals(firstURI.getScheme(), secondURI.getScheme());
20+
assertEquals(firstURI.getPath(), secondURI.getPath());
21+
}
22+
23+
@Test
24+
public void whenCreatingURLs_thenSameInfo() throws MalformedURLException {
25+
URL firstURL = new URL("http://theuser:thepassword@somehost:80/path/to/file?thequery#somefragment");
26+
URL secondURL = new URL("http", "somehost", 80, "/path/to/file");
27+
28+
assertEquals(firstURL.getHost(), secondURL.getHost());
29+
assertEquals(firstURL.getPath(), secondURL.getPath());
30+
}
31+
32+
@Test
33+
public void whenCreatingURI_thenCorrect() {
34+
URI uri = URI.create("urn:isbn:1234567890");
35+
36+
assertNotNull(uri);
37+
}
38+
39+
@Test(expected = MalformedURLException.class)
40+
public void whenCreatingURLs_thenException() throws MalformedURLException {
41+
URL theURL = new URL("otherprotocol://somehost/path/to/file");
42+
43+
assertNotNull(theURL);
44+
}
45+
46+
@Test
47+
public void givenObjects_whenConverting_thenCorrect() throws MalformedURLException, URISyntaxException {
48+
String aURIString = "http://somehost:80/path?thequery";
49+
URI uri = new URI(aURIString);
50+
URL url = new URL(aURIString);
51+
52+
URL toURL = uri.toURL();
53+
URI toURI = url.toURI();
54+
55+
assertNotNull(url);
56+
assertNotNull(uri);
57+
assertEquals(toURL.toString(), toURI.toString());
58+
}
59+
60+
@Test(expected = MalformedURLException.class)
61+
public void givenURI_whenConvertingToURL_thenException() throws MalformedURLException, URISyntaxException {
62+
URI uri = new URI("somescheme://someauthority/path?thequery");
63+
64+
URL url = uri.toURL();
65+
66+
assertNotNull(url);
67+
}
68+
69+
@Test
70+
public void givenURL_whenGettingContents_thenCorrect() throws MalformedURLException, IOException {
71+
URL url = new URL("http://courses.baeldung.com");
72+
73+
String contents = IOUtils.toString(url.openStream());
74+
75+
assertTrue(contents.contains("<!DOCTYPE html>"));
76+
}
77+
78+
}

‎jackson/src/test/java/com/baeldung/jackson/test/JacksonDeserializationUnitTest.java‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
package com.baeldung.jackson.test;
22

3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.hamcrest.CoreMatchers.not;
35
import static org.hamcrest.Matchers.equalTo;
46
import static org.hamcrest.Matchers.notNullValue;
57
import static org.junit.Assert.assertNotNull;
68
import static org.junit.Assert.assertThat;
79

810
import java.io.IOException;
11+
import java.time.ZoneId;
12+
import java.time.ZonedDateTime;
13+
14+
import org.junit.Test;
915

1016
import com.baeldung.jackson.deserialization.ItemDeserializer;
1117
import com.baeldung.jackson.dtos.Item;
1218
import com.baeldung.jackson.dtos.ItemWithSerializer;
1319
import com.baeldung.jackson.dtos.MyDto;
1420
import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreUnknown;
15-
import org.junit.Test;
16-
1721
import com.fasterxml.jackson.core.JsonFactory;
1822
import com.fasterxml.jackson.core.JsonParseException;
1923
import com.fasterxml.jackson.core.JsonParser;
2024
import com.fasterxml.jackson.databind.DeserializationFeature;
2125
import com.fasterxml.jackson.databind.JsonMappingException;
2226
import com.fasterxml.jackson.databind.JsonNode;
2327
import com.fasterxml.jackson.databind.ObjectMapper;
28+
import com.fasterxml.jackson.databind.SerializationFeature;
2429
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
2530
import com.fasterxml.jackson.databind.module.SimpleModule;
2631

@@ -165,4 +170,35 @@ public final void givenDeserializerIsOnClass_whenDeserializingCustomRepresentati
165170
assertThat(readValue, notNullValue());
166171
}
167172

173+
@Test
174+
public void whenDeserialisingZonedDateTimeWithDefaults_thenTimeZoneIsNotPreserved() throws IOException {
175+
ObjectMapper objectMapper = new ObjectMapper();
176+
objectMapper.findAndRegisterModules();
177+
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
178+
// construct a new instance of ZonedDateTime
179+
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));
180+
String converted = objectMapper.writeValueAsString(now);
181+
// restore an instance of ZonedDateTime from String
182+
ZonedDateTime restored = objectMapper.readValue(converted, ZonedDateTime.class);
183+
System.out.println("serialized: " + now);
184+
System.out.println("restored: " + restored);
185+
assertThat(now, is(not(restored)));
186+
}
187+
188+
@Test
189+
public void whenDeserialisingZonedDateTimeWithFeaturesDisabled_thenTimeZoneIsPreserved() throws IOException {
190+
ObjectMapper objectMapper = new ObjectMapper();
191+
objectMapper.findAndRegisterModules();
192+
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
193+
objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
194+
// construct a new instance of ZonedDateTime
195+
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));
196+
String converted = objectMapper.writeValueAsString(now);
197+
// restore an instance of ZonedDateTime from String
198+
ZonedDateTime restored = objectMapper.readValue(converted, ZonedDateTime.class);
199+
System.out.println("serialized: " + now);
200+
System.out.println("restored: " + restored);
201+
assertThat(now, is(restored));
202+
}
203+
168204
}

spring-batch/src/main/java/org/baeldung/spring_batch_intro/App.java renamed to spring-batch/src/main/java/org/baeldung/batch/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.baeldung.spring_batch_intro;
1+
package org.baeldung.batch;
22

33
import org.springframework.batch.core.Job;
44
import org.springframework.batch.core.JobExecution;

spring-batch/src/main/java/org/baeldung/spring_batch_intro/SpringBatchConfig.java renamed to spring-batch/src/main/java/org/baeldung/batch/SpringBatchConfig.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
package org.baeldung.spring_batch_intro;
1+
package org.baeldung.batch;
22

3-
import java.net.MalformedURLException;
4-
import java.text.ParseException;
5-
6-
import org.baeldung.spring_batch_intro.model.Transaction;
7-
import org.baeldung.spring_batch_intro.service.CustomItemProcessor;
8-
import org.baeldung.spring_batch_intro.service.RecordFieldSetMapper;
3+
import org.baeldung.batch.model.Transaction;
4+
import org.baeldung.batch.service.CustomItemProcessor;
5+
import org.baeldung.batch.service.RecordFieldSetMapper;
96
import org.springframework.batch.core.Job;
107
import org.springframework.batch.core.Step;
118
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
@@ -26,6 +23,9 @@
2623
import org.springframework.oxm.Marshaller;
2724
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
2825

26+
import java.net.MalformedURLException;
27+
import java.text.ParseException;
28+
2929
public class SpringBatchConfig {
3030
@Autowired
3131
private JobBuilderFactory jobs;
@@ -43,7 +43,7 @@ public class SpringBatchConfig {
4343
public ItemReader<Transaction> itemReader() throws UnexpectedInputException, ParseException {
4444
FlatFileItemReader<Transaction> reader = new FlatFileItemReader<Transaction>();
4545
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
46-
String[] tokens = { "username", "userid", "transactiondate", "amount" };
46+
String[] tokens = {"username", "userid", "transactiondate", "amount"};
4747
tokenizer.setNames(tokens);
4848
reader.setResource(inputCsv);
4949
DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<Transaction>();
@@ -71,13 +71,13 @@ public ItemWriter<Transaction> itemWriter(Marshaller marshaller) throws Malforme
7171
@Bean
7272
public Marshaller marshaller() {
7373
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
74-
marshaller.setClassesToBeBound(new Class[] { Transaction.class });
74+
marshaller.setClassesToBeBound(Transaction.class);
7575
return marshaller;
7676
}
7777

7878
@Bean
7979
protected Step step1(ItemReader<Transaction> reader, ItemProcessor<Transaction, Transaction> processor, ItemWriter<Transaction> writer) {
80-
return steps.get("step1").<Transaction, Transaction> chunk(10).reader(reader).processor(processor).writer(writer).build();
80+
return steps.get("step1").<Transaction, Transaction>chunk(10).reader(reader).processor(processor).writer(writer).build();
8181
}
8282

8383
@Bean(name = "firstBatchJob")

spring-batch/src/main/java/org/baeldung/spring_batch_intro/SpringConfig.java renamed to spring-batch/src/main/java/org/baeldung/batch/SpringConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.baeldung.spring_batch_intro;
1+
package org.baeldung.batch;
22

33
import java.net.MalformedURLException;
44

spring-batch/src/main/java/org/baeldung/spring_batch_intro/model/Transaction.java renamed to spring-batch/src/main/java/org/baeldung/batch/model/Transaction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.baeldung.spring_batch_intro.model;
1+
package org.baeldung.batch.model;
22

33
import java.util.Date;
44

spring-batch/src/main/java/org/baeldung/spring_batch_intro/partitioner/CustomMultiResourcePartitioner.java renamed to spring-batch/src/main/java/org/baeldung/batch/partitioner/CustomMultiResourcePartitioner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.baeldung.spring_batch_intro.partitioner;
17+
package org.baeldung.batch.partitioner;
1818

1919
import java.util.HashMap;
2020
import java.util.Map;

0 commit comments

Comments
 (0)