Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -0,0 +1 @@
|
||||
Hello World from fileTest.txt!!!
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.baeldung.file;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
|
||||
public class FileOperationsTest {
|
||||
|
||||
@Test
|
||||
public void givenFileName_whenUsingClassloader_thenFileData() throws IOException {
|
||||
String expectedData = "Hello World from fileTest.txt!!!";
|
||||
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
InputStream inputStream = classLoader.getResourceAsStream("fileTest.txt");
|
||||
String data = readFromInputStream(inputStream);
|
||||
|
||||
Assert.assertThat(data, containsString(expectedData));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFileNameAsAbsolutePath_whenUsingClasspath_thenFileData() throws IOException {
|
||||
String expectedData = "Hello World from fileTest.txt!!!";
|
||||
|
||||
Class clazz = FileOperationsTest.class;
|
||||
InputStream inputStream = clazz.getResourceAsStream("/fileTest.txt");
|
||||
String data = readFromInputStream(inputStream);
|
||||
|
||||
Assert.assertThat(data, containsString(expectedData));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenURLName_whenUsingURL_thenFileData() throws IOException {
|
||||
String expectedData = "Baeldung";
|
||||
|
||||
URL urlObject = new URL("http://www.baeldung.com/");
|
||||
URLConnection urlConnection = urlObject.openConnection();
|
||||
|
||||
InputStream inputStream = urlConnection.getInputStream();
|
||||
String data = readFromInputStream(inputStream);
|
||||
|
||||
Assert.assertThat(data, containsString(expectedData));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFileName_whenUsingJarFile_thenFileData() throws IOException {
|
||||
String expectedData = "BSD License";
|
||||
|
||||
Class clazz = Matchers.class;
|
||||
InputStream inputStream = clazz.getResourceAsStream("/LICENSE.txt");
|
||||
String data = readFromInputStream(inputStream);
|
||||
|
||||
Assert.assertThat(data, containsString(expectedData));
|
||||
}
|
||||
|
||||
private String readFromInputStream(InputStream inputStream) throws IOException {
|
||||
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
|
||||
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
|
||||
StringBuilder resultStringBuilder = new StringBuilder();
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
resultStringBuilder.append(line);
|
||||
resultStringBuilder.append("\n");
|
||||
}
|
||||
bufferedReader.close();
|
||||
inputStreamReader.close();
|
||||
inputStream.close();
|
||||
return resultStringBuilder.toString();
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public class JavaFolderSizeTest {
|
||||
@Before
|
||||
public void init() {
|
||||
final String separator = File.separator;
|
||||
path = "src" + separator + "test" + separator + "resources";
|
||||
path = String.format("src%stest%sresources", separator, separator);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -79,7 +79,9 @@ public class JavaFolderSizeTest {
|
||||
final File folder = new File(path);
|
||||
|
||||
final Iterable<File> files = com.google.common.io.Files.fileTreeTraverser().breadthFirstTraversal(folder);
|
||||
final long size = StreamSupport.stream(files.spliterator(), false).filter(f -> f.isFile()).mapToLong(File::length).sum();
|
||||
final long size = StreamSupport.stream(files.spliterator(), false)
|
||||
.filter(File::isFile)
|
||||
.mapToLong(File::length).sum();
|
||||
|
||||
assertEquals(expectedSize, size);
|
||||
}
|
||||
@@ -101,13 +103,11 @@ public class JavaFolderSizeTest {
|
||||
long length = 0;
|
||||
final File[] files = folder.listFiles();
|
||||
|
||||
final int count = files.length;
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (files[i].isFile()) {
|
||||
length += files[i].length();
|
||||
for (File file : files) {
|
||||
if (file.isFile()) {
|
||||
length += file.length();
|
||||
} else {
|
||||
length += getFolderSize(files[i]);
|
||||
length += getFolderSize(file);
|
||||
}
|
||||
}
|
||||
return length;
|
||||
|
||||
@@ -123,6 +123,12 @@
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>1.10</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.java.regex;
|
||||
|
||||
public class Result {
|
||||
private boolean found = false;
|
||||
private int count = 0;
|
||||
|
||||
public Result() {
|
||||
|
||||
}
|
||||
|
||||
public boolean isFound() {
|
||||
return found;
|
||||
}
|
||||
|
||||
public void setFound(boolean found) {
|
||||
this.found = found;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(int count) {
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,90 @@
|
||||
package org.baeldung.java.md5;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.hash.HashCode;
|
||||
import com.google.common.hash.Hashing;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.*;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class JavaMD5Test {
|
||||
|
||||
|
||||
String filename = "src/test/resources/test_md5.txt";
|
||||
String checksum = "5EB63BBBE01EEED093CB22BB8F5ACDC3";
|
||||
|
||||
String hash = "35454B055CC325EA1AF2126E27707052";
|
||||
String password = "ILoveJava";
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void givenPassword_whenHashing_thenVerifying() throws NoSuchAlgorithmException {
|
||||
String hash = "35454B055CC325EA1AF2126E27707052";
|
||||
String password = "ILoveJava";
|
||||
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
md.update(password.getBytes());
|
||||
byte[] digest = md.digest();
|
||||
String myHash = DatatypeConverter.printHexBinary(digest).toUpperCase();
|
||||
|
||||
assertThat(myHash.equals(hash)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFile_generatingChecksum_thenVerifying() throws NoSuchAlgorithmException, IOException {
|
||||
String filename = "src/test/resources/test_md5.txt";
|
||||
String checksum = "5EB63BBBE01EEED093CB22BB8F5ACDC3";
|
||||
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
md.update(Files.readAllBytes(Paths.get(filename)));
|
||||
byte[] digest = md.digest();
|
||||
String myChecksum = DatatypeConverter
|
||||
.printHexBinary(digest).toUpperCase();
|
||||
|
||||
assertThat(myChecksum.equals(checksum)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPassword_whenHashingUsingCommons_thenVerifying() {
|
||||
String hash = "35454B055CC325EA1AF2126E27707052";
|
||||
String password = "ILoveJava";
|
||||
|
||||
String md5Hex = DigestUtils
|
||||
.md5Hex(password).toUpperCase();
|
||||
|
||||
assertThat(md5Hex.equals(hash)).isTrue();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenFile_whenChecksumUsingGuava_thenVerifying() throws IOException {
|
||||
String filename = "src/test/resources/test_md5.txt";
|
||||
String checksum = "5EB63BBBE01EEED093CB22BB8F5ACDC3";
|
||||
|
||||
HashCode hash = com.google.common.io.Files
|
||||
.hash(new File(filename), Hashing.md5());
|
||||
String myChecksum = hash.toString()
|
||||
.toUpperCase();
|
||||
|
||||
assertThat(myChecksum.equals(checksum)).isTrue();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -96,6 +96,7 @@
|
||||
<module>spring-mvc-java</module>
|
||||
<module>spring-mvc-no-xml</module>
|
||||
<module>spring-mvc-xml</module>
|
||||
<module>spring-mvc-tiles</module>
|
||||
<module>spring-openid</module>
|
||||
<module>spring-protobuf</module>
|
||||
<module>spring-quartz</module>
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
<module>spring-cloud-config</module>
|
||||
<module>spring-cloud-eureka</module>
|
||||
<module>spring-cloud-hystrix</module>
|
||||
<module>spring-cloud-integration</module>
|
||||
<module>spring-cloud-bootstrap</module>
|
||||
</modules>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
||||
+3
-3
@@ -6,7 +6,7 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud-integration</artifactId>
|
||||
<artifactId>spring-cloud</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
@@ -17,9 +17,9 @@
|
||||
<module>resource</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>part-1</artifactId>
|
||||
|
||||
<artifactId>spring-cloud-integration</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -1,15 +0,0 @@
|
||||
<?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.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud-integration</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>part-1</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -0,0 +1,85 @@
|
||||
<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</groupId>
|
||||
<artifactId>spring-mvc-tiles</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
<name>spring-mvc-tiles</name>
|
||||
<description>Integrating Spring MVC with Apache Tiles</description>
|
||||
|
||||
<properties>
|
||||
<springframework.version>4.3.2.RELEASE</springframework.version>
|
||||
<apachetiles.version>3.0.5</apachetiles.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${springframework.version}</version>
|
||||
</dependency>
|
||||
<!-- Apache Tiles -->
|
||||
<dependency>
|
||||
<groupId>org.apache.tiles</groupId>
|
||||
<artifactId>tiles-jsp</artifactId>
|
||||
<version>${apachetiles.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Servlet+JSP+JSTL -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet.jsp</groupId>
|
||||
<artifactId>javax.servlet.jsp-api</artifactId>
|
||||
<version>2.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.2</version>
|
||||
<configuration>
|
||||
<source>1.7</source>
|
||||
<target>1.7</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>2.4</version>
|
||||
<configuration>
|
||||
<warSourceDirectory>src/main/webapp</warSourceDirectory>
|
||||
<warName>spring-mvc-tiles</warName>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
<finalName>spring-mvc-tiles</finalName>
|
||||
</build>
|
||||
</project>
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.tiles.springmvc;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = "com.baeldung.tiles.springmvc")
|
||||
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {
|
||||
|
||||
/**
|
||||
* Configure TilesConfigurer.
|
||||
*/
|
||||
@Bean
|
||||
public TilesConfigurer tilesConfigurer() {
|
||||
TilesConfigurer tilesConfigurer = new TilesConfigurer();
|
||||
tilesConfigurer.setDefinitions(new String[] { "/WEB-INF/views/**/tiles.xml" });
|
||||
tilesConfigurer.setCheckRefresh(true);
|
||||
return tilesConfigurer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure ViewResolvers to deliver views.
|
||||
*/
|
||||
@Override
|
||||
public void configureViewResolvers(ViewResolverRegistry registry) {
|
||||
TilesViewResolver viewResolver = new TilesViewResolver();
|
||||
registry.viewResolver(viewResolver);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure ResourceHandlers to serve static resources
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.tiles.springmvc;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/")
|
||||
public class ApplicationController {
|
||||
|
||||
@RequestMapping(value = { "/" }, method = RequestMethod.GET)
|
||||
public String homePage(ModelMap model) {
|
||||
return "home";
|
||||
}
|
||||
|
||||
@RequestMapping(value = { "/apachetiles" }, method = RequestMethod.GET)
|
||||
public String productsPage(ModelMap model) {
|
||||
return "apachetiles";
|
||||
}
|
||||
|
||||
@RequestMapping(value = { "/springmvc" }, method = RequestMethod.GET)
|
||||
public String contactUsPage(ModelMap model) {
|
||||
return "springmvc";
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.tiles.springmvc;
|
||||
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return new Class[] { ApplicationConfiguration.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[] { "/" };
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user