Merge with main eugen branch

This commit is contained in:
michal_aibin 2016-11-12 22:15:57 +01:00
parent 52a66320cb
commit 94bfcd3b26
12 changed files with 697 additions and 0 deletions

View File

@ -0,0 +1,46 @@
package com.baeldung.hexToAscii;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class HexToAscii {
@Test
public static void whenHexToAscii() {
String asciiString = "http://www.baeldung.com/jackson-serialize-dates";
String hexEquivalent = "687474703a2f2f7777772e6261656c64756e672e636f6d2f6a61636b736f6e2d73657269616c697a652d6461746573";
assertEquals(asciiString, hexToAscii(hexEquivalent));
}
@Test
public static void whenAsciiToHex() {
String asciiString = "http://www.baeldung.com/jackson-serialize-dates";
String hexEquivalent = "687474703a2f2f7777772e6261656c64756e672e636f6d2f6a61636b736f6e2d73657269616c697a652d6461746573";
assertEquals(hexEquivalent, asciiToHex(asciiString));
}
//
private static String asciiToHex(String asciiStr) {
char[] chars = asciiStr.toCharArray();
StringBuilder hex = new StringBuilder();
for (char ch : chars) {
hex.append(Integer.toHexString((int) ch));
}
return hex.toString();
}
private static String hexToAscii(String hexStr) {
StringBuilder output = new StringBuilder("");
for (int i = 0; i < hexStr.length(); i += 2) {
String str = hexStr.substring(i, i + 2);
output.append((char) Integer.parseInt(str, 16));
}
return output.toString();
}
}

View File

@ -0,0 +1,63 @@
package org.baeldung.equalshashcode.entities;
import java.util.List;
import java.util.Set;
public class ComplexClass {
private List<?> genericList;
private Set<Integer> integerSet;
public ComplexClass(List<?> genericArrayList, Set<Integer> integerHashSet) {
super();
this.genericList = genericArrayList;
this.integerSet = integerHashSet;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((genericList == null) ? 0 : genericList.hashCode());
result = prime * result + ((integerSet == null) ? 0 : integerSet.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof ComplexClass))
return false;
ComplexClass other = (ComplexClass) obj;
if (genericList == null) {
if (other.genericList != null)
return false;
} else if (!genericList.equals(other.genericList))
return false;
if (integerSet == null) {
if (other.integerSet != null)
return false;
} else if (!integerSet.equals(other.integerSet))
return false;
return true;
}
protected List<?> getGenericList() {
return genericList;
}
protected void setGenericArrayList(List<?> genericList) {
this.genericList = genericList;
}
protected Set<Integer> getIntegerSet() {
return integerSet;
}
protected void setIntegerSet(Set<Integer> integerSet) {
this.integerSet = integerSet;
}
}

View File

@ -0,0 +1,54 @@
package org.baeldung.equalshashcode.entities;
public class PrimitiveClass {
private boolean primitiveBoolean;
private int primitiveInt;
public PrimitiveClass(boolean primitiveBoolean, int primitiveInt) {
super();
this.primitiveBoolean = primitiveBoolean;
this.primitiveInt = primitiveInt;
}
protected boolean isPrimitiveBoolean() {
return primitiveBoolean;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (primitiveBoolean ? 1231 : 1237);
result = prime * result + primitiveInt;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PrimitiveClass other = (PrimitiveClass) obj;
if (primitiveBoolean != other.primitiveBoolean)
return false;
if (primitiveInt != other.primitiveInt)
return false;
return true;
}
protected void setPrimitiveBoolean(boolean primitiveBoolean) {
this.primitiveBoolean = primitiveBoolean;
}
protected int getPrimitiveInt() {
return primitiveInt;
}
protected void setPrimitiveInt(int primitiveInt) {
this.primitiveInt = primitiveInt;
}
}

View File

@ -0,0 +1,58 @@
package org.baeldung.equalshashcode.entities;
public class Rectangle extends Shape {
private double width;
private double length;
public Rectangle(double width, double length) {
this.width = width;
this.length = length;
}
@Override
public double area() {
return width * length;
}
@Override
public double perimeter() {
return 2 * (width + length);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(length);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(width);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Rectangle other = (Rectangle) obj;
if (Double.doubleToLongBits(length) != Double.doubleToLongBits(other.length))
return false;
if (Double.doubleToLongBits(width) != Double.doubleToLongBits(other.width))
return false;
return true;
}
protected double getWidth() {
return width;
}
protected double getLength() {
return length;
}
}

