Merge branch 'master' into pr/858-tim
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.java.nio2.visitor;
|
||||
|
||||
import static java.nio.file.FileVisitResult.CONTINUE;
|
||||
import static java.nio.file.FileVisitResult.TERMINATE;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.FileVisitor;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
|
||||
public class FileSearchExample implements FileVisitor<Path> {
|
||||
private static String FILE_NAME;
|
||||
private static Path START_DIR;
|
||||
|
||||
public FileSearchExample(String fileName, Path startingDir) {
|
||||
FILE_NAME = fileName;
|
||||
START_DIR = startingDir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
||||
return CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||
String fileName = file.getFileName().toString();
|
||||
if (FILE_NAME.equals(fileName)) {
|
||||
System.out.println("File found: " + file.toString());
|
||||
return TERMINATE;
|
||||
}
|
||||
return CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
|
||||
System.out.println("Failed to access file: " + file.toString());
|
||||
return CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
||||
boolean finishedSearch = Files.isSameFile(dir, START_DIR);
|
||||
if (finishedSearch) {
|
||||
System.out.println("File:" + FILE_NAME + " not found");
|
||||
return TERMINATE;
|
||||
}
|
||||
return CONTINUE;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
Path startingDir = Paths.get("C:/Users/new/Desktop");
|
||||
FileSearchExample crawler = new FileSearchExample("hibernate.txt", startingDir);
|
||||
Files.walkFileTree(startingDir, crawler);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.java.nio2.visitor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.FileVisitor;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
|
||||
public class FileVisitorImpl implements FileVisitor<Path> {
|
||||
|
||||
@Override
|
||||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFileFailed(Path file, IOException exc) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.java.nio2.watcher;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardWatchEventKinds;
|
||||
import java.nio.file.WatchEvent;
|
||||
import java.nio.file.WatchKey;
|
||||
import java.nio.file.WatchService;
|
||||
|
||||
public class DirectoryWatcherExample {
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
WatchService watchService = FileSystems.getDefault().newWatchService();
|
||||
Path path = Paths.get(System.getProperty("user.home"));
|
||||
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
|
||||
WatchKey key;
|
||||
while ((key = watchService.take()) != null) {
|
||||
for (WatchEvent<?> event : key.pollEvents()) {
|
||||
System.out.println("Event kind:" + event.kind() + ". File affected: " + event.context() + ".");
|
||||
}
|
||||
key.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
package com.baeldung.file;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
@@ -14,12 +15,6 @@ import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FileOperationsUnitTest {
|
||||
|
||||
@Test
|
||||
@@ -58,9 +53,9 @@ public class FileOperationsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenURLName_whenUsingURL_thenFileData() throws IOException {
|
||||
String expectedData = "Baeldung";
|
||||
String expectedData = "Example Domain";
|
||||
|
||||
URL urlObject = new URL("http://www.baeldung.com/");
|
||||
URL urlObject = new URL("http://www.example.com/");
|
||||
|
||||
URLConnection urlConnection = urlObject.openConnection();
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.baeldung.java.nio2.async;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.AsynchronousSocketChannel;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
public class AsyncEchoClient {
|
||||
private AsynchronousSocketChannel client;
|
||||
private Future<Void> future;
|
||||
private static AsyncEchoClient instance;
|
||||
|
||||
private AsyncEchoClient() {
|
||||
try {
|
||||
client = AsynchronousSocketChannel.open();
|
||||
InetSocketAddress hostAddress = new InetSocketAddress("localhost", 4999);
|
||||
future = client.connect(hostAddress);
|
||||
start();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static AsyncEchoClient getInstance() {
|
||||
if (instance == null)
|
||||
instance = new AsyncEchoClient();
|
||||
return instance;
|
||||
}
|
||||
|
||||
private void start() {
|
||||
try {
|
||||
future.get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public String sendMessage(String message) {
|
||||
byte[] byteMsg = new String(message).getBytes();
|
||||
ByteBuffer buffer = ByteBuffer.wrap(byteMsg);
|
||||
Future<Integer> writeResult = client.write(buffer);
|
||||
|
||||
while (!writeResult.isDone()) {
|
||||
// do nothing
|
||||
}
|
||||
buffer.flip();
|
||||
Future<Integer> readResult = client.read(buffer);
|
||||
while (!readResult.isDone()) {
|
||||
|
||||
}
|
||||
String echo = new String(buffer.array()).trim();
|
||||
buffer.clear();
|
||||
return echo;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
try {
|
||||
client.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
AsyncEchoClient client = AsyncEchoClient.getInstance();
|
||||
client.start();
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
String line = null;
|
||||
System.out.println("Message to server:");
|
||||
while ((line = br.readLine()) != null) {
|
||||
String response = client.sendMessage(line);
|
||||
System.out.println("response from server: " + response);
|
||||
System.out.println("Message to server:");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.baeldung.java.nio2.async;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.AsynchronousServerSocketChannel;
|
||||
import java.nio.channels.AsynchronousSocketChannel;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
public class AsyncEchoServer {
|
||||
private AsynchronousServerSocketChannel serverChannel;
|
||||
private Future<AsynchronousSocketChannel> acceptResult;
|
||||
private AsynchronousSocketChannel clientChannel;
|
||||
|
||||
public AsyncEchoServer() {
|
||||
try {
|
||||
serverChannel = AsynchronousServerSocketChannel.open();
|
||||
InetSocketAddress hostAddress = new InetSocketAddress("localhost", 4999);
|
||||
serverChannel.bind(hostAddress);
|
||||
acceptResult = serverChannel.accept();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void runServer() {
|
||||
try {
|
||||
clientChannel = acceptResult.get();
|
||||
if ((clientChannel != null) && (clientChannel.isOpen())) {
|
||||
while (true) {
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocate(32);
|
||||
Future<Integer> readResult = clientChannel.read(buffer);
|
||||
|
||||
while (!readResult.isDone()) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
buffer.flip();
|
||||
String message = new String(buffer.array()).trim();
|
||||
if (message.equals("bye")) {
|
||||
break; // while loop
|
||||
}
|
||||
buffer = ByteBuffer.wrap(new String(message).getBytes());
|
||||
Future<Integer> writeResult = clientChannel.write(buffer);
|
||||
while (!writeResult.isDone()) {
|
||||
// do nothing
|
||||
}
|
||||
buffer.clear();
|
||||
|
||||
} // while()
|
||||
|
||||
clientChannel.close();
|
||||
serverChannel.close();
|
||||
|
||||
}
|
||||
} catch (InterruptedException | ExecutionException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
AsyncEchoServer server = new AsyncEchoServer();
|
||||
server.runServer();
|
||||
}
|
||||
public static Process start() throws IOException, InterruptedException {
|
||||
String javaHome = System.getProperty("java.home");
|
||||
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
|
||||
String classpath = System.getProperty("java.class.path");
|
||||
String className = AsyncEchoServer.class.getCanonicalName();
|
||||
|
||||
ProcessBuilder builder = new ProcessBuilder(javaBin, "-cp", classpath, className);
|
||||
|
||||
return builder.start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.baeldung.java.nio2.async;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.AsynchronousServerSocketChannel;
|
||||
import java.nio.channels.AsynchronousSocketChannel;
|
||||
import java.nio.channels.CompletionHandler;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class AsyncEchoServer2 {
|
||||
private AsynchronousServerSocketChannel serverChannel;
|
||||
private AsynchronousSocketChannel clientChannel;
|
||||
|
||||
public AsyncEchoServer2() {
|
||||
try {
|
||||
serverChannel = AsynchronousServerSocketChannel.open();
|
||||
InetSocketAddress hostAddress = new InetSocketAddress("localhost", 4999);
|
||||
serverChannel.bind(hostAddress);
|
||||
while (true) {
|
||||
|
||||
serverChannel.accept(null, new CompletionHandler<AsynchronousSocketChannel, Object>() {
|
||||
|
||||
@Override
|
||||
public void completed(AsynchronousSocketChannel result, Object attachment) {
|
||||
if (serverChannel.isOpen())
|
||||
serverChannel.accept(null, this);
|
||||
clientChannel = result;
|
||||
if ((clientChannel != null) && (clientChannel.isOpen())) {
|
||||
ReadWriteHandler handler = new ReadWriteHandler();
|
||||
ByteBuffer buffer = ByteBuffer.allocate(32);
|
||||
Map<String, Object> readInfo = new HashMap<>();
|
||||
readInfo.put("action", "read");
|
||||
readInfo.put("buffer", buffer);
|
||||
clientChannel.read(buffer, readInfo, handler);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Throwable exc, Object attachment) {
|
||||
// process error
|
||||
}
|
||||
});
|
||||
try {
|
||||
System.in.read();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
class ReadWriteHandler implements CompletionHandler<Integer, Map<String, Object>> {
|
||||
|
||||
@Override
|
||||
public void completed(Integer result, Map<String, Object> attachment) {
|
||||
String action = (String) attachment.get("action");
|
||||
if ("read".equals(action)) {
|
||||
ByteBuffer buffer = (ByteBuffer) attachment.get("buffer");
|
||||
buffer.flip();
|
||||
attachment.put("action", "write");
|
||||
clientChannel.write(buffer, attachment, this);
|
||||
buffer.clear();
|
||||
} else if ("write".equals(action)) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(32);
|
||||
attachment.put("action", "read");
|
||||
attachment.put("buffer", buffer);
|
||||
clientChannel.read(buffer, attachment, this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Throwable exc, Map<String, Object> attachment) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
new AsyncEchoServer2();
|
||||
}
|
||||
|
||||
public static Process start() throws IOException, InterruptedException {
|
||||
String javaHome = System.getProperty("java.home");
|
||||
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
|
||||
String classpath = System.getProperty("java.class.path");
|
||||
String className = AsyncEchoServer2.class.getCanonicalName();
|
||||
|
||||
ProcessBuilder builder = new ProcessBuilder(javaBin, "-cp", classpath, className);
|
||||
|
||||
return builder.start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.java.nio2.async;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AsyncEchoTest {
|
||||
|
||||
Process server;
|
||||
AsyncEchoClient client;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException, InterruptedException {
|
||||
server = AsyncEchoServer2.start();
|
||||
client = AsyncEchoClient.getInstance();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServerClient_whenServerEchosMessage_thenCorrect() {
|
||||
String resp1 = client.sendMessage("hello");
|
||||
String resp2 = client.sendMessage("world");
|
||||
assertEquals("hello", resp1);
|
||||
assertEquals("world", resp2);
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown() throws IOException {
|
||||
server.destroy();
|
||||
client.stop();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package com.baeldung.java.nio2.async;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.AsynchronousFileChannel;
|
||||
import java.nio.channels.CompletionHandler;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class AsyncFileTest {
|
||||
@Test
|
||||
public void givenPath_whenReadsContentWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException {
|
||||
Path path = Paths.get(URI.create(this.getClass().getClassLoader().getResource("file.txt").toString()));
|
||||
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
|
||||
Future<Integer> operation = fileChannel.read(buffer, 0);
|
||||
|
||||
operation.get();
|
||||
|
||||
String fileContent = new String(buffer.array()).trim();
|
||||
buffer.clear();
|
||||
|
||||
assertEquals(fileContent, "baeldung.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenReadsContentWithCompletionHandler_thenCorrect() throws IOException {
|
||||
Path path = Paths.get(URI.create(AsyncFileTest.class.getResource("/file.txt").toString()));
|
||||
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
|
||||
fileChannel.read(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
|
||||
|
||||
@Override
|
||||
public void completed(Integer result, ByteBuffer attachment) {
|
||||
// result is number of bytes read
|
||||
// attachment is the buffer
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Throwable exc, ByteBuffer attachment) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPathAndContent_whenWritesToFileWithFuture_thenCorrect() throws IOException, ExecutionException, InterruptedException {
|
||||
String fileName = "temp";
|
||||
Path path = Paths.get(fileName);
|
||||
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
long position = 0;
|
||||
|
||||
buffer.put("hello world".getBytes());
|
||||
buffer.flip();
|
||||
|
||||
Future<Integer> operation = fileChannel.write(buffer, position);
|
||||
buffer.clear();
|
||||
|
||||
operation.get();
|
||||
|
||||
String content = readContent(path);
|
||||
assertEquals("hello world", content);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPathAndContent_whenWritesToFileWithHandler_thenCorrect() throws IOException {
|
||||
String fileName = UUID.randomUUID().toString();
|
||||
Path path = Paths.get(fileName);
|
||||
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE,StandardOpenOption.DELETE_ON_CLOSE);
|
||||
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
buffer.put("hello world".getBytes());
|
||||
buffer.flip();
|
||||
|
||||
fileChannel.write(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
|
||||
|
||||
@Override
|
||||
public void completed(Integer result, ByteBuffer attachment) {
|
||||
// result is number of bytes written
|
||||
// attachment is the buffer
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(Throwable exc, ByteBuffer attachment) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static String readContent(Path file) throws ExecutionException, InterruptedException {
|
||||
AsynchronousFileChannel fileChannel = null;
|
||||
try {
|
||||
fileChannel = AsynchronousFileChannel.open(file, StandardOpenOption.READ);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
|
||||
Future<Integer> operation = fileChannel.read(buffer, 0);
|
||||
|
||||
operation.get();
|
||||
|
||||
String fileContent = new String(buffer.array()).trim();
|
||||
buffer.clear();
|
||||
return fileContent;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.java.nio2.attributes;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.attribute.BasicFileAttributeView;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class BasicAttribsTest {
|
||||
private static final String HOME = System.getProperty("user.home");
|
||||
private static BasicFileAttributes basicAttribs;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() throws IOException {
|
||||
Path home = Paths.get(HOME);
|
||||
BasicFileAttributeView basicView = Files.getFileAttributeView(home, BasicFileAttributeView.class);
|
||||
basicAttribs = basicView.readAttributes();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFileTimes_whenComparesThem_ThenCorrect() {
|
||||
FileTime created = basicAttribs.creationTime();
|
||||
FileTime modified = basicAttribs.lastModifiedTime();
|
||||
FileTime accessed = basicAttribs.lastAccessTime();
|
||||
|
||||
System.out.println("Created: " + created);
|
||||
System.out.println("Modified: " + modified);
|
||||
System.out.println("Accessed: " + accessed);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenGetsFileSize_thenCorrect() {
|
||||
long size = basicAttribs.size();
|
||||
assertTrue(size > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenChecksIfDirectory_thenCorrect() {
|
||||
boolean isDir = basicAttribs.isDirectory();
|
||||
assertTrue(isDir);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenChecksIfFile_thenCorrect() {
|
||||
boolean isFile = basicAttribs.isRegularFile();
|
||||
assertFalse(isFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenChecksIfSymLink_thenCorrect() {
|
||||
boolean isSymLink = basicAttribs.isSymbolicLink();
|
||||
assertFalse(isSymLink);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenChecksIfOther_thenCorrect() {
|
||||
boolean isOther = basicAttribs.isOther();
|
||||
assertFalse(isOther);
|
||||
}
|
||||
}
|
||||
+15
-21
@@ -1,21 +1,15 @@
|
||||
package org.baeldung.java.collections;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
public class CollectionsConcatenateUnitTest {
|
||||
|
||||
@@ -63,16 +57,16 @@ public class CollectionsConcatenateUnitTest {
|
||||
Assert.assertEquals(Arrays.asList("S", "T", "U", "V"), collectionCombined);
|
||||
}
|
||||
|
||||
public static <E> Iterable<E> concat(Iterable<? extends E> list1, Iterable<? extends E> list2) {
|
||||
public static <E> Iterable<E> concat(Iterable<? extends E> i1, Iterable<? extends E> i2) {
|
||||
return new Iterable<E>() {
|
||||
public Iterator<E> iterator() {
|
||||
return new Iterator<E>() {
|
||||
protected Iterator<? extends E> listIterator = list1.iterator();
|
||||
protected Boolean checkedHasNext;
|
||||
protected E nextValue;
|
||||
Iterator<? extends E> listIterator = i1.iterator();
|
||||
Boolean checkedHasNext;
|
||||
E nextValue;
|
||||
private boolean startTheSecond;
|
||||
|
||||
public void theNext() {
|
||||
void theNext() {
|
||||
if (listIterator.hasNext()) {
|
||||
checkedHasNext = true;
|
||||
nextValue = listIterator.next();
|
||||
@@ -80,7 +74,7 @@ public class CollectionsConcatenateUnitTest {
|
||||
checkedHasNext = false;
|
||||
else {
|
||||
startTheSecond = true;
|
||||
listIterator = list2.iterator();
|
||||
listIterator = i2.iterator();
|
||||
theNext();
|
||||
}
|
||||
}
|
||||
@@ -107,7 +101,7 @@ public class CollectionsConcatenateUnitTest {
|
||||
}
|
||||
|
||||
public static <E> List<E> makeListFromIterable(Iterable<E> iter) {
|
||||
List<E> list = new ArrayList<E>();
|
||||
List<E> list = new ArrayList<>();
|
||||
for (E item : iter) {
|
||||
list.add(item);
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
baeldung.com
|
||||
@@ -109,6 +109,7 @@
|
||||
<module>spring-katharsis</module>
|
||||
<module>spring-mockito</module>
|
||||
<module>spring-mvc-java</module>
|
||||
<module>spring-mvc-forms</module>
|
||||
<module>spring-mvc-no-xml</module>
|
||||
<module>spring-mvc-tiles</module>
|
||||
<module>spring-mvc-velocity</module>
|
||||
|
||||
@@ -7,20 +7,15 @@ import org.springframework.context.annotation.Configuration;
|
||||
public class FactoryBeanAppConfig {
|
||||
|
||||
@Bean
|
||||
public ToolFactory tool() {
|
||||
public ToolFactory toolFactory() {
|
||||
ToolFactory factory = new ToolFactory();
|
||||
factory.setFactoryId(7070);
|
||||
factory.setToolId(2);
|
||||
factory.setToolName("wrench");
|
||||
factory.setToolPrice(3.7);
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Worker worker() throws Exception {
|
||||
Worker worker = new Worker();
|
||||
worker.setNumber("1002");
|
||||
worker.setTool(tool().getObject());
|
||||
return worker;
|
||||
public Tool tool() throws Exception {
|
||||
return toolFactory().getObject();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class InitializationToolFactory implements FactoryBean<Tool>, InitializingBean {
|
||||
|
||||
private int factoryId;
|
||||
private int toolId;
|
||||
private String toolName;
|
||||
private double toolPrice;
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
checkArgument(!StringUtils.isEmpty(toolName), "tool name cannot be empty");
|
||||
checkArgument(toolPrice >= 0, "tool price should not be less than 0");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tool getObject() throws Exception {
|
||||
return new Tool(toolId, toolName, toolPrice);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return Tool.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getFactoryId() {
|
||||
return factoryId;
|
||||
}
|
||||
|
||||
public void setFactoryId(int factoryId) {
|
||||
this.factoryId = factoryId;
|
||||
}
|
||||
|
||||
public int getToolId() {
|
||||
return toolId;
|
||||
}
|
||||
|
||||
public void setToolId(int toolId) {
|
||||
this.toolId = toolId;
|
||||
}
|
||||
|
||||
public String getToolName() {
|
||||
return toolName;
|
||||
}
|
||||
|
||||
public void setToolName(String toolName) {
|
||||
this.toolName = toolName;
|
||||
}
|
||||
|
||||
public double getToolPrice() {
|
||||
return toolPrice;
|
||||
}
|
||||
|
||||
public void setToolPrice(double toolPrice) {
|
||||
this.toolPrice = toolPrice;
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,6 @@ public class NonSingleToolFactory extends AbstractFactoryBean<Tool> {
|
||||
|
||||
private int factoryId;
|
||||
private int toolId;
|
||||
private String toolName;
|
||||
private double toolPrice;
|
||||
|
||||
public NonSingleToolFactory() {
|
||||
setSingleton(false);
|
||||
@@ -20,7 +18,7 @@ public class NonSingleToolFactory extends AbstractFactoryBean<Tool> {
|
||||
|
||||
@Override
|
||||
protected Tool createInstance() throws Exception {
|
||||
return new Tool(toolId, toolName, toolPrice);
|
||||
return new Tool(toolId);
|
||||
}
|
||||
|
||||
public int getFactoryId() {
|
||||
@@ -38,20 +36,4 @@ public class NonSingleToolFactory extends AbstractFactoryBean<Tool> {
|
||||
public void setToolId(int toolId) {
|
||||
this.toolId = toolId;
|
||||
}
|
||||
|
||||
public String getToolName() {
|
||||
return toolName;
|
||||
}
|
||||
|
||||
public void setToolName(String toolName) {
|
||||
this.toolName = toolName;
|
||||
}
|
||||
|
||||
public double getToolPrice() {
|
||||
return toolPrice;
|
||||
}
|
||||
|
||||
public void setToolPrice(double toolPrice) {
|
||||
this.toolPrice = toolPrice;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class PostConstructToolFactory implements FactoryBean<Tool> {
|
||||
|
||||
private int factoryId;
|
||||
private int toolId;
|
||||
private String toolName;
|
||||
private double toolPrice;
|
||||
|
||||
@Override
|
||||
public Tool getObject() throws Exception {
|
||||
return new Tool(toolId, toolName, toolPrice);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return Tool.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void checkParams() {
|
||||
checkArgument(!StringUtils.isEmpty(toolName), "tool name cannot be empty");
|
||||
checkArgument(toolPrice >= 0, "tool price should not be less than 0");
|
||||
}
|
||||
|
||||
public int getFactoryId() {
|
||||
return factoryId;
|
||||
}
|
||||
|
||||
public void setFactoryId(int factoryId) {
|
||||
this.factoryId = factoryId;
|
||||
}
|
||||
|
||||
public int getToolId() {
|
||||
return toolId;
|
||||
}
|
||||
|
||||
public void setToolId(int toolId) {
|
||||
this.toolId = toolId;
|
||||
}
|
||||
|
||||
public String getToolName() {
|
||||
return toolName;
|
||||
}
|
||||
|
||||
public void setToolName(String toolName) {
|
||||
this.toolName = toolName;
|
||||
}
|
||||
|
||||
public double getToolPrice() {
|
||||
return toolPrice;
|
||||
}
|
||||
|
||||
public void setToolPrice(double toolPrice) {
|
||||
this.toolPrice = toolPrice;
|
||||
}
|
||||
}
|
||||
@@ -7,8 +7,6 @@ public class SingleToolFactory extends AbstractFactoryBean<Tool> {
|
||||
|
||||
private int factoryId;
|
||||
private int toolId;
|
||||
private String toolName;
|
||||
private double toolPrice;
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
@@ -17,7 +15,7 @@ public class SingleToolFactory extends AbstractFactoryBean<Tool> {
|
||||
|
||||
@Override
|
||||
protected Tool createInstance() throws Exception {
|
||||
return new Tool(toolId, toolName, toolPrice);
|
||||
return new Tool(toolId);
|
||||
}
|
||||
|
||||
public int getFactoryId() {
|
||||
@@ -35,20 +33,4 @@ public class SingleToolFactory extends AbstractFactoryBean<Tool> {
|
||||
public void setToolId(int toolId) {
|
||||
this.toolId = toolId;
|
||||
}
|
||||
|
||||
public String getToolName() {
|
||||
return toolName;
|
||||
}
|
||||
|
||||
public void setToolName(String toolName) {
|
||||
this.toolName = toolName;
|
||||
}
|
||||
|
||||
public double getToolPrice() {
|
||||
return toolPrice;
|
||||
}
|
||||
|
||||
public void setToolPrice(double toolPrice) {
|
||||
this.toolPrice = toolPrice;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
public class Tool {
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
private double price;
|
||||
|
||||
public Tool(int id, String name, double price) {
|
||||
public Tool() {
|
||||
}
|
||||
|
||||
public Tool(int id) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
@@ -19,20 +17,4 @@ public class Tool {
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,10 @@ public class ToolFactory implements FactoryBean<Tool> {
|
||||
|
||||
private int factoryId;
|
||||
private int toolId;
|
||||
private String toolName;
|
||||
private double toolPrice;
|
||||
|
||||
@Override
|
||||
public Tool getObject() throws Exception {
|
||||
return new Tool(toolId, toolName, toolPrice);
|
||||
return new Tool(toolId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -39,20 +37,4 @@ public class ToolFactory implements FactoryBean<Tool> {
|
||||
public void setToolId(int toolId) {
|
||||
this.toolId = toolId;
|
||||
}
|
||||
|
||||
public String getToolName() {
|
||||
return toolName;
|
||||
}
|
||||
|
||||
public void setToolName(String toolName) {
|
||||
this.toolName = toolName;
|
||||
}
|
||||
|
||||
public double getToolPrice() {
|
||||
return toolPrice;
|
||||
}
|
||||
|
||||
public void setToolPrice(double toolPrice) {
|
||||
this.toolPrice = toolPrice;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
public class Worker {
|
||||
|
||||
private String number;
|
||||
private Tool tool;
|
||||
|
||||
public Worker() {}
|
||||
|
||||
public Worker(String number, Tool tool) {
|
||||
this.number = number;
|
||||
this.tool = tool;
|
||||
}
|
||||
|
||||
public String getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(String number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public Tool getTool() {
|
||||
return tool;
|
||||
}
|
||||
|
||||
public void setTool(Tool tool) {
|
||||
this.tool = tool;
|
||||
}
|
||||
}
|
||||
@@ -6,34 +6,10 @@
|
||||
<bean id="singleTool" class="com.baeldung.factorybean.SingleToolFactory">
|
||||
<property name="factoryId" value="3001"/>
|
||||
<property name="toolId" value="1"/>
|
||||
<property name="toolName" value="screwdriver"/>
|
||||
<property name="toolPrice" value="1.5"/>
|
||||
</bean>
|
||||
|
||||
<bean id="nonSingleTool" class="com.baeldung.factorybean.NonSingleToolFactory">
|
||||
<property name="factoryId" value="3002"/>
|
||||
<property name="toolId" value="2"/>
|
||||
<property name="toolName" value="screwdriver"/>
|
||||
<property name="toolPrice" value="1.5"/>
|
||||
</bean>
|
||||
|
||||
<bean id="worker1" class="com.baeldung.factorybean.Worker">
|
||||
<property name="number" value="50001"/>
|
||||
<property name="tool" ref="singleTool"/>
|
||||
</bean>
|
||||
|
||||
<bean id="worker2" class="com.baeldung.factorybean.Worker">
|
||||
<property name="number" value="50002"/>
|
||||
<property name="tool" ref="singleTool"/>
|
||||
</bean>
|
||||
|
||||
<bean id="worker3" class="com.baeldung.factorybean.Worker">
|
||||
<property name="number" value="50003"/>
|
||||
<property name="tool" ref="nonSingleTool"/>
|
||||
</bean>
|
||||
|
||||
<bean id="worker4" class="com.baeldung.factorybean.Worker">
|
||||
<property name="number" value="50004"/>
|
||||
<property name="tool" ref="nonSingleTool"/>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="initializationTool" class="com.baeldung.factorybean.InitializationToolFactory">
|
||||
<property name="factoryId" value="1010"/>
|
||||
<property name="toolId" value="1"/>
|
||||
<property name="toolName" value="screwdriver"/>
|
||||
<property name="toolPrice" value="-1"/>
|
||||
</bean>
|
||||
|
||||
<bean id="worker" class="com.baeldung.factorybean.Worker">
|
||||
<property name="number" value="36"/>
|
||||
<property name="tool" ref="initializationTool"/>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -1,20 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<context:annotation-config/>
|
||||
|
||||
<bean id="postConstructTool" class="com.baeldung.factorybean.PostConstructToolFactory">
|
||||
<property name="factoryId" value="2020"/>
|
||||
<property name="toolId" value="1"/>
|
||||
<property name="toolName" value=""/>
|
||||
<property name="toolPrice" value="2.2"/>
|
||||
</bean>
|
||||
|
||||
<bean id="worker" class="com.baeldung.factorybean.Worker">
|
||||
<property name="number" value="37"/>
|
||||
<property name="tool" ref="postConstructTool"/>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -6,12 +6,5 @@
|
||||
<bean id="tool" class="com.baeldung.factorybean.ToolFactory">
|
||||
<property name="factoryId" value="9090"/>
|
||||
<property name="toolId" value="1"/>
|
||||
<property name="toolName" value="screwdriver"/>
|
||||
<property name="toolPrice" value="1.5"/>
|
||||
</bean>
|
||||
|
||||
<bean id="worker" class="com.baeldung.factorybean.Worker">
|
||||
<property name="number" value="1001"/>
|
||||
<property name="tool" ref="tool"/>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -4,33 +4,36 @@ import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "classpath:factorybean-abstract-spring-ctx.xml" })
|
||||
public class AbstractFactoryBeanTest {
|
||||
|
||||
|
||||
@Resource(name = "singleTool")
|
||||
private Tool tool1;
|
||||
@Resource(name = "singleTool")
|
||||
private Tool tool2;
|
||||
@Resource(name = "nonSingleTool")
|
||||
private Tool tool3;
|
||||
@Resource(name = "nonSingleTool")
|
||||
private Tool tool4;
|
||||
|
||||
@Test
|
||||
public void testSingleToolFactory() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-abstract-spring-ctx.xml");
|
||||
|
||||
Worker worker1 = (Worker) context.getBean("worker1");
|
||||
Worker worker2 = (Worker) context.getBean("worker2");
|
||||
|
||||
assertThat(worker1.getNumber(), equalTo("50001"));
|
||||
assertThat(worker2.getNumber(), equalTo("50002"));
|
||||
assertTrue(worker1.getTool() == worker2.getTool());
|
||||
assertThat(tool1.getId(), equalTo(1));
|
||||
assertTrue(tool1 == tool2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonSingleToolFactory() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-abstract-spring-ctx.xml");
|
||||
|
||||
Worker worker3 = (Worker) context.getBean("worker3");
|
||||
Worker worker4 = (Worker) context.getBean("worker4");
|
||||
|
||||
assertThat(worker3.getNumber(), equalTo("50003"));
|
||||
assertThat(worker4.getNumber(), equalTo("50004"));
|
||||
assertTrue(worker3.getTool() != worker4.getTool());
|
||||
assertThat(tool3.getId(), equalTo(2));
|
||||
assertThat(tool4.getId(), equalTo(2));
|
||||
assertTrue(tool3 != tool4);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class FactoryBeanInitializeTest {
|
||||
|
||||
@Test(expected = BeanCreationException.class)
|
||||
public void testInitializationToolFactory() {
|
||||
new ClassPathXmlApplicationContext("classpath:factorybean-init-spring-ctx.xml");
|
||||
}
|
||||
|
||||
@Test(expected = BeanCreationException.class)
|
||||
public void testPostConstructToolFactory() {
|
||||
new ClassPathXmlApplicationContext("classpath:factorybean-postconstruct-spring-ctx.xml");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = FactoryBeanAppConfig.class)
|
||||
public class FactoryBeanJavaConfigTest {
|
||||
|
||||
@Resource
|
||||
private Tool tool;
|
||||
@Resource(name = "&toolFactory")
|
||||
private ToolFactory toolFactory;
|
||||
|
||||
@Test
|
||||
public void testConstructWorkerByJava() {
|
||||
assertThat(tool.getId(), equalTo(2));
|
||||
assertThat(toolFactory.getFactoryId(), equalTo(7070));
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class FactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void testConstructWorkerByXml() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-spring-ctx.xml");
|
||||
|
||||
Worker worker = (Worker) context.getBean("worker");
|
||||
assertThat(worker.getNumber(), equalTo("1001"));
|
||||
assertThat(worker.getTool().getId(), equalTo(1));
|
||||
assertThat(worker.getTool().getName(), equalTo("screwdriver"));
|
||||
assertThat(worker.getTool().getPrice(), equalTo(1.5));
|
||||
|
||||
ToolFactory toolFactory = (ToolFactory) context.getBean("&tool");
|
||||
assertThat(toolFactory.getFactoryId(), equalTo(9090));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructWorkerByJava() {
|
||||
ApplicationContext context = new AnnotationConfigApplicationContext(FactoryBeanAppConfig.class);
|
||||
|
||||
Worker worker = (Worker) context.getBean("worker");
|
||||
assertThat(worker.getNumber(), equalTo("1002"));
|
||||
assertThat(worker.getTool().getId(), equalTo(2));
|
||||
assertThat(worker.getTool().getName(), equalTo("wrench"));
|
||||
assertThat(worker.getTool().getPrice(), equalTo(3.7));
|
||||
|
||||
ToolFactory toolFactory = (ToolFactory) context.getBean("&tool");
|
||||
assertThat(toolFactory.getFactoryId(), equalTo(7070));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.factorybean;
|
||||
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "classpath:factorybean-spring-ctx.xml" })
|
||||
public class FactoryBeanXmlConfigTest {
|
||||
|
||||
@Resource
|
||||
private Tool tool;
|
||||
@Resource(name = "&tool")
|
||||
private ToolFactory toolFactory;
|
||||
|
||||
@Test
|
||||
public void testConstructWorkerByXml() {
|
||||
assertThat(tool.getId(), equalTo(1));
|
||||
assertThat(toolFactory.getFactoryId(), equalTo(9090));
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user