2014年2月15日土曜日

DbUnit用のxmlデータを取得するLoader

DbUnitでエクスポートしたxmlデータをMapやbeanにロードするクラスです。
assertの比較元のデータを作る際に利用できそうなので作ってみました。
実際にテーブルが存在しなくてもDbUnitのxml形式でデータを作れば読み込むことができます。
dbunitの部分とreflectionの部分を別のクラスに分けたかったのですが、いずれやることにして取りあえずこのまま公開します。
例のごとくあまりテストはしていません。
使用しているライブラリは下記の通りです。
 ・commons-lang3-3.2.1.jar
 ・dbunit-2.4.9.jar
 ・guava-16.0.1.jar
+各ライブラリで必要なライブラリ

FlatXmlDataSetLoader.java
package my.junit.util;

import java.io.File;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.dbunit.dataset.Column;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.ITableMetaData;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;

import com.google.common.base.CaseFormat;

public class FlatXmlDataSetLoader {

 /**
  * 日付用フォーマット
  */
 private static final String[] DEFAULT_DATE_FORMATS = {
   "yyyy-MM-dd hh:mm:ss.SSS", "yyyy-MM-dd hh:mm:ss", "yyyy-MM-dd" };

 /**
  * java.util.Date用フォーマット
  */
 private String[] dateFormats = null;

 /**
  * コンストラクタ
  */
 public FlatXmlDataSetLoader() {
  this.dateFormats = DEFAULT_DATE_FORMATS;
 }

 /**
  * コンストラクタ
  */
 public FlatXmlDataSetLoader(String... dateFormats) {
  this.dateFormats = dateFormats;
 }

 /**
  * DbUnit用のxmlファイルの1レコード分をMap化したリストを取得します。
  *
  * @param filePath
  *            xmlファイルのパス
  * @param tableName
  *            取得したいテーブル
  * @return 1レコード分をMapに設定したリスト
  */
 public List<Map<String, Object>> importToMapList(String filePath,
   String tableName) {
  try {
   IDataSet dataSet = new FlatXmlDataSetBuilder().build(new File(
     filePath));
   ITable table = dataSet.getTable(tableName);
   ITableMetaData meta = table.getTableMetaData();
   Column[] columns = meta.getColumns();

   List<Map<String, Object>> records = new ArrayList<Map<String, Object>>();
   for (int i = 0; i < table.getRowCount(); i++) {
    Map<String, Object> record = new HashMap<String, Object>();
    for (Column column : columns) {
     String columnName = column.getColumnName();
     record.put(columnName, table.getValue(i, columnName));
    }
    records.add(record);
   }
   return records;
  } catch (MalformedURLException e) {
   throw new IllegalArgumentException(e);
  } catch (DataSetException e) {
   throw new IllegalArgumentException(e);
  }
 }

 /**
  * DbUnit用のxmlファイルの1レコード分をbean化したリストを取得します。
  *
  * @param filePath
  *            xmlファイルのパス
  * @param tableName
  *            取得したいテーブル
  * @param beanClass
  *            設定対象のbean class
  * @return 1レコード分をbeanに設定したリスト
  */
 public <B> List<B> importToBeanList(String filePath, String tableName,
   Class<B> beanClass) {
  List<Map<String, Object>> snakeMapList = importToMapList(filePath,
    tableName);
  List<Map<String, Object>> camelMapList = new ArrayList<Map<String, Object>>();
  for (Map<String, Object> snakeMap : snakeMapList) {
   camelMapList.add(convertToLowerCamelKey(snakeMap));
  }
  return convertMapListToBeanList(camelMapList, beanClass);
 }

 /**
  * Mapのリストをbeanのリストに変換します。
  *
  * @param mapList
  *            Mapの
  * @param beanClass
  *            設定対象のbean class
  * @return beanのリスト
  */
 public <B> List<B> convertMapListToBeanList(
   List<Map<String, Object>> mapList, Class<B> beanClass) {
  Map<String, Field> fieldsInfo = getFieldInfo(beanClass);
  List<B> beans = new ArrayList<B>();
  for (Map<String, Object> map : mapList) {
   try {
    beans.add(convertMapToBean(map, beanClass.newInstance(),
      fieldsInfo));
   } catch (InstantiationException e) {
    throw new IllegalArgumentException(e);
   } catch (IllegalAccessException e) {
    throw new IllegalArgumentException(e);
   }
  }
  return beans;
 }

