Skip to content

Commit 71b8657

Browse files
committed
Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-16822
2 parents 88b0322 + 9d825c4 commit 71b8657

File tree

75 files changed

+1194
-107
lines changed

Some content is hidden

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

75 files changed

+1194
-107
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.exceptions;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
7+
public class CheckedUncheckedExceptions {
8+
public static void checkedExceptionWithThrows() throws FileNotFoundException {
9+
File file = new File("not_existing_file.txt");
10+
FileInputStream stream = new FileInputStream(file);
11+
}
12+
13+
public static void checkedExceptionWithTryCatch() {
14+
File file = new File("not_existing_file.txt");
15+
try {
16+
FileInputStream stream = new FileInputStream(file);
17+
} catch (FileNotFoundException e) {
18+
e.printStackTrace();
19+
}
20+
}
21+
22+
public static int divideByZero() {
23+
int numerator = 1;
24+
int denominator = 0;
25+
return numerator / denominator;
26+
}
27+
28+
public static void checkFile(String fileName) throws IncorrectFileNameException {
29+
if (fileName == null || fileName.isEmpty()) {
30+
throw new NullOrEmptyException("The filename is null.");
31+
}
32+
if (!isCorrectFileName(fileName)) {
33+
throw new IncorrectFileNameException("Incorrect filename : " + fileName);
34+
}
35+
}
36+
37+
private static boolean isCorrectFileName(String fileName) {
38+
if (fileName.equals("wrongFileName.txt"))
39+
return false;
40+
else
41+
return true;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.exceptions;
2+
3+
public class IncorrectFileNameException extends Exception {
4+
private static final long serialVersionUID = 1L;
5+
6+
public IncorrectFileNameException(String errorMessage) {
7+
super(errorMessage);
8+
}
9+
10+
public IncorrectFileNameException(String errorMessage, Throwable thr) {
11+
super(errorMessage, thr);
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.exceptions;
2+
3+
public class NullOrEmptyException extends RuntimeException {
4+
5+
private static final long serialVersionUID = 1L;
6+
7+
public NullOrEmptyException(String errorMessage) {
8+
super(errorMessage);
9+
}
10+
11+
public NullOrEmptyException(String errorMessage, Throwable thr) {
12+
super(errorMessage, thr);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.exceptions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertThrows;
4+
5+
import java.io.FileNotFoundException;
6+
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.api.Test;
9+
10+
/**
11+
* Tests the {@link CheckedUncheckedExceptions}.
12+
*/
13+
public class CheckedUncheckedExceptionsUnitTest {
14+
15+
@Test
16+
public void whenFileNotExist_thenThrowException() {
17+
assertThrows(FileNotFoundException.class, () -> {
18+
CheckedUncheckedExceptions.checkedExceptionWithThrows();
19+
});
20+
}
21+
22+
@Test
23+
public void whenTryCatchExcetpion_thenSuccess() {
24+
try {
25+
CheckedUncheckedExceptions.checkedExceptionWithTryCatch();
26+
} catch (Exception e) {
27+
Assertions.fail(e.getMessage());
28+
}
29+
}
30+
31+
@Test
32+
public void whenDivideByZero_thenThrowException() {
33+
assertThrows(ArithmeticException.class, () -> {
34+
CheckedUncheckedExceptions.divideByZero();
35+
});
36+
}
37+
38+
@Test
39+
public void whenInvalidFile_thenThrowException() {
40+
41+
assertThrows(IncorrectFileNameException.class, () -> {
42+
CheckedUncheckedExceptions.checkFile("wrongFileName.txt");
43+
});
44+
}
45+
46+
@Test
47+
public void whenNullOrEmptyFile_thenThrowException() {
48+
assertThrows(NullOrEmptyException.class, () -> {
49+
CheckedUncheckedExceptions.checkFile(null);
50+
});
51+
assertThrows(NullOrEmptyException.class, () -> {
52+
CheckedUncheckedExceptions.checkFile("");
53+
});
54+
}
55+
}

gradle-5/.gitignore

Lines changed: 0 additions & 6 deletions
This file was deleted.

java-blockchain/.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
*.class
2+
3+
0.*
4+
5+
#folders#
6+
/target
7+
/neoDb*
8+
/data
9+
/src/main/webapp/WEB-INF/classes
10+
*/META-INF/*
11+
.resourceCache
12+
13+
# Packaged files #
14+
*.jar
15+
*.war
16+
*.ear
17+
18+
# Files generated by integration tests
19+
*.txt
20+
backup-pom.xml
21+
/bin/
22+
/temp
23+
24+
#IntelliJ specific
25+
.idea/
26+
*.iml

java-blockchain/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=========
2+
3+
## Basic Implementation of Blockchian in Java
4+
5+
### Relevant Articles:
6+
- []()

java-blockchain/pom.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.baeldung.blockchain</groupId>
6+
<artifactId>java-blockchain</artifactId>
7+
<version>0.1.0-SNAPSHOT</version>
8+
<name>java-blockchain</name>
9+
<packaging>jar</packaging>
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>parent-java</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<relativePath>../parent-java</relativePath>
15+
</parent>
16+
<build>
17+
<finalName>java-blockchain</finalName>
18+
<resources>
19+
<resource>
20+
<directory>src/main/resources</directory>
21+
<filtering>true</filtering>
22+
</resource>
23+
</resources>
24+
<plugins>
25+
<plugin>
26+
<groupId>org.apache.maven.plugins</groupId>
27+
<artifactId>maven-compiler-plugin</artifactId>
28+
<version>${maven-compiler-plugin.version}</version>
29+
<configuration>
30+
<source>${maven.compiler.source}</source>
31+
<target>${maven.compiler.target}</target>
32+
</configuration>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
<properties>
37+
<maven.compiler.source>1.8</maven.compiler.source>
38+
<maven.compiler.target>1.8</maven.compiler.target>
39+
</properties>
40+
</project>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.blockchain;
2+
3+
import java.io.UnsupportedEncodingException;
4+
import java.security.MessageDigest;
5+
import java.security.NoSuchAlgorithmException;
6+
import java.util.Date;
7+
import java.util.logging.Level;
8+
import java.util.logging.Logger;
9+
10+
public class Block {
11+
12+
private static Logger logger = Logger.getLogger(Block.class.getName());
13+
14+
private String hash;
15+
private String previousHash;
16+
private String data;
17+
private long timeStamp;
18+
private int nonce;
19+
20+
public Block(String data, String previousHash) {
21+
this.data = data;
22+
this.previousHash = previousHash;
23+
this.timeStamp = new Date().getTime();
24+
this.hash = calculateBlockHash();
25+
}
26+
27+
public String mineBlock(int prefix) {
28+
String prefixString = new String(new char[prefix]).replace('\0', '0');
29+
while (!hash.substring(0, prefix)
30+
.equals(prefixString)) {
31+
nonce++;
32+
hash = calculateBlockHash();
33+
}
34+
return hash;
35+
}
36+
37+
public String calculateBlockHash() {
38+
String dataToHash = previousHash + Long.toString(timeStamp) + Integer.toString(nonce) + data;
39+
MessageDigest digest = null;
40+
byte[] bytes = null;
41+
try {
42+
digest = MessageDigest.getInstance("SHA-256");
43+
bytes = digest.digest(dataToHash.getBytes("UTF-8"));
44+
} catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
45+
logger.log(Level.SEVERE, ex.getMessage());
46+
}
47+
StringBuffer buffer = new StringBuffer();
48+
for (byte b : bytes) {
49+
buffer.append(String.format("%02x", b));
50+
}
51+
return buffer.toString();
52+
}
53+
54+
public String getHash() {
55+
return this.hash;
56+
}
57+
58+
public String getPreviousHash() {
59+
return this.previousHash;
60+
}
61+
62+
public void setData(String data) {
63+
this.data = data;
64+
}
65+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
6+
</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<root level="INFO">
11+
<appender-ref ref="STDOUT" />
12+
</root>
13+
</configuration>

0 commit comments

Comments
 (0)