Skip to content

Commit 91b1b3c

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents ebf3387 + b5c733d commit 91b1b3c

32 files changed

Lines changed: 823 additions & 112 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.generics;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class Generics {
8+
9+
// definition of a generic method
10+
public static <T> List<T> fromArrayToList(T[] a) {
11+
List<T> list = new ArrayList<>();
12+
Arrays.stream(a).forEach(list::add);
13+
return list;
14+
}
15+
16+
// example of a generic method that has Number as an upper bound for T
17+
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
18+
List<T> list = new ArrayList<>();
19+
Arrays.stream(a).forEach(list::add);
20+
return list;
21+
}
22+
23+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.generics;
2+
3+
import org.junit.Test;
4+
5+
import java.util.List;
6+
7+
import static org.hamcrest.CoreMatchers.hasItems;
8+
import static org.hamcrest.MatcherAssert.assertThat;
9+
10+
public class GenericsTest {
11+
12+
// testing the generic method with Integer
13+
@Test
14+
public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
15+
Integer[] intArray = {1, 2, 3, 4, 5};
16+
List<Integer> list = Generics.fromArrayToList(intArray);
17+
18+
assertThat(list, hasItems(intArray));
19+
}
20+
21+
// testing the generic method with String
22+
@Test
23+
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
24+
String[] stringArray = {"hello1", "hello2", "hello3", "hello4", "hello5"};
25+
List<String> list = Generics.fromArrayToList(stringArray);
26+
27+
assertThat(list, hasItems(stringArray));
28+
}
29+
30+
// testing the generic method with Number as upper bound with Integer
31+
// if we test fromArrayToListWithUpperBound with any type that doesn't
32+
// extend Number it will fail to compile
33+
@Test
34+
public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() {
35+
Integer[] intArray = {1, 2, 3, 4, 5};
36+
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
37+
38+
assertThat(list, hasItems(intArray));
39+
}
40+
41+
}
Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,141 @@
11
package org.baeldung.java.io;
22

3-
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
4-
import static org.junit.Assert.assertTrue;
3+
import org.apache.commons.io.FileUtils;
4+
import org.junit.AfterClass;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
57

68
import java.io.File;
79
import java.io.IOException;
810
import java.nio.file.FileSystemException;
911
import java.nio.file.Files;
1012
import java.nio.file.Path;
1113
import java.nio.file.Paths;
14+
import java.util.UUID;
1215

13-
import org.apache.commons.io.FileUtils;
14-
import org.junit.Test;
16+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
17+
import static org.junit.Assert.assertTrue;
1518