 /**
  * Mapのリストをbeanのリストに変換します。
  *
  * @param map
  *            Map
  * @param beanClass
  *            設定対象のbean class
  * @return bean
  */
 public <B> B convertMapToBean(Map<String, Object> map, Class<B> beanClass) {
  try {
   return convertMapToBean(map, beanClass.newInstance());
  } catch (InstantiationException e) {
   throw new IllegalArgumentException(e);
  } catch (IllegalAccessException e) {
   throw new IllegalArgumentException(e);
  }
 }

 /**
  * Mapのリストをbeanのリストに変換します。
  *
  * @param map
  *            Map
  * @param bean
  *            設定対象のbean
  * @return bean
  */
 public <B> B convertMapToBean(Map<String, Object> map, B bean) {
  Map<String, Field> fieldMap = getFieldInfo(bean.getClass());
  return convertMapToBean(map, bean, fieldMap);
 }

 /**
  * Mapのリストをbeanのリストに変換します。
  *
  * @param map
  *            Map
  * @param bean
  *            設定対象のbean
  * @param fieldsInfo
  *            beanのフィールド情報
  * @return bean
  */
 private <B> B convertMapToBean(Map<String, Object> map, B bean,
   Map<String, Field> fieldsInfo) {
  Set<Entry<String, Field>> fieldSet = fieldsInfo.entrySet();
  for (Entry<String, Field> fieldEntry : fieldSet) {
   Field field = fieldEntry.getValue();
   Object value = map.get(fieldEntry.getKey());
   setFieldValue(bean, field, value);
  }
  return bean;
 }

 /**
  * クラスのフィールド情報を取得します。
  *
  * @param beanClass
  *            対象のクラス
  * @return フィールド名をキーにしたフィールドのマップ
  */
 private static Map<String, Field> getFieldInfo(Class<?> beanClass) {
  Field[] fields = FieldUtils.getAllFields(beanClass);
  Map<String, Field> fieldMap = new HashMap<String, Field>();
  for (Field field : fields) {
   String key = field.getName();
   fieldMap.put(key, field);
  }
  return fieldMap;
 }

 /**
  * マップのキーをスネークケースからキャメルケースに変換します。
  *
  * @param snakeMap
  *            キーがスネークケースのマップ
  * @return キーがキャメルケースのマップ
  */
 private static <V> Map<String, V> convertToLowerCamelKey(
   Map<String, V> snakeMap) {
  Map<String, V> camelMap = new HashMap<String, V>();
  Set<String> snakeKeys = snakeMap.keySet();
  for (String snake : snakeKeys) {
   String camel = CaseFormat.UPPER_UNDERSCORE.to(
     CaseFormat.LOWER_CAMEL, snake);
   camelMap.put(camel, snakeMap.get(snake));
  }
  return camelMap;
 }

 /**
  * fieldのclassに合わせた形式に変換し値を設定
  *
  * @param bean
  *            設定対象のbean
  * @param field
  *            設定対象のfield
  * @param value
  *            設定する値
  * @return true:設定完了、false:未設定
  */
 private boolean setFieldValue(Object bean, Field field, Object value) {
  if (value == null) {
   // nullの場合
   return false;
  }
  try {
   field.setAccessible(true);
   String val = value.toString();
   if (field.getType().equals(String.class)) {
    field.set(bean, val);
   } else if (field.getType().equals(char[].class)) {
    field.set(bean, val.toCharArray());
   } else if (field.getType().equals(Boolean.class)) {
    field.set(bean, new Boolean(val));
   } else if (field.getType().equals(boolean.class)) {
    field.set(bean, new Boolean(val).booleanValue());
   } else if (field.getType().equals(Byte.class)) {
    field.set(bean, Byte.valueOf(val));
   } else if (field.getType().equals(byte.class)) {
    field.set(bean, Byte.valueOf(val).byteValue());
   } else if (field.getType().equals(Integer.class)) {
    field.set(bean, Integer.valueOf(val));
   } else if (field.getType().equals(int.class)) {
    field.set(bean, Integer.valueOf(val));
   } else if (field.getType().equals(Long.class)) {
    field.set(bean, Long.valueOf(val));
   } else if (field.getType().equals(long.class)) {
    field.set(bean, Long.valueOf(val));
   } else if (field.getType().equals(Short.class)) {
    field.set(bean, Short.valueOf(val));
   } else if (field.getType().equals(short.class)) {
    field.set(bean, Short.valueOf(val));
   } else if (field.getType().equals(Double.class)) {
    field.set(bean, Double.valueOf(val));
   } else if (field.getType().equals(double.class)) {
    field.set(bean, Double.valueOf(val));
   } else if (field.getType().equals(BigDecimal.class)) {
    field.set(bean, new BigDecimal(val));
   } else if (field.getType().equals(Date.class)) {
    field.set(bean, DateUtils.parseDate(val, dateFormats));
   } else if (field.getType().equals(Timestamp.class)) {
    Date date = DateUtils.parseDate(val, dateFormats);
    field.set(bean, new Timestamp(date.getTime()));
   } else {
    return false;
   }
  } catch (Exception e) {
   throw new IllegalArgumentException(e);
  } finally {
   field.setAccessible(false);
  }
  return true;
 }

}



