KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openedit > links > LinkTest


1 /*
2  * Created on Dec 22, 2004
3  */

4 package org.openedit.links;
5
6 import com.openedit.BaseTestCase;
7 import com.openedit.BaseWebPageRequest;
8 import com.openedit.WebPageRequest;
9 import com.openedit.page.Page;
10 import com.openedit.page.PageAction;
11
12 /**
13  * @author cburkey
14  *
15  */

16 public class LinkTest extends BaseTestCase
17 {
18
19     /**
20      * @param inArg0
21      */

22     public LinkTest(String JavaDoc inArg0)
23     {
24         super(inArg0);
25     }
26 // public void testLoading() throws Exception
27
// {
28
// String original = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<linktree>\n" +
29
// " <a HREF=\"/\" userdata=\"root\" id=\"1234\" text=\"root\">\n" +
30
// " <a HREF=\"/test/html\" userdata=\"/test/html\" id=\"12345\" text=\"Testing\"/>\n" +
31
// " </a>\n" +
32
// "</linktree>\n";
33
//
34
// StringReader reader = new StringReader(original);
35
// XmlLinkLoader loader = new XmlLinkLoader();
36
// LinkTree tree = loader.loadLinks(reader);
37
//
38
// String results = tree.renderLinks();
39
// assertNotNull(results);
40
//
41
// //assertEquals("<a HREF=\"/test/html\">Testing</a>\n",results);
42
//
43
// StringWriter out = new StringWriter();
44
// loader.saveLinks(tree, out, "UTF-8");
45
//
46
// assertEquals(original,out.toString());
47
// }
48

49     public void testCrumb() throws Exception JavaDoc
50     {
51         LinkModule module = (LinkModule)getModule("LinkTree");
52         WebPageRequest req = getFixture().createPageRequest("/salecatalogs.html");
53
54         PageAction action = new PageAction("LinkTree.loadLinks");
55         req.setCurrentAction(action);
56         module.loadLinks(req);
57         module.setSelectedLinkByUrl( req );
58         
59         //req = getFixture().createPageRequest("/linktree/linktest.html");
60
((BaseWebPageRequest)req).setContentPage(getPage("linktree/linktest.html"));
61         req.setCurrentAction(action);
62         module.loadLinks(req);
63         module.setSelectedLinkByUrl( req );
64
65         LinkTree tree = (LinkTree)req.getPageValue("linktree");
66         Link selectedLink = tree.getSelectedLink();
67         assertEquals( selectedLink.getId(),"lancelot1");
68         assertTrue( selectedLink.isSelected());
69         assertTrue( selectedLink.getParentLink().isChildSelected());
70         
71         Link root = tree.findSelectedParentLink(0);
72         assertEquals(root,tree.getRootLink());
73
74         Link child = tree.findSelectedParentLink(1);
75         assertEquals(selectedLink.getParentLink(),child);
76
77     }
78     
79     /*
80     public void testAdd() throws Exception
81     {
82         LinkTree tree = new LinkTree();
83         
84         tree.addLink("/","123","/","/","root");
85         tree.addLink("support","1234","/","/support","support");
86         tree.addLink("about","12345","1234","/support/about","about");
87         
88         Link newone = tree.addLink("about1","123456","12345","/support/about/index.html","Test link");
89         //assertEquals( 4, tree.getAllLinks().size() );
90         assertEquals(3,tree.getLink(newone.getId()).getDepth());
91     }
92     */

93     public void testChangeId() throws Exception JavaDoc
94     {
95         // create tree with root of id1, children of id2 & id3
96
LinkModule module = (LinkModule)getModule("LinkTree");
97         WebPageRequest req = getFixture().createPageRequest("/linktree/linktest.html");
98         PageAction action = new PageAction("LinkTree.loadLinks");
99         req.setCurrentAction(action);
100         module.loadLinks(req);
101         module.setSelectedLinkByUrl( req );
102         LinkTree tree = (LinkTree)req.getPageValue("linktree");
103
104         // change name of id1 to root
105
Link rootLink = tree.getLink( "charleston" );
106         tree.changeLinkId( rootLink, "root" );
107
108         // verify
109
Link changedRootLink = tree.getLink( "root" );
110         assertTrue( changedRootLink == rootLink );
111         Link oldRootLink = tree.getLink( "charleston" );
112         assertNull( oldRootLink );
113     }
114     public void testFileSize() throws Exception JavaDoc
115     {
116     
117     LinkModule module = (LinkModule)getModule("LinkTree");
118     WebPageRequest req = getFixture().createPageRequest("/linktree/linktest.html");
119     PageAction action = new PageAction("LinkTree.loadSizer");
120     req.setCurrentAction(action);
121     module.loadSizer(req);
122     FileSize files = (FileSize)req.getPageValue("filesize");
123     String JavaDoc ks = files.inKs("/scales.gif");
124     assertEquals("5",ks);
125     
126 }
127     public void testRedirect() throws Exception JavaDoc
128     {
129         WebPageRequest req = getFixture().createPageRequest("/noexist1/index.html");
130         getFixture().getEngine().executePathActions(req);
131         String JavaDoc re = (String JavaDoc)req.getPageValue("redirect");
132         assertEquals( "/exist/index.html", re);
133     }
134     public void testRedirectWildcards() throws Exception JavaDoc
135     {
136         LinkModule module = (LinkModule)getModule("LinkTree");
137         WebPageRequest req = getFixture().createPageRequest("/noexist2/index.html");
138         getFixture().getEngine().executePathActions(req);
139         String JavaDoc re = (String JavaDoc)req.getPageValue("redirect");
140         assertEquals( "/exist/index.html", re);
141
142         LinkTree tree = module.getLinkTree(req);
143
144         String JavaDoc res = tree.findRedirect( "/noexist2/sub1/more/stuff/stuff.html" );
145         //<a id="testing3" HREF="../../../../noexist2/sub1/*" redirectpath="/exist/*" />
146
assertEquals( "/exist/more/stuff/stuff.html", res);
147
148         //<a HREF="../../../../store/products/*" id="testing3" text="Lancelot" redirectpath="http://acme.com/store/products/*" />
149
re = tree.findRedirect( "/store/products/stuff.html" );
150         assertEquals( "http://acme.com/store/products/stuff.html", re);
151     }
152
153     public void testEdit() throws Exception JavaDoc
154     {
155         LinkModule module = (LinkModule)getModule("LinkTree");
156         Page draft = getPage("/links.draft.xml");
157         getFixture().getPageManager().removePage(draft);
158         
159         WebPageRequest req = getFixture().createPageRequest("/index.html");
160         req.getUser().put("oe.edit.draftmode", "true");
161         req.setRequestParameter("linkid", "index");
162
163         LinkTree tree = module.loadLinks(req);
164
165         module.appendLink(req);
166         
167         assertNotNull(tree);
168         String JavaDoc newId = tree.getSelectedLink().getId();
169         
170         req = getFixture().createPageRequest("/index.html");
171         req.setRequestParameter("linkid", newId);
172
173         module.appendLink(req);
174         tree = module.loadLinks(req);
175         assertTrue(!newId.equals(tree.getSelectedLink().getId()));
176     }
177     
178     public void testDuplicateSelection() throws Exception JavaDoc
179     {
180         LinkModule module = (LinkModule)getModule("LinkTree");
181         WebPageRequest req = getFixture().createPageRequest("/linktree/fake.html");
182         PageAction action = new PageAction("LinkTree.loadLinks");
183         req.setCurrentAction(action);
184         module.loadLinks(req);
185         module.setSelectedLinkByUrl( req );
186         LinkTree tree = (LinkTree)req.getPageValue("linktree");
187
188         // change name of id1 to root
189
Link rootLink = tree.getSelectedLink();
190         assertTrue(rootLink.isSelected());
191         
192         Link child = tree.getLink("fake2");
193         assertTrue( child.isSelected());
194         
195     }
196 }
197
Popular Tags