1619
public class JavaFileUnitTest {
1720

18-
// create a file
21+
private static final String TEMP_DIR = "src/test/resources/temp" + UUID.randomUUID().toString();
22+
23+
24+
@BeforeClass
25+
public static void setup() throws IOException {
26+
Files.createDirectory(Paths.get(TEMP_DIR));
27+
}
28+
29+
@AfterClass
30+
public static void cleanup() throws IOException {
31+
FileUtils.deleteDirectory(new File(TEMP_DIR));
32+
}
1933

2034
@Test
2135
public final void givenUsingJDK6_whenCreatingFile_thenCorrect() throws IOException {
22-
final File newFile = new File("src/test/resources/newFile_jdk6.txt");
36+
final File newFile = new File(TEMP_DIR + "/newFile_jdk6.txt");
2337
final boolean success = newFile.createNewFile();
2438

2539
assertTrue(success);
2640
}
2741

2842
@Test
2943
public final void givenUsingJDK7nio2_whenCreatingFile_thenCorrect() throws IOException {
30-
final Path newFilePath = Paths.get("src/test/resources/newFile_jdk7.txt");
44+
final Path newFilePath = Paths.get(TEMP_DIR + "/newFile_jdk7.txt");
3145
Files.createFile(newFilePath);
3246
}
3347

3448
@Test
3549
public final void givenUsingCommonsIo_whenCreatingFile_thenCorrect() throws IOException {
36-
FileUtils.touch(new File("src/test/resources/newFile_commonsio.txt"));
50+
FileUtils.touch(new File(TEMP_DIR + "/newFile_commonsio.txt"));
3751
}
3852

3953
@Test
4054
public final void givenUsingGuava_whenCreatingFile_thenCorrect() throws IOException {
41-
com.google.common.io.Files.touch(new File("src/test/resources/newFile_guava.txt"));
55+
com.google.common.io.Files.touch(new File(TEMP_DIR + "/newFile_guava.txt"));
4256
}
4357

4458
// move a file
4559

4660
@Test
4761
public final void givenUsingJDK6_whenMovingFile_thenCorrect() throws IOException {
48-
final File fileToMove = new File("src/test/resources/toMoveFile_jdk6.txt");
62+
final File fileToMove = new File(TEMP_DIR + "/toMoveFile_jdk6.txt");
4963
fileToMove.createNewFile();// .exists();
50-
final File destDir = new File("src/test/resources/");
64+
final File destDir = new File(TEMP_DIR + "/");
5165
destDir.mkdir();
5266

53-
final boolean isMoved = fileToMove.renameTo(new File("src/test/resources/movedFile_jdk6.txt"));
67+
final boolean isMoved = fileToMove.renameTo(new File(TEMP_DIR + "/movedFile_jdk6.txt"));
5468
if (!isMoved) {
55-
throw new FileSystemException("src/test/resources/movedFile_jdk6.txt");
69+
throw new FileSystemException(TEMP_DIR + "/movedFile_jdk6.txt");
5670
}
5771
}
5872

5973
@Test
6074
public final void givenUsingJDK7Nio2_whenMovingFile_thenCorrect() throws IOException {
61-
final Path fileToMovePath = Files.createFile(Paths.get("src/test/resources/" + randomAlphabetic(5) + ".txt"));
62-
final Path targetPath = Paths.get("src/main/resources/");
75+
final Path fileToMovePath = Files.createFile(Paths.get(TEMP_DIR + "/" + randomAlphabetic(5) + ".txt"));
76+
final Path targetPath = Paths.get(TEMP_DIR + "/");
6377

6478
Files.move(fileToMovePath, targetPath.resolve(fileToMovePath.getFileName()));
6579
}
6680

6781
@Test
6882
public final void givenUsingGuava_whenMovingFile_thenCorrect() throws IOException {
69-
final File fileToMove = new File("src/test/resources/fileToMove.txt");
83+
final File fileToMove = new File(TEMP_DIR + "/fileToMove.txt");
7084
fileToMove.createNewFile();
71-
final File destDir = new File("src/main/resources/");
85+
final File destDir = new File(TEMP_DIR + "/temp");
7286
final File targetFile = new File(destDir, fileToMove.getName());
7387
com.google.common.io.Files.createParentDirs(targetFile);
7488
com.google.common.io.Files.move(fileToMove, targetFile);
7589
}
7690

7791
@Test
7892
public final void givenUsingApache_whenMovingFile_thenCorrect() throws IOException {
79-
FileUtils.touch(new File("src/test/resources/fileToMove_apache.txt"));
80-
FileUtils.moveFile(FileUtils.getFile("src/test/resources/fileToMove_apache.txt"), FileUtils.getFile("src/test/resources/fileMoved_apache2.txt"));
93+
FileUtils.touch(new File(TEMP_DIR + "/fileToMove_apache.txt"));
94+
FileUtils.moveFile(FileUtils.getFile(TEMP_DIR + "/fileToMove_apache.txt"), FileUtils.getFile(TEMP_DIR + "/fileMoved_apache2.txt"));
8195
}
8296

8397
@Test
8498
public final void givenUsingApache_whenMovingFileApproach2_thenCorrect() throws IOException {
85-
FileUtils.touch(new File("src/test/resources/fileToMove_apache.txt"));
86-
FileUtils.moveFileToDirectory(FileUtils.getFile("src/test/resources/fileToMove_apache.txt"), FileUtils.getFile("src/main/resources/"), true);
99+
FileUtils.touch(new File(TEMP_DIR + "/fileToMove_apache.txt"));
100+
Files.createDirectory(Paths.get(TEMP_DIR + "/temp"));
101+
FileUtils.moveFileToDirectory(FileUtils.getFile(TEMP_DIR + "/fileToMove_apache.txt"), FileUtils.getFile(TEMP_DIR + "/temp"), true);
87102
}
88103

89104
// delete a file
90105

91106
@Test
92107
public final void givenUsingJDK6_whenDeletingAFile_thenCorrect() throws IOException {
93-
new File("src/test/resources/fileToDelete_jdk6.txt").createNewFile();
108+
new File(TEMP_DIR + "/fileToDelete_jdk6.txt").createNewFile();
94109

95-
final File fileToDelete = new File("src/test/resources/fileToDelete_jdk6.txt");
110+
final File fileToDelete = new File(TEMP_DIR + "/fileToDelete_jdk6.txt");
96111
final boolean success = fileToDelete.delete();
97112

98113
assertTrue(success);
99114
}
100115

101116
@Test
102117
public final void givenUsingJDK7nio2_whenDeletingAFile_thenCorrect() throws IOException {
103-
Files.createFile(Paths.get("src/test/resources/fileToDelete_jdk7.txt"));
118+
Files.createFile(Paths.get(TEMP_DIR + "/fileToDelete_jdk7.txt"));
104119

105-
final Path fileToDeletePath = Paths.get("src/test/resources/fileToDelete_jdk7.txt");
120+
final Path fileToDeletePath = Paths.get(TEMP_DIR + "/fileToDelete_jdk7.txt");
106121
Files.delete(fileToDeletePath);
107122
}
108123

109124
@Test
110125
public final void givenUsingCommonsIo_whenDeletingAFileV1_thenCorrect() throws IOException {
111-
FileUtils.touch(new File("src/test/resources/fileToDelete_commonsIo.txt"));
126+
FileUtils.touch(new File(TEMP_DIR + "/fileToDelete_commonsIo.txt"));
112127

113-
final File fileToDelete = FileUtils.getFile("src/test/resources/fileToDelete_commonsIo.txt");
128+
final File fileToDelete = FileUtils.getFile(TEMP_DIR + "/fileToDelete_commonsIo.txt");
114129
final boolean success = FileUtils.deleteQuietly(fileToDelete);
115130

116131
assertTrue(success);
117132
}
118133

119134
@Test
120135
public void givenUsingCommonsIo_whenDeletingAFileV2_thenCorrect() throws IOException {
121-
FileUtils.touch(new File("src/test/resources/fileToDelete.txt"));
136+
FileUtils.touch(new File(TEMP_DIR + "/fileToDelete.txt"));
122137

123-
FileUtils.forceDelete(FileUtils.getFile("src/test/resources/fileToDelete.txt"));
138+
FileUtils.forceDelete(FileUtils.getFile(TEMP_DIR + "/fileToDelete.txt"));
124139
}
125140

126141
}

