001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependency;
034:
035: import java.util.*;
036: import java.io.*;
037:
038: import junit.framework.*;
039:
040: public class TestTextCyclePrinter extends TestCase {
041: private Node a_package;
042: private Node b_package;
043: private Node c_package;
044:
045: protected void setUp() throws Exception {
046: super .setUp();
047:
048: NodeFactory factory = new NodeFactory();
049:
050: a_package = factory.createPackage("a");
051: b_package = factory.createPackage("b");
052: c_package = factory.createPackage("c");
053: }
054:
055: public void testVisitCycleWith2NodeCycle() {
056: List<Node> nodes = new ArrayList<Node>();
057: nodes.add(a_package);
058: nodes.add(b_package);
059: Cycle cycle = new Cycle(nodes);
060:
061: StringWriter expected = new StringWriter();
062: PrintWriter pw = new PrintWriter(expected);
063: pw.println(a_package);
064: pw.println(" --> " + b_package);
065: pw.println(" --> " + a_package);
066: pw.close();
067:
068: StringWriter buffer = new StringWriter();
069: PrintWriter out = new PrintWriter(buffer);
070: CyclePrinter printer = new TextCyclePrinter(out);
071: printer.visitCycle(cycle);
072: out.close();
073:
074: assertEquals(expected.toString(), buffer.toString());
075: }
076:
077: public void testVisitCycleWith2NodeCycleWithIndentText() {
078: List<Node> nodes = new ArrayList<Node>();
079: nodes.add(a_package);
080: nodes.add(b_package);
081: Cycle cycle = new Cycle(nodes);
082:
083: StringWriter expected = new StringWriter();
084: PrintWriter pw = new PrintWriter(expected);
085: pw.println(a_package);
086: pw.println("*--> " + b_package);
087: pw.println("**--> " + a_package);
088: pw.close();
089:
090: StringWriter buffer = new StringWriter();
091: PrintWriter out = new PrintWriter(buffer);
092: CyclePrinter printer = new TextCyclePrinter(out);
093: printer.setIndentText("*");
094: printer.visitCycle(cycle);
095: out.close();
096:
097: assertEquals(expected.toString(), buffer.toString());
098: }
099:
100: public void testVisitCycleWith3NodeCycle() {
101: List<Node> nodes = new ArrayList<Node>();
102: nodes.add(a_package);
103: nodes.add(b_package);
104: nodes.add(c_package);
105: Cycle cycle = new Cycle(nodes);
106:
107: StringWriter expected = new StringWriter();
108: PrintWriter pw = new PrintWriter(expected);
109: pw.println(a_package);
110: pw.println(" --> " + b_package);
111: pw.println(" --> " + c_package);
112: pw.println(" --> " + a_package);
113: pw.close();
114:
115: StringWriter buffer = new StringWriter();
116: PrintWriter out = new PrintWriter(buffer);
117: CyclePrinter printer = new TextCyclePrinter(out);
118: printer.visitCycle(cycle);
119: out.close();
120:
121: assertEquals(expected.toString(), buffer.toString());
122: }
123:
124: public void testVisitCyclesWith2NodeCycle() {
125: List<Node> nodes = new ArrayList<Node>();
126: nodes.add(a_package);
127: nodes.add(b_package);
128: Cycle cycle = new Cycle(nodes);
129:
130: StringWriter expected = new StringWriter();
131: PrintWriter pw = new PrintWriter(expected);
132: pw.println(a_package);
133: pw.println(" --> " + b_package);
134: pw.println(" --> " + a_package);
135: pw.close();
136:
137: StringWriter buffer = new StringWriter();
138: PrintWriter out = new PrintWriter(buffer);
139: CyclePrinter printer = new TextCyclePrinter(out);
140: printer.visitCycles(Collections.singletonList(cycle));
141: out.close();
142:
143: assertEquals(expected.toString(), buffer.toString());
144: }
145:
146: public void testVisitCyclesWith3NodeCycle() {
147: List<Node> nodes = new ArrayList<Node>();
148: nodes.add(a_package);
149: nodes.add(b_package);
150: nodes.add(c_package);
151: Cycle cycle = new Cycle(nodes);
152:
153: StringWriter expected = new StringWriter();
154: PrintWriter pw = new PrintWriter(expected);
155: pw.println(a_package);
156: pw.println(" --> " + b_package);
157: pw.println(" --> " + c_package);
158: pw.println(" --> " + a_package);
159: pw.close();
160:
161: StringWriter buffer = new StringWriter();
162: PrintWriter out = new PrintWriter(buffer);
163: CyclePrinter printer = new TextCyclePrinter(out);
164: printer.visitCycles(Collections.singletonList(cycle));
165: out.close();
166:
167: assertEquals(expected.toString(), buffer.toString());
168: }
169:
170: public void testVisitCyclesWith2Cycles() throws IOException {
171: List<Cycle> cycles = new ArrayList<Cycle>();
172:
173: List<Node> nodes1 = new ArrayList<Node>();
174: nodes1.add(a_package);
175: nodes1.add(b_package);
176: cycles.add(new Cycle(nodes1));
177:
178: List<Node> nodes2 = new ArrayList<Node>();
179: nodes2.add(a_package);
180: nodes2.add(b_package);
181: nodes2.add(c_package);
182: cycles.add(new Cycle(nodes2));
183:
184: StringWriter expected = new StringWriter();
185: PrintWriter pw = new PrintWriter(expected);
186: pw.println(a_package);
187: pw.println(" --> " + b_package);
188: pw.println(" --> " + a_package);
189: pw.println(a_package);
190: pw.println(" --> " + b_package);
191: pw.println(" --> " + c_package);
192: pw.println(" --> " + a_package);
193: pw.close();
194:
195: StringWriter buffer = new StringWriter();
196: PrintWriter out = new PrintWriter(buffer);
197: CyclePrinter printer = new TextCyclePrinter(out);
198: printer.visitCycles(cycles);
199: out.close();
200:
201: assertEquals(expected.toString(), buffer.toString());
202: }
203: }
|