KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > shim > SiteMapTreeTest


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.shim;
22
23 import java.util.*;
24 import java.sql.*;
25 import junit.framework.*;
26 import org.apache.log4j.*;
27 import com.methodhead.persistable.*;
28 import com.methodhead.test.*;
29 import com.methodhead.tree.*;
30
31 public class SiteMapTreeTest extends DbTestCase {
32
33   public SiteMapTreeTest( String JavaDoc name ) {
34     super( name );
35   }
36
37   private FoldingTreeNode node1_ = null;
38   private FoldingTreeNode node2_ = null;
39   private FoldingTreeNode node3_ = null;
40   private FoldingTreeNode node4_ = null;
41   private FoldingTreeNode node5_ = null;
42
43   private Link link1_ = null;
44   private Link link2_ = null;
45   private Link link3_ = null;
46   private Link link4_ = null;
47   private Link link5_ = null;
48
49   private SiteMap siteMap1_ = null;
50
51   private void createData() {
52     // Node1
53
// Node2
54
// Node3
55
// Node4
56
// Node5
57
node1_ = new FoldingTreeNode();
58     node1_.setUserObject( new Integer JavaDoc( 1 ) );
59     node2_ = new FoldingTreeNode();
60     node2_.setUserObject( new Integer JavaDoc( 2 ) );
61     node3_ = new FoldingTreeNode();
62     node3_.setUserObject( new Integer JavaDoc( 3 ) );
63     node4_ = new FoldingTreeNode();
64     node4_.setUserObject( new Integer JavaDoc( 4 ) );
65     node5_ = new FoldingTreeNode();
66     node5_.setUserObject( new Integer JavaDoc( 5 ) );
67
68     node1_.add( node2_ );
69     node2_.add( node3_ );
70     node1_.add( node4_ );
71     node1_.add( node5_ );
72
73     link1_ = new Link();
74     link1_.setPageId( 1 );
75     link1_.setTitle( "Page1" );
76     link2_ = new Link();
77     link2_.setPageId( 2 );
78     link2_.setTitle( "Page2" );
79     link3_ = new Link();
80     link3_.setPageId( 3 );
81     link3_.setTitle( "Page3" );
82     link4_ = new Link();
83     link4_.setPageId( 4 );
84     link4_.setTitle( "Page4" );
85     link5_ = new Link();
86     link5_.setPageId( 5 );
87     link5_.setTitle( "Page5" );
88
89     link1_.add( link2_ );
90     link2_.add( link3_ );
91     link1_.add( link4_ );
92     link1_.add( link5_ );
93
94     siteMap1_ = new SiteMap();
95     siteMap1_.setRoot( link1_ );
96   }
97
98   protected void setUp() {
99     //setLogLevel( Level.DEBUG );
100
try {
101     }
102     catch ( Exception JavaDoc e ) {
103       fail( e.getMessage() );
104     }
105   }
106
107   protected void tearDown() {
108   }
109
110   public void testBuild() {
111     try {
112       FoldingTreeNode node = null;
113       FoldingTreeNode subnode = null;
114       FoldingTreeNode subsubnode = null;
115       SiteMapTree tree = null;
116
117       createData();
118       tree = new SiteMapTree();
119       tree.build( siteMap1_ );
120       node = ( FoldingTreeNode )tree.getRoot();
121
122       assertNotNull( node );
123       assertEquals( new Integer JavaDoc( 1 ), node.getUserObject() );
124       assertEquals( "Page1", node.getLabel() );
125       assertEquals( "editPage.do?id=1", node.getUrl() );
126       assertEquals( "PAGE", node.getIconHint() );
127       assertEquals( 3, node.getChildCount() );
128
129       subnode = ( FoldingTreeNode )node.getChildAt( 0 );
130       assertEquals( new Integer JavaDoc( 2 ), subnode.getUserObject() );
131       assertEquals( "Page2", subnode.getLabel() );
132       assertEquals( "editPage.do?id=2", subnode.getUrl() );
133       assertEquals( "PAGE", subnode.getIconHint() );
134       assertEquals( 1, subnode.getChildCount() );
135
136       subsubnode = ( FoldingTreeNode )subnode.getChildAt( 0 );
137       assertEquals( new Integer JavaDoc( 3 ), subsubnode.getUserObject() );
138       assertEquals( "Page3", subsubnode.getLabel() );
139       assertEquals( "editPage.do?id=3", subsubnode.getUrl() );
140       assertEquals( "PAGE", subsubnode.getIconHint() );
141       assertEquals( 0, subsubnode.getChildCount() );
142
143       subnode = ( FoldingTreeNode )node.getChildAt( 1 );
144       assertEquals( new Integer JavaDoc( 4 ), subnode.getUserObject() );
145       assertEquals( "Page4", subnode.getLabel() );
146       assertEquals( "editPage.do?id=4", subnode.getUrl() );
147       assertEquals( "PAGE", subnode.getIconHint() );
148       assertEquals( 0, subnode.getChildCount() );
149
150       subnode = ( FoldingTreeNode )node.getChildAt( 2 );
151       assertEquals( new Integer JavaDoc( 5 ), subnode.getUserObject() );
152       assertEquals( "Page5", subnode.getLabel() );
153       assertEquals( "editPage.do?id=5", subnode.getUrl() );
154       assertEquals( "PAGE", subnode.getIconHint() );
155       assertEquals( 0, subnode.getChildCount() );
156     }
157     catch ( Exception JavaDoc e ) {
158       e.printStackTrace();
159       fail();
160     }
161   }
162
163   public void testFind() {
164     try {
165       FoldingTreeNode node = null;
166       SiteMapTree tree = null;
167
168       createData();
169       tree = new SiteMapTree();
170       tree.setRoot( node1_ );
171
172       assertNull( tree.find( 666 ) );
173
174       node = tree.find( 3 );
175       assertNotNull( node );
176       assertEquals( node, node3_ );
177
178       node = tree.find( 5 );
179       assertNotNull( node );
180       assertEquals( node, node5_ );
181     }
182     catch ( Exception JavaDoc e ) {
183       e.printStackTrace();
184       fail();
185     }
186   }
187 }
188
Popular Tags