]> source.dussan.org Git - archiva.git/blob
59f32dc78c7b05e598a7c281df5f9a9a6745fc74
[archiva.git] /
1 package org.apache.archiva.rss;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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
30 import javax.inject.Inject;
31 import java.util.ArrayList;
32 import java.util.Date;
33 import java.util.List;
34 import org.apache.archiva.test.ArchivaSpringJUnit4ClassRunner;
35
36 /**
37  * @version
38  */
39 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
40 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml"} )
41 public class RssFeedGeneratorTest
42     extends TestCase
43 {
44     @Inject
45     private RssFeedGenerator generator;
46
47     @Before
48     public void setUp()
49         throws Exception
50     {
51         super.setUp();
52
53     }
54
55     @SuppressWarnings("unchecked")
56     @Test
57     public void testNewFeed()
58         throws Exception
59     {
60         List<RssFeedEntry> entries = new ArrayList<RssFeedEntry>();
61         RssFeedEntry entry = new RssFeedEntry( "Item 1" );
62         
63         Date whenGathered = new Date( System.currentTimeMillis() );
64
65         entry.setDescription( "RSS 2.0 feed item 1." );
66         entry.setPublishedDate( whenGathered );
67         entries.add( entry );
68
69         entry = new RssFeedEntry( "Item 2" );
70         entry.setDescription( "RSS 2.0 feed item 2." );
71         entry.setPublishedDate( whenGathered );
72         entries.add( entry );
73
74         entry = new RssFeedEntry( "Item 3" );
75         entry.setDescription( "RSS 2.0 feed item 3." );
76         entry.setPublishedDate( whenGathered );
77         entries.add( entry );
78
79         SyndFeed feed =
80             generator.generateFeed( "Test Feed", "The test feed from Archiva.", entries );
81
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() );
86
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() );
92     }
93
94     @Test
95     public void testNoDataEntries()
96         throws Exception
97     {
98         List<RssFeedEntry> entries = new ArrayList<RssFeedEntry>();
99         SyndFeed feed =
100             generator.generateFeed( "Test Feed", "The test feed from Archiva.", entries );
101
102         assertNull( feed );
103     }
104
105     /*
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.
108      * 
109     public void testUpdateFeed()
110         throws Exception
111     {
112         generator.setRssDirectory( getBasedir() + "/target/test-classes/rss-feeds/" );
113
114         List<RssFeedEntry> entries = new ArrayList<RssFeedEntry>();
115         RssFeedEntry entry = new RssFeedEntry( "Item 1" );
116
117         entry.setDescription( "RSS 2.0 feed item 1." );
118         entries.add( entry );
119
120         entry = new RssFeedEntry( "Item 2" );
121         entry.setDescription( "RSS 2.0 feed item 2." );
122         entries.add( entry );
123
124         generator.generateFeed( "Test Feed", "The test feed from Archiva.", entries,
125                                 "generated-test-update-rss2.0-feed.xml" );
126
127         File outputFile = new File( getBasedir(), "/target/test-classes/rss-feeds/generated-test-update-rss2.0-feed.xml" );
128         String generatedContent = FileUtils.readFileToString( outputFile );
129
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 );
134
135         String expectedItem1 =
136             "<channel><item><title>Item 1</title></item><item><title>Item 2</title></item></channel>";
137         
138         XMLAssert.assertXpathsEqual( "//channel/item/title", expectedItem1, "//channel/item/title", generatedContent );
139
140         //update existing rss feed
141         entries = new ArrayList<RssFeedEntry>();
142         entry = new RssFeedEntry( "Item 3" );
143
144         entry.setDescription( "RSS 2.0 feed item 3." );
145         entries.add( entry );
146
147         entry = new RssFeedEntry( "Item 4" );
148         entry.setDescription( "RSS 2.0 feed item 4." );
149         entries.add( entry );
150
151         generator.generateFeed( "Test Feed", "The test feed from Archiva.", entries,
152                                 "generated-test-update-rss2.0-feed.xml" );
153         
154         outputFile = new File( getBasedir(), "/target/test-classes/rss-feeds/generated-test-update-rss2.0-feed.xml" );        
155         generatedContent = FileUtils.readFileToString( outputFile );       
156         
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 );
161
162         expectedItem1 =
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 );
166         
167         outputFile.deleteOnExit();
168     }
169      */
170
171 }