17 февр. 2012 г.

Linux command line under Windows

Let me share my experience about setting up linux terminal under Windows.

Some time ago i worked on Mac OS and so accustomed to the unix command line that I can't live without it when i had switch to Windows. That's why i decided to setup linux console under Windows.


Main requirements was:

1. Linux terminal itself - i need to do my usual work using unix commands.
2. Terminal window should supports transparency (i like this feature on Mac's terminal)
3. Terminal window should supports tabs - this is a "must have" feature for terminal window

A natural choise for No. 1 is cygwin project. It gives you full power of linux console under Windows


but its default terminal window doesn't satisfy 2'nd and third conditions. Let's fix this!
cygwin contains an excellent termial emulator - mintty:


it supports transparency as well as other options:

Let's add tabs. There is an excellent free Windows application Window Tabs,
it allows to add tabs to any kind of application:

You can setup it so tabs will be shown only for mintty process. You can use mintty's Alt+F2 shortcut to open new tab and setup shortcut to switch beetween tabs. The only restriction of free version is - max tabs number in one windows set: 3. But is's enough for most of my needs.

Thanks for attention!

16 февр. 2012 г.

Change IP settings from command line on Windows

I writing this post just as a reminder to myself, but maybe it also helps to someone else.

So to change IP settings from command line on Windows follow these steps:

1. Get name of your network interface:

> ipconfig



in this case interface has the name "Local Area Connection 3"

2. If you want to setup IP and DNS to be resolved by DHCP run following command:


> netsh interface ip set address "<interface name>" dhcp
> netsh interface ip set dns " <interface name> " dhcp


3. If you want to use static IP and DNS use following commands:

> netsh interface ip set address " <interface name> " static 192.168.0.2 255.255.255.0 gateway=192.168.0.1 gw=1

This will set static IP and gateway, then you can add any number of DNS servers:

> netsh interface ip add dns " <interface name> " 192.168.0.1
> netsh interface ip add dns " <interface name> " 8.8.8.8

Thats all,
enjoy :)

11 февр. 2012 г.

Small HTTP web server for functional testing

Some time ago I needed a webserver for functional testing of my java HTTP client library.
I looked for a web server which satisfy following conditions:

1. Embeddable - the main condition.
2. Easy to integrate with unit testing frameworks
3. Small amount of code and config to setup server
4. Quick startup and shutdown.
5. Small size of the library.

The first candidate which satisfy main condition is Jetty.
It is cool but it doesn't satisfy all other my conditions.

Next candidate is com.sun.net.httpserver.HttpServer which is embedded into JDK 1.6 and higher.
It is excellent candidate for my needs, so i decided to use it.
I've implemented some kind of abstraction layer over HttpServer to fully satisfify 2-nd and 3-d conditions and as a result i wrote very small library which i called anhttpserver (Another HTTP Server)

here is basic usage example of that library:

import anhttpserver.ByteArrayHandlerAdapter;
import anhttpserver.DefaultSimpleHttpServer;
import anhttpserver.HttpRequestContext;
import anhttpserver.SimpleHttpServer;


import java.io.IOException;

public class SimpleServer {

    public static void main(String[] args) throws Exception {
        SimpleHttpServer server = new DefaultSimpleHttpServer();
        server.start();
        server.addHandler("/index", new ByteArrayHandlerAdapter() {
            public byte[] getResponseAsByteArray(HttpRequestContext httpRequestContext) throws IOException {
                return "Hello world".getBytes();
            }
        });
    }
}

Now point your favorite browser to http://localhost:8000/index - you should see "Hello world".

Isn't it easy?


Let's write basic JUnit test which will show, how easy to write functional tests using this library:

import anhttpserver.ByteArrayHandlerAdapter;
import anhttpserver.DefaultSimpleHttpServer;
import anhttpserver.HttpRequestContext;
import anhttpserver.SimpleHttpServer;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import static org.junit.Assert.assertEquals;

public class SimpleTest {

    private SimpleHttpServer server;

    @Before
    public void init() {
        server = new DefaultSimpleHttpServer();
        server.start();
    }


    @After
    public void finish() {
        server.stop();
    }

    //In our example this is function we test,
    //in your case most probablt it will be some other entity
    private String getResult(String urlString) throws Exception {
        URLConnection connection = (new URL(urlString)).openConnection();
        InputStream is = connection.getInputStream();
        String result = IOUtils.toString(is);
        is.close();
        return result;
    }


    @Test
    public void basicServerTest() throws Exception {
        server.addHandler("/", new ByteArrayHandlerAdapter() {
            @Override
            public byte[] getResponseAsByteArray(HttpRequestContext httpRequestContext) throws IOException {
                String response = "Hello world!";
                return response.getBytes();
            }
        });

        assertEquals("Hello world!", getResult("http://localhost:8000"));
    }
}

More examples of using anhttpserver library you can see in source code of anhttpserver and anhttpclient projects which are open source and released under the MIT license.

Also there is one non usual example of usage of this library - jstreamserver (HTTP Live Streaming for IPad and IPhone).

Here i used anhttpclient not for functional testing but as an HTTP server.
For sure i do not recommend to use it in such way - i didn't perform any stability and performance testing, but this example is proof of the viability of anhttpserver.

You can download sources of anhttpserver here: https://github.com/sprilukin/anhttpserver

Thank you for attention,
Comments are welcome.