Move getPersonParts to SchemaUtil and add tests
Change-Id: I50d52d3f5f8a879ecd51389ccb82767acca6e56f
This commit is contained in:
@@ -16,15 +16,25 @@ package com.google.gerrit.server.index;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
import com.google.common.base.CharMatcher;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import org.eclipse.jgit.lib.PersonIdent;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class SchemaUtil {
|
||||
public static <V> ImmutableMap<Integer, Schema<V>> schemasFromClass(
|
||||
@@ -67,6 +77,20 @@ public class SchemaUtil {
|
||||
return schema(ImmutableList.copyOf(fields));
|
||||
}
|
||||
|
||||
public static Set<String> getPersonParts(PersonIdent person) {
|
||||
if (person == null) {
|
||||
return ImmutableSet.of();
|
||||
}
|
||||
HashSet<String> parts = Sets.newHashSet();
|
||||
String email = person.getEmailAddress().toLowerCase();
|
||||
parts.add(email);
|
||||
parts.addAll(Arrays.asList(email.split("@")));
|
||||
Splitter s = Splitter.on(CharMatcher.anyOf("@.- ")).omitEmptyStrings();
|
||||
Iterables.addAll(parts, s.split(email));
|
||||
Iterables.addAll(parts, s.split(person.getName().toLowerCase()));
|
||||
return parts;
|
||||
}
|
||||
|
||||
private SchemaUtil() {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ package com.google.gerrit.server.index.change;
|
||||
|
||||
import static com.google.common.base.MoreObjects.firstNonNull;
|
||||
|
||||
import com.google.common.base.CharMatcher;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
@@ -32,6 +31,7 @@ import com.google.gerrit.reviewdb.client.PatchSet;
|
||||
import com.google.gerrit.reviewdb.client.PatchSetApproval;
|
||||
import com.google.gerrit.server.index.FieldDef;
|
||||
import com.google.gerrit.server.index.FieldType;
|
||||
import com.google.gerrit.server.index.SchemaUtil;
|
||||
import com.google.gerrit.server.query.change.ChangeData;
|
||||
import com.google.gerrit.server.query.change.ChangeData.ChangedLines;
|
||||
import com.google.gerrit.server.query.change.ChangeQueryBuilder;
|
||||
@@ -41,14 +41,12 @@ import com.google.gwtorm.protobuf.ProtobufCodec;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.protobuf.CodedOutputStream;
|
||||
|
||||
import org.eclipse.jgit.lib.PersonIdent;
|
||||
import org.eclipse.jgit.revwalk.FooterLine;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@@ -372,23 +370,9 @@ public class ChangeField {
|
||||
}
|
||||
};
|
||||
|
||||
private static Set<String> getPersonParts(PersonIdent person) {
|
||||
if (person == null) {
|
||||
return ImmutableSet.of();
|
||||
}
|
||||
HashSet<String> parts = Sets.newHashSet();
|
||||
String email = person.getEmailAddress().toLowerCase();
|
||||
parts.add(email);
|
||||
parts.addAll(Arrays.asList(email.split("@")));
|
||||
Splitter s = Splitter.on(CharMatcher.anyOf("@.- ")).omitEmptyStrings();
|
||||
Iterables.addAll(parts, s.split(email));
|
||||
Iterables.addAll(parts, s.split(person.getName().toLowerCase()));
|
||||
return parts;
|
||||
}
|
||||
|
||||
public static Set<String> getAuthorParts(ChangeData cd) throws OrmException {
|
||||
try {
|
||||
return getPersonParts(cd.getAuthor());
|
||||
return SchemaUtil.getPersonParts(cd.getAuthor());
|
||||
} catch (IOException e) {
|
||||
throw new OrmException(e);
|
||||
}
|
||||
@@ -396,7 +380,7 @@ public class ChangeField {
|
||||
|
||||
public static Set<String> getCommitterParts(ChangeData cd) throws OrmException {
|
||||
try {
|
||||
return getPersonParts(cd.getCommitter());
|
||||
return SchemaUtil.getPersonParts(cd.getCommitter());
|
||||
} catch (IOException e) {
|
||||
throw new OrmException(e);
|
||||
}
|
||||
|
||||
@@ -15,10 +15,12 @@
|
||||
package com.google.gerrit.server.index;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.gerrit.server.index.SchemaUtil.getPersonParts;
|
||||
import static com.google.gerrit.server.index.SchemaUtil.schema;
|
||||
|
||||
import com.google.gerrit.testutil.GerritBaseTests;
|
||||
|
||||
import org.eclipse.jgit.lib.PersonIdent;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -47,4 +49,23 @@ public class SchemaUtilTest extends GerritBaseTests {
|
||||
exception.expect(IllegalArgumentException.class);
|
||||
SchemaUtil.schemasFromClass(TestSchemas.class, Object.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPersonPartsExtractsParts() {
|
||||
// PersonIdent allows empty email, which should be extracted as the empty
|
||||
// string. However, it converts empty names to null internally.
|
||||
assertThat(getPersonParts(new PersonIdent("", ""))).containsExactly("");
|
||||
assertThat(getPersonParts(new PersonIdent("foo bar", "")))
|
||||
.containsExactly("foo", "bar", "");
|
||||
|
||||
assertThat(getPersonParts(new PersonIdent("", "foo@example.com")))
|
||||
.containsExactly(
|
||||
"foo@example.com", "foo", "example.com", "example", "com");
|
||||
assertThat(
|
||||
getPersonParts(new PersonIdent("foO J. bAr", "bA-z@exAmple.cOm")))
|
||||
.containsExactly(
|
||||
"foo", "j", "bar",
|
||||
"ba-z@example.com", "ba-z", "ba", "z",
|
||||
"example.com", "example", "com");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user