2014年2月11日火曜日

JUnit用のテキストファイルを比較するMatcher

JUnit4で使用可能なテキストファイル同士の内容をマッチングするMatcherです。
JUnit用と言っていますがhamcrestしか使っていません。
取りあえず作ってみた状態なのでテストはせず、ちょっと動かしてみた程度です。直さなければいけない部分もあるかと思いますが、作ったことを忘れてしまいそうなので上げておきます。
使用しているライブラリは下記の通りです。
 ・hamcrest-core-1.3.jar
+各ライブラリで必要なライブラリ

TextInputStreamMatcher.java
package my.junit.matcher;

import java.io.InputStream;

import org.hamcrest.Matcher;

/**
 * テキスト形式でInputStreamの比較を行えるMatcher
 *
 * @author blog owner
 *
 * @param <T>
 *            任意のクラス
 */
class TextInputStreamMatcher extends AbstractTextFileMatcher<InputStream> {

 /**
  * コンストラクタ
  */
 protected TextInputStreamMatcher(InputStream expected) {
  this(expected, CHAR_SET);
 }

 /**
  * コンストラクタ
  */
 protected TextInputStreamMatcher(InputStream expected, String charSet) {
  super();
  this.charSet = charSet;
  expectedName = "expected file";
  expectedIs = expected;
 }

 /**
  * <pre>
  * テキストファイル比較用のMatcherを取得します。
  * キャラクターセットはUTF-8です。
  * </pre>
  *
  * @param expected
  *            予想されるファイル
  * @return テキストファイル比較用のMatcher
  */
 public static Matcher<InputStream> equalsContentsOf(InputStream expected) {
  return new TextInputStreamMatcher(expected);
 }

 /**
  * テキストファイル比較用のMatcherを取得します。
  *
  * @param expected
  *            予想されるファイル
  * @param charSet
  *            キャラクターセット
  * @return テキストファイル比較用のMatcher
  */
 public static Matcher<InputStream> equalsContentsOf(InputStream expected,
   String charSet) {
  return new TextInputStreamMatcher(expected, charSet);
 }

 /**
  * {@inheritDoc}
  */
 @Override
 protected InputStream getActualIs(InputStream actual) {
  actualIs = actual;
  return actualIs;
 }

}


TextFileMatcher.java
package my.junit.matcher;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.hamcrest.Matcher;

/**
 * テキストファイルの比較を行えるMatcher
 *
 * @author blog owner
 *
 * @param <T>
 *            任意のクラス
 */
class TextFileMatcher extends AbstractTextFileMatcher<File> {

 /**
  * コンストラクタ
  */
 protected TextFileMatcher(File expected) {
  this(expected, CHAR_SET);
 }

 /**
  * コンストラクタ
  */
 protected TextFileMatcher(File expected, String charSet) {
  super();
  this.charSet = charSet;
  File expectedFile = expected;
  expectedName = expectedFile.getPath();
  try {
   expectedIs = new FileInputStream(expectedFile);
  } catch (FileNotFoundException e) {
   throw new IllegalArgumentException(e);
  }
 }

 /**
  * <pre>
  * テキストファイル比較用のMatcherを取得します。
  * キャラクターセットはUTF-8です。
  * </pre>
  *
  * @param expected
  *            予想されるファイル
  * @return テキストファイル比較用のMatcher
  */
 public static Matcher<File> equalsContentsOf(File expected) {
  return new TextFileMatcher(expected);
 }

 /**
  * テキストファイル比較用のMatcherを取得します。
  *
  * @param expected
  *            予想されるファイル
  * @param charSet
  *            キャラクターセット
  * @return テキストファイル比較用のMatcher
  */
 public static Matcher<File> equalsContentsOf(File expected, String charSet) {
  return new TextFileMatcher(expected, charSet);
 }

 /**
  * {@inheritDoc}
  */
 @Override
 protected InputStream getActualIs(File actual) {
  actualName = actual.getPath();
  try {
   actualIs = new FileInputStream(actual);
  } catch (FileNotFoundException e) {
   throw new IllegalArgumentException(e);
  }
  return actualIs;
 }

}


TextFilePathMatcher.java
package my.junit.matcher;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.hamcrest.Matcher;

/**
 * テキストファイルの比較を行えるMatcher
 *
 * @author blog owner
 *
 * @param <T>
 *            任意のクラス
 */
class TextFilePathMatcher extends AbstractTextFileMatcher<String> {

 /**
  * コンストラクタ
  */
 protected TextFilePathMatcher(String expected) {
  this(expected, CHAR_SET);
 }

 /**
  * コンストラクタ
  */
 protected TextFilePathMatcher(String expected, String charSet) {
  super();
  this.charSet = charSet;
  String expectedPath = (String) expected;
  File expectedFile = new File(expectedPath);
  expectedName = expectedFile.getPath();
  try {
   expectedIs = new FileInputStream(expectedFile);
  } catch (FileNotFoundException e) {
   throw new IllegalArgumentException(e);
  }
 }

 /**
  * <pre>
  * テキストファイル比較用のMatcherを取得します。
  * キャラクターセットはUTF-8です。
  * </pre>
  *
  * @param expected
  *            予想されるファイルのパス
  * @return テキストファイル比較用のMatcher
  */
 public static Matcher<String> equalsContentsOf(String expected) {
  return new TextFilePathMatcher(expected);
 }

 /**
  * テキストファイル比較用のMatcherを取得します。
  *
  * @param expected
  *            予想されるファイルのパス
  * @param charSet
  *            キャラクターセット
  * @return テキストファイル比較用のMatcher
  */
 public static Matcher<String> equalsContentsOf(String expected,
   String charSet) {
  return new TextFilePathMatcher(expected, charSet);
 }

 /**
  * {@inheritDoc}
  */
 @Override
 protected InputStream getActualIs(String actual) {
  File actualFile = new File(actual);
  actualName = actualFile.getPath();
  try {
   actualIs = new FileInputStream(actualFile);
  } catch (FileNotFoundException e) {
   throw new IllegalArgumentException(e);
  }
  return actualIs;
 }

}


AbstractTextFileMatcher.java
package my.junit.matcher;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

/**
 * テキストファイルの比較を行えるMatcherの抽象クラス
 *
 * @author blog owner
 *
 * @param <T>
 *            任意のクラス
 */
abstract class AbstractTextFileMatcher<T> extends TypeSafeMatcher<T> {

 /**
  * デフォルトキャラクターセット
  */
 protected static final String CHAR_SET = "UTF-8";

 /**
  * キャラクターセット
  */
 protected String charSet = null;

 /**
  * 予想されるファイルの表示名
  */
 protected String expectedName = null;

 /**
  * 予想されるファイルのインプットストリーム
  */
 protected InputStream expectedIs = null;

 /**
  * 実際のファイルの表示名
  */
 protected String actualName = null;

 /**
  * 実際のファイルのインプットストリーム
  */
 protected InputStream actualIs = null;

 /**
  * 不一致だった予想されるファイルの行
  */
 protected List<String> unmatchedExpectedLines = new ArrayList<String>();

 /**
  * 不一致だった実際のファイルの行
  */
 protected List<String> unmatchedActualLines = new ArrayList<String>();

 /**
  * 不一致だった行番号
  */
 protected List<Integer> unmatchedLineNums = new ArrayList<Integer>();

 /**
  * デフォルトコンストラクタ
  */
 protected AbstractTextFileMatcher() {
  super();
 }

 /**
  * コンストラクタ
  */
 protected AbstractTextFileMatcher(String expectedName,
   InputStream expectedIs) {
  this(expectedName, expectedIs, CHAR_SET);
 }

 /**
  * コンストラクタ
  */
 protected AbstractTextFileMatcher(String expectedName,
   InputStream expectedIs, String charSet) {
  this.expectedName = expectedName;
  this.expectedIs = expectedIs;
  this.charSet = charSet;
 }

 /**
  * 実際のファイルのインプットストリームを取得します。
  *
  * @param actual
  *            実際のファイルの情報
  * @return 実際のファイルのインプットストリーム
  */
 protected abstract InputStream getActualIs(T actual);

 /**
  * 実際のファイルの表示名を取得する。
  *
  * @return 実際のファイルの表示名
  */
 private String getActualName() {
  if (actualName == null) {
   return "actual file";
  } else {
   return actualName;
  }
 }

