📄 blogentrytest.java
字号:
/* * Copyright (c) 2003-2006, Simon Brown * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * - Neither the name of Pebble nor the names of its contributors may * be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */package net.sourceforge.pebble.domain;import java.beans.PropertyChangeEvent;import java.text.DecimalFormat;import java.util.*;/** * Tests for the BlogEntry class. * * @author Simon Brown */public class BlogEntryTest extends SingleBlogTestCase { private BlogEntry blogEntry; protected void setUp() throws Exception { super.setUp(); blogEntry = new BlogEntry(blog); blogEntry.setTitle("A title"); blogEntry.setBody("Some body"); blogEntry.setExcerpt("Some excerpt"); blogEntry.setAuthor("An author"); blogEntry.setDate(new Date()); blogEntry.setEventsEnabled(true); } /** * Tests the construction. */ public void testConstruction() { assertNotNull(blogEntry.getId()); assertEquals("A title", blogEntry.getTitle()); assertEquals("Some body", blogEntry.getBody()); assertNotNull(blogEntry.getCategories()); assertEquals(0, blogEntry.getCategories().size()); assertTrue(blogEntry.isCommentsEnabled()); assertNotNull(blogEntry.getComments()); assertEquals(0, blogEntry.getComments().size()); assertNotNull(blogEntry.getTrackBacks()); assertEquals(0, blogEntry.getTrackBacks().size()); assertNotNull(blogEntry.getDate()); assertNotNull(blogEntry.getAuthor()); assertFalse(blogEntry.isAggregated()); assertNotNull(blogEntry.getTags()); assertEquals(0, blogEntry.getTagsAsList().size()); } /** * Tests that the root blog is setup correctly. */ public void testGetRootBlog() { assertEquals(blog, blogEntry.getBlog()); } /** * Tests that the root is setup correctly. */ public void testDay() { //assertEquals(dailyBlog, blogEntry.getDay()); } /** * Tests for the id. */ public void testId() { String id = "" + blogEntry.getDate().getTime(); assertEquals(id, blogEntry.getId()); } /** * Tests for the title. */ public void testTitle() { blogEntry.setTitle("A new title"); assertEquals("A new title", blogEntry.getTitle()); } /** * Tests for the subtitle. */ public void testSubtitle() { blogEntry.setSubtitle("A new subtitle"); assertEquals("A new subtitle", blogEntry.getSubtitle()); } /** * Tests for the body. */ public void testBody() { blogEntry.setBody("A new body"); assertEquals("A new body", blogEntry.getBody()); } /** * Tests for the excerpt. */ public void testExcerpt() { blogEntry.setExcerpt("An excerpt"); assertEquals("An excerpt", blogEntry.getExcerpt()); } /** * Tests for the categories. */ public void testCategories() { assertEquals(0, blogEntry.getCategories().size()); Category category = new Category("1", "One"); blogEntry.addCategory(category); assertEquals(1, blogEntry.getCategories().size()); assertTrue(blogEntry.getCategories().contains(category)); // just check that we can't add null categories! blogEntry.addCategory(null); assertFalse(blogEntry.getCategories().contains(null)); } /** * Tests for the "in category" check. */ public void testInCategory() { CategoryBuilder builder = new CategoryBuilder(blog); Category apple = new Category("/apple", "Apple"); Category java = new Category("/java", "Java"); Category junit = new Category("/java/junit", "JUnit"); builder.addCategory(apple); builder.addCategory(java); builder.addCategory(junit); blogEntry.addCategory(apple); assertTrue(blogEntry.inCategory(apple)); assertFalse(blogEntry.inCategory(java)); assertFalse(blogEntry.inCategory(junit)); assertTrue(blogEntry.inCategory(null)); blogEntry.addCategory(junit); assertTrue(blogEntry.inCategory(apple)); assertTrue(blogEntry.inCategory(java)); assertTrue(blogEntry.inCategory(junit)); } /** * Tests that all categories can be removed. */ public void testAllCategoriesCanBeRemoved() { CategoryBuilder builder = new CategoryBuilder(blog); Category apple = new Category("/apple", "Apple"); Category java = new Category("/java", "Java"); Category junit = new Category("/java/junit", "JUnit"); builder.addCategory(apple); builder.addCategory(java); builder.addCategory(junit); blogEntry.addCategory(apple); blogEntry.addCategory(junit); blogEntry.removeAllCategories(); assertEquals(0, blogEntry.getCategories().size()); } /** * Tests for the tags. */ public void testTagsSeparatedByCommas() { blogEntry.setTags("some, tags"); assertEquals("some tags", blogEntry.getTags()); List tags = blogEntry.getTagsAsList(); assertEquals(2, tags.size()); assertEquals(blog.getTag("some"), tags.get(0)); assertEquals(blog.getTag("tags"), tags.get(1)); } /** * Tests for the tags. */ public void testTagsSeparatedByWhitespace() { blogEntry.setTags("some tags"); assertEquals("some tags", blogEntry.getTags()); List tags = blogEntry.getTagsAsList(); assertEquals(2, tags.size()); assertEquals(blog.getTag("some"), tags.get(0)); assertEquals(blog.getTag("tags"), tags.get(1)); } /** * Tests for the date. */ public void testDate() { assertNotNull(blogEntry.getDate()); } /** * Tests for the author. */ public void testAuthor() { blogEntry.setAuthor("A new author"); assertEquals("A new author", blogEntry.getAuthor()); } /** * Tests the aggregated property. */ public void testAggregated() { blogEntry.setOriginalPermalink("http://www.simongbrown.com/blog/2003/04/01.html#a123456789"); assertTrue(blogEntry.isAggregated()); blogEntry.setOriginalPermalink(null); assertFalse(blogEntry.isAggregated()); blogEntry.setOriginalPermalink(""); assertFalse(blogEntry.isAggregated()); } /** * Tests the comments enabled property. */ public void testCommentsEnabled() { assertTrue(blogEntry.isCommentsEnabled()); blogEntry.setCommentsEnabled(false); assertFalse(blogEntry.isCommentsEnabled()); } /** * Tests the links that will refer to a blog entry and its comments. */ public void testLinks() { DecimalFormat format = new DecimalFormat("00"); Calendar cal = blog.getCalendar(); cal.setTime(blogEntry.getDate()); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1; int day = cal.get(Calendar.DAY_OF_MONTH); assertEquals("http://www.yourdomain.com/blog/" + year + "/" + format.format(month) + "/" + format.format(day) + "/" + blogEntry.getId() + ".html", blogEntry.getPermalink()); assertEquals("http://www.yourdomain.com/blog/" + year + "/" + format.format(month) + "/" + format.format(day) + "/" + blogEntry.getId() + ".html", blogEntry.getLocalPermalink()); assertEquals(blogEntry.getLocalPermalink() + "#comments", blogEntry.getCommentsLink()); assertEquals(blogEntry.getLocalPermalink() + "#trackbacks" , blogEntry.getTrackBacksLink()); } /** * Tests that a blog entry can correctly manage blog comments. */ public void testComments() { Comment blogComment1, blogComment2; Calendar cal1, cal2; cal1 = blog.getCalendar(); cal1.set(Calendar.DAY_OF_MONTH, 1); cal2 = blog.getCalendar(); cal2.set(Calendar.DAY_OF_MONTH, 2); // check that there are no comments to start with assertEquals(0, blogEntry.getNumberOfComments()); assertEquals(0, blogEntry.getComments().size()); // now create a new comment blogComment1 = blogEntry.createComment("", "Body", "Author", "me@somedomain.com", "http://www.google.com", "127.0.0.1", cal1.getTime(), State.APPROVED); assertNotNull(blogComment1); assertEquals("Re: " + blogEntry.getTitle(), blogComment1.getTitle()); assertEquals("Body", blogComment1.getBody()); assertEquals("Author", blogComment1.getAuthor()); assertEquals("http://www.google.com", blogComment1.getWebsite()); assertNotNull(blogComment1.getDate()); assertEquals(blogEntry, blogComment1.getBlogEntry()); // the comment hasn't been added yet assertEquals(0, blogEntry.getNumberOfComments()); assertEquals(0, blogEntry.getComments().size()); assertFalse(blogEntry.getComments().contains(blogComment1)); // let's now add the comment blogEntry.addComment(blogComment1); assertEquals(1, blogEntry.getNumberOfComments()); assertEquals(1, blogEntry.getComments().size()); assertTrue(blogEntry.getComments().contains(blogComment1)); // and now add another comment blogComment2 = blogEntry.createComment("Title 2", "Body 2", "Author 2", "me2@somedomain.com", "http://www.yahoo.com", "127.0.0.1", cal2.getTime(), State.APPROVED); assertEquals("Title 2", blogComment2.getTitle()); assertEquals(1, blogEntry.getNumberOfComments()); assertTrue(blogEntry.getComments().contains(blogComment1)); assertFalse(blogEntry.getComments().contains(blogComment2)); blogEntry.addComment(blogComment2); assertEquals(2, blogEntry.getNumberOfComments()); assertEquals(2, blogEntry.getComments().size()); assertTrue(blogEntry.getComments().contains(blogComment1)); assertTrue(blogEntry.getComments().contains(blogComment2)); // check that we can't add the same comment more than once blogEntry.addComment(blogComment2); assertEquals(2, blogEntry.getNumberOfComments()); // and now delete the comments blogEntry.removeComment(blogComment1.getId()); assertFalse(blogEntry.getComments().contains(blogComment1)); assertTrue(blogEntry.getComments().contains(blogComment2));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -