1 package org.apache.archiva.rss;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.sun.syndication.feed.synd.SyndEntry;
23 import com.sun.syndication.feed.synd.SyndFeed;
24 import junit.framework.TestCase;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.springframework.test.context.ContextConfiguration;
29 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
31 import javax.inject.Inject;
32 import java.util.ArrayList;
33 import java.util.Date;
34 import java.util.List;
39 @RunWith( SpringJUnit4ClassRunner.class )
40 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml"} )
41 public class RssFeedGeneratorTest
45 private RssFeedGenerator generator;
55 @SuppressWarnings("unchecked")
57 public void testNewFeed()
60 List<RssFeedEntry> entries = new ArrayList<RssFeedEntry>();
61 RssFeedEntry entry = new RssFeedEntry( "Item 1" );
63 Date whenGathered = new Date( System.currentTimeMillis() );
65 entry.setDescription( "RSS 2.0 feed item 1." );
66 entry.setPublishedDate( whenGathered );
69 entry = new RssFeedEntry( "Item 2" );
70 entry.setDescription( "RSS 2.0 feed item 2." );
71 entry.setPublishedDate( whenGathered );
74 entry = new RssFeedEntry( "Item 3" );
75 entry.setDescription( "RSS 2.0 feed item 3." );
76 entry.setPublishedDate( whenGathered );
80 generator.generateFeed( "Test Feed", "The test feed from Archiva.", entries );
82 assertEquals( "Test Feed", feed.getTitle() );
83 assertEquals( "The test feed from Archiva.", feed.getDescription() );
84 assertEquals( "en-us", feed.getLanguage() );
85 assertEquals( entries.get( 2 ).getPublishedDate(), feed.getPublishedDate() );
87 List<SyndEntry> syndEntries = feed.getEntries();
88 assertEquals( 3, syndEntries.size() );
89 assertEquals( "Item 1", syndEntries.get( 0 ).getTitle() );
90 assertEquals( "Item 2", syndEntries.get( 1 ).getTitle() );
91 assertEquals( "Item 3", syndEntries.get( 2 ).getTitle() );
95 public void testNoDataEntries()
98 List<RssFeedEntry> entries = new ArrayList<RssFeedEntry>();
100 generator.generateFeed( "Test Feed", "The test feed from Archiva.", entries );
106 * this test might need to be removed since
107 * no updates are happening in the feeds anymore since everything's processed from the db.
109 public void testUpdateFeed()
112 generator.setRssDirectory( getBasedir() + "/target/test-classes/rss-feeds/" );
114 List<RssFeedEntry> entries = new ArrayList<RssFeedEntry>();
115 RssFeedEntry entry = new RssFeedEntry( "Item 1" );
117 entry.setDescription( "RSS 2.0 feed item 1." );
118 entries.add( entry );
120 entry = new RssFeedEntry( "Item 2" );
121 entry.setDescription( "RSS 2.0 feed item 2." );
122 entries.add( entry );
124 generator.generateFeed( "Test Feed", "The test feed from Archiva.", entries,
125 "generated-test-update-rss2.0-feed.xml" );
127 File outputFile = new File( getBasedir(), "/target/test-classes/rss-feeds/generated-test-update-rss2.0-feed.xml" );
128 String generatedContent = FileUtils.readFileToString( outputFile );
130 XMLAssert.assertXpathEvaluatesTo( "Test Feed", "//channel/title", generatedContent );
131 XMLAssert.assertXpathEvaluatesTo( "http://localhost:8080/archiva/rss/generated-test-update-rss2.0-feed.xml", "//channel/link", generatedContent );
132 XMLAssert.assertXpathEvaluatesTo( "The test feed from Archiva.", "//channel/description", generatedContent );
133 XMLAssert.assertXpathEvaluatesTo( "en-us", "//channel/language", generatedContent );
135 String expectedItem1 =
136 "<channel><item><title>Item 1</title></item><item><title>Item 2</title></item></channel>";
138 XMLAssert.assertXpathsEqual( "//channel/item/title", expectedItem1, "//channel/item/title", generatedContent );
140 //update existing rss feed
141 entries = new ArrayList<RssFeedEntry>();
142 entry = new RssFeedEntry( "Item 3" );
144 entry.setDescription( "RSS 2.0 feed item 3." );
145 entries.add( entry );
147 entry = new RssFeedEntry( "Item 4" );
148 entry.setDescription( "RSS 2.0 feed item 4." );
149 entries.add( entry );
151 generator.generateFeed( "Test Feed", "The test feed from Archiva.", entries,
152 "generated-test-update-rss2.0-feed.xml" );
154 outputFile = new File( getBasedir(), "/target/test-classes/rss-feeds/generated-test-update-rss2.0-feed.xml" );
155 generatedContent = FileUtils.readFileToString( outputFile );
157 XMLAssert.assertXpathEvaluatesTo( "Test Feed", "//channel/title", generatedContent );
158 XMLAssert.assertXpathEvaluatesTo( "http://localhost:8080/archiva/rss/generated-test-update-rss2.0-feed.xml", "//channel/link", generatedContent );
159 XMLAssert.assertXpathEvaluatesTo( "The test feed from Archiva.", "//channel/description", generatedContent );
160 XMLAssert.assertXpathEvaluatesTo( "en-us", "//channel/language", generatedContent );
163 "<channel><item><title>Item 1</title></item><item><title>Item 2</title></item>"
164 + "<item><title>Item 3</title></item><item><title>Item 4</title></item></channel>";
165 XMLAssert.assertXpathsEqual( "//channel/item/title", expectedItem1, "//channel/item/title", generatedContent );
167 outputFile.deleteOnExit();