 /**
  * 比較を行います。
  *
  * @param actual
  *            実際の値
  * @return 比較結果
  */
 @Override
 public boolean matchesSafely(T actual) {
  // インプットストリームを取得
  actualIs = getActualIs(actual);

  BufferedReader expectedReader = null;
  try {
   // 予想されるファイル
   expectedReader = new BufferedReader(new InputStreamReader(
     expectedIs, charSet));
  } catch (UnsupportedEncodingException e) {
   throw new IllegalArgumentException(e);
  }

  BufferedReader actualReader = null;
  try {
   // 実際のファイル
   actualReader = new BufferedReader(new InputStreamReader(actualIs,
     charSet));
  } catch (UnsupportedEncodingException e) {
   throw new IllegalArgumentException(e);
  }

  int lineNum = 0;
  String expectedLine = null;
  String actualLine = null;
  try {
   while ((expectedLine = expectedReader.readLine()) != null
     & (actualLine = actualReader.readLine()) != null) {
    lineNum++;
    if (!expectedLine.equals(actualLine)) {
     // 1行分の内容が一致しない
     unmatchedLineNums.add(Integer.valueOf(lineNum));
     unmatchedExpectedLines.add(expectedLine);
     unmatchedActualLines.add(actualLine);
    }
   }
   if (expectedLine != null || actualLine != null) {
    // 行数が一致しない
    lineNum++;
    unmatchedLineNums.add(Integer.valueOf(lineNum));
    unmatchedExpectedLines.add(expectedLine);
    unmatchedActualLines.add(actualLine);
   }
  } catch (IOException e) {
   // どうにもならないので例外
   throw new IllegalStateException(e);
  } finally {
   try {
    expectedReader.close();
   } catch (IOException e) {
    // 比較は終わっているので握りつぶす
   }
   try {
    actualReader.close();
   } catch (IOException e) {
    // 比較は終わっているので握りつぶす
   }
  }
  return unmatchedLineNums.isEmpty();
 }

 /**
  * エラーの場合に表示する、実際の値を示す文字列を追加します。
  *
  * @param actual
  *            実際の値
  * @param description
  *            エラー時の文章
  */
 @Override
 public void describeMismatchSafely(T actual, Description mismatchDescription) {
  setUpDescription(mismatchDescription, getActualName(),
    unmatchedActualLines, unmatchedLineNums);
 }

 /**
  * エラーの場合に表示する、予測の値を示す文字列を追加します。
  *
  * @param description
  *            エラー時の文章
  */
 @Override
 public void describeTo(Description description) {
  setUpDescription(description, expectedName, unmatchedExpectedLines,
    unmatchedLineNums);
 }

 /**
  * エラーの詳細を設定します。
  *
  * @param description
  *            設定先
  * @param name
  *            表示名
  * @param lines
  *            エラーになった行の内容
  * @param lineNums
  *            エラーになった行番号
  */
 private void setUpDescription(Description description, String name,
   List<String> lines, List<Integer> lineNums) {
  description.appendText(name + " = ");
  if (lineNums.isEmpty()) {
   description.appendText("想定外のエラー");
  }
  for (int i = 0; i < lineNums.size(); i++) {
   if (i != 0) {
    description.appendText(", ");
   }
   description.appendText(lineNums.get(i) + ":");
   description.appendValue(lines.get(i));
  }
  return;
 }

}



2014年2月9日日曜日

JUnit用のマッチングするフィールドを指定できるMatcher(Iterable版)

JUnit4で使用可能なbeanの一部のフィールドだけでマッチングを行うMatcherの入力をIterableに対応させたものです。
ほとんどテストをしていないのは同様ですが、やっぱり作ったことを忘れてしまいそうなので上げておきます。
使用しているライブラリは下記の通りです。
 ・hamcrest-core-1.3.jar
 ・commons-lang3-3.2.1.jar
 ・JUnit用のマッチングするフィールドを指定できるMatcher
+各ライブラリで必要なライブラリ

IterableBeanPropertiesMatcher.java
package my.junit.matcher;

import org.hamcrest.Matcher;

/**
 * 一部のプロパティを選択してIterableの比較を行えるMatcher
 *
 * @author blog owner
 *
 * @param <T>
 *            任意のクラス
 */