‎ejb/ejb-client/pom.xml‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
<?xml version="1.0"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
5-
<parent>
6-
<groupId>com.baeldung.ejb</groupId>
7-
<artifactId>ejb</artifactId>
8-
<version>1.0-SNAPSHOT</version>
9-
</parent>
10-
<artifactId>ejb-client</artifactId>
11-
<name>EJB3 Client Maven</name>
12-
<description>EJB3 Client Maven</description>
13-
<properties>
14-
<junit.version>4.12</junit.version>
15-
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
16-
</properties>
17-
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.baeldung.ejb</groupId>
7+
<artifactId>ejb</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>ejb-client</artifactId>
11+
<name>EJB3 Client Maven</name>
12+
<description>EJB3 Client Maven</description>
1813
<dependencies>
1914
<dependency>
2015
<groupId>org.wildfly</groupId>
@@ -49,6 +44,4 @@
4944
</plugin>
5045
</plugins>
5146
</build>
52-
53-
5447
</project>

‎ejb/ejb-client/src/main/resources/jboss-ejb-client.properties‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMO
55
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT=false
66
remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS=${host.auth:JBOSS-LOCAL-USER}
77
remote.connection.default.username=testUser
8-
remote.connection.default.password=admin1234!
8+
remote.connection.default.password=admin1234!

‎ejb/ejb-remote/pom.xml‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212

1313
<!-- <name>ejb-remote</name> -->
1414
<dependencies>
15-
<dependency>
16-
<groupId>org.jboss.spec.javax.ejb</groupId>
17-
<artifactId>jboss-ejb-api_3.2_spec</artifactId>
18-
<scope>provided</scope>
19-
</dependency>
15+
<dependency>
16+
<groupId>javax</groupId>
17+
<artifactId>javaee-api</artifactId>
18+
<version>7.0</version>
19+
<scope>provided</scope>
20+
</dependency>
2021
</dependencies>
2122

2223
<build>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package com.baeldung.ejb.tutorial;
22

3+
import javax.annotation.Resource;
4+
import javax.ejb.SessionContext;
35
import javax.ejb.Stateless;
46

57
@Stateless(name = "HelloWorld")
68
public class HelloWorldBean implements HelloWorld {
79

10+
@Resource
11+
private SessionContext context;
12+
813
@Override
914
public String getHelloWorld() {
1015
return "Welcome to EJB Tutorial!";
1116
}
1217

18+
1319
}

‎ejb/pom.xml‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@
3636
</dependency>
3737

3838
<dependency>
39-
<groupId>org.jboss.spec</groupId>
40-
<artifactId>jboss-javaee-7.0</artifactId>
41-
<version>1.0.1.Final</version>
42-
<type>pom</type>
43-
<scope>import</scope>
39+
<groupId>javax</groupId>
40+
<artifactId>javaee-api</artifactId>
41+
<version>7.0</version>
42+
<scope>provided</scope>
4443
</dependency>
4544

4645
<dependency>

‎spring-core/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
### Relevant Articles:
22
- [Wiring in Spring: @Autowired, @Resource and @Inject](http://www.baeldung.com/spring-annotations-resource-inject-autowire)
3+
- [Exploring the Spring BeanFactory API](http://www.baeldung.com/spring-beanfactory)

‎spring-core/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
<version>4.12</version>
4848
<scope>test</scope>
4949
</dependency>
50+
<dependency>
51+
<groupId>com.google.guava</groupId>
52+
<artifactId>guava</artifactId>
53+
<version>20.0</version>
54+
</dependency>
5055
</dependencies>
5156

5257
<build>

0 commit comments

Comments
 (0)