KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > template > TemplateServiceImplTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.template;
18
19 import java.io.InputStream JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.alfresco.repo.dictionary.DictionaryComponent;
26 import org.alfresco.repo.dictionary.DictionaryDAO;
27 import org.alfresco.repo.dictionary.M2Model;
28 import org.alfresco.repo.node.BaseNodeServiceTest;
29 import org.alfresco.repo.policy.PolicyComponent;
30 import org.alfresco.repo.security.authentication.AuthenticationComponent;
31 import org.alfresco.repo.transaction.TransactionUtil;
32 import org.alfresco.service.ServiceRegistry;
33 import org.alfresco.service.cmr.repository.ContentService;
34 import org.alfresco.service.cmr.repository.NodeRef;
35 import org.alfresco.service.cmr.repository.NodeService;
36 import org.alfresco.service.cmr.repository.StoreRef;
37 import org.alfresco.service.cmr.repository.TemplateNode;
38 import org.alfresco.service.cmr.repository.TemplateService;
39 import org.alfresco.service.transaction.TransactionService;
40 import org.alfresco.util.ApplicationContextHelper;
41 import org.springframework.context.ApplicationContext;
42
43 /**
44  * @author Kevin Roast
45  */

46 public class TemplateServiceImplTest extends TestCase
47 {
48     private static final ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
49     
50     private ContentService contentService;
51     private TemplateService templateService;
52     private NodeService nodeService;
53     private TransactionService transactionService;
54     private ServiceRegistry serviceRegistry;
55     private AuthenticationComponent authenticationComponent;
56     
57     /*
58      * @see junit.framework.TestCase#setUp()
59      */

60     protected void setUp() throws Exception JavaDoc
61     {
62         super.setUp();
63         
64         transactionService = (TransactionService)this.ctx.getBean("transactionComponent");
65         contentService = (ContentService)this.ctx.getBean("contentService");
66         nodeService = (NodeService)this.ctx.getBean("nodeService");
67         templateService = (TemplateService)this.ctx.getBean("templateService");
68         serviceRegistry = (ServiceRegistry)this.ctx.getBean("ServiceRegistry");
69         
70         this.authenticationComponent = (AuthenticationComponent)ctx.getBean("authenticationComponent");
71         this.authenticationComponent.setSystemUserAsCurrentUser();
72         
73         DictionaryDAO dictionaryDao = (DictionaryDAO)ctx.getBean("dictionaryDAO");
74         
75         // load the system model
76
ClassLoader JavaDoc cl = BaseNodeServiceTest.class.getClassLoader();
77         InputStream JavaDoc modelStream = cl.getResourceAsStream("alfresco/model/contentModel.xml");
78         assertNotNull(modelStream);
79         M2Model model = M2Model.createModel(modelStream);
80         dictionaryDao.putModel(model);
81         
82         // load the test model
83
modelStream = cl.getResourceAsStream("org/alfresco/repo/node/BaseNodeServiceTest_model.xml");
84         assertNotNull(modelStream);
85         model = M2Model.createModel(modelStream);
86         dictionaryDao.putModel(model);
87         
88         DictionaryComponent dictionary = new DictionaryComponent();
89         dictionary.setDictionaryDAO(dictionaryDao);
90         BaseNodeServiceTest.loadModel(ctx);
91     }
92
93     @Override JavaDoc
94     protected void tearDown() throws Exception JavaDoc
95     {
96         authenticationComponent.clearCurrentSecurityContext();
97         super.tearDown();
98     }
99     
100     public void testTemplates()
101     {
102         TransactionUtil.executeInUserTransaction(
103             transactionService,
104             new TransactionUtil.TransactionWork<Object JavaDoc>()
105             {
106                 public Object JavaDoc doWork() throws Exception JavaDoc
107                 {
108                     StoreRef store = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "template_" + System.currentTimeMillis());
109                     NodeRef root = nodeService.getRootNode(store);
110                     BaseNodeServiceTest.buildNodeGraph(nodeService, root);
111                     
112                     // check the default template engine exists
113
assertNotNull(templateService.getTemplateProcessor("freemarker"));
114                     
115                     // create test model
116
Map JavaDoc model = new HashMap JavaDoc(7, 1.0f);
117                     
118                     model.put("root", new TemplateNode(root, serviceRegistry, null));
119                     
120                     // execute on test template
121
String JavaDoc output = templateService.processTemplate("freemarker", TEMPLATE_1, model);
122                     
123                     // check template contains the expected output
124
assertTrue( (output.indexOf(root.getId()) != -1) );
125                     
126                     System.out.print(output);
127                     
128                     return null;
129                 }
130             });
131     }
132     
133     private static final String JavaDoc TEMPLATE_1 = "org/alfresco/repo/template/test_template1.ftl";
134 }
135
Popular Tags