View Javadoc

1   /*
2    * $Id: ClasspathPackageProviderTest.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.config;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.struts2.dispatcher.ServletDispatcherResult;
28  import org.apache.struts2.util.StrutsTestCaseHelper;
29  
30  import com.opensymphony.xwork2.config.Configuration;
31  import com.opensymphony.xwork2.config.entities.ActionConfig;
32  import com.opensymphony.xwork2.config.entities.PackageConfig;
33  import com.opensymphony.xwork2.config.entities.ResultConfig;
34  import com.opensymphony.xwork2.config.entities.ResultTypeConfig;
35  import com.opensymphony.xwork2.config.impl.DefaultConfiguration;
36  
37  import junit.framework.TestCase;
38  
39  public class ClasspathPackageProviderTest extends TestCase {
40  
41      ClasspathPackageProvider provider;
42      Configuration config;
43  
44      public void setUp() throws Exception {
45          provider = new ClasspathPackageProvider();
46          provider.setActionPackages("org.apache.struts2.config");
47          config = new DefaultConfiguration();
48          PackageConfig strutsDefault = new PackageConfig.Builder("struts-default")
49                  .addResultTypeConfig(new ResultTypeConfig.Builder("dispatcher", ServletDispatcherResult.class.getName())
50                          .defaultResultParam("location")
51                          .build())
52                  .defaultResultType("dispatcher")
53                  .build();
54          config.addPackageConfig("struts-default", strutsDefault);
55          PackageConfig customPackage = new PackageConfig.Builder("custom-package")
56              .namespace("/custom")
57              .build();
58          config.addPackageConfig("custom-package", customPackage);
59          provider.init(config);
60          provider.loadPackages();
61      }
62      
63      public void tearDown() throws Exception {
64          provider = null;
65          config = null;
66      }
67  
68      public void testFoundRootPackages() {
69          assertEquals(6, config.getPackageConfigs().size());
70          PackageConfig pkg = config.getPackageConfig("org.apache.struts2.config");
71          assertNotNull(pkg);
72          Map configs = pkg.getActionConfigs();
73          assertNotNull(configs);
74          // assertEquals(1, configs.size());
75          ActionConfig actionConfig = (ActionConfig) configs.get("customParentPackage");
76          assertNotNull(actionConfig);
77      }
78      
79      public void testDisableScanning() {
80          provider = new ClasspathPackageProvider();
81          provider.setActionPackages("org.apache.struts2.config");
82          provider.setDisableActionScanning("true");
83          config = new DefaultConfiguration();
84          provider.init(config);
85          provider.loadPackages();
86          
87          assertEquals(0, config.getPackageConfigs().size());
88      }
89  
90      public void testParentPackage() {
91          PackageConfig pkg = config.getPackageConfig("org.apache.struts2.config");
92          // assertEquals(2, pkg.getParents().size());
93          Map configs = pkg.getActionConfigs();
94          ActionConfig config = (ActionConfig) configs.get("customParentPackage");
95          assertNotNull(config);
96          assertEquals("/custom", pkg.getNamespace());
97      }
98  
99      public void testCustomNamespace() {
100         PackageConfig pkg = config.getPackageConfig("org.apache.struts2.config.CustomNamespaceAction");
101         Map configs = pkg.getAllActionConfigs();
102         // assertEquals(2, configs.size());
103         ActionConfig config = (ActionConfig) configs.get("customNamespace");
104         assertEquals(config.getPackageName(), pkg.getName());
105         assertEquals(1, pkg.getParents().size());
106         assertNotNull(config);
107         assertEquals("/mynamespace", pkg.getNamespace());
108         ActionConfig ac = (ActionConfig) configs.get("customParentPackage");
109         assertNotNull(ac);
110     }
111     
112     public void testCustomActionAnnotation() {
113         PackageConfig pkg = config.getPackageConfig("org.apache.struts2.config.AnnotatedAction");
114         Map configs = pkg.getAllActionConfigs();
115         // assertEquals(2, configs.size());
116         ActionConfig config = (ActionConfig) configs.get("myaction");
117         assertNotNull(config);
118     }
119     
120     public void testCustomActionAnnotationOfAnyName() {
121         PackageConfig pkg = config.getPackageConfig("org.apache.struts2.config");
122         Map configs = pkg.getAllActionConfigs();
123         // assertEquals(2, configs.size());
124         ActionConfig config = (ActionConfig) configs.get("myaction2");
125         assertNotNull(config);
126     }
127     
128     public void testResultAnnotations() {
129         PackageConfig pkg = config.getPackageConfig("org.apache.struts2.config.cltest");
130         assertEquals("/cltest", pkg.getNamespace());
131         ActionConfig acfg = pkg.getActionConfigs().get("twoResult");
132         assertNotNull(acfg);
133         assertEquals(3, acfg.getResults().size());
134     }
135 
136     public void testActionImplementation() {
137         PackageConfig pkg = config.getPackageConfig("org.apache.struts2.config.cltest");
138         assertEquals("/cltest", pkg.getNamespace());
139         ActionConfig acfg = pkg.getActionConfigs().get("actionImpl");
140         assertNotNull(acfg);
141     }
142 }