Recent Notes

Displaying keyword search results 1 - 10
Created by Fang on March 30, 2012 10:07:25    Last update: March 08, 2013 13:41:57
After a user resets a password, I want to force the user to change the password before she gets access to secured content. This is usually done with a servlet filter. But with Spring MVC, you can also use a HandlerInterceptor . According to Spring JavaDoc: HandlerInterceptor is basically similar to a Servlet 2.3 Filter, but in contrast to the latter it just allows custom pre-processing with the option of prohibiting the execution of the handler itself, and custom post-processing. Filters are more powerful, for example they allow for exchanging the request and response objects that are handed down the chain. Note that a filter gets configured in web.xml, a HandlerInterceptor in the application context. As a basic guideline, fine-grained handler-related preprocessing tasks are candidates...
Created by Fang on January 04, 2013 14:16:58    Last update: January 04, 2013 14:16:58
Junit does not support specifying execution order of tests until 4.11. The methods were simply invoked in the order returned by the reflection API. So, the tests are executed in a unspecified but deterministic order, i.e., you have no control over the order of execution, but if you repeat the tests, they are run in the same sequence each time. For version 4.11, you can specify the order with the FixMethodOrder annotation: import org.junit.runners.MethodSorters; imp... From the release notes : Test execution order By design, JUnit does not specify the execution order of test method invocations. Until now, the methods were simply invoked in the order returned by the reflection API. However, using the JVM order is unwise since the Java platform does not specify...
Created by Fang on September 07, 2009 20:44:15    Last update: November 03, 2011 14:43:19
Step 1: Repackage a web app as EAR A Java EE application is a multimodule Maven project. At the very least you'll need to package a WAR and an EAR. To get started, I'll simply re-package the simple webapp as an EAR. Create a directory named javaee-app Copy the webapp from here to javaee-app . Rename struts1app to webapp . Create pom.xml under javaee-app : <project> <modelVersion>4.0.0</modelVersion>... Create a directory named ear under javaee-app . Create pom.xml under ear : <project> <modelVersion>4.0.0</modelVersion>... Modify pom.xml in the webapp directory so that it looks like this: <project> <modelVersion>4.0.0</modelVersion> ... Build with " mvn package " in the javaee-app directory. You can see that ear-1.0.ear is successfully generated in javaee-app/ear/target . Maven successfully resolves dependencies between the sub-projects....
Created by freyo on September 07, 2011 16:46:14    Last update: September 07, 2011 19:23:00
The Android unit test framework is based on JUnit 3 , not JUnit 4. Test cases have to extend junit.framework.TestCase or a subclass (such as android.test.InstrumentationTestCase ). Tests are identified by public methods whose name starts with test , not methods annotated with @Test (as in JUnit 4). An Android test suite is packaged as an APK, just like the application being tested. To create a test package, first you need to identify the application package it is testing. Google suggests to put the test package source in a directory named tests/ alongside the src/ directory of the main application. At runtime, Android instrumentation loads both the test package and the application under test into the same process. Therefore, the tests can invoke methods on...
Created by Dr. Xi on June 22, 2011 07:33:45    Last update: June 22, 2011 11:57:54
Demo code for CSV parsing with OpenCSV . Java code: import java.io.*; import au.com.bytecode.opencs... Test with a simple CSV file: psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,... The parser worked correctly: Line 1 has 11 values: |psmith01| |CLASS2B|... Test with a more complicated CSV file: "psmith01 abc", "CLASS2B " , " Peter... Result: Line 1 has 4 values: |psmith01 abc| |CLASS... The parser: Escaped quote and backslash correctly Ignored spaces before the quotation mark - sometimes (see below) Counted spaces after the right quotation mark till the comma as content, including the right quotation mark (bug). Ignored improperly quoted item - silently (third line) Indeed, the OpenCSV parser has a problem with spaces: Input: "Smith, Jack", "210-345-8888" "Smith, J... Result: Line 1 has 2 values: | Smith, Jack| |210-3......
Created by Dr. Xi on June 22, 2011 09:42:35    Last update: June 22, 2011 11:39:40
Demo code for CSV parsing with Skife CSV . Java code: import java.io.*; import java.util.*; import... Test with a simple CSV file: psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,... The parser worked correctly: Line 1 has 11 values: |psmith01| |CLASS2B|... Test with a more complicated CSV file: "psmith01 abc", "CLASS2B " , " Peter... Result: Line 1 has 4 values: |psmith01 abc| | CLAS... The parser worked correctly. But notice that it counts the spaces outside of the quotes significant, and doing so consistently. One more test for spaces: "Smith, Jack" , "210-345-8888" "Smith, Jack"... Result: Line 1 has 2 values: | Smith, Jack | | 210... Add a new line in item two: "One", "Two ", "Three" Result: Line 1 has 2 values: |One| | Two| L...
Created by Dr. Xi on June 21, 2011 15:41:51    Last update: June 22, 2011 11:33:36
Demo code for CSV parsing with Apache Commons CSV parser . Java code: import java.io.*; import org.apache.commons.csv... Test with a simple CSV file: psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,... Result: Line 1 has 11 values: |psmith01| |CLASS2B|... The parser worked correctly. Test with a more complicated CSV file: "psmith01 abc", "CLASS2B " , " Peter... Result: Line 1 has 4 values: |psmith01 abc| |CLASS... The third line is invalid input, but throwing a Java IOException is a bit grave. Also, the parser is not able to escape a backslash. Add a new line in item two: "One", "Two ", "Three" Result: Line 2 has 3 values: |One| |Two | |...
Created by Dr. Xi on June 21, 2011 15:54:00    Last update: June 22, 2011 11:33:09
Demo code for CSV parsing with SuperCSV parser . Java code: import java.io.*; import java.util.List; imp... Test with a simple CSV file: psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,... The parser worked correctly: Line 1 has 11 values: |psmith01| |CLASS2B|... Test with a more complicated CSV file: "psmith01 abc", "CLASS2B " , " Peter... The parser messed up on all three lines: Line 1 has 5 values: |psmith01 abc| |CLASS... Using two lines input: "psmith01 abc", "CLASS2B " , " Peter... It generates an exception: Line 1 has 5 values: |psmith01 abc| |CLASS... Add a new line in item two: "One", "Two ", "Three" Result: Line 2 has 3 values: |One| |Two | |...
Created by Dr. Xi on June 22, 2011 07:12:02    Last update: June 22, 2011 11:32:02
Demo code for CSV parsing with OstermillerUtils . Java code: import java.io.*; import java.util.List; imp... Test with a simple CSV file: psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,... The parser worked correctly: Line 1 has 11 values: |psmith01| |CLASS2B|... Test with a more complicated CSV file: "psmith01 abc", "CLASS2B " , " Peter... The parser messed up on all three lines: Line 1 has 6 values: |psmith01 abc| | "CLA... Putting a space before the first quote: "Smith, Jack","210-345-8888" It dismissed the quotes: Line 1 has 3 values: | "Smith| | Jack"| ... Add a new line in item two: "One", "Two ", "Three" Result: Line 1 has 2 values: |One| | "Two| ...
Created by alfa on June 07, 2011 11:34:26    Last update: June 07, 2011 11:36:37
This is an example that uses dynamic proxies to trace method calls (in logging) and print out elapsed times for them. Because dynamic proxies can only be generated for interfaces, the service classes must be implemented with interface-implementation pairs. Create services A and B. A.java : public interface A { public void service1()... AImpl.java : import java.util.Random; public class AImpl... B.java : public interface B { public void service1()... BImpl.java : public class BImpl implements B { public vo... The call trace proxy: import java.lang.reflect.*; class TraceProx... The performance proxy: import java.lang.reflect.*; class Performan... The service factory: import java.lang.reflect.*; public class Se... The test class: public class Test { public static void main... The output: Entering AImpl.service1 Entering BImpl.service1... The above example has no information...
Previous  1 2 Next