1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.surf;
20  
21  import java.io.File;
22  import java.net.URL;
23  
24  import javax.servlet.ServletContext;
25  import javax.servlet.ServletException;
26  
27  import junit.framework.TestCase;
28  
29  import org.junit.Ignore;
30  import org.springframework.context.ApplicationContext;
31  import org.springframework.extensions.surf.exception.WebFrameworkServiceException;
32  import org.springframework.extensions.surf.site.Model;
33  import org.springframework.mock.web.MockServletConfig;
34  import org.springframework.web.context.support.XmlWebApplicationContext;
35  import org.springframework.web.servlet.DispatcherServlet;
36  
37  /**
38   * Tests the Surf API directly using mock objects
39   * 
40   * @author muzquiano
41   */
42  @Ignore public abstract class AbstractSurfTestCase extends TestCase
43  {
44  	protected MockServletConfig servletConfig;
45  	protected DispatcherServlet dispatcherServlet;
46  	protected Model api;
47  	
48  	public void setUp() throws ServletException
49  	{
50  		initTestDataFolder();
51  		
52  		servletConfig = new MockServletConfig(new SurfServletContext(getTestDataFolder()), "root");
53  		
54  		MockServletConfig surfConfig = new MockServletConfig(servletConfig.getServletContext(), "surf");
55  		surfConfig.addInitParameter("publishContext", "false");
56  		surfConfig.addInitParameter("class", "notWritable");
57  		surfConfig.addInitParameter("unknownParam", "someValue");
58  		
59  		dispatcherServlet = new DispatcherServlet();
60  		dispatcherServlet.setContextClass(XmlWebApplicationContext.class);
61  		dispatcherServlet.setContextConfigLocation(getConfigLocation().toString());
62  		dispatcherServlet.init(surfConfig);
63  	}
64  	
65  	public void tearDown() throws ServletException
66  	{
67  		destroyTestDataFolder();
68  	}
69  	
70  	public StringBuilder getConfigLocation()
71  	{
72  		StringBuilder sb = new StringBuilder();
73  		sb.append("classpath*:org/springframework/extensions/webscripts/*-context.xml");
74  		sb.append(",");
75  		sb.append("classpath*:org/springframework/extensions/surf/*-context.xml");
76  		sb.append(",");
77  		sb.append("classpath*:org/springframework/extensions/surf/surf-test-context.xml");
78  		
79  		return sb;		
80  	}
81  	
82  	public ServletContext getServletContext() 
83  	{
84  		return servletConfig.getServletContext();
85  	}
86  	
87  	public ApplicationContext getApplicationContext()
88  	{
89  		return dispatcherServlet.getWebApplicationContext();
90  	}
91  	
92  	public WebFrameworkServiceRegistry getServiceRegistry()
93  	{
94  		return (WebFrameworkServiceRegistry) getApplicationContext().getBean("webframework.service.registry");
95  	}
96  	
97  	public Model getModel() throws WebFrameworkServiceException
98  	{
99  		if (api == null)
100 		{
101 			ModelPersistenceContext mpc = new ModelPersistenceContext(null);
102 			api = new Model(getServiceRegistry().getWebFrameworkManager().getObjectManager(mpc));
103 		}
104 		
105 		return api;
106 	}
107 	
108 	public static boolean deleteDirectory(File path) 
109 	{
110 		if( path.exists() )
111 		{
112 			File[] files = path.listFiles();
113 			for(int i=0; i<files.length; i++) 
114 			{
115 				if(files[i].isDirectory()) 
116 				{
117 					deleteDirectory(files[i]);
118 				}
119 				else 
120 				{
121 					files[i].delete();
122 				}
123 			}
124 		}
125 
126 		return( path.delete() );
127 	}
128 	
129 	public static File getTestDataFolder() 
130 	{
131 		Class anyTestClass = AbstractSurfTestCase.class;
132 		
133 		final String clsUri = anyTestClass.getName().replace('.','/') + ".class";
134 		final URL url = anyTestClass.getClassLoader().getResource(clsUri);
135 		final String clsPath = url.getPath();
136 		final File root = new File(clsPath.substring(0, clsPath.length() - clsUri.length()));
137 		
138 		return new File(root.getParentFile(), "test-data");		
139 	}
140 	
141 	public static void initTestDataFolder() 
142 	{		
143 		File f = getTestDataFolder();		
144 		deleteDirectory(f);
145 		f.mkdirs();
146 		
147 		File site = new File(f, "site");
148 		site.mkdirs();
149 
150 		// init model object directories
151 		// TODO - autogenerate?
152 		new File(site, "chrome").mkdirs();
153 		new File(site, "components").mkdirs();
154 		new File(site, "component-types").mkdirs();
155 		new File(site, "configurations").mkdirs();
156 		new File(site, "content-associations").mkdirs();
157 		new File(site, "page-associations").mkdirs();
158 		new File(site, "pages").mkdirs();
159 		new File(site, "page-types").mkdirs();
160 		new File(site, "template-instances").mkdirs();
161 		new File(site, "template-types").mkdirs();
162 		new File(site, "themes").mkdirs();
163 		
164 		File webscripts = new File(f, "webscripts");
165 		webscripts.mkdirs();
166 		
167 		File templates = new File(f, "templates");
168 		templates.mkdirs();
169 	}
170 
171 	public static void destroyTestDataFolder() 
172 	{		
173 		File f = getTestDataFolder();		
174 		deleteDirectory(f);
175 	}
176 	
177 }