use of com.google.common.collect.ListMultimap in project android by JetBrains.
the class ResourceNameValidatorTest method testIds2.
public void testIds2() throws Exception {
final Map<ResourceType, ListMultimap<String, ResourceItem>> map = Maps.newHashMap();
ListMultimap<String, ResourceItem> multimap = ArrayListMultimap.create();
map.put(ResourceType.ID, multimap);
multimap.put("foo1", new ResourceItem("foo1", ResourceType.ID, null, null));
multimap.put("foo3", new ResourceItem("foo3", ResourceType.ID, null, null));
multimap.put("foo.4", new ResourceItem("foo.4", ResourceType.ID, null, null));
multimap.put("foo_5", new ResourceItem("foo_5", ResourceType.ID, null, null));
multimap.put("foo-6", new ResourceItem("foo-6", ResourceType.ID, null, null));
multimap.put("foo:7", new ResourceItem("foo:7", ResourceType.ID, null, null));
LocalResourceRepository resources = new LocalResourceRepository("unit test") {
@NonNull
@Override
protected Map<ResourceType, ListMultimap<String, ResourceItem>> getMap() {
return map;
}
@Nullable
@Override
protected ListMultimap<String, ResourceItem> getMap(ResourceType type, boolean create) {
return map.get(type);
}
@NotNull
@Override
protected Set<VirtualFile> computeResourceDirs() {
return ImmutableSet.of();
}
};
ResourceNameValidator validator = ResourceNameValidator.create(false, resources, ResourceType.ID);
assertEquals("foo1 already exists", validator.getErrorText("foo1"));
assertEquals(null, validator.getErrorText("foo2"));
assertEquals("foo3 already exists", validator.getErrorText("foo3"));
assertEquals("foo_4 already exists", validator.getErrorText("foo.4"));
assertEquals("foo_4 already exists", validator.getErrorText("foo:4"));
assertEquals("foo_4 already exists", validator.getErrorText("foo-4"));
assertEquals("foo_4 already exists", validator.getErrorText("foo_4"));
assertEquals("foo_5 already exists", validator.getErrorText("foo.5"));
assertEquals("foo_5 already exists", validator.getErrorText("foo:5"));
assertEquals("foo_5 already exists", validator.getErrorText("foo-5"));
assertEquals("foo_5 already exists", validator.getErrorText("foo_5"));
assertEquals("foo_6 already exists", validator.getErrorText("foo.6"));
assertEquals("foo_6 already exists", validator.getErrorText("foo:6"));
assertEquals("foo_6 already exists", validator.getErrorText("foo-6"));
assertEquals("foo_6 already exists", validator.getErrorText("foo_6"));
assertEquals("foo_7 already exists", validator.getErrorText("foo.7"));
assertEquals("foo_7 already exists", validator.getErrorText("foo:7"));
assertEquals("foo_7 already exists", validator.getErrorText("foo-7"));
assertEquals("foo_7 already exists", validator.getErrorText("foo_7"));
}
use of com.google.common.collect.ListMultimap in project android by JetBrains.
the class ResourceClassGeneratorTest method test.
public void test() throws Exception {
final ResourceRepository repository = TestResourceRepository.createRes2(false, new Object[] { "layout/layout1.xml", "<!--contents doesn't matter-->", "layout-land/layout1.xml", "<!--contents doesn't matter-->", "values/styles.xml", "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + " <style name=\"MyTheme.Dark\" parent=\"android:Theme.Light\">\n" + " <item name=\"android:textColor\">#999999</item>\n" + " <item name=\"foo\">?android:colorForeground</item>\n" + " </style>\n" + " <declare-styleable name=\"GridLayout_Layout\">\n" + " <attr name=\"android:layout_width\" />\n" + " <attr name=\"android:layout_height\" />\n" + " <attr name=\"layout_columnSpan\" format=\"integer\" min=\"1\" />\n" + " <attr name=\"layout_gravity\">\n" + " <flag name=\"top\" value=\"0x30\" />\n" + " <flag name=\"bottom\" value=\"0x50\" />\n" + " <flag name=\"center_vertical\" value=\"0x10\" />\n" + " </attr>\n" + " </declare-styleable>\n" + "</resources>\n", "values/strings.xml", "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + " <item type=\"id\" name=\"action_bar_refresh\" />\n" + " <item type=\"dimen\" name=\"dialog_min_width_major\">45%</item>\n" + " <string name=\"show_all_apps\">All</string>\n" + " <string name=\"menu_wallpaper\">Wallpaper</string>\n" + "</resources>\n", "values-es/strings.xml", "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + " <string name=\"show_all_apps\">Todo</string>\n" + "</resources>\n" });
LocalResourceRepository resources = new LocalResourceRepository("test") {
@NonNull
@Override
protected Map<ResourceType, ListMultimap<String, ResourceItem>> getMap() {
return repository.getItems();
}
@Nullable
@Override
protected ListMultimap<String, ResourceItem> getMap(ResourceType type, boolean create) {
return repository.getItems().get(type);
}
@NotNull
@Override
protected Set<VirtualFile> computeResourceDirs() {
return ImmutableSet.of();
}
};
AppResourceRepository appResources = new AppResourceRepository(myFacet, Collections.singletonList(resources), Collections.<FileResourceRepository>emptyList());
ResourceClassGenerator generator = ResourceClassGenerator.create(appResources);
assertNotNull(generator);
String name = "my.test.pkg.R";
Class<?> clz = generateClass(generator, name);
assertNotNull(clz);
assertEquals(name, clz.getName());
assertTrue(Modifier.isPublic(clz.getModifiers()));
assertTrue(Modifier.isFinal(clz.getModifiers()));
assertFalse(Modifier.isInterface(clz.getModifiers()));
Object r = clz.newInstance();
assertNotNull(r);
name = "my.test.pkg.R$string";
clz = generateClass(generator, name);
assertNotNull(clz);
assertEquals(name, clz.getName());
assertTrue(Modifier.isPublic(clz.getModifiers()));
assertTrue(Modifier.isFinal(clz.getModifiers()));
assertFalse(Modifier.isInterface(clz.getModifiers()));
try {
clz.getField("nonexistent");
fail("Shouldn't find nonexistent fields");
} catch (NoSuchFieldException e) {
// pass
}
Field field1 = clz.getField("menu_wallpaper");
Object value1 = field1.get(null);
assertEquals(Integer.TYPE, field1.getType());
assertNotNull(value1);
assertEquals(2, clz.getFields().length);
Field field2 = clz.getField("show_all_apps");
assertNotNull(field2);
assertEquals(Integer.TYPE, field2.getType());
assertTrue(Modifier.isPublic(field2.getModifiers()));
assertTrue(Modifier.isFinal(field2.getModifiers()));
assertTrue(Modifier.isStatic(field2.getModifiers()));
assertFalse(Modifier.isSynchronized(field2.getModifiers()));
assertFalse(Modifier.isTransient(field2.getModifiers()));
assertFalse(Modifier.isStrict(field2.getModifiers()));
assertFalse(Modifier.isVolatile(field2.getModifiers()));
r = clz.newInstance();
assertNotNull(r);
// Make sure the id's match what we've dynamically allocated in the resource repository
@SuppressWarnings("deprecation") Pair<ResourceType, String> pair = appResources.resolveResourceId((Integer) clz.getField("menu_wallpaper").get(null));
assertNotNull(pair);
assertEquals(ResourceType.STRING, pair.getFirst());
assertEquals("menu_wallpaper", pair.getSecond());
assertEquals(clz.getField("menu_wallpaper").get(null), appResources.getResourceId(ResourceType.STRING, "menu_wallpaper"));
assertEquals(clz.getField("show_all_apps").get(null), appResources.getResourceId(ResourceType.STRING, "show_all_apps"));
// Test attr class!
name = "my.test.pkg.R$attr";
clz = generateClass(generator, name);
assertNotNull(clz);
assertEquals(name, clz.getName());
assertTrue(Modifier.isPublic(clz.getModifiers()));
assertTrue(Modifier.isFinal(clz.getModifiers()));
assertFalse(Modifier.isInterface(clz.getModifiers()));
assertEquals(2, clz.getFields().length);
field1 = clz.getField("layout_gravity");
assertNotNull(field1);
Object gravityValue = field1.get(null);
Object layoutColumnSpanValue = clz.getField("layout_columnSpan").get(null);
// Test style class
styleTest(generator);
// Run the same test to check caching.
styleTest(generator);
// Test styleable class!
styleableTest(generator, gravityValue, layoutColumnSpanValue);
// Run the same test again to ensure that caching is working as expected.
styleableTest(generator, gravityValue, layoutColumnSpanValue);
name = "my.test.pkg.R$id";
clz = generateClass(generator, name);
assertNotNull(clz);
r = clz.newInstance();
assertNotNull(r);
assertEquals(name, clz.getName());
// getEnclosingClass() results in generating all R classes. So, this should be called at the end
// so that tests for caching work as expected.
Class<?> enclosingClass = clz.getEnclosingClass();
assertNotNull(enclosingClass);
// TODO: Flag and enum values should also be created as id's by the ValueResourceParser
//assertNotNull(clz.getField("top"));
//assertNotNull(clz.getField("bottom"));
//assertNotNull(clz.getField("center_vertical"));
}
use of com.google.common.collect.ListMultimap in project gerrit by GerritCodeReview.
the class ExternalIdsConsistencyChecker method check.
private List<ConsistencyProblemInfo> check(Repository repo, ObjectId commit) throws IOException {
List<ConsistencyProblemInfo> problems = new ArrayList<>();
ListMultimap<String, ExternalId.Key> emails = MultimapBuilder.hashKeys().arrayListValues().build();
try (RevWalk rw = new RevWalk(repo)) {
NoteMap noteMap = ExternalIdReader.readNoteMap(rw, commit);
for (Note note : noteMap) {
byte[] raw = rw.getObjectReader().open(note.getData(), OBJ_BLOB).getCachedBytes(ExternalIdReader.MAX_NOTE_SZ);
try {
ExternalId extId = ExternalId.parse(note.getName(), raw);
problems.addAll(validateExternalId(extId));
if (extId.email() != null) {
emails.put(extId.email(), extId.key());
}
} catch (ConfigInvalidException e) {
addError(String.format(e.getMessage()), problems);
}
}
}
emails.asMap().entrySet().stream().filter(e -> e.getValue().size() > 1).forEach(e -> addError(String.format("Email '%s' is not unique, it's used by the following external IDs: %s", e.getKey(), e.getValue().stream().map(k -> "'" + k.get() + "'").sorted().collect(joining(", "))), problems));
return problems;
}
Aggregations