class IterableBeanPropertiesMatcher<T> extends
  AbstractIterablePropertiesMatcher<T> {

 /**
  * コンストラクタ
  *
  * @param expected
  *            予想される値
  * @param properties
  *            比較対象のプロパティ
  */
 IterableBeanPropertiesMatcher(Iterable<T> expected, String... properties) {
  super();
  this.expected = expected;
  for (T bean : expected) {
   this.matchers.add(new BeanPropertiesMatcher<T>(bean, properties));
  }
 }

 /**
  * Matcherを取得します。
  *
  * @param expected
  *            予想される値
  * @param propeties
  *            比較対象のプロパティ
  * @return 一部のプロパティを選択してIterableの比較を行えるMatcher
  */
 public static <T> Matcher<Iterable<T>> containsValueOf(
   Iterable<T> expected, String... propeties) {
  return new IterableBeanPropertiesMatcher<T>(expected, propeties);
 }

}


IterableBeanIgnorePropertiesMatcher.java
package my.junit.matcher;

import org.hamcrest.Matcher;

/**
 * 一部のプロパティを無視してbean配列、コレクションの比較を行えるMatcher
 *
 * @author blog owner
 *
 * @param <T>
 *            任意のクラス
 */
class IterableBeanIgnorePropertiesMatcher<T> extends
  AbstractIterablePropertiesMatcher<T> {

 /**
  * コンストラクタ
  *
  * @param expected
  *            予想される値
  * @param properties
  *            比較対象外のプロパティ
  */
 IterableBeanIgnorePropertiesMatcher(Iterable<T> expected, String... ignoreProperties) {
  super();
  this.expected = expected;
  for (T bean : expected) {
   this.matchers.add(new BeanIgnorePropertiesMatcher<T>(bean, ignoreProperties));
  }
 }

 /**
  * Matcherを取得します。
  *
  * @param expected
  *            予想される値
  * @param ignoreProperties
  *            比較対象外のプロパティ
  * @return 一部のプロパティを無視してIterableの比較を行えるMatcher
  */
 public static <T> Matcher<Iterable<T>> containsExceptValueOf(
   Iterable<T> expected, String... ignoreProperties) {
  return new IterableBeanIgnorePropertiesMatcher<T>(expected, ignoreProperties);
 }

}


SelectIterablePropertiesMatcher.java
package my.junit.matcher;

import org.hamcrest.Matcher;


interface SelectIterablePropertiesMatcher<T> extends Matcher<Iterable<T>>{
 // 特になし
}


AbstractIterablePropertiesMatcher.java
package my.junit.matcher;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

/**
 * 一部のプロパティを指定してbean配列、コレクションの比較を行えるMatcher
 *
 * @author blog owner
 *
 * @param <T>
 *            任意のクラス
 */
abstract class AbstractIterablePropertiesMatcher<T> extends
  TypeSafeMatcher<Iterable<T>> implements
  SelectIterablePropertiesMatcher<T> {

 /**
  * 予想される値毎のMatcher
  */
 protected List<SelectPropertiesMatcher<T>> matchers = new ArrayList<SelectPropertiesMatcher<T>>();

 /**
  * 予想される値
  */
 protected Iterable<T> expected = null;

 /**
  * 実際の値
  */
 protected Iterable<T> actual = null;

 /**
  * コンストラクタ
  */
 protected AbstractIterablePropertiesMatcher() {
 }

 /**
  * 比較を行います。
  *
  * @param actual
  *            実際の値
  * @return 比較結果
  */
 @Override
 public boolean matchesSafely(Iterable<T> actual) {
  this.actual = actual;
  if (expected == actual) {
   // 完全に同じもの
   return true;
  }
  if (expected == null || actual == null) {
   // 片方だけnullだから違う
   return false;
  }
  // ここからは配列やコレクション内の要素毎に比較
  Iterator<T> actualIte = actual.iterator();
  Iterator<SelectPropertiesMatcher<T>> matcherIte = matchers.iterator();
  boolean result = true;
  while (matcherIte.hasNext() & actualIte.hasNext()) {
   T actualEntry = actualIte.next();
   SelectPropertiesMatcher<T> matcher = matcherIte.next();
   if (!matcher.matches(actualEntry)) {
    result = false;
   }
  }
  if ((!matcherIte.hasNext() && !actualIte.hasNext()) == false) {
   // 要素数が合っていない場合はfalse
   return false;
  }
  // 一致しなかったMatcherがない場合はtrue
  return result;
 }

 /**
  * エラーの場合に表示する、実際の値を示す文字列を追加します。
  *
  * @param actual
  *            実際の値
  * @param description
  *            エラー時の文章
  */
 @Override
 public void describeMismatchSafely(Iterable<T> actual,
   Description mismatchDescription) {
  if (actual == null) {
   mismatchDescription.appendValue(null);
   return;
  }
  int index = -1;
  boolean first = true;
  for (SelectPropertiesMatcher<T> matcher : matchers) {
   index++;
   if (matcher.getUnmatchProperties().isEmpty()) {
    continue;
   }
   if (first) {
    first = false;
   } else {
    mismatchDescription.appendText(", ");
   }
   mismatchDescription.appendText("[" + Integer.valueOf(index) + "] ");
   matcher.describeMismatch(mismatchDescription);
  }
 }

 /**
  * エラーの場合に表示する、予測の値を示す文字列を追加します。
  *
  * @param description
  *            エラー時の文章
  */
 @Override
 public void describeTo(Description description) {
  if (actual == null) {
   description.appendValue(expected.getClass().getName());
   return;
  }
  int index = -1;
  boolean first = true;
  for (SelectPropertiesMatcher<T> matcher : matchers) {
   index++;
   if (matcher.getUnmatchProperties().isEmpty()) {
    continue;
   }
   if (first) {
    first = false;
   } else {
    description.appendText(", ");
   }
   description.appendText("[" + Integer.valueOf(index) + "] ");
   matcher.describeTo(description);
  }
 }

}

