1   package com.sun.syndication.unittest;
2   
3   import com.sun.syndication.feed.rss.Content;
4   import com.sun.syndication.feed.rss.Channel;
5   import com.sun.syndication.feed.rss.Item;
6   import com.sun.syndication.io.WireFeedOutput;
7   import com.sun.syndication.io.WireFeedInput;
8   import junit.framework.TestCase;
9   
10  import java.io.Writer;
11  import java.io.StringWriter;
12  import java.io.Reader;
13  import java.io.StringReader;
14  
15  public class TestRss20 extends TestCase {
16  
17      private String createFeed() throws Exception {
18          Channel channel = new Channel();
19          channel.setLink("");
20          channel.setTitle("");
21          channel.setFeedType("rss_2.0");
22          channel.setDescription("");
23          Item item = new Item();
24          Content content = new Content();
25          content.setType("text/plain");
26          content.setValue("hello");
27          item.setContent(content);
28          channel.getItems().add(item);
29  
30          StringWriter sw = new StringWriter();
31          WireFeedOutput output = new WireFeedOutput();
32          output.output(channel, sw);
33          sw.close();
34  
35          return sw.toString();
36      }
37  
38      public void testWrite() throws Exception {
39          String s = createFeed();
40          int hellos = 0;
41          while (s.contains("hello")) {
42              hellos++;
43              s = s.substring(s.indexOf("hello") + "hello".length());
44          }
45          assertEquals(1, hellos); // if content:encoded is more than one this should fail
46      }
47  
48      public void testRead() throws Exception {
49          Reader r = new StringReader(createFeed());
50          WireFeedInput input = new WireFeedInput();
51          Channel ch = (Channel) input.build(r);
52      }
53  
54      public void testReadWrite() throws Exception {
55          Reader r = new StringReader(createFeed());
56          WireFeedInput input = new WireFeedInput();
57          Channel channel = (Channel) input.build(r);
58          StringWriter sw = new StringWriter();
59          WireFeedOutput output = new WireFeedOutput();
60          output.output(channel, sw);
61          sw.close();
62          r = new StringReader(sw.toString());
63          channel = (Channel) input.build(r);
64          sw = new StringWriter();
65          output.output(channel, sw);
66          sw.close();
67          System.out.println(sw);
68      }
69  
70  }