View Javadoc
1   /*
2    * Copyright 2012 Oracle Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.fatwire.gst.foundation.controller.action.support;
18  
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.junit.Assert;
24  import org.junit.Test;
25  
26  import COM.FutureTense.Interfaces.ICS;
27  
28  import com.fatwire.gst.foundation.controller.action.Factory;
29  import com.fatwire.gst.foundation.controller.annotation.ServiceProducer;
30  import com.fatwire.gst.foundation.test.MockICS;
31  
32  /**
33   * @author dolf
34   * 
35   */
36  public class BaseFactoryTest {
37  
38      public static class Foo {
39          public Foo(ICS ics) {
40  
41          }
42  
43      }
44  
45      public static class SBar {
46          public SBar() {
47  
48          }
49  
50      }
51  
52      public static class Bar extends Foo {
53  
54          public Bar(ICS ics) {
55              super(ics);
56          }
57  
58      }
59  
60      public static class FooBar extends Foo {
61  
62          public FooBar(ICS ics) {
63              super(ics);
64          }
65  
66      }
67  
68      /**
69       * Test method for
70       * {@link com.fatwire.gst.foundation.controller.action.support.BaseFactory#getObject(java.lang.String, java.lang.Class)}
71       * .
72       */
73      @Test
74      public void testGetObject() {
75          MockICS ics = new MockICS();
76  
77          BaseFactory bf = new ListFactory(ics);
78          List<?> foo = bf.getObject("foo", List.class);
79          Assert.assertNotNull(foo);
80      }
81  
82      @Test
83      public void testGetObject_from_parent() {
84          MockICS ics = new MockICS();
85  
86          BaseFactory bf = new CollectionFactory(ics, new ListFactory(ics));
87          List<?> foo = bf.getObject("foo", List.class);
88          Assert.assertNotNull(foo);
89      }
90  
91      @Test
92      public void testGetObject_not_exists() {
93          MockICS ics = new MockICS();
94  
95          BaseFactory bf = new CollectionFactory(ics, new ListFactory(ics));
96          String foo = bf.getObject("foo", String.class);
97          Assert.assertNull(foo);
98      }
99  
100     @Test
101     public void testGetObject_foo() {
102         MockICS ics = new MockICS();
103 
104         BaseFactory bf = new FooFactory(ics);
105         Foo foo = bf.getObject("foo", Foo.class);
106         Assert.assertNotNull(foo);
107         Assert.assertTrue(foo instanceof Bar);
108     }
109 
110     @Test
111     public void testGetObject_bar_ctor() {
112         MockICS ics = new MockICS();
113 
114         BaseFactory bf = new FooFactory(ics, new FooBarFactory(ics));
115         Bar foo = bf.getObject("foo", Bar.class);
116         Assert.assertNotNull(foo);
117     }
118 
119     @Test
120     public void testGetObject_foobar() {
121         MockICS ics = new MockICS();
122 
123         BaseFactory bf = new FooFactory(ics, new FooBarFactory(ics));
124         FooBar foo = bf.getObject("foo", FooBar.class);
125         Assert.assertNotNull(foo);
126     }
127 
128     @Test
129     public void testGetObject_abar() {
130         MockICS ics = new MockICS();
131 
132         BaseFactory bf = new CBarFactory(ics, new FooBarFactory(ics));
133         FooBar foo = bf.getObject("foo", FooBar.class);
134         Assert.assertNotNull(foo);
135         List<?> list = bf.getObject("list", List.class);
136         Assert.assertNotNull(list);
137     }
138 
139     @Test
140     public void testGetObject_sbar() {
141         MockICS ics = new MockICS();
142         Factory root = new CBarFactory(ics);
143         BaseFactory bf = new BaseFactory(ics, root) {
144 
145             @Override
146             protected Class<?>[] factoryClasses(ICS ics) {
147                 return new Class[] { SBarFactory.class };
148             }
149 
150         };
151         SBar foo = bf.getObject("foobar2", SBar.class);
152         Assert.assertNotNull(foo);
153         List<?> list = bf.getObject("list", List.class);
154         Assert.assertNotNull(list);
155     }
156 
157     class ListFactory extends BaseFactory {
158 
159         public ListFactory(MockICS ics, Factory... roots) {
160             super(ics, roots);
161         }
162 
163         public List<?> createList(ICS ics) {
164             return Collections.emptyList();
165         }
166 
167     }
168 
169     class CollectionFactory extends BaseFactory {
170 
171         public CollectionFactory(MockICS ics, Factory... roots) {
172             super(ics, roots);
173         }
174 
175         public Collection<?> createCollection(ICS ics) {
176             return Collections.emptyList();
177         }
178 
179     }
180 
181     class FooFactory extends BaseFactory {
182 
183         public FooFactory(MockICS ics, Factory... roots) {
184             super(ics, roots);
185         }
186 
187         public Foo createFoo(ICS ics) {
188             return new Bar(ics);
189         }
190     }
191 
192     class FooBarFactory extends BaseFactory {
193 
194         public FooBarFactory(MockICS ics, Factory... roots) {
195             super(ics, roots);
196         }
197 
198         public List<?> createList(ICS ics) {
199             return Collections.emptyList();
200         }
201 
202         public FooBar createFooBar(ICS ics) {
203             return new FooBar(ics);
204         }
205 
206     }
207 
208     abstract class ABarFactory extends BaseFactory {
209 
210         public ABarFactory(MockICS ics, Factory... roots) {
211             super(ics, roots);
212         }
213 
214         public List<?> createList(ICS ics) {
215             return Collections.emptyList();
216         }
217 
218         public FooBar createFooBar(ICS ics) {
219             return new FooBar(ics);
220         }
221 
222     }
223 
224     class CBarFactory extends ABarFactory {
225 
226         public CBarFactory(MockICS ics, Factory... roots) {
227             super(ics, roots);
228         }
229 
230         @ServiceProducer(name = "foobar")
231         public FooBar createFooBar(ICS ics) {
232             return new FooBar(ics);
233         }
234 
235     }
236 
237     static class SBarFactory {
238 
239         @ServiceProducer(name = "foobar2")
240         public static SBar createFooBar(ICS ics) {
241             return new SBar();
242         }
243 
244     }
245 
246 }