Skip to content

Commit 06e4ae5

Browse files
authored
Fix null pointer in ITunesGenerator (#448)
It throws a `NullPointerException` when `image` is set but `imageUri` is not set. The fix is to check which field is set.
1 parent 2906048 commit 06e4ae5

2 files changed

Lines changed: 80 additions & 13 deletions

File tree

rome-modules/src/main/java/com/rometools/modules/itunes/io/ITunesGenerator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ public void generate(final Module module, final Element element) {
140140
}
141141

142142
if (itunes.getImage() != null) {
143+
final Element image = generateSimpleElement("image", "");
144+
image.setAttribute("href", itunes.getImage().toString());
145+
element.addContent(image);
146+
} else if (itunes.getImageUri() != null) {
143147
final Element image = generateSimpleElement("image", "");
144148
image.setAttribute("href", itunes.getImageUri().toString());
145149
element.addContent(image);

rome-modules/src/test/java/com/rometools/modules/itunes/ITunesGeneratorTest.java

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,23 @@
1919
*/
2020
package com.rometools.modules.itunes;
2121

22-
import java.io.File;
23-
import java.io.StringWriter;
24-
import java.util.List;
25-
26-
import junit.framework.Test;
27-
import junit.framework.TestSuite;
28-
29-
import org.slf4j.Logger;
30-
import org.slf4j.LoggerFactory;
31-
3222
import com.rometools.modules.AbstractTestCase;
33-
import com.rometools.modules.itunes.AbstractITunesObject;
34-
import com.rometools.modules.itunes.FeedInformation;
35-
import com.rometools.modules.itunes.FeedInformationImpl;
3623
import com.rometools.modules.itunes.types.Category;
3724
import com.rometools.rome.feed.synd.SyndEntry;
3825
import com.rometools.rome.feed.synd.SyndFeed;
3926
import com.rometools.rome.feed.synd.SyndFeedImpl;
4027
import com.rometools.rome.io.SyndFeedInput;
4128
import com.rometools.rome.io.SyndFeedOutput;
4229
import com.rometools.rome.io.XmlReader;
30+
import java.io.ByteArrayInputStream;
31+
import java.io.File;
32+
import java.io.StringWriter;
33+
import java.net.URL;
34+
import java.util.List;
35+
import junit.framework.Test;
36+
import junit.framework.TestSuite;
37+
import org.slf4j.Logger;
38+
import org.slf4j.LoggerFactory;
4339

4440
public class ITunesGeneratorTest extends AbstractTestCase {
4541

@@ -114,6 +110,73 @@ public void testCreate() throws Exception {
114110
final StringWriter writer = new StringWriter();
115111
output.output(feed, writer);
116112
LOG.debug("{}", writer);
113+
}
114+
115+
public void testImage() throws Exception {
116+
SyndFeed feed = new SyndFeedImpl();
117+
feed.setFeedType("rss_2.0");
118+
feed.setTitle("title");
119+
feed.setDescription("description");
120+
feed.setLink("https://example.org");
121+
122+
FeedInformation itunesFeed = new FeedInformationImpl();
123+
itunesFeed.setImage(new URL("https://example.org/test.png"));
124+
feed.getModules().add(itunesFeed);
125+
126+
String xml = new SyndFeedOutput().outputString(feed);
127+
128+
AbstractITunesObject parsedItunesFeed =
129+
(AbstractITunesObject) new SyndFeedInput()
130+
.build(new XmlReader(new ByteArrayInputStream(xml.getBytes("UTF-8"))))
131+
.getModule(AbstractITunesObject.URI);
132+
assertEquals(new URL("https://example.org/test.png"), parsedItunesFeed.getImage());
133+
assertEquals(new java.net.URI("https://example.org/test.png"),
134+
parsedItunesFeed.getImageUri());
135+
}
136+
137+
public void testImageUri() throws Exception {
138+
SyndFeed feed = new SyndFeedImpl();
139+
feed.setFeedType("rss_2.0");
140+
feed.setTitle("title");
141+
feed.setDescription("description");
142+
feed.setLink("https://example.org");
143+
144+
FeedInformation itunesFeed = new FeedInformationImpl();
145+
itunesFeed.setImageUri(new java.net.URI("https://example.org/test.png"));
146+
feed.getModules().add(itunesFeed);
147+
148+
String xml = new SyndFeedOutput().outputString(feed);
149+
150+
AbstractITunesObject parsedItunesFeed =
151+
(AbstractITunesObject) new SyndFeedInput()
152+
.build(new XmlReader(new ByteArrayInputStream(xml.getBytes("UTF-8"))))
153+
.getModule(AbstractITunesObject.URI);
154+
assertEquals(new java.net.URI("https://example.org/test.png"),
155+
parsedItunesFeed.getImageUri());
156+
assertEquals(new URL("https://example.org/test.png"),
157+
parsedItunesFeed.getImage());
158+
}
117159

160+
public void testImageTakesPrecedenceOverImageUri() throws Exception {
161+
SyndFeed feed = new SyndFeedImpl();
162+
feed.setFeedType("rss_2.0");
163+
feed.setTitle("title");
164+
feed.setDescription("description");
165+
feed.setLink("https://example.org");
166+
167+
FeedInformation itunesFeed = new FeedInformationImpl();
168+
itunesFeed.setImage(new URL("https://example.org/test1.png"));
169+
itunesFeed.setImageUri(new java.net.URI("https://example.org/test2.png"));
170+
feed.getModules().add(itunesFeed);
171+
172+
String xml = new SyndFeedOutput().outputString(feed);
173+
174+
AbstractITunesObject parsedItunesFeed =
175+
(AbstractITunesObject) new SyndFeedInput()
176+
.build(new XmlReader(new ByteArrayInputStream(xml.getBytes("UTF-8"))))
177+
.getModule(AbstractITunesObject.URI);
178+
assertEquals(new URL("https://example.org/test1.png"), parsedItunesFeed.getImage());
179+
assertEquals(new java.net.URI("https://example.org/test1.png"),
180+
parsedItunesFeed.getImageUri());
118181
}
119182
}

0 commit comments

Comments
 (0)