Skip to content

Commit

Permalink
Fix null pointer in ITunesGenerator (#448)
Browse files Browse the repository at this point in the history
It throws a `NullPointerException` when `image` is set but `imageUri` is
not set. The fix is to check which field is set.
  • Loading branch information
mishako committed Jun 8, 2020
1 parent 2906048 commit 06e4ae5
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ public void generate(final Module module, final Element element) {
}

if (itunes.getImage() != null) {
final Element image = generateSimpleElement("image", "");
image.setAttribute("href", itunes.getImage().toString());
element.addContent(image);
} else if (itunes.getImageUri() != null) {
final Element image = generateSimpleElement("image", "");
image.setAttribute("href", itunes.getImageUri().toString());
element.addContent(image);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,23 @@
*/
package com.rometools.modules.itunes;

import java.io.File;
import java.io.StringWriter;
import java.util.List;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.rometools.modules.AbstractTestCase;
import com.rometools.modules.itunes.AbstractITunesObject;
import com.rometools.modules.itunes.FeedInformation;
import com.rometools.modules.itunes.FeedInformationImpl;
import com.rometools.modules.itunes.types.Category;
import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.feed.synd.SyndFeedImpl;
import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.SyndFeedOutput;
import com.rometools.rome.io.XmlReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.StringWriter;
import java.net.URL;
import java.util.List;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ITunesGeneratorTest extends AbstractTestCase {

Expand Down Expand Up @@ -114,6 +110,73 @@ public void testCreate() throws Exception {
final StringWriter writer = new StringWriter();
output.output(feed, writer);
LOG.debug("{}", writer);
}

public void testImage() throws Exception {
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
feed.setTitle("title");
feed.setDescription("description");
feed.setLink("https://example.org");

FeedInformation itunesFeed = new FeedInformationImpl();
itunesFeed.setImage(new URL("https://example.org/test.png"));
feed.getModules().add(itunesFeed);

String xml = new SyndFeedOutput().outputString(feed);

AbstractITunesObject parsedItunesFeed =
(AbstractITunesObject) new SyndFeedInput()
.build(new XmlReader(new ByteArrayInputStream(xml.getBytes("UTF-8"))))
.getModule(AbstractITunesObject.URI);
assertEquals(new URL("https://example.org/test.png"), parsedItunesFeed.getImage());
assertEquals(new java.net.URI("https://example.org/test.png"),
parsedItunesFeed.getImageUri());
}

public void testImageUri() throws Exception {
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
feed.setTitle("title");
feed.setDescription("description");
feed.setLink("https://example.org");

FeedInformation itunesFeed = new FeedInformationImpl();
itunesFeed.setImageUri(new java.net.URI("https://example.org/test.png"));
feed.getModules().add(itunesFeed);

String xml = new SyndFeedOutput().outputString(feed);

AbstractITunesObject parsedItunesFeed =
(AbstractITunesObject) new SyndFeedInput()
.build(new XmlReader(new ByteArrayInputStream(xml.getBytes("UTF-8"))))
.getModule(AbstractITunesObject.URI);
assertEquals(new java.net.URI("https://example.org/test.png"),
parsedItunesFeed.getImageUri());
assertEquals(new URL("https://example.org/test.png"),
parsedItunesFeed.getImage());
}

public void testImageTakesPrecedenceOverImageUri() throws Exception {
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
feed.setTitle("title");
feed.setDescription("description");
feed.setLink("https://example.org");

FeedInformation itunesFeed = new FeedInformationImpl();
itunesFeed.setImage(new URL("https://example.org/test1.png"));
itunesFeed.setImageUri(new java.net.URI("https://example.org/test2.png"));
feed.getModules().add(itunesFeed);

String xml = new SyndFeedOutput().outputString(feed);

AbstractITunesObject parsedItunesFeed =
(AbstractITunesObject) new SyndFeedInput()
.build(new XmlReader(new ByteArrayInputStream(xml.getBytes("UTF-8"))))
.getModule(AbstractITunesObject.URI);
assertEquals(new URL("https://example.org/test1.png"), parsedItunesFeed.getImage());
assertEquals(new java.net.URI("https://example.org/test1.png"),
parsedItunesFeed.getImageUri());
}
}

0 comments on commit 06e4ae5

Please sign in to comment.