From fa4240311ddab4a4bbfb317927f6286b145c254e Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 28 Sep 2014 15:16:46 +0300 Subject: [PATCH 1/3] small persistence fixes --- .../java/sandbox/SandboxJavaTest.java | 72 ++++++++++++++++--- .../src/main/resources/application.properties | 4 +- 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/core-java/src/test/java/org/baeldung/java/sandbox/SandboxJavaTest.java b/core-java/src/test/java/org/baeldung/java/sandbox/SandboxJavaTest.java index f746ce63e2..5127ccb10c 100644 --- a/core-java/src/test/java/org/baeldung/java/sandbox/SandboxJavaTest.java +++ b/core-java/src/test/java/org/baeldung/java/sandbox/SandboxJavaTest.java @@ -9,21 +9,75 @@ import org.junit.Test; public class SandboxJavaTest { @Test - public void givenUsingTimer_whenSchedulingTaskOnce_thenCorrect() throws InterruptedException { - final Timer timer = new Timer("Thread's name"); - System.out.println("Current time:" + new Date()); - System.out.println("Thread name: " + Thread.currentThread().getName()); - + public void givenUsingTimer_whenSchedulingTimerTaskOnce_thenCorrect() throws InterruptedException { final TimerTask timerTask = new TimerTask() { @Override public void run() { - System.out.println("Time when task performed" + new Date()); - System.out.println("Thread name: " + Thread.currentThread().getName()); - timer.cancel(); + System.out.println("Time when was task performed" + new Date()); + System.out.println("Thread's name: " + Thread.currentThread().getName()); } }; - final long delay = 2 * 1000; + final Timer timer = new Timer("Thread's name"); + System.out.println("Current time:" + new Date()); + System.out.println("Thread's name: " + Thread.currentThread().getName()); + final long delay = 2L * 1000L; timer.schedule(timerTask, delay); + Thread.sleep(delay); + } + + @Test + public void givenUsingTimer_whenSchedulingRepeatedTask_thenCorrect() throws InterruptedException { + final TimerTask repeatedTask = new TimerTask() { + int count = 0; + + @Override + public void run() { + count++; + System.out.println("Time when task was performed: " + new Date()); + System.out.println("Thread's name: " + Thread.currentThread().getName()); + if (count >= 5) { + cancel(); + } + } + }; + final Timer timer = new Timer("Timer thread"); + System.out.println("Current time: " + new Date()); + System.out.println("Thread's name: " + Thread.currentThread().getName()); + final long delay = 2L * 1000L; + final long period = 1L * 1000L; + timer.scheduleAtFixedRate(repeatedTask, delay, period); + Thread.sleep(delay + period * 5L); + } + + @Test + public void givenUsingTimer_whenSchedulingRepeatedCustomTimerTask_thenCorrect() throws InterruptedException { + class MyTask extends TimerTask { + long timesToRun = 0; + long timesRunned = 0; + + public void setTimesToRun(final int timesToRun) { + this.timesToRun = timesToRun; + } + + @Override + public void run() { + timesRunned++; + System.out.println("Task performed on: " + new Date()); + System.out.println("Thread's name: " + Thread.currentThread().getName()); + if (timesRunned >= timesToRun) { + cancel(); + } + } + } + final MyTask repeatedTask = new MyTask(); + repeatedTask.setTimesToRun(5); + final long delay = 2L * 1000L; + final long period = 1L * 1000L; + final Timer timer = new Timer("Timer"); + System.out.println("Current time: " + new Date()); + System.out.println("Thread's name: " + Thread.currentThread().getName()); + timer.scheduleAtFixedRate(repeatedTask, delay, period); + Thread.sleep(delay + period * repeatedTask.timesToRun); } } diff --git a/spring-security-login-error-handling/src/main/resources/application.properties b/spring-security-login-error-handling/src/main/resources/application.properties index 70a8ae5daa..f80dba9ba5 100644 --- a/spring-security-login-error-handling/src/main/resources/application.properties +++ b/spring-security-login-error-handling/src/main/resources/application.properties @@ -1,10 +1,10 @@ ################### DataSource Configuration ########################## jdbc.driverClassName=com.mysql.jdbc.Driver -jdbc.url=jdbc:mysql://localhost:3306/AUTHDATA +jdbc.url=jdbc:mysql://localhost:3306/authdata?createDatabaseIfNotExist=true jdbc.user=tutorialuser jdbc.pass=tutorialmy5ql init-db=false ################### Hibernate Configuration ########################## hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true -hibernate.hbm2ddl.auto=validate +hibernate.hbm2ddl.auto=create-drop From 0e3c0f36df187e7319b13bf7985d9fcf82bb7c69 Mon Sep 17 00:00:00 2001 From: eugenp Date: Tue, 30 Sep 2014 01:42:55 +0300 Subject: [PATCH 2/3] cleanup work --- .../java/org/baeldung/java/io/TestWriter.java | 146 +++++++++--------- 1 file changed, 69 insertions(+), 77 deletions(-) diff --git a/core-java/src/test/java/org/baeldung/java/io/TestWriter.java b/core-java/src/test/java/org/baeldung/java/io/TestWriter.java index b8895f7f0a..0c794d0809 100644 --- a/core-java/src/test/java/org/baeldung/java/io/TestWriter.java +++ b/core-java/src/test/java/org/baeldung/java/io/TestWriter.java @@ -1,4 +1,4 @@ -package test; +package org.baeldung.java.io; import java.io.BufferedOutputStream; import java.io.BufferedReader; @@ -32,167 +32,159 @@ public class TestWriter extends TestCase { private String fileName4 = "test4.txt"; private String fileName5 = "test5.txt"; - public TestWriter(String name) { + public TestWriter(final String name) { super(name); } + @Override protected void setUp() throws Exception { super.setUp(); } + @Override protected void tearDown() throws Exception { super.tearDown(); } - - - public void whenWriteStringUsingBufferedWritter_thenCorrect() throws IOException{ - String str = "Hello"; - BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3)); + + public void whenWriteStringUsingBufferedWritter_thenCorrect() throws IOException { + final String str = "Hello"; + final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3)); writer.write(str); writer.close(); } - - public void whenAppendStringUsingBufferedWritter_thenOldContentShouldExistToo() throws IOException{ - String str = "World"; - BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3 , true)); + + public void whenAppendStringUsingBufferedWritter_thenOldContentShouldExistToo() throws IOException { + final String str = "World"; + final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3, true)); writer.append(' '); writer.append(str); writer.close(); } - - public void whenWriteFormattedStringUsingPrintWriter_thenOutputShouldBeFormatted() throws IOException{ - FileWriter fileWriter = new FileWriter(fileName); - PrintWriter printWriter = new PrintWriter(fileWriter); - printWriter.printf("Product name is %s and its price is %d $","iPhone",1000); + + public void whenWriteFormattedStringUsingPrintWriter_thenOutputShouldBeFormatted() throws IOException { + final FileWriter fileWriter = new FileWriter(fileName); + final PrintWriter printWriter = new PrintWriter(fileWriter); + printWriter.printf("Product name is %s and its price is %d $", "iPhone", 1000); printWriter.close(); } - - public void whenWriteStringUsingFileOutputStream_thenCorrect() throws IOException{ - String str = "Hello"; - FileOutputStream outputStream = new FileOutputStream(fileName3); - byte[] strToBytes = str.getBytes(); + + public void whenWriteStringUsingFileOutputStream_thenCorrect() throws IOException { + final String str = "Hello"; + final FileOutputStream outputStream = new FileOutputStream(fileName3); + final byte[] strToBytes = str.getBytes(); outputStream.write(strToBytes); outputStream.close(); } public void whenWriteStringWithDataOutputStream_thenReadShouldBeTheSame() throws IOException { - String value = "Hello"; - FileOutputStream fos = new FileOutputStream(fileName1); - DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos)); + final String value = "Hello"; + final FileOutputStream fos = new FileOutputStream(fileName1); + final DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos)); outStream.writeUTF(value); outStream.close(); - + String result; - FileInputStream fis = new FileInputStream(fileName1); - DataInputStream reader = new DataInputStream(fis); + final FileInputStream fis = new FileInputStream(fileName1); + final DataInputStream reader = new DataInputStream(fis); result = reader.readUTF(); reader.close(); - + assertEquals(value, result); } public void whenWriteToPositionAndEditIt_thenItShouldChange() { - int data1 = 2014; - int data2 = 1500; + final int data1 = 2014; + final int data2 = 1500; testWriteToPosition(data1, 4); assertEquals(data1, testReadFromPosition(4)); testWriteToPosition(data2, 4); assertEquals(data2, testReadFromPosition(4)); } - public void whenTryToLockFile_thenItShouldBeLocked() throws IOException { - RandomAccessFile stream = new RandomAccessFile(fileName4, "rw"); - FileChannel channel = stream.getChannel(); + final RandomAccessFile stream = new RandomAccessFile(fileName4, "rw"); + final FileChannel channel = stream.getChannel(); FileLock lock = null; try { lock = channel.tryLock(); - } catch (OverlappingFileLockException e) { + } catch (final OverlappingFileLockException e) { stream.close(); channel.close(); } stream.writeChars("test lock"); lock.release(); - + stream.close(); channel.close(); } - - - public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame() throws IOException{ - RandomAccessFile stream = new RandomAccessFile(fileName5, "rw"); - FileChannel channel = stream.getChannel(); - String value = "Hello"; - byte[] strBytes = value.getBytes(); - ByteBuffer buffer = ByteBuffer.allocate(strBytes.length); + public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame() throws IOException { + final RandomAccessFile stream = new RandomAccessFile(fileName5, "rw"); + final FileChannel channel = stream.getChannel(); + final String value = "Hello"; + final byte[] strBytes = value.getBytes(); + final ByteBuffer buffer = ByteBuffer.allocate(strBytes.length); buffer.put(strBytes); buffer.flip(); - channel.write(buffer); + channel.write(buffer); stream.close(); channel.close(); - - RandomAccessFile reader = new RandomAccessFile(fileName5, "r"); - assertEquals(value , reader.readLine()); + + final RandomAccessFile reader = new RandomAccessFile(fileName5, "r"); + assertEquals(value, reader.readLine()); reader.close(); - + } - - public void whenWriteToTmpFile_thenCorrect() throws IOException{ - String toWrite = "Hello"; - File tmpFile = File.createTempFile("test", ".tmp"); - FileWriter writer = new FileWriter(tmpFile); + + public void whenWriteToTmpFile_thenCorrect() throws IOException { + final String toWrite = "Hello"; + final File tmpFile = File.createTempFile("test", ".tmp"); + final FileWriter writer = new FileWriter(tmpFile); writer.write(toWrite); writer.close(); - - BufferedReader reader = new BufferedReader(new FileReader(tmpFile)); + + final BufferedReader reader = new BufferedReader(new FileReader(tmpFile)); assertEquals(toWrite, reader.readLine()); reader.close(); } - - public void whenWriteUsingJava7_thenCorrect() throws IOException{ - String str = "Hello"; - - Path path = Paths.get(fileName3); - byte[] strToBytes = str.getBytes(); - + + public void whenWriteUsingJava7_thenCorrect() throws IOException { + final String str = "Hello"; + + final Path path = Paths.get(fileName3); + final byte[] strToBytes = str.getBytes(); + Files.write(path, strToBytes); - - String read = Files.readAllLines(path).get(0); + + final String read = Files.readAllLines(path).get(0); assertEquals(str, read); } - + // use RandomAccessFile to write data at specific position in the file - public void testWriteToPosition(int data, long position) { + public void testWriteToPosition(final int data, final long position) { try { - RandomAccessFile writer = new RandomAccessFile(fileName2, "rw"); + final RandomAccessFile writer = new RandomAccessFile(fileName2, "rw"); writer.seek(position); writer.writeInt(data); writer.close(); - } catch (Exception e) { + } catch (final Exception e) { System.out.println(e.getLocalizedMessage()); } } - - // use RandomAccessFile to read data from specific position in the file - public int testReadFromPosition(long position) { + public int testReadFromPosition(final long position) { int result = 0; try { - RandomAccessFile reader = new RandomAccessFile(fileName2, "r"); + final RandomAccessFile reader = new RandomAccessFile(fileName2, "r"); reader.seek(position); result = reader.readInt(); reader.close(); - } catch (Exception e) { + } catch (final Exception e) { System.out.println(e.getLocalizedMessage()); } return result; } - - - - } From 426af122003f14851a997fb885aabf802fc02312 Mon Sep 17 00:00:00 2001 From: eugenp Date: Tue, 30 Sep 2014 01:47:23 +0300 Subject: [PATCH 3/3] testing work for writing to fiule --- .../java/org/baeldung/java/io/TestWriter.java | 60 ++++++++----------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/core-java/src/test/java/org/baeldung/java/io/TestWriter.java b/core-java/src/test/java/org/baeldung/java/io/TestWriter.java index 0c794d0809..43fd0c148f 100644 --- a/core-java/src/test/java/org/baeldung/java/io/TestWriter.java +++ b/core-java/src/test/java/org/baeldung/java/io/TestWriter.java @@ -1,5 +1,7 @@ package org.baeldung.java.io; +import static org.junit.Assert.assertEquals; + import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -21,9 +23,9 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import junit.framework.TestCase; +import org.junit.Test; -public class TestWriter extends TestCase { +public class TestWriter { private String fileName = "test.txt"; private String fileName1 = "test1.txt"; @@ -32,20 +34,7 @@ public class TestWriter extends TestCase { private String fileName4 = "test4.txt"; private String fileName5 = "test5.txt"; - public TestWriter(final String name) { - super(name); - } - - @Override - protected void setUp() throws Exception { - super.setUp(); - } - - @Override - protected void tearDown() throws Exception { - super.tearDown(); - } - + @Test public void whenWriteStringUsingBufferedWritter_thenCorrect() throws IOException { final String str = "Hello"; final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3)); @@ -53,6 +42,7 @@ public class TestWriter extends TestCase { writer.close(); } + @Test public void whenAppendStringUsingBufferedWritter_thenOldContentShouldExistToo() throws IOException { final String str = "World"; final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3, true)); @@ -61,6 +51,7 @@ public class TestWriter extends TestCase { writer.close(); } + @Test public void whenWriteFormattedStringUsingPrintWriter_thenOutputShouldBeFormatted() throws IOException { final FileWriter fileWriter = new FileWriter(fileName); final PrintWriter printWriter = new PrintWriter(fileWriter); @@ -68,6 +59,7 @@ public class TestWriter extends TestCase { printWriter.close(); } + @Test public void whenWriteStringUsingFileOutputStream_thenCorrect() throws IOException { final String str = "Hello"; final FileOutputStream outputStream = new FileOutputStream(fileName3); @@ -76,6 +68,7 @@ public class TestWriter extends TestCase { outputStream.close(); } + @Test public void whenWriteStringWithDataOutputStream_thenReadShouldBeTheSame() throws IOException { final String value = "Hello"; final FileOutputStream fos = new FileOutputStream(fileName1); @@ -92,7 +85,8 @@ public class TestWriter extends TestCase { assertEquals(value, result); } - public void whenWriteToPositionAndEditIt_thenItShouldChange() { + @Test + public void whenWriteToPositionAndEditIt_thenItShouldChange() throws IOException { final int data1 = 2014; final int data2 = 1500; testWriteToPosition(data1, 4); @@ -101,6 +95,7 @@ public class TestWriter extends TestCase { assertEquals(data2, testReadFromPosition(4)); } + @Test public void whenTryToLockFile_thenItShouldBeLocked() throws IOException { final RandomAccessFile stream = new RandomAccessFile(fileName4, "rw"); final FileChannel channel = stream.getChannel(); @@ -119,6 +114,7 @@ public class TestWriter extends TestCase { channel.close(); } + @Test public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame() throws IOException { final RandomAccessFile stream = new RandomAccessFile(fileName5, "rw"); final FileChannel channel = stream.getChannel(); @@ -137,6 +133,7 @@ public class TestWriter extends TestCase { } + @Test public void whenWriteToTmpFile_thenCorrect() throws IOException { final String toWrite = "Hello"; final File tmpFile = File.createTempFile("test", ".tmp"); @@ -149,6 +146,7 @@ public class TestWriter extends TestCase { reader.close(); } + @Test public void whenWriteUsingJava7_thenCorrect() throws IOException { final String str = "Hello"; @@ -162,28 +160,20 @@ public class TestWriter extends TestCase { } // use RandomAccessFile to write data at specific position in the file - public void testWriteToPosition(final int data, final long position) { - try { - final RandomAccessFile writer = new RandomAccessFile(fileName2, "rw"); - writer.seek(position); - writer.writeInt(data); - writer.close(); - } catch (final Exception e) { - System.out.println(e.getLocalizedMessage()); - } + private void testWriteToPosition(final int data, final long position) throws IOException { + final RandomAccessFile writer = new RandomAccessFile(fileName2, "rw"); + writer.seek(position); + writer.writeInt(data); + writer.close(); } // use RandomAccessFile to read data from specific position in the file - public int testReadFromPosition(final long position) { + private int testReadFromPosition(final long position) throws IOException { int result = 0; - try { - final RandomAccessFile reader = new RandomAccessFile(fileName2, "r"); - reader.seek(position); - result = reader.readInt(); - reader.close(); - } catch (final Exception e) { - System.out.println(e.getLocalizedMessage()); - } + final RandomAccessFile reader = new RandomAccessFile(fileName2, "r"); + reader.seek(position); + result = reader.readInt(); + reader.close(); return result; }