maven2でjettyを起動してseleniumを使う

<?xml version="1.0" encoding="UTF-8"?>

<project
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>jp.gr.java_conf.xazen</groupId>
  <artifactId>examples-maven2</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <description></description>
  
  <build>
    <plugins>
    
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <encoding>UTF-8</encoding>
          <optimize>true</optimize>
        </configuration>
      </plugin>
      
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>**/integration/*Test.java</exclude>
          </excludes>
        </configuration>
        <executions>
          <execution>
            <id>surefire-it</id>
            <phase>integration-test</phase>
            <goals>
              <goal>test</goal>
            </goals>
            <configuration>
              <skip>false</skip>
              <excludes>
                <exclude>none</exclude>
              </excludes>
              <includes>
                <include>**/integration/*Test.java</include>
              </includes>
            </configuration>
          </execution>
        </executions>
      </plugin>
      
      <plugin>
        <groupId>org.codehaus.cargo</groupId>
        <artifactId>cargo-maven2-plugin</artifactId>
        <executions>
          <execution>
            <id>start-container</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>start</goal>
            </goals>
          </execution>
          <execution>
            <id>stop-container</id>
            <phase>post-integration-test</phase>
            <goals>
              <goal>stop</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <wait>false</wait>
          <container>
            <containerId>jetty6x</containerId>
            <type>embedded</type>
          </container>
          <configuration>
            <deployables>
              <deployable>
                <properties>
                  <context>/</context>
                </properties>
              </deployable>
            </deployables>
          </configuration>
        </configuration>
      </plugin>
      
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>selenium-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>start-selenium-server</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>start-server</goal>
            </goals>
            <configuration>
              <background>true</background>
              <logOutput>true</logOutput>
              <multiWindow>true</multiWindow>
            </configuration>
          </execution>
          <execution>
            <id>stop-selenium-server</id>
            <phase>post-integration-test</phase>
            <goals>
              <goal>stop-server</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>clean</goal>
              <goal>check</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-site-plugin</artifactId>
        <configuration>
          <locales>ja</locales>
          <inputEncoding>UTF-8</inputEncoding>
          <outputEncoding>UTF-8</outputEncoding>
        </configuration>
      </plugin>
      
    </plugins>
  </build>
  
  <reporting>
    <plugins>
      
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
      </plugin>
      
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
      </plugin>
      
    </plugins>
  </reporting>

  <dependencies>
    
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.4</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
      <groupId>org.openqa.selenium.client-drivers</groupId>
      <artifactId>selenium-java-client-driver</artifactId>
      <version>0.9.2</version>
      <scope>test</scope>
    </dependency>
    
  </dependencies>

</project>
package jp.gr.java_conf.xazen.examples.maven2.integration;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.thoughtworks.selenium.DefaultSelenium;

public class ExamplesMaven2IntegrationTest {

    private DefaultSelenium selenium;
    
    @Before
    public void openBrowser() throws Exception {
        selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://localhost:8080");
        selenium.start();
    }
    
    @After
    public void closeBrowser() throws Exception {
        selenium.stop();
    }

    @Test
    public void HelloWorldを返すページ() {
        selenium.open("/hello");
        assertEquals("Hello, World", selenium.getText("//h1"));
    }

}
  • itはプロジェクトを分けた方が良いかもしれない
    • 2.1からsrc/it/javaがintegration-testフェーズで使えるとかなんとか
    • integration-testのをsrc/it/javaとしたけど認識されなかった
  • cobertura-maven-pluginでintegration-testフェーズのカバレッジが計測できない
  • Firefoxが起動しただけでテストが実行されないことが頻繁にある

-->

http://d.hatena.ne.jp/coolstyle/20070319#1174308234
どこかでチラっと見たような気がして探したらorg.apache.tapestry.dom.DefaultMarkupModelにありました。tranditionalなHTMLにするために故意にそうしてるみたいです。MarkupModelの他の実装でsrc/test/java配下にXMLMarkupModelというのがあって、これを使うと<br/>とかにも対応できそうなので試してみたらできました。方法は、id:butyricacidさんがやってたdecorateしようとして他のオブジェクトを返す、というやり方です。

public static MarkupWriterFactory decorateMarkupWriterFactory(
    @InjectService("ComponentInvocationMap") final ComponentInvocationMap componentInvocationMap,
    Object delegate) {
  return new MarkupWriterFactory() {
    public MarkupWriter newMarkupWriter() {
      return new MarkupWriterImpl(new DefaultMarkupModel() {
        @Override
        public EndTagStyle getEndTagStyle(String element) {
          return EndTagStyle.ABBREVIATE;
        }
      }, componentInvocationMap);
    }
  };
}

できるにはできるけど、<br>こんなのも</br>ありになるXMLだし。ComponentInvocationMapとMarkupWriterImplがinternalなのでちょっと気持ち悪い。けど今は他にやり方が思いつかないので仕方ない。

tapestry-simple(quickstart) archetypeで作ったプロジェクトでAutoRegisterが動かない

app.diconをsrc/main/resources配下に置いた場合、ここがJava Build PathのLibraryにあるのがマズい。AutoRegisterがsrc/main/resourcesを起点にクラスファイルを探そうとするので何も登録されず終い。とりあえずLibraryから消してSourceに追加してあげるようにすればOK。

ナナオのカラーユニバーサルデザイン対応ワイドモニターが欲しい!

@S2Inject

いちいち

@Inject("s2:")

とか書くの面倒だし、そもそも文字列だし間違えるじゃん、というわけでS2Injectアノテーションを作ってみた。と言っても"s2:"と書かなくて済むようになるだけなんだが。。。

@S2Inject
@S2Inject("s2:")
@S2Inject("")

は最初の例と等価。

@Inject("s2:greeting")

@S2Inject("greeting")
@S2Inject("s2:greeting")

と同じ。