📄 blogtest.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 net.sourceforge.pebble.Constants;import net.sourceforge.pebble.PebbleContext;import net.sourceforge.pebble.event.DefaultEventDispatcher;import net.sourceforge.pebble.api.event.blog.BlogEvent;import net.sourceforge.pebble.api.event.blog.BlogListener;import net.sourceforge.pebble.logging.CombinedLogFormatLogger;import net.sourceforge.pebble.permalink.DefaultPermalinkProvider;import java.io.File;import java.util.*;/** * Tests for the Blog class. * * @author Simon Brown */public class BlogTest extends SingleBlogTestCase { public void testConstructionOfDefaultInstance() { assertEquals(new File(TEST_BLOG_LOCATION, "blogs/default").getAbsolutePath(), blog.getRoot()); assertNull(blog.getBlog()); assertEquals("My blog", blog.getName()); assertEquals("", blog.getDescription()); assertEquals("Blog Owner", blog.getAuthor()); assertEquals("blog@yourdomain.com", blog.getEmail()); assertEquals(TimeZone.getTimeZone("Europe/London"), blog.getTimeZone()); assertEquals("en", blog.getLanguage()); assertEquals("GB", blog.getCountry()); assertEquals("UTF-8", blog.getCharacterEncoding()); assertEquals(3, blog.getRecentBlogEntriesOnHomePage()); assertEquals(3, blog.getRecentResponsesOnHomePage()); assertTrue(blog.isPublic()); assertFalse(blog.isPrivate()); assertEquals("net.sourceforge.pebble.permalink.DefaultPermalinkProvider", blog.getPermalinkProviderName()); assertTrue(blog.getPermalinkProvider() instanceof DefaultPermalinkProvider); assertEquals("net.sourceforge.pebble.event.DefaultEventDispatcher", blog.getEventDispatcherName()); assertTrue(blog.getEventDispatcher() instanceof DefaultEventDispatcher); assertEquals("net.sourceforge.pebble.logging.CombinedLogFormatLogger", blog.getLoggerName()); assertTrue(blog.getLogger() instanceof CombinedLogFormatLogger); } /** * Tests that we can get a specific property. */ public void testGetProperty() { assertEquals(blog.getName(), blog.getProperty(Blog.NAME_KEY)); } /** * Tests that we can get a specific property. */ public void testGetProperties() { Properties props = blog.getProperties(); assertNotNull(props); assertEquals(blog.getName(), props.getProperty(Blog.NAME_KEY)); } /** * Tests that we can set a specific property. */ public void testSetProperty() { blog.setProperty(Blog.NAME_KEY, "New name"); assertEquals("New name", blog.getProperty(Blog.NAME_KEY)); assertEquals("New name", blog.getName()); // and a new property blog.setProperty("aNewPropertyKey", "A new property value"); assertEquals("A new property value", blog.getProperty("aNewPropertyKey")); } /** * Tests that we can store properties. public void testStoreProperties() { blog.setProperty("aNewPropertyKey", "A new property value"); try { blog.storeProperties(); blog = new Blog(TEST_BLOG_LOCATION.getAbsolutePath()); assertEquals("A new property value", blog.getProperty("aNewPropertyKey")); // and clean up blog.removeProperty("aNewPropertyKey"); blog.storeProperties(); } catch (BlogServiceException e) { fail(); } } */ /** * Tests that we can remove a specific property. */ public void testRemoveProperty() { blog.setProperty("aNewPropertyKey", "A new property value"); assertEquals("A new property value", blog.getProperty("aNewPropertyKey")); blog.removeProperty("aNewPropertyKey"); assertNull(blog.getProperty("aNewPropertyKey")); } /** * Tests that the correct calendar (with timezone) is created. */ public void testCalendar() { Calendar cal = blog.getCalendar(); assertEquals(blog.getTimeZone(), cal.getTimeZone()); } /** * Tests that we can get a specific Year instance. */ public void testGetBlogForYear() { Calendar cal = blog.getCalendar(); Year year = blog.getBlogForYear(cal.get(Calendar.YEAR)); assertNotNull(year); assertEquals(cal.get(Calendar.YEAR), year.getYear()); } /** * Tests that we can get a previous Year instance. */ public void testGetBlogForPreviousYear() { Calendar cal = blog.getCalendar(); Year year = blog.getBlogForYear(cal.get(Calendar.YEAR)); year = blog.getBlogForPreviousYear(year); assertNotNull(year); assertEquals(cal.get(Calendar.YEAR)-1, year.getYear()); } /** * Tests that we can get a next Year instance. */ public void testGetBlogForNextYear() { Calendar cal = blog.getCalendar(); Year year = blog.getBlogForYear(cal.get(Calendar.YEAR)); year = blog.getBlogForNextYear(year); assertNotNull(year); assertEquals(cal.get(Calendar.YEAR)+1, year.getYear()); } /** * Tests that we can get the first Month instance. */ public void testGetBlogForFirstMonth() { Month month = blog.getBlogForFirstMonth(); assertNotNull(month);// assertEquals(blog.getBlogForFirstYear(), month.getYear()); Calendar cal = blog.getCalendar(); assertEquals(cal.get(Calendar.MONTH)+1, month.getMonth()); } /** * Tests that we can get a Month instance. */ public void testGetBlogForMonth() { Month month = blog.getBlogForMonth(2003, 4); assertNotNull(month); assertEquals(2003, month.getYear().getYear()); assertEquals(4, month.getMonth()); } /** * Tests that we can get the Month instance for this month. */ public void testGetBlogForThisMonth() { Calendar cal = blog.getCalendar(); Month month = blog.getBlogForThisMonth(); assertNotNull(month); assertEquals(cal.get(Calendar.YEAR), month.getYear().getYear()); assertEquals(cal.get(Calendar.MONTH) + 1, month.getMonth()); } /** * Tests that we can get a Day instance. */ public void testGetBlogForDay() { Day day = blog.getBlogForDay(2003, 7, 14); assertNotNull(day); assertEquals(2003, day.getMonth().getYear().getYear()); assertEquals(7, day.getMonth().getMonth()); assertEquals(14, day.getDay()); } /** * Tests that we can get a Day instance. */ public void testGetBlogForDate() { Calendar cal = blog.getCalendar(); cal.set(Calendar.YEAR, 2003); cal.set(Calendar.MONTH, 6); cal.set(Calendar.DAY_OF_MONTH, 14); Day day = blog.getBlogForDay(cal.getTime()); assertNotNull(day); assertEquals(2003, day.getMonth().getYear().getYear()); assertEquals(7, day.getMonth().getMonth()); assertEquals(14, day.getDay()); } /** * Tests that we can get the Day instance for today. */ public void testGetBlogForToday() { Calendar cal = blog.getCalendar(); Day day = blog.getBlogForToday(); assertNotNull(day); assertEquals(cal.get(Calendar.YEAR), day.getMonth().getYear().getYear()); assertEquals(cal.get(Calendar.MONTH) + 1, day.getMonth().getMonth()); assertEquals(cal.get(Calendar.DAY_OF_MONTH), day.getDay()); } /** * Tests that blog owners can be assigned. */ public void testAssignBlogOwners() { blog.setProperty(Blog.BLOG_OWNERS_KEY, "user1"); assertEquals("user1", blog.getProperty(Blog.BLOG_OWNERS_KEY)); assertEquals("user1", blog.getBlogOwnersAsString()); Collection users = blog.getUsersInRole(Constants.BLOG_OWNER_ROLE); assertEquals(1, users.size()); assertTrue(users.contains("user1")); blog.setProperty(Blog.BLOG_OWNERS_KEY, "user1,user2"); assertEquals("user1,user2", blog.getProperty(Blog.BLOG_OWNERS_KEY)); assertEquals("user1,user2", blog.getBlogOwnersAsString()); users = blog.getUsersInRole(Constants.BLOG_OWNER_ROLE); assertEquals(2, users.size()); assertTrue(users.contains("user1")); assertTrue(users.contains("user2")); } /** * Tests that blog owners can be assigned. */ public void testNullBlogOwners() { blog.removeProperty(Blog.BLOG_OWNERS_KEY); assertEquals(null, blog.getBlogOwnersAsString()); Collection users = blog.getUsersInRole(Constants.BLOG_OWNER_ROLE); assertEquals(0, users.size()); } /** * Tests that it can be determined that a user is a blog owner. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -