View Javadoc

1   /**
2    * This file Copyright (c) 2008-2013 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.rssaggregator.generator;
35  
36  import static org.junit.Assert.assertEquals;
37  import static org.junit.Assert.assertNotNull;
38  import static org.junit.Assert.assertTrue;
39  import static org.junit.Assert.fail;
40  import info.magnolia.module.ModuleRegistry;
41  import info.magnolia.module.ModuleRegistryImpl;
42  import info.magnolia.module.model.ModuleDefinition;
43  import info.magnolia.module.rssaggregator.RSSAggregator;
44  import info.magnolia.test.ComponentsTestUtil;
45  
46  import java.util.Hashtable;
47  import java.util.List;
48  import java.util.Map;
49  
50  import org.junit.After;
51  import org.junit.Before;
52  import org.junit.Test;
53  
54  import com.sun.syndication.feed.synd.SyndEntry;
55  import com.sun.syndication.feed.synd.SyndFeed;
56  
57  /**
58   * Tests {@link FeedGeneratorResolver}.
59   *
60   * @author Rob van der Linden Vooren
61   */
62  public class FeedGeneratorResolverTest {
63  
64      private FeedGeneratorResolver resolver;
65      private Map<String, String[]> parameters;
66      private ModuleRegistry moduleRegistery;
67  
68      @Before
69      public void setUp() {
70          parameters = new Hashtable<String, String[]>();
71          ModuleDefinition def = new ModuleDefinition();
72          def.setClassName(RSSAggregator.class.getCanonicalName());
73          moduleRegistery = new ModuleRegistryImpl();
74          moduleRegistery.registerModuleDefinition("rssaggregator", def);
75          moduleRegistery.registerModuleInstance("rssaggregator", new RSSAggregator());
76          ComponentsTestUtil.setInstance(ModuleRegistry.class, moduleRegistery);
77          resolver = new FeedGeneratorResolver(moduleRegistery,new RSSModuleFeedGenerator());
78      }
79  
80      @After
81      public void tearDown() {
82          moduleRegistery = null;
83          ComponentsTestUtil.clear();
84      }
85  
86      @Test
87      public void testResolve() {
88  
89          parameters.put("generatorName", new String[]{"testGenerator"});
90          parameters.put("testValue", new String[]{"blah"});
91          RSSAggregator module = (RSSAggregator) moduleRegistery.getModuleInstance("rssaggregator");
92          assertNotNull(module);
93          module.addFeedGenerator("testGenerator", new TestableFeedGenerator());
94          FeedGenerator generator = resolver.resolve(parameters);
95          assertNotNull(generator);
96          assertTrue(generator instanceof TestableFeedGenerator);
97          assertEquals("blah", ((TestableFeedGenerator) generator).getTestValue());
98      }
99  
100     @Test
101     public void testResolve_throwsExceptionWhenUnresolvable() {
102         parameters.put("generatorName", new String[]{"no-factory-registered-for-this-generator"});
103 
104         try {
105             resolver.resolve(parameters);
106             fail("FeedGeneratorConstructionException expected");
107         } catch (FeedGeneratorConstructionException exception) {
108             assertNotNull(exception.getMessage());
109         }
110     }
111 
112     public static class TestableFeedGenerator extends AbstractSyndFeedGenerator implements Cloneable {
113 
114         private String testValue;
115 
116         public void setTestValue(String testValue) {
117             this.testValue = testValue;
118         }
119 
120         @Override
121         public void setFeedInfo(SyndFeed feed) {
122             throw new UnsupportedOperationException("Not implemented");
123         }
124 
125        public String getTestValue() {
126             return testValue;
127         }
128 
129         @Override
130         public List<SyndEntry> loadFeedEntries() {
131             throw new UnsupportedOperationException("Not implemented");
132         }
133 
134         @Override
135         public Object clone() throws CloneNotSupportedException {
136             return super.clone();
137         }
138     }
139 }