View File

@ -0,0 +1,7 @@
package org.baeldung.equalshashcode.entities;
public abstract class Shape {
public abstract double area();
public abstract double perimeter();
}

View File

@ -0,0 +1,58 @@
package org.baeldung.equalshashcode.entities;
import java.awt.Color;
public class Square extends Rectangle {
Color color;
public Square(double width, Color color) {
super(width, width);
this.color = color;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((color == null) ? 0 : color.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (!(obj instanceof Square)) {
return false;
}
Square other = (Square) obj;
if (color == null) {
if (other.color != null) {
return false;
}
} else if (!color.equals(other.color)) {
return false;
}
return true;
}
protected Color getColor() {
return color;
}
protected void setColor(Color color) {
this.color = color;
}
}

View File

@ -0,0 +1,11 @@
package org.baeldung.executable;
import javax.swing.JOptionPane;
public class ExecutableMavenJar {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "It worked!", "Executable Jar with Maven", 1);
}
}

View File

@ -0,0 +1,122 @@
package com.baeldung.java.networking.interfaces;
import static org.junit.Assert.*;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.List;
import org.junit.Test;
public class NetworkInterfaceTest {
@Test
public void givenName_whenReturnsNetworkInterface_thenCorrect() throws SocketException {
NetworkInterface nif = NetworkInterface.getByName("lo");
assertNotNull(nif);
}
@Test
public void givenInExistentName_whenReturnsNull_thenCorrect() throws SocketException {
NetworkInterface nif = NetworkInterface.getByName("inexistent_name");
assertNull(nif);
}
@Test
public void givenIP_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException {
byte[] ip = new byte[] { 127, 0, 0, 1 };
NetworkInterface nif = NetworkInterface.getByInetAddress(InetAddress.getByAddress(ip));
assertNotNull(nif);
}
@Test
public void givenHostName_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException {
NetworkInterface nif = NetworkInterface.getByInetAddress(InetAddress.getByName("localhost"));
assertNotNull(nif);
}
@Test
public void givenLocalHost_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException {
NetworkInterface nif = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
assertNotNull(nif);
}
@Test
public void givenLoopBack_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException {
NetworkInterface nif = NetworkInterface.getByInetAddress(InetAddress.getLoopbackAddress());
assertNotNull(nif);
}
@Test
public void givenIndex_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException {
NetworkInterface nif = NetworkInterface.getByIndex(0);
assertNotNull(nif);
}
@Test
public void givenInterface_whenReturnsInetAddresses_thenCorrect() throws SocketException, UnknownHostException {
NetworkInterface nif = NetworkInterface.getByName("lo");
Enumeration<InetAddress> addressEnum = nif.getInetAddresses();
InetAddress address = addressEnum.nextElement();
assertEquals("127.0.0.1", address.getHostAddress());
}
@Test
public void givenInterface_whenReturnsInterfaceAddresses_thenCorrect() throws SocketException, UnknownHostException {
NetworkInterface nif = NetworkInterface.getByName("lo");
List<InterfaceAddress> addressEnum = nif.getInterfaceAddresses();
InterfaceAddress address = addressEnum.get(0);
InetAddress localAddress = address.getAddress();
InetAddress broadCastAddress = address.getBroadcast();
assertEquals("127.0.0.1", localAddress.getHostAddress());
assertEquals("127.255.255.255", broadCastAddress.getHostAddress());
}
@Test
public void givenInterface_whenChecksIfLoopback_thenCorrect() throws SocketException, UnknownHostException {
NetworkInterface nif = NetworkInterface.getByName("lo");
assertTrue(nif.isLoopback());
}
@Test
public void givenInterface_whenChecksIfUp_thenCorrect() throws SocketException, UnknownHostException {
NetworkInterface nif = NetworkInterface.getByName("lo");
assertTrue(nif.isUp());
}
@Test
public void givenInterface_whenChecksIfPointToPoint_thenCorrect() throws SocketException, UnknownHostException {
NetworkInterface nif = NetworkInterface.getByName("lo");
assertFalse(nif.isPointToPoint());
}
@Test
public void givenInterface_whenChecksIfVirtual_thenCorrect() throws SocketException, UnknownHostException {
NetworkInterface nif = NetworkInterface.getByName("lo");
assertFalse(nif.isVirtual());
}
@Test
public void givenInterface_whenChecksMulticastSupport_thenCorrect() throws SocketException, UnknownHostException {
NetworkInterface nif = NetworkInterface.getByName("lo");
assertTrue(nif.supportsMulticast());
}
@Test
public void givenInterface_whenGetsMacAddress_thenCorrect() throws SocketException, UnknownHostException {
NetworkInterface nif = NetworkInterface.getByName("lo");
byte[] bytes = nif.getHardwareAddress();
assertNotNull(bytes);
}
@Test
public void givenInterface_whenGetsMTU_thenCorrect() throws SocketException, UnknownHostException {
NetworkInterface nif = NetworkInterface.getByName("net0");
int mtu = nif.getMTU();
assertEquals(1500, mtu);
}
}

