さて何を作ろうか

プロジェクトを作成

SwapMojo?

使用例

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>jp.hondo</groupId>
    <artifactId>maven-swap-plugin-test</artifactId>
    <version>1.0</version>
    
    <build>
        <plugins>
            <plugin>
                <groupId>jp.hondoh</groupId>
                <artifactId>hondoh-swap-plugin</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>swapfiles</goal>
                        </goals>
                        <configuration>
                            <ifProp>os.name</ifProp>
                            <is>Mac.*</is>
                            <from>${basedir}/target/test-classes/app.properties</from>
                            <to>${basedir}/target/test-classes/app_macos.properties</to>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

SwapMojoTest?

SwapMojo mojo = (SwapMojo) lookupMojo("goal", new File(pom));
mojo.execute();

でテストが実行される

package jp.hondoh.maven.swap.plugin;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import static org.codehaus.plexus.PlexusTestCase.getBasedir;
import org.junit.Test;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.equalTo;

/**
 * test swap mojo.
 * @author Atsushi HONDOH (kagyuu@hondou.homedns.org)
 */
public class SwapMojoTest extends AbstractMojoTestCase {

    @Test
    public void testExecuteSwapEjbPersistenceXml() throws Exception {
        String main1 = getContents(new File(getBasedir(), "target/classes/META-INF/persistence.xml"));
        String test1 = getContents(new File(getBasedir(), "target/test-classes/META-INF/persistence.xml"));
        
        File testPom = new File(getBasedir(), "src/test/resources/pom_test1.xml");
        SwapMojo mojo = (SwapMojo) lookupMojo("swapfiles", testPom);
        mojo.execute();
        
        String main2 = getContents(new File(getBasedir(), "target/classes/META-INF/persistence.xml"));
        String test2 = getContents(new File(getBasedir(), "target/test-classes/META-INF/persistence.xml"));
        
        // Swaped
        assertThat(main2, is(equalTo(test1)));
        assertThat(test2, is(equalTo(main1)));
    }
    
    @Test
    public void testExecuteEnvIsMatch() throws Exception {
        String main1 = getContents(new File(getBasedir(), "target/classes/META-INF/persistence.xml"));
        String test1 = getContents(new File(getBasedir(), "target/test-classes/META-INF/persistence.xml"));
        
        File testPom = new File(getBasedir(), "src/test/resources/pom_test2.xml");
        SwapMojo mojo = (SwapMojo) lookupMojo("swapfiles", testPom);
        mojo.execute();
        
        String main2 = getContents(new File(getBasedir(), "target/classes/META-INF/persistence.xml"));
        String test2 = getContents(new File(getBasedir(), "target/test-classes/META-INF/persistence.xml"));
        
        // Swaped ( if run on Mac )
        assertThat(main2, is(equalTo(test1)));
        assertThat(test2, is(equalTo(main1)));
    }
    
    @Test
    public void testExecuteEnvIsNotMatch() throws Exception {
        String main1 = getContents(new File(getBasedir(), "target/classes/META-INF/persistence.xml"));
        String test1 = getContents(new File(getBasedir(), "target/test-classes/META-INF/persistence.xml"));
        
        File testPom = new File(getBasedir(), "src/test/resources/pom_test3.xml");
        SwapMojo mojo = (SwapMojo) lookupMojo("swapfiles", testPom);
        mojo.execute();
        
        String main2 = getContents(new File(getBasedir(), "target/classes/META-INF/persistence.xml"));
        String test2 = getContents(new File(getBasedir(), "target/test-classes/META-INF/persistence.xml"));
        
        // Not swaped ( if run on Linux )
        assertThat(main2, is(equalTo(main1)));
        assertThat(test2, is(equalTo(test1)));
    }
    
    private String getContents(File f) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(f));
        String line =  br.readLine();
        br.close();
        
        return line;
    }
}

参考文献


Java Maven


添付ファイル: filemvn1.png 2183件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2013-04-20 (土) 23:13:34 (4268d)
Short-URL: http://at-sushi.com/pukiwiki/index.php?cmd=s&k=d891abade8
ISBN10
ISBN13
9784061426061