svnno****@sourc*****
svnno****@sourc*****
2009年 2月 10日 (火) 01:30:33 JST
Revision: 2648 http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=2648 Author: daisuke_m Date: 2009-02-10 01:30:32 +0900 (Tue, 10 Feb 2009) Log Message: ----------- AbstractIdentifierValidatorの実装完成。 Modified Paths: -------------- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/validator/impl/AbstractIdentifierValidator.java artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/validator/impl/AbstractIdentifierValidatorTest.java -------------- next part -------------- Modified: artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/validator/impl/AbstractIdentifierValidator.java =================================================================== --- artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/validator/impl/AbstractIdentifierValidator.java 2009-02-09 16:21:25 UTC (rev 2647) +++ artemis/trunk/jiemamy-core/src/main/java/org/jiemamy/validator/impl/AbstractIdentifierValidator.java 2009-02-09 16:30:32 UTC (rev 2648) @@ -19,6 +19,7 @@ package org.jiemamy.validator.impl; import java.util.Collection; +import java.util.Locale; import java.util.regex.Pattern; import org.apache.commons.lang.ArrayUtils; @@ -31,6 +32,7 @@ import org.jiemamy.model.entity.EntityModel; import org.jiemamy.model.entity.TableModel; import org.jiemamy.model.index.IndexModel; +import org.jiemamy.validator.AbstractProblem; import org.jiemamy.validator.AbstractValidator; import org.jiemamy.validator.Problem; @@ -41,7 +43,7 @@ */ public abstract class AbstractIdentifierValidator extends AbstractValidator { - private Collection<Problem> result; + Collection<Problem> result; /** 識別子としての妥当性を判断する正規表現パターン */ private Pattern identifierPattern; @@ -69,19 +71,19 @@ result = super.validate(rootModel); for (DomainModel domainModel : rootModel.getDomains()) { - check(domainModel.getName()); + isValid(domainModel.getName()); } for (EntityModel entityModel : rootModel.getEntities()) { - check(entityModel.getName()); + isValid(entityModel.getName()); if (entityModel instanceof TableModel) { TableModel tableModel = (TableModel) entityModel; for (AttributeModel attributeModel : tableModel.getAttributes()) { - check(attributeModel.getName()); + isValid(attributeModel.getName()); } for (IndexModel indexModel : tableModel.getIndexes()) { - check(indexModel.getName()); + isValid(indexModel.getName()); } } } @@ -89,20 +91,69 @@ return result; } - boolean check(String name) { + boolean isValid(String name) { + boolean valid = true; if (name == null) { - return false; + return valid; } - boolean invalid = false; if (identifierPattern.matcher(name).matches() == false) { - // UNDONE 正規表現にマッチしないProblem - invalid = true; + result.add(new InvalidNameProblem(name, identifierPattern)); + valid = false; } if (ArrayUtils.contains(reservedWords.getReservedWords(), name)) { - // UNDONE 予約語と衝突したProblem - invalid = true; + result.add(new ReservedWordProblem(name)); + valid = false; } - return invalid; + return valid; } + + static class InvalidNameProblem extends AbstractProblem { + + /** + * インスタンスを生成する。 + * @param name + * @param pattern + */ + public InvalidNameProblem(String name, Pattern pattern) { + super("X0000"); // TODO エラーコード設定 + registerMessage(Locale.JAPAN, "識別子{0}は、正規表現{1}にマッチしません"); + setArguments(new Object[] { + name, + pattern.pattern() + }); + } + + /** + * {@inheritDoc} + */ + public Severity getSeverity() { + return Severity.ERROR; + } + + } + + static class ReservedWordProblem extends AbstractProblem { + + /** + * インスタンスを生成する。 + * @param name + */ + public ReservedWordProblem(String name) { + super("X0000"); // TODO エラーコード設定 + registerMessage(Locale.JAPAN, "予約語{0}が識別子として使用されています"); + setArguments(new Object[] { + name + }); + } + + /** + * {@inheritDoc} + */ + public Severity getSeverity() { + return Severity.ERROR; + } + + } + } Modified: artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/validator/impl/AbstractIdentifierValidatorTest.java =================================================================== --- artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/validator/impl/AbstractIdentifierValidatorTest.java 2009-02-09 16:21:25 UTC (rev 2647) +++ artemis/trunk/jiemamy-core/src/test/java/org/jiemamy/validator/impl/AbstractIdentifierValidatorTest.java 2009-02-09 16:30:32 UTC (rev 2648) @@ -61,17 +61,25 @@ */ @Test public void test_バリデータ動作テスト() throws Exception { + assertThat(validator.result.size(), is(0)); // 基本的な正規表現バリデート - assertThat(validator.check("ABC"), is(false)); - assertThat(validator.check("012"), is(true)); - assertThat(validator.check("ABC012"), is(false)); - assertThat(validator.check("abc012"), is(true)); - assertThat(validator.check(""), is(true)); + assertThat(validator.isValid("ABC"), is(true)); + assertThat(validator.result.size(), is(0)); + assertThat(validator.isValid("012"), is(false)); + assertThat(validator.result.size(), is(1)); + assertThat(validator.isValid("ABC012"), is(true)); + assertThat(validator.result.size(), is(1)); + assertThat(validator.isValid("abc012"), is(false)); + assertThat(validator.result.size(), is(2)); + assertThat(validator.isValid(""), is(false)); + assertThat(validator.result.size(), is(3)); + assertThat(validator.isValid(null), is(true)); // nullは無条件に通過 + assertThat(validator.result.size(), is(3)); // 予約語バリデート - assertThat(validator.check("RESERVED1"), is(true)); - assertThat(validator.check("RESERVED2"), is(true)); - assertThat(validator.check("RESERVED3"), is(false)); + assertThat(validator.isValid("RESERVED1"), is(false)); + assertThat(validator.isValid("RESERVED2"), is(false)); + assertThat(validator.isValid("RESERVED3"), is(true)); }