View File

@ -0,0 +1,195 @@
package com.baeldung.java.nio2;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.net.URI;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import org.junit.Test;
public class PathTest {
private static final String HOME = System.getProperty("user.home");
// creating a path
@Test
public void givenPathString_whenCreatesPathObject_thenCorrect() {
Path p = Paths.get("/articles/baeldung");
assertEquals("\\articles\\baeldung", p.toString());
}
@Test
public void givenPathParts_whenCreatesPathObject_thenCorrect() {
Path p = Paths.get("/articles", "baeldung");
assertEquals("\\articles\\baeldung", p.toString());
}
// retrieving path info
@Test
public void givenPath_whenRetrievesFileName_thenCorrect() {
Path p = Paths.get("/articles/baeldung/logs");
assertEquals("logs", p.getFileName().toString());
}
@Test
public void givenPath_whenRetrievesNameByIndex_thenCorrect() {
Path p = Paths.get("/articles/baeldung/logs");
assertEquals("articles", p.getName(0).toString());
assertEquals("baeldung", p.getName(1).toString());
assertEquals("logs", p.getName(2).toString());
}
@Test
public void givenPath_whenCountsParts_thenCorrect() {
Path p = Paths.get("/articles/baeldung/logs");
assertEquals(3, p.getNameCount());
}
@Test
public void givenPath_whenCanRetrieveSubsequenceByIndex_thenCorrect() {
Path p = Paths.get("/articles/baeldung/logs");
assertEquals("articles", p.subpath(0, 1).toString());
assertEquals("articles\\baeldung", p.subpath(0, 2).toString());
assertEquals("articles\\baeldung\\logs", p.subpath(0, 3).toString());
assertEquals("baeldung", p.subpath(1, 2).toString());
assertEquals("baeldung\\logs", p.subpath(1, 3).toString());
assertEquals("logs", p.subpath(2, 3).toString());
}
@Test
public void givenPath_whenRetrievesParent_thenCorrect() {
Path p1 = Paths.get("/articles/baeldung/logs");
Path p2 = Paths.get("/articles/baeldung");
Path p3 = Paths.get("/articles");
Path p4 = Paths.get("/");
assertEquals("\\articles\\baeldung", p1.getParent().toString());
assertEquals("\\articles", p2.getParent().toString());
assertEquals("\\", p3.getParent().toString());
assertEquals(null, p4.getParent());
}
@Test
public void givenPath_whenRetrievesRoot_thenCorrect() {
Path p1 = Paths.get("/articles/baeldung/logs");
Path p2 = Paths.get("c:/articles/baeldung/logs");
assertEquals("\\", p1.getRoot().toString());
assertEquals("c:\\", p2.getRoot().toString());
}
// removing redundancies from path
@Test
public void givenPath_whenRemovesRedundancies_thenCorrect1() {
Path p = Paths.get("/home/./baeldung/articles");
p = p.normalize();
assertEquals("\\home\\baeldung\\articles", p.toString());
}
@Test
public void givenPath_whenRemovesRedundancies_thenCorrect2() {
Path p = Paths.get("/home/baeldung/../articles");
p = p.normalize();
assertEquals("\\home\\articles", p.toString());
}
// converting a path
@Test
public void givenPath_whenConvertsToBrowseablePath_thenCorrect() {
Path p = Paths.get("/home/baeldung/articles.html");
URI uri = p.toUri();
assertEquals("file:///E:/home/baeldung/articles.html", uri.toString());
}
@Test
public void givenPath_whenConvertsToAbsolutePath_thenCorrect() {
Path p = Paths.get("/home/baeldung/articles.html");
assertEquals("E:\\home\\baeldung\\articles.html", p.toAbsolutePath().toString());
}
@Test
public void givenAbsolutePath_whenRetainsAsAbsolute_thenCorrect() {
Path p = Paths.get("E:\\home\\baeldung\\articles.html");
assertEquals("E:\\home\\baeldung\\articles.html", p.toAbsolutePath().toString());
}
@Test
public void givenExistingPath_whenGetsRealPathToFile_thenCorrect() throws IOException {
Path p = Paths.get(HOME);
assertEquals(HOME, p.toRealPath().toString());
}
@Test(expected = NoSuchFileException.class)
public void givenInExistentPath_whenFailsToConvert_thenCorrect() throws IOException {
Path p = Paths.get("E:\\home\\baeldung\\articles.html");
p.toRealPath();
}
// joining paths
@Test
public void givenTwoPaths_whenJoinsAndResolves_thenCorrect() throws IOException {
Path p = Paths.get("/baeldung/articles");
assertEquals("\\baeldung\\articles\\java", p.resolve("java").toString());
}
@Test
public void givenAbsolutePath_whenResolutionRetainsIt_thenCorrect() throws IOException {
Path p = Paths.get("/baeldung/articles");
assertEquals("C:\\baeldung\\articles\\java", p.resolve("C:\\baeldung\\articles\\java").toString());
}
@Test
public void givenPathWithRoot_whenResolutionRetainsIt_thenCorrect2() throws IOException {
Path p = Paths.get("/baeldung/articles");
assertEquals("\\java", p.resolve("/java").toString());
}
// creating a path between 2 paths
@Test
public void givenSiblingPaths_whenCreatesPathToOther_thenCorrect() throws IOException {
Path p1 = Paths.get("articles");
Path p2 = Paths.get("authors");
assertEquals("..\\authors", p1.relativize(p2).toString());
assertEquals("..\\articles", p2.relativize(p1).toString());
}
@Test
public void givenNonSiblingPaths_whenCreatesPathToOther_thenCorrect() throws IOException {
Path p1 = Paths.get("/baeldung");
Path p2 = Paths.get("/baeldung/authors/articles");
assertEquals("authors\\articles", p1.relativize(p2).toString());
assertEquals("..\\..", p2.relativize(p1).toString());
}
// comparing 2 paths
@Test
public void givenTwoPaths_whenTestsEquality_thenCorrect() throws IOException {
Path p1 = Paths.get("/baeldung/articles");
Path p2 = Paths.get("/baeldung/articles");
Path p3 = Paths.get("/baeldung/authors");
assertTrue(p1.equals(p2));
assertFalse(p1.equals(p3));
}
@Test
public void givenPath_whenInspectsStart_thenCorrect() {
Path p1 = Paths.get("/baeldung/articles");
assertTrue(p1.startsWith("/baeldung"));
}
@Test
public void givenPath_whenInspectsEnd_thenCorrect() {
Path p1 = Paths.get("/baeldung/articles");
assertTrue(p1.endsWith("articles"));
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.enterprise.patterns.front.controller.filters;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class AuditFilter extends BaseFilter {
@Override
public void doFilter(
ServletRequest request,
ServletResponse response,
FilterChain chain
) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpSession session = httpServletRequest.getSession(false);
if (session != null && session.getAttribute("username") != null) {
request.setAttribute("username", session.getAttribute("username"));
}
chain.doFilter(request, response);
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.enterprise.patterns.front.controller.filters;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(servletNames = "front-controller")
public class VisitorCounterFilter extends BaseFilter {
private int counter;
@Override
public void doFilter(
ServletRequest request,
ServletResponse response,
FilterChain chain
) throws IOException, ServletException {
request.setAttribute("counter", ++counter);
chain.doFilter(request, response);
}
}

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.enterprise.patterns</groupId>
<artifactId>enterprise-patterns-parent</artifactId>
<packaging>pom</packaging>
<modules>
<module>spring-dispatcher-servlet</module>
</modules>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>