launching 3rd party servlets using jetty for unit testing


References

Apache Maven

<dependency>
  <!-- I'm not sure this is the minimum dependency -->
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-servlet-tester</artifactId>
  <scope>test</scope>
</dependency>

TestNG


    private static final PORT;

    static {
        PORT = ThreadLocalRandom.current().nextInt(1024, 65536);
    }

    private static Server SERVER;

    @BeforeClass
    public static void startServer() throws Exception {
        SERVER = new Server(PORT);
        final ServletHandler handler = new ServletHandler();
        SERVER.addServeltHandler(handler);
        handler.addServletWithMapping(SomeServlet.class, "/some");
        SERVER.start();
    }

    @AfterClass
    public static void stopServer() throws Exception {
        SERVER.stop();
        SERVER = null;
    }

    @Test
    public void test() throws MalformedURLException {
        final URL url = new URL("http://localhost:" + PORT + "/some");
    }

completePendingCommand of Commons-Net


When I tried like this,

try(OutputStream fileStream = ftpClient.storeFileStream(remote)) {
    // do some
    final boolean commandCompleted
        = ftpClient.completePendingCommand();
    // check the result
}

I found that completePendingCommand() hangs.
The method apparently have to be invoked after the stream is closed.

try(OutputStream fileStream = ftpClient.storeFileStream(remote)) {
    // do some
}
final boolean commandCompleted
    = ftpClient.completePendingCommand();
// check the result

required standard open options for creating or replacing a file


I just found a bug on my own code.

final FileChannel fileChannel = FileChannel.open(
    targetPath,
    StandardOpenOption.CREATE, // create if not exist
    StandardOpenOption.WRITE // i'm gonna write on
);

Above code seemed working, but when I tried to replace a bigger file with a smaller one, the file size remains and the only leading(?) portion of file contents replaced.

final FileChannel fileChannel = FileChannel.open(
    targetPath,
    StandardOpenOption.CREATE,
    StandardOpenOption.TRUNCATE_EXISTING, // truncate the file!!!
    StandardOpenOption.WRITE
);

java_home environment variable


Windows

N/A

OS X

installations

$ ls -l /Library/Java/JavaVirtualMachines/
....
$

/usr/libexec/java_home

$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdkx.y.z_mm.jdk/Contents/Home

$ /usr/libexec/java_home -v 1.8
/Library/Java/JavaVirtualMachines/jdk1.8.0_mm.jdk/Contents/Home

$ /usr/libexec/java_home -v 1.7
/Library/Java/JavaVirtualMachines/jdk1.7.0_mm.jdk/Contents/Home

~/.profile

$ cat ~/.profile
...
export JAVA7_HOME=$(/usr/libexec/java_home -v 1.7)
export JAVA8_HOME=$(/usr/libexec/java_home -v 1.8)
export JAVA_HOME=$JAVA8_HOME
...

$ echo $JAVA7_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home

$ echo $JAVA8_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home

$

Ubuntu

See installing latest oracle jdk in old ubuntu.

mapping a milliseconds parameter directly into a date instance in jax-rs


@POST
public void myResource(
    @QueryParam("milliseconds")
    @NotNull
    final Long milliseconds) {

    final Date date = new Date(milliseconds);
    myService(date);
}

I wrote some codes using ParamConverter and ParamConverterProvider.
And now I can do like this.

@POST
public void myResource(
    @QueryParam("milliseconds")
    @NotNull
    @MillisecondsParam
    final Date date) {

    myService(date);
}

Here are 3 classes for this.

curl을 이용해 바이너리 파일 올릴 때 주의할 점


curl을 이용해서 PUT 메서드를 테스트하던 중 데이터가 이상하게 올라가는 현상을 발견했습니다.

$ curl -X PUT -H "Content-Type: image/jpeg" --data @a.jpg http://...

올라가긴 잘 올라가는 것 같은데 브라우저로 이미지를 불러보면 안보입니다.
검색결과, --data 옵션은 --data-ascii와 같다고 하는 군요.
다음과 같이 했더니 잘 올라갑니다.

$ curl -X PUT -H "Content-Type: image/jpeg" --data-binary @a.jpg http://...