001package org.apache.turbine.services.velocity;
002
003
004/*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements.  See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership.  The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License.  You may obtain a copy of the License at
012 *
013 *   http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied.  See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024import org.apache.commons.configuration2.Configuration;
025import org.apache.turbine.Turbine;
026import org.apache.turbine.annotation.AnnotationProcessor;
027import org.apache.turbine.annotation.TurbineService;
028import org.apache.turbine.test.BaseTestCase;
029import org.apache.turbine.util.TurbineConfig;
030import org.apache.velocity.app.VelocityEngine;
031import org.junit.jupiter.api.AfterAll;
032import org.junit.jupiter.api.BeforeAll;
033import org.junit.jupiter.api.Test;
034import org.junit.jupiter.api.TestInstance;
035
036import java.io.File;
037
038import static org.junit.jupiter.api.Assertions.assertEquals;
039import static org.junit.jupiter.api.Assertions.assertNotNull;
040
041/**
042 * Tests startup of the Velocity Service and translation of various
043 * path patterns.
044 *
045 * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
046 *
047 */
048
049@TestInstance(TestInstance.Lifecycle.PER_CLASS)
050public class PathConverterTest
051    extends BaseTestCase
052{
053    private static TurbineConfig tc = null;
054
055    @TurbineService
056    private VelocityService vs = null;
057
058    @BeforeAll
059    public void setUp() throws Exception {
060        tc = new TurbineConfig(".", "/conf/test/TemplateService.properties");
061        tc.initialize();
062
063        AnnotationProcessor.process(this);
064        //vs = (VelocityService) TurbineServices.getInstance().getService(VelocityService.SERVICE_NAME);
065        assertNotNull(vs);
066    }
067
068    @AfterAll
069    public void destroy() throws Exception {
070        vs.shutdown();
071        tc.dispose();
072    }
073
074    @Test
075    void testService()
076        throws Exception
077    {
078        // Can we start the service?
079        assertNotNull(vs, "Could not load Service!");
080    }
081
082    @Test
083     void testPathTranslation()
084        throws Exception
085    {
086        Configuration conf = vs.getConfiguration();
087        VelocityEngine ve = new VelocityEngine();
088        ((TurbineVelocityService) vs).setVelocityProperties(ve, conf);
089
090        String rootPath = Turbine.getRealPath("");
091
092        String test1 = (String) ve.getProperty("test1.resource.loader.path");
093        assertNotNull( test1, "No Test1 Property found");
094        assertEquals( String.join(File.separator, rootPath, "relative", "path"), test1,
095                "Test1 Path translation failed");
096
097        String test2 = (String) ve.getProperty("test2.resource.loader.path");
098        assertNotNull( test2, "No Test2 Property found");
099        assertEquals(String.join(File.separator, rootPath, "absolute", "path"), test2,
100                "Test2 Path translation failed");
101
102        String test3 = (String) ve.getProperty("test3.resource.loader.path");
103        assertNotNull( test3, "No Test3 Property found");
104        assertEquals(
105                rootPath +File.separator+"jar-file.jar!/", test3,
106                "Test3 Path translation failed");
107
108        String test4 = (String) ve.getProperty("test4.resource.loader.path");
109        assertNotNull( test4, "No Test4 Property found");
110        assertEquals(rootPath +File.separator+"jar-file.jar!/with/some/extensions" , test4,
111                "Test4 Path translation failed");
112
113        String test5 = (String) ve.getProperty("test5.resource.loader.path");
114        assertNotNull( test5,"No Test5 Property found");
115        assertEquals(rootPath
116                +File.separator+"jar-file.jar" , test5,
117                "Test5 Path translation failed");
118
119        String test6 = (String) ve.getProperty("test6.resource.loader.path");
120        assertNotNull(test6, "No Test6 Property found");
121        assertEquals("jar:http://jar.on.website/" , test6,
122                "Test6 Path translation failed");
123
124        String test7 = (String) ve.getProperty("test7.resource.loader.path");
125        assertNotNull(test7, "No Test7 Property found");
126        assertEquals(String.join(File.separator, rootPath, "file", "system", "reference"), test7,
127                "Test7 Path translation failed");
128
129        String test8 = (String) ve.getProperty("test8.resource.loader.path");
130        assertNotNull(test8, "No Test8 Property found");
131        assertEquals("http://reference.on.website/", test8,
132                "Test8 Path translation failed");
133    }
134}