1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.om.page;
18  
19  // Java imports
20  import java.util.Iterator;
21  
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  
26  import org.apache.jetspeed.om.page.psml.FragmentImpl;
27  import org.apache.jetspeed.om.page.psml.PageImpl;
28  
29  /***
30   * TestMarshalPsml
31   *
32   * @author <a href="taylor@apache.org">David Sean Taylor</a>
33   * @version $Id: TestPageObjectModel.java 516448 2007-03-09 16:25:47Z ate $
34   */
35  public class TestPageObjectModel extends TestCase
36  {
37  
38      /***
39       * Defines the testcase name for JUnit.
40       *
41       * @param name the testcase's name.
42       */
43      public TestPageObjectModel( String name )
44      {
45          super( name );
46      }
47  
48      /***
49       * Start the tests.
50       *
51       * @param args the arguments. Not used
52       */
53      public static void main(String args[])
54      {
55          junit.awtui.TestRunner.main( new String[] { TestPageObjectModel.class.getName() } );
56      }
57  
58      public void setup()
59      {
60          System.out.println("Setup: Testing Page Object Model Implementation");
61      }
62  
63      /***
64       * Creates the test suite.
65       *
66       * @return a test suite (<code>TestSuite</code>) that includes all methods
67       *         starting with "test"
68       */
69      public static Test suite()
70      {
71          // All methods starting with "test" will be executed in the test suite.
72          return new TestSuite(TestPageObjectModel.class);
73      }
74  
75      private Page buildBasePage()
76      {
77          PageImpl page = new PageImpl();
78          page.setId("MyPageID");
79  
80          FragmentImpl frag = new FragmentImpl();
81          frag.setId("Frag1");
82          frag.setType(Fragment.LAYOUT);
83  
84          page.setRootFragment(frag);
85  
86          return page;
87      }
88  
89      public void testBasicPage() throws Exception
90      {
91          System.out.println("Testing simple Page creation");
92  
93          Page page = buildBasePage();
94          assertTrue(page.getId().equals("MyPageID"));
95          Fragment root = page.getRootFragment();
96          assertNotNull(root);
97          assertTrue(root.getId().equals("Frag1"));
98          assertTrue(root.getType().equals(Fragment.LAYOUT));
99          assertNull(root.getTitle());
100     }
101 
102     public void testFragmentManipulation() throws Exception
103     {
104         System.out.println("Testing Fragments manipulation");
105 
106         // Build a page with a few fragments
107         Page page = buildBasePage();
108         Fragment root = page.getRootFragment();
109         assertNotNull(root.getFragments());
110 
111         FragmentImpl frag1 = new FragmentImpl();
112         frag1.setId("F1");
113         frag1.setType(Fragment.PORTLET);
114         frag1.setName("Portlet1");
115         root.getFragments().add(frag1);
116 
117         FragmentImpl frag2 = new FragmentImpl();
118         frag2.setId("F2");
119         frag2.setType(Fragment.LAYOUT);
120         frag2.setName("TwoColumns");
121         frag2.setDecorator("test");
122 
123         FragmentImpl frag3 = new FragmentImpl();
124         frag3.setId("F3");
125         frag3.setType(Fragment.PORTLET);
126         frag3.setName("Portlet3");
127         frag3.setDecorator("test");
128         frag3.setState("minimized");
129         frag2.getFragments().add(frag3);
130         root.getFragments().add(frag2);
131 
132         //Check the construct
133         assertTrue(root.getFragments().size()==2);
134         Iterator i = root.getFragments().iterator();
135         FragmentImpl f = (FragmentImpl)i.next();
136         assertNotNull(f);
137         assertTrue(f.getName().equals("Portlet1"));
138         assertTrue(f.getType().equals(Fragment.PORTLET));
139         assertTrue(f.getId().equals("F1"));
140         assertNull(f.getTitle());
141         assertNull(f.getDecorator());
142         assertNull(f.getState());
143         assertTrue(f.getFragments().size()==0);
144         f = (FragmentImpl)i.next();
145         assertNotNull(f);
146         assertTrue(f.getName().equals("TwoColumns"));
147         assertTrue(f.getType().equals(Fragment.LAYOUT));
148         assertTrue(f.getFragments().size()==1);
149         assertTrue(f.getDecorator().equals("test"));
150         assertTrue(f.getFragments().size()==1);
151         i = f.getFragments().iterator();
152         frag1 = (FragmentImpl)i.next();
153         assertNotNull(frag1);
154         assertTrue(frag1.getName().equals("Portlet3"));
155         assertTrue(frag1.getType().equals(Fragment.PORTLET));
156 
157         //Now change the inner child to a new portlet
158         frag2 = new FragmentImpl();
159         frag2.setId("FR4");
160         frag2.setType(Fragment.PORTLET);
161         frag2.setName("P4");
162 
163         frag3 = (FragmentImpl)page.getFragmentById("F3");
164         assertNotNull(frag3);
165         f.getFragments().remove(frag3);
166         frag3 = (FragmentImpl)page.getFragmentById("F3");
167         assertNull(frag3);
168         f.getFragments().add(frag2);
169         assertTrue(f.getFragments().size()==1);
170         f = (FragmentImpl)f.getFragments().get(0);
171         assertNotNull(f);
172         assertTrue(f.getName().equals("P4"));
173     }
174 }