Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/eugenp/tutorials
- Loading branch information
Showing
80 changed files
with
2,153 additions
and
1,544 deletions.
There are no files selected for viewing
101 changes: 61 additions & 40 deletions
101
cdi/src/test/java/com/baeldung/test/dependencyinjection/ImageProcessorUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,91 @@ | ||
package com.baeldung.test.dependencyinjection; | ||
|
||
import com.baeldung.dependencyinjection.imagefileeditors.GifFileEditor; | ||
import com.baeldung.dependencyinjection.imagefileeditors.JpgFileEditor; | ||
import com.baeldung.dependencyinjection.imagefileeditors.PngFileEditor; | ||
import com.baeldung.dependencyinjection.imageprocessors.ImageFileProcessor; | ||
import com.baeldung.dependencyinjection.loggers.TimeLogger; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.within; | ||
|
||
import java.text.ParseException; | ||
import java.time.LocalTime; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
import org.jboss.weld.environment.se.Weld; | ||
import org.jboss.weld.environment.se.WeldContainer; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import com.baeldung.dependencyinjection.imagefileeditors.PngFileEditor; | ||
import com.baeldung.dependencyinjection.imageprocessors.ImageFileProcessor; | ||
import com.baeldung.dependencyinjection.loggers.TimeLogger; | ||
|
||
public class ImageProcessorUnitTest { | ||
|
||
private static ImageFileProcessor imageFileProcessor; | ||
private static SimpleDateFormat dateFormat; | ||
private static Calendar calendar; | ||
|
||
|
||
|
||
@BeforeClass | ||
public static void setImageProcessorInstance() { | ||
Weld weld = new Weld(); | ||
WeldContainer container = weld.initialize(); | ||
imageFileProcessor = container.select(ImageFileProcessor.class).get(); | ||
imageFileProcessor = container.select(ImageFileProcessor.class) | ||
.get(); | ||
container.shutdown(); | ||
} | ||
|
||
@BeforeClass | ||
public static void setSimpleDateFormatInstance() { | ||
dateFormat = new SimpleDateFormat("HH:mm"); | ||
} | ||
|
||
@BeforeClass | ||
public static void setCalendarInstance() { | ||
calendar = Calendar.getInstance(); | ||
} | ||
|
||
|
||
@Test | ||
public void givenImageProcessorInstance_whenInjectedPngFileEditorandTimeLoggerInstances_thenTwoAssertions() { | ||
assertThat(imageFileProcessor.getImageFileditor()).isInstanceOf(PngFileEditor.class); | ||
assertThat(imageFileProcessor.getTimeLogger()).isInstanceOf(TimeLogger.class); | ||
} | ||
|
||
@Test | ||
public void givenImageProcessorInstance_whenCalledopenFile_thenOneAssertion() { | ||
String currentTime = dateFormat.format(calendar.getTime()); | ||
assertThat(imageFileProcessor.openFile("file1.png")).isEqualTo("Opening PNG file file1.png at: " + currentTime); | ||
public void givenImageProcessorInstance_whenCalledopenFile_thenOneAssertion() throws ParseException { | ||
LocalTime currentTime = LocalTime.now(); | ||
|
||
String openFileLog = imageFileProcessor.openFile("file1.png"); | ||
assertThat(openFileLog).contains("Opening PNG file file1.png at: "); | ||
|
||
LocalTime loggedTime = getLoggedTime(openFileLog); | ||
assertThat(loggedTime).isCloseTo(currentTime, within(2, ChronoUnit.MINUTES)); | ||
} | ||
|
||
@Test | ||
public void givenImageProcessorInstance_whenCallededitFile_thenOneAssertion() { | ||
String currentTime = dateFormat.format(calendar.getTime()); | ||
assertThat(imageFileProcessor.editFile("file1.png")).isEqualTo("Editing PNG file file1.png at: " + currentTime); | ||
public void givenImageProcessorInstance_whenCallededitFile_thenOneAssertion() throws ParseException { | ||
LocalTime currentTime = LocalTime.now(); | ||
|
||
String editFileLog = imageFileProcessor.editFile("file1.png"); | ||
assertThat(editFileLog).contains("Editing PNG file file1.png at: "); | ||
|
||
LocalTime loggedTime = getLoggedTime(editFileLog); | ||
assertThat(loggedTime).isCloseTo(currentTime, within(2, ChronoUnit.MINUTES)); | ||
} | ||
|
||
@Test | ||
public void givenImageProcessorInstance_whenCalledwriteFile_thenOneAssertion() { | ||
String currentTime = dateFormat.format(calendar.getTime()); | ||
assertThat(imageFileProcessor.writeFile("file1.png")).isEqualTo("Writing PNG file file1.png at: " + currentTime); | ||
public void givenImageProcessorInstance_whenCalledwriteFile_thenOneAssertion() throws ParseException { | ||
LocalTime currentTime = LocalTime.now(); | ||
|
||
String writeFileLog = imageFileProcessor.writeFile("file1.png"); | ||
assertThat(writeFileLog).contains("Writing PNG file file1.png at: "); | ||
|
||
LocalTime loggedTime = getLoggedTime(writeFileLog); | ||
assertThat(loggedTime).isCloseTo(currentTime, within(2, ChronoUnit.MINUTES)); | ||
} | ||
|
||
@Test | ||
public void givenImageProcessorInstance_whenCalledsaveFile_thenOneAssertion() { | ||
String currentTime = dateFormat.format(calendar.getTime()); | ||
assertThat(imageFileProcessor.saveFile("file1.png")).isEqualTo("Saving PNG file file1.png at: " + currentTime); | ||
public void givenImageProcessorInstance_whenCalledsaveFile_thenOneAssertion() throws ParseException { | ||
LocalTime currentTime = LocalTime.now(); | ||
|
||
String saveFileLog = imageFileProcessor.saveFile("file1.png"); | ||
assertThat(saveFileLog).contains("Saving PNG file file1.png at: "); | ||
|
||
LocalTime loggedTime = getLoggedTime(saveFileLog); | ||
assertThat(loggedTime).isCloseTo(currentTime, within(2, ChronoUnit.MINUTES)); | ||
} | ||
|
||
private LocalTime getLoggedTime(String log) throws ParseException { | ||
String logTimeString = log.split("at: ")[1]; | ||
|
||
int hour = Integer.valueOf(logTimeString.split(":")[0]); | ||
int minutes = Integer.valueOf(logTimeString.split(":")[1]); | ||
|
||
LocalTime loggedTime = LocalTime.of(hour, minutes); | ||
return loggedTime; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
core-java-8/src/main/java/com/baeldung/optional/PersonRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.baeldung.optional; | ||
|
||
public class PersonRepository { | ||
|
||
public String findNameById(String id) { | ||
return id == null ? null : "Name"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
core-java-9/src/main/java/com/baeldung/java9/process/ChildProcess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.baeldung.java9.process; | ||
|
||
import java.util.Scanner; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class ChildProcess { | ||
|
||
public static void main(String[] args) { | ||
@SuppressWarnings("resource") | ||
Scanner input = new Scanner(System.in); | ||
Logger log = Logger.getLogger(ChildProcess.class.getName()); | ||
log.log(Level.INFO, input.nextLine()); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
core-java-9/src/main/java/com/baeldung/java9/process/OutputStreamExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.baeldung.java9.process; | ||
|
||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class OutputStreamExample { | ||
|
||
public static void main(String[] args) { | ||
Logger log = Logger.getLogger(OutputStreamExample.class.getName()); | ||
log.log(Level.INFO, Integer.toString(sum(1,2))); | ||
} | ||
|
||
public static int sum(int a, int b) { | ||
return a + b; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
core-java-9/src/main/java/com/baeldung/java9/process/ProcessCompilationError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.baeldung.java9.process; | ||
|
||
public class ProcessCompilationError { | ||
//This method has been written to generate error to display | ||
//how process errorStream() can consume error | ||
public static void(); | ||
} |
110 changes: 110 additions & 0 deletions
110
core-java-9/src/main/java/com/baeldung/java9/process/ProcessUnderstanding.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.baeldung.java9.process; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStreamWriter; | ||
import java.io.Writer; | ||
import java.util.Optional; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class ProcessUnderstanding { | ||
|
||
public static int compileAndRunJavaProgram() throws IOException { | ||
Process process = Runtime.getRuntime() | ||
.exec("javac -cp src src\\main\\java\\com\\baeldung\\java9\\process\\OutputStreamExample.java"); | ||
process = Runtime.getRuntime() | ||
.exec("java -cp src/main/java com.baeldung.java9.process.OutputStreamExample"); | ||
BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream())); | ||
int value = Integer.parseInt(output.readLine()); | ||
return value; | ||
} | ||
|
||
public static String getErrorStreamExample() throws IOException { | ||
Process process = Runtime.getRuntime() | ||
.exec("javac -cp src src\\main\\java\\com\\baeldung\\java9\\process\\ProcessCompilationError.java"); | ||
BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream())); | ||
String errorString = error.readLine(); | ||
return errorString; | ||
} | ||
|
||
public static void creatingNewProcess() throws IOException { | ||
ProcessBuilder builder = new ProcessBuilder("notepad.exe"); | ||
Process process = builder.start(); | ||
} | ||
|
||
public static int filterProcessWithStreamsInSpecificRangeReturnCount() { | ||
return (int) ProcessHandle.allProcesses() | ||
.filter(ph -> (ph.pid() > 10000 && ph.pid() < 50000)) | ||
.count(); | ||
} | ||
|
||
public static void destroyingProcessCreatedBySameProcess() throws IOException, InterruptedException { | ||
ProcessBuilder builder = new ProcessBuilder("notepad.exe"); | ||
Process process = builder.start(); | ||
Thread.sleep(10000); | ||
process.destroy(); | ||
} | ||
|
||
public static void destroyingProcessCreatedByDifferentProcess() { | ||
// find out the process id of current running task by checking | ||
// task manager in windows and enter the integer value | ||
Optional<ProcessHandle> optionalProcessHandle = ProcessHandle.of(5232); | ||
ProcessHandle processHandle = optionalProcessHandle.get(); | ||
processHandle.destroy(); | ||
} | ||
|
||
public static int waitForExample() throws IOException, InterruptedException { | ||
ProcessBuilder builder = new ProcessBuilder("notepad.exe"); | ||
Process process = builder.start(); | ||
return process.waitFor(); | ||
} | ||
|
||
public static int exitValueExample() throws IOException { | ||
ProcessBuilder builder = new ProcessBuilder("notepad.exe"); | ||
Process process = builder.start(); | ||
process.destroy(); | ||
return process.exitValue(); | ||
} | ||
|
||
public static void destroyExample() throws IOException, InterruptedException { | ||
ProcessBuilder builder = new ProcessBuilder("notepad.exe"); | ||
Process process = builder.start(); | ||
Thread.sleep(10000); | ||
process.destroy(); | ||
} | ||
|
||
public static void destroyForciblyExample() throws IOException, InterruptedException { | ||
ProcessBuilder builder = new ProcessBuilder("notepad.exe"); | ||
Process process = builder.start(); | ||
Thread.sleep(10000); | ||
process.destroy(); | ||
if (process.isAlive()) { | ||
process.destroyForcibly(); | ||
} | ||
} | ||
|
||
public static void outputStreamDemo() throws IOException, InterruptedException { | ||
Logger log = Logger.getLogger(ProcessUnderstanding.class.getName()); | ||
Process pr = Runtime.getRuntime() | ||
.exec("javac -cp src src\\main\\java\\com\\baeldung\\java9\\process\\ChildProcess.java"); | ||
final Process process = Runtime.getRuntime() | ||
.exec("java -cp src/main/java com.baeldung.java9.process.ChildProcess"); | ||
try (Writer w = new OutputStreamWriter(process.getOutputStream(), "UTF-8")) { | ||
w.write("send to child\n"); | ||
} | ||
new Thread(() -> { | ||
try { | ||
int c; | ||
while ((c = process.getInputStream() | ||
.read()) != -1) | ||
System.out.write((byte) c); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
}).start(); | ||
// send to child | ||
log.log(Level.INFO, "rc=" + process.waitFor()); | ||
} | ||
} |
Oops, something went wrong.