JUnit用のマッチングするフィールドを指定できるMatcher

JUnit4で使用可能なbeanの一部のフィールドだけでマッチングを行うMatcherです。
JUnit用と言っていますがhamcrestしか使っていません。
ほとんどテストをしていないのですが、作ったことを忘れてしまいそうなので上げておきます。
使用しているライブラリは下記の通りです。
 ・hamcrest-core-1.3.jar
 ・commons-lang3-3.2.1.jar
+各ライブラリで必要なライブラリ

BeanPropertiesMatcher.java
package my.junit.matcher;

import org.hamcrest.Matcher;

/**
 * 一部のプロパティを選択してbeanの比較を行えるMatcher
 *
 * @author blog owner
 *
 * @param <T>
 *            任意のクラス
 */
class BeanPropertiesMatcher<T> extends AbstractBeanPropertiesMatcher<T> {

 /**
  * コンストラクタ
  *
  * @param expected
  *            予想される値
  * @param properties
  *            比較対象のプロパティ
  */
 BeanPropertiesMatcher(T expected, String... properties) {
  super();
  this.expected = expected;
  this.properties = properties;
 }

 /**
  * Matcherを取得します。
  *
  * @param expected
  *            予想される値
  * @param propeties
  *            比較対象のプロパティ
  * @return 一部のプロパティを選択して比較を行えるMatcher
  */
 public static <T> Matcher<T> equalsValueOf(T expected, String... propeties) {
  return new BeanPropertiesMatcher<T>(expected, propeties);
 }

}

BeanIgnorePropertiesMatcher.java
package my.junit.matcher;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.hamcrest.Matcher;

/**
 * 一部のプロパティを無視してbeanの比較を行えるMatcher
 *
 * @author blog owner
 *
 * @param <T>
 *            任意のクラス
 */
class BeanIgnorePropertiesMatcher<T> extends AbstractBeanPropertiesMatcher<T> {

 /**
  * コンストラクタ
  *
  * @param expected
  *            予想される値
  * @param ignoreProperties
  *            比較対象外のプロパティ
  */
 BeanIgnorePropertiesMatcher(T expected, String[] ignoreProperties) {
  super();
  this.expected = expected;
  if(expected == null){
   this.properties = new String[0];
  }else{
   // 全てのFieldから比較対象外のプロパティを除いたプロパティ配列を作る
   List<String> properties = new ArrayList<String>();
   Field[] fields = expected.getClass().getDeclaredFields();
   for (Field field : fields) {
    if (!ArrayUtils.contains(ignoreProperties, field.getName())) {
     properties.add(field.getName());
    }
   }
   this.properties = properties.toArray(new String[properties.size()]);
  }
 }

 /**
  * Matcherを取得します。
  *
  * @param expected
  *            予想される値
  * @param ignoreProperties
  *            比較対象外のプロパティ
  * @return 一部のプロパティを無視して比較を行えるMatcher
  */
 public static <T> Matcher<T> equalsExceptValueOf(T expected, String... ignoreProperties) {
  return new BeanIgnorePropertiesMatcher<T>(expected, ignoreProperties);
 }

}
SelectPropertiesMatcher.java
package my.junit.matcher;

import java.util.List;

import org.hamcrest.Description;
import org.hamcrest.Matcher;

interface SelectPropertiesMatcher<T> extends Matcher<T> {

 /**
  * 予想される値を取得します。
  *
  * @return 予想される値
  */
 T getExpected();

 /**
  * 実際の値を取得します。
  *
  * @return 実際の値
  */
 Object getActual();

