Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
refactor and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Dec 9, 2012
1 parent 16af672 commit 5c3ecb0
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 39 deletions.
@@ -1,11 +1,13 @@
package org.zanata.webtrans.client.presenter;

import java.util.HashSet;
import java.util.Set;

import org.zanata.webtrans.shared.model.DocumentInfo;
import com.beust.jcommander.internal.Sets;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;

/**
* Filters documents by their full path + name, with substring and exact
Expand All @@ -20,7 +22,8 @@ public final class PathDocumentFilter
{
private static final String DOCUMENT_FILTER_LIST_DELIMITER = ",";

private HashSet<String> patterns = new HashSet<String>();
private Set<String> patterns = Sets.newHashSet();
private Set<String> patternsInLowerCase = Sets.newHashSet();
private boolean isFullText = false;
private boolean caseSensitive = false;

Expand All @@ -31,53 +34,50 @@ public boolean accept(DocumentInfo value)
return true;
}
String fullPath = value.getPath() + value.getName();
if (!caseSensitive)
{
fullPath = fullPath.toLowerCase();
}
for (String pattern : patterns)
{
if (!caseSensitive)
{
pattern = pattern.toLowerCase();
}
if (isFullText)
{
if (fullPath.equals(pattern))
{
return true;
}
}
else if (fullPath.contains(pattern))
{
return true;
}
}
return false; // didn't match any patterns

Iterable<String> patternsToUse = caseSensitive ? patterns : patternsInLowerCase;
fullPath = caseSensitive ? fullPath : fullPath.toLowerCase();

Optional<String> matchedPattern = Iterables.tryFind(patternsToUse, new MatchPredicate(isFullText, fullPath));
return matchedPattern.isPresent();
}

public void setPattern(String pattern)
public PathDocumentFilter setPattern(String pattern)
{
patterns.clear();
String[] patternCandidates = pattern.split(DOCUMENT_FILTER_LIST_DELIMITER);

for (String candidate : patternCandidates)
{
candidate = candidate.trim();
if (candidate.length() != 0)
{
patterns.add(candidate);
}
}
Splitter splitter = Splitter.on(DOCUMENT_FILTER_LIST_DELIMITER).trimResults().omitEmptyStrings();
Iterables.addAll(patterns, splitter.split(pattern));
Iterables.addAll(patternsInLowerCase, splitter.split(pattern.toLowerCase()));
return this;
}

public void setFullText(boolean fullText)
public PathDocumentFilter setFullText(boolean fullText)
{
isFullText = fullText;
return this;
}

public void setCaseSensitive(boolean caseSensitive)
public PathDocumentFilter setCaseSensitive(boolean caseSensitive)
{
this.caseSensitive = caseSensitive;
return this;
}

private static class MatchPredicate implements Predicate<String>
{
private final boolean isFullText;
private final String fullPath;

private MatchPredicate(boolean isFullText, String fullPath)
{
this.isFullText = isFullText;
this.fullPath = fullPath;
}

@Override
public boolean apply(String input)
{
return isFullText ? fullPath.equals(input) : fullPath.contains(input);
}
}
}
@@ -0,0 +1,67 @@
package org.zanata.webtrans.client.presenter;

import org.hamcrest.Matchers;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.common.LocaleId;
import org.zanata.common.TranslationStats;
import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.DocumentInfo;

import static org.hamcrest.MatcherAssert.*;

/**
* @author Patrick Huang <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
public class PathDocumentFilterTest
{
private PathDocumentFilter filter;

@BeforeMethod
public void setUp() throws Exception
{
filter = new PathDocumentFilter();
}

private static DocumentInfo docInfo(String name, String path)
{
return new DocumentInfo(new DocumentId(1L), name, path, LocaleId.EN_US, new TranslationStats());
}

@Test
public void testAcceptWithCaseInsensitiveAndNotExactMatch() throws Exception
{
filter.setPattern("a,b b, c , , d");

assertThat(filter.accept(docInfo("a", "/pot/")), Matchers.equalTo(true));
assertThat(filter.accept(docInfo("b b", "/pot/")), Matchers.equalTo(true));
assertThat(filter.accept(docInfo("c", "/pot/")), Matchers.equalTo(true));
assertThat(filter.accept(docInfo("C", "/pot/")), Matchers.equalTo(true));
assertThat(filter.accept(docInfo("d", "/pot/")), Matchers.equalTo(true));
assertThat(filter.accept(docInfo("b", "/pot/")), Matchers.equalTo(false));
}

@Test
public void testAcceptWithCaseSensitiveAndNotExactMatch() throws Exception
{
filter.setPattern("a").setCaseSensitive(true);

assertThat(filter.accept(docInfo("a", "/pot/")), Matchers.equalTo(true));
assertThat(filter.accept(docInfo("A", "/pot/")), Matchers.equalTo(false));
}

@Test
public void testSetFullText() throws Exception
{
filter.setPattern("/pot/a").setFullText(true);

assertThat(filter.accept(docInfo("a", "/pot/")), Matchers.equalTo(true));
assertThat(filter.accept(docInfo("a", "")), Matchers.equalTo(false));
}

@Test
public void alwaysAcceptIfNoPattern()
{
assertThat(filter.accept(docInfo("a", "/pot/")), Matchers.equalTo(true));
}
}

0 comments on commit 5c3ecb0

Please sign in to comment.