001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc.internal.util;
016:
017: import java.util.Arrays;
018: import java.util.List;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.tapestry.ioc.Orderable;
022: import org.apache.tapestry.ioc.internal.IOCInternalTestCase;
023: import org.testng.annotations.Test;
024:
025: public class OrdererTest extends IOCInternalTestCase {
026: @Test
027: public void no_dependencies() {
028: Log log = mockLog();
029:
030: replay();
031:
032: Orderer<String> o = new Orderer<String>(log);
033:
034: o.add("fred", "FRED");
035: o.add("barney", "BARNEY");
036: o.add("wilma", "WILMA");
037: o.add("betty", "BETTY");
038:
039: List<String> ordered = o.getOrdered();
040:
041: assertEquals(ordered, Arrays.asList("FRED", "BARNEY", "WILMA",
042: "BETTY"));
043:
044: verify();
045: }
046:
047: @Test
048: public void missing_constraint_type() {
049: Log log = mockLog();
050:
051: log.warn(UtilMessages.constraintFormat("fred", "barney"));
052:
053: replay();
054:
055: Orderer<String> o = new Orderer<String>(log);
056:
057: o.add("fred", "FRED");
058: o.add("barney", "BARNEY", "fred");
059: o.add("wilma", "WILMA");
060: o.add("betty", "BETTY");
061:
062: List<String> ordered = o.getOrdered();
063:
064: assertEquals(ordered, Arrays.asList("FRED", "BARNEY", "WILMA",
065: "BETTY"));
066:
067: verify();
068: }
069:
070: @Test
071: public void unknown_constraint_type() {
072: Log log = mockLog();
073:
074: log
075: .warn(UtilMessages.constraintFormat("nearby:fred",
076: "barney"));
077:
078: replay();
079:
080: Orderer<String> o = new Orderer<String>(log);
081:
082: o.add("fred", "FRED");
083: o.add("barney", "BARNEY", "nearby:fred");
084: o.add("wilma", "WILMA");
085: o.add("betty", "BETTY");
086:
087: List<String> ordered = o.getOrdered();
088:
089: assertEquals(ordered, Arrays.asList("FRED", "BARNEY", "WILMA",
090: "BETTY"));
091:
092: verify();
093: }
094:
095: @Test
096: public void nulls_not_included_in_result() {
097: Log log = mockLog();
098:
099: replay();
100:
101: Orderer<String> o = new Orderer<String>(log);
102:
103: o.add("fred", "FRED");
104: o.add("barney", "BARNEY");
105: o.add("zippo", null);
106: o.add("wilma", "WILMA");
107: o.add("groucho", null);
108: o.add("betty", "BETTY");
109:
110: List<String> ordered = o.getOrdered();
111:
112: assertEquals(ordered, Arrays.asList("FRED", "BARNEY", "WILMA",
113: "BETTY"));
114:
115: verify();
116: }
117:
118: @Test
119: public void duplicate_id() {
120: Log log = mockLog();
121:
122: replay();
123:
124: Orderer<String> o = new Orderer<String>(log);
125:
126: o.add("fred", "FRED");
127: o.add("barney", "BARNEY");
128: o.add("wilma", "WILMA");
129:
130: verify();
131:
132: log.warn(UtilMessages.duplicateOrderer("fred"), null);
133:
134: replay();
135:
136: o.add("fred", "FRED2");
137:
138: verify();
139:
140: replay();
141:
142: o.add("betty", "BETTY");
143:
144: List<String> ordered = o.getOrdered();
145:
146: assertEquals(ordered, Arrays.asList("FRED", "BARNEY", "WILMA",
147: "BETTY"));
148:
149: verify();
150: }
151:
152: @Test
153: public void leader() {
154: Log log = mockLog();
155:
156: replay();
157:
158: Orderer<String> o = new Orderer<String>(log);
159:
160: o.add("fred", "FRED");
161: o.add("barney", "BARNEY", "before:*");
162: o.add("wilma", "WILMA");
163: o.add("betty", "BETTY");
164:
165: List<String> ordered = o.getOrdered();
166:
167: assertEquals(ordered, Arrays.asList("BARNEY", "FRED", "WILMA",
168: "BETTY"));
169:
170: verify();
171: }
172:
173: @Test
174: public void trailer() {
175: Log log = mockLog();
176:
177: replay();
178:
179: Orderer<String> o = new Orderer<String>(log);
180:
181: o.add("fred", "FRED");
182: o.add("barney", "BARNEY", "after:*");
183: o.add("wilma", "WILMA");
184: o.add("betty", "BETTY");
185:
186: List<String> ordered = o.getOrdered();
187:
188: assertEquals(ordered.get(3), "BARNEY");
189:
190: verify();
191: }
192:
193: @Test
194: public void prereqs() {
195: Log log = mockLog();
196:
197: replay();
198:
199: Orderer<String> o = new Orderer<String>(log);
200:
201: o.add("fred", "FRED", "after:wilma");
202: o.add("barney", "BARNEY", "after:fred,betty");
203: o.add("wilma", "WILMA");
204: o.add("betty", "BETTY");
205:
206: List<String> ordered = o.getOrdered();
207:
208: assertEquals(ordered, Arrays.asList("WILMA", "FRED", "BETTY",
209: "BARNEY"));
210:
211: verify();
212: }
213:
214: @Test
215: public void pre_and_post_reqs() {
216: Log log = mockLog();
217:
218: replay();
219:
220: Orderer<String> o = new Orderer<String>(log);
221:
222: o.add("fred", "FRED", "after:wilma");
223: o.add("barney", "BARNEY", "after:fred,betty");
224: o.add("wilma", "WILMA");
225: o.add("betty", "BETTY", "before:wilma");
226:
227: List<String> ordered = o.getOrdered();
228:
229: assertEquals(ordered, Arrays.asList("BETTY", "WILMA", "FRED",
230: "BARNEY"));
231:
232: verify();
233: }
234:
235: @Test
236: public void case_insensitivity() {
237: Log log = mockLog();
238:
239: replay();
240:
241: Orderer<String> o = new Orderer<String>(log);
242:
243: o.add("fred", "FRED", "after:Wilma");
244: o.add("barney", "BARNEY", "after:Fred,BETTY");
245: o.add("wilma", "WILMA");
246: o.add("betty", "BETTY", "before:Wilma");
247:
248: List<String> ordered = o.getOrdered();
249:
250: assertEquals(ordered, Arrays.asList("BETTY", "WILMA", "FRED",
251: "BARNEY"));
252:
253: verify();
254: }
255:
256: @Test
257: public void dependency_cycle() {
258: Log log = mockLog();
259:
260: log
261: .warn(
262: "Unable to add 'barney' as a dependency of 'betty', as that forms a "
263: + "dependency cycle ('betty' depends on itself via 'barney'). "
264: + "The dependency has been ignored.",
265: null);
266:
267: replay();
268:
269: Orderer<String> o = new Orderer<String>(log);
270:
271: o.add("fred", "FRED", "after:wilma");
272: o.add("barney", "BARNEY", "after:fred,betty");
273: o.add("wilma", "WILMA");
274: o.add("betty", "BETTY", "after:barney", "before:wilma");
275:
276: List<String> ordered = o.getOrdered();
277:
278: assertEquals(ordered, Arrays.asList("BETTY", "WILMA", "FRED",
279: "BARNEY"));
280:
281: verify();
282: }
283:
284: @Test
285: public void toString_Orderable() {
286: Orderable<String> simple = new Orderable<String>("simple",
287: "SIMPLE");
288:
289: assertEquals(simple.toString(), "Orderable[simple SIMPLE]");
290:
291: Orderable<String> complex = new Orderable<String>("complex",
292: "COMPLEX", "after:foo", "before:bar");
293:
294: assertEquals(complex.toString(),
295: "Orderable[complex after:foo before:bar COMPLEX]");
296: }
297:
298: @SuppressWarnings("unchecked")
299: @Test
300: public void toString_DependencyNode() {
301: Log log = mockLog();
302:
303: replay();
304:
305: DependencyNode<String> node1 = new DependencyNode<String>(log,
306: new Orderable("node1", "NODE1"));
307:
308: assertEquals(node1.toString(), "[node1]");
309:
310: DependencyNode<String> node2 = new DependencyNode<String>(log,
311: new Orderable("node2", "NODE2"));
312:
313: DependencyNode<String> node3 = new DependencyNode<String>(log,
314: new Orderable("node3", "NODE3"));
315:
316: DependencyNode<String> node4 = new DependencyNode<String>(log,
317: new Orderable("node4", "NODE4"));
318:
319: DependencyNode<String> node5 = new DependencyNode<String>(log,
320: new Orderable("node5", "NODE5"));
321:
322: node2.addDependency(node1);
323: node1.addDependency(node3);
324: node1.addDependency(node4);
325: node5.addDependency(node1);
326:
327: assertEquals(node5.toString(),
328: "[node5: [node1: [node3], [node4]]]");
329:
330: verify();
331: }
332: }
|