 /**
  * 一致しなかったプロパティを取得します。
  *
  * @return 一致しなかったプロパティ
  */
 List<String> getUnmatchProperties();

 /**
  * エラーの場合に表示する、実際の値を示す文字列を追加します。
  *
  * @param description
  *            エラー時の文章
  */
 public void describeMismatch(Description mismatchDescription);

}
AbstractBeanPropertiesMatcher.java
package my.junit.matcher;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

/**
 * 一部のプロパティを指定してbeanの比較を行えるMatcher
 *
 * @author blog owner
 *
 * @param <T>
 *            任意のクラス
 */
abstract class AbstractBeanPropertiesMatcher<T> extends TypeSafeMatcher<T>
  implements SelectPropertiesMatcher<T> {

 /**
  * 予想される値
  */
 protected T expected = null;

 /**
  * 実際の値
  */
 protected T actual = null;

 /**
  * 比較対象のプロパティ
  */
 protected String[] properties = null;

 /**
  * 一致しなかったプロパティ
  */
 protected List<String> unmatchProperties = new ArrayList<String>();

 /**
  * コンストラクタ
  */
 protected AbstractBeanPropertiesMatcher() {
 }

 /**
  * 比較を行います。
  *
  * @param actual
  *            実際の値
  * @return 比較結果
  */
 @Override
 public boolean matchesSafely(T actual) {
  this.actual = actual;
  if (expected == actual) {
   // 完全に同じもの
   return true;
  }
  if (expected == null || actual == null) {
   // 片方だけnullだから違う
   return false;
  }
  // 指定のプロパティを取得しながら比較する
  for (String property : properties) {
   Object expectedValue = null;
   Object actualValue = null;
   try {
    // 予測した値
    expectedValue = FieldUtils.readDeclaredField(expected,
      property, true);
    // 実際の値
    actualValue = FieldUtils.readDeclaredField(actual, property,
      true);
   } catch (Exception e) {
    throw new IllegalArgumentException(e);
   }
   if (expectedValue == actualValue) {
    // 完全に同じもの
    continue;
   }
   if (expectedValue == null || actualValue == null) {
    // 片方だけnullだから違う
    unmatchProperties.add(property);
    continue;
   }
   if (!expectedValue.equals(actualValue)) {
    // 違う値
    unmatchProperties.add(property);
    continue;
   }
  }
  // 一致しなかったプロパティがない場合はtrue
  return unmatchProperties.isEmpty();
 }

 /**
  * エラーの場合に表示する、実際の値を示す文字列を追加します。
  *
  * @param description
  *            エラー時の文章
  */
 public void describeMismatch(Description mismatchDescription) {
  describeMismatchSafely(actual, mismatchDescription);
 }

 /**
  * エラーの場合に表示する、実際の値を示す文字列を追加します。
  *
  * @param actual
  *            実際の値
  * @param description
  *            エラー時の文章
  */
 @Override
 public void describeMismatchSafely(T actual, Description mismatchDescription) {
  setUpDescription(mismatchDescription, actual, unmatchProperties);
 }

 /**
  * エラーの場合に表示する、予測の値を示す文字列を追加します。
  *
  * @param description
  *            エラー時の文章
  */
 @Override
 public void describeTo(Description description) {
  setUpDescription(description, expected, unmatchProperties);
 }

 /**
  * エラーの詳細を設定します。
  *
  * @param description
  *            設定先
  * @param bean
  *            対象のbean
  * @param properties
  *            対象のプロパティ名リスト
  */
 private void setUpDescription(Description description, Object bean,
   List<String> properties) {
  if (bean == null) {
   description.appendValue(null);
   return;
  }
  if (properties.isEmpty()) {
   description.appendValue(bean.getClass().getName());
   return;
  }
  int i = -1;
  for (String property : properties) {
   i++;
   if (i != 0) {
    description.appendText(", ");
   }
   description.appendText(property + "=");
   try {
    description.appendValue(FieldUtils.readDeclaredField(bean,
      property, true));
   } catch (IllegalAccessException e) {
    throw new IllegalArgumentException(e);
   }
  }
  return;
 }

 /**
  * 予想される値を取得します。
  *
  * @return 予想される値
  */
 public T getExpected() {
  return expected;
 }

 /**
  * 実際の値を取得します。
  *
  * @return 実際の値
  */
 public Object getActual() {
  return actual;
 }

 /**
  * 一致しなかったプロパティを取得します。
  *
  * @return 一致しなかったプロパティ
  */
 public List<String> getUnmatchProperties() {
  return unmatchProperties;
 }

}