WeblogManager mgr = WebloggerFactory.getWeblogger().getWeblogManager();
testWeblog = TestUtils.getManagedWebsite(testWeblog);
testUser = TestUtils.getManagedUser(testUser);
Weblog testWeblog2 = TestUtils.setupWeblog("entryTestWeblog2", testUser);
try {
// let's make sure we are starting from scratch
// site-wide
List tags = mgr.getTags(null, null, null, -1);
assertEquals(0, tags.size());
// first weblog
tags = mgr.getTags(testWeblog, null, null, -1);
assertEquals(0, tags.size());
// second weblog
tags = mgr.getTags(testWeblog2, null, null, -1);
assertEquals(0, tags.size());
// setup some test entries to use
WeblogEntry entry = TestUtils.setupWeblogEntry("entry1",
testWeblog.getDefaultCategory(), testWeblog, testUser);
entry.addTag("one");
entry.addTag("two");
mgr.saveWeblogEntry(entry);
entry = TestUtils.setupWeblogEntry("entry2",
testWeblog.getDefaultCategory(), testWeblog, testUser);
entry.addTag("one");
entry.addTag("two");
entry.addTag("three");
mgr.saveWeblogEntry(entry);
TestUtils.endSession(true);
testWeblog = TestUtils.getManagedWebsite(testWeblog);
tags = mgr.getTags(testWeblog, null, null, -1);
assertEquals(3, tags.size());
HashMap expectedWeblogTags = new HashMap();
expectedWeblogTags.put("one", new Integer(2));
expectedWeblogTags.put("two", new Integer(2));
expectedWeblogTags.put("three", new Integer(1));
for (Iterator it = tags.iterator(); it.hasNext();) {
TagStat stat = (TagStat) it.next();
if (!expectedWeblogTags.containsKey(stat.getName()))
fail("Unexpected tagName.");
Integer expectedCount = (Integer)expectedWeblogTags.get(stat.getName());
assertEquals(expectedCount.intValue(), stat.getCount());
}
// now add another entry in another blog
testWeblog2 = TestUtils.getManagedWebsite(testWeblog2);
testUser = TestUtils.getManagedUser(testUser);
entry = TestUtils.setupWeblogEntry("entry3",
testWeblog2.getDefaultCategory(), testWeblog2, testUser);
entry.addTag("one");
entry.addTag("three");
entry.addTag("four");
mgr.saveWeblogEntry(entry);
TestUtils.endSession(true);
// let's fetch "site" tags now
tags = mgr.getTags(null, null, null, -1);
assertEquals(4, tags.size());
HashMap expectedSiteTags = new HashMap();
expectedSiteTags.put("one", new Integer(3));
expectedSiteTags.put("two", new Integer(2));
expectedSiteTags.put("three", new Integer(2));
expectedSiteTags.put("four", new Integer(1));
for (Iterator it = tags.iterator(); it.hasNext();) {
TagStat stat = (TagStat) it.next();
if (!expectedSiteTags.containsKey(stat.getName()))
fail("Unexpected tagName.");
Integer expectedCount = (Integer) expectedSiteTags.get(stat.getName());
assertEquals(expectedCount.intValue(), stat.getCount());
}
TestUtils.endSession(true);
testWeblog = TestUtils.getManagedWebsite(testWeblog);
entry = mgr.getWeblogEntryByAnchor(testWeblog, "entry2");
List updateTags = new ArrayList();
updateTags.add("one");
updateTags.add("three");
updateTags.add("five");
entry.updateTags(updateTags);
mgr.saveWeblogEntry(entry);
TestUtils.endSession(true);
testWeblog = TestUtils.getManagedWebsite(testWeblog);
tags = mgr.getTags(testWeblog, null, null, -1);
assertEquals(4, tags.size());
expectedWeblogTags = new HashMap();
expectedWeblogTags.put("one", new Integer(2));
expectedWeblogTags.put("two", new Integer(1));
expectedWeblogTags.put("three", new Integer(1));
expectedWeblogTags.put("five", new Integer(1));
for (Iterator it = tags.iterator(); it.hasNext();) {
TagStat stat = (TagStat) it.next();
if (!expectedWeblogTags.containsKey(stat.getName()))
fail("Unexpected tagName.");
Integer expectedCount =
(Integer)expectedWeblogTags.get(stat.getName());
assertEquals(stat.getName(),
expectedCount.intValue(), stat.getCount());
}
tags = mgr.getTags(null, null, null, -1);
assertEquals(5, tags.size());
expectedSiteTags = new HashMap();
expectedSiteTags.put("one", new Integer(3));
expectedSiteTags.put("two", new Integer(1));
expectedSiteTags.put("three", new Integer(2));
expectedSiteTags.put("four", new Integer(1));
expectedSiteTags.put("five", new Integer(1));
for (Iterator it = tags.iterator(); it.hasNext();) {
TagStat stat = (TagStat) it.next();
if (!expectedSiteTags.containsKey(stat.getName()))
fail("Unexpected tagName.");
Integer expectedCount = (Integer)expectedSiteTags.get(stat.getName());
assertEquals(stat.getName(), expectedCount.intValue(), stat.getCount());
}
TestUtils.teardownWeblog(testWeblog2.getId());
TestUtils.endSession(true);
} catch (Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);