    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 static org.junit.Assert.assertEquals;
    025import static org.junit.Assert.assertNotNull;
    026
    027import org.apache.commons.collections.ExtendedProperties;
    028import org.apache.commons.configuration.Configuration;
    029import org.apache.turbine.Turbine;
    030import org.apache.turbine.services.TurbineServices;
    031import org.apache.turbine.test.BaseTestCase;
    032import org.apache.turbine.util.TurbineConfig;
    033import org.junit.AfterClass;
    034import org.junit.BeforeClass;
    035import org.junit.Test;
    036
    037/**
    038 * Tests startup of the Velocity Service and translation of various
    039 * path patterns.
    040 *
    041 * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
    042 * @version $Id: PathConverterTest.java 1812628 2017-10-19 12:34:25Z gk $
    043 */
    044
    045public class PathConverterTest
    046    extends BaseTestCase
    047{
    048    private static TurbineConfig tc = null;
    049    private static VelocityService vs = null;
    050    private static String fileSeperator = System.getProperty("file.separator");
    051
    052
    053    @BeforeClass
    054    public static void setUp() throws Exception {
    055        tc = new TurbineConfig(".", "/conf/test/TemplateService.properties");
    056        tc.initialize();
    057
    058        vs = (VelocityService) TurbineServices.getInstance().getService(VelocityService.SERVICE_NAME);
    059    }
    060
    061    @AfterClass
    062    public static void destroy() throws Exception {
    063        vs.shutdown();
    064        tc.dispose();
    065    }
    066
    067    @Test public void testService()
    068        throws Exception
    069    {
    070
    071        // Can we start the service?
    072        assertNotNull("Could not load Service!", vs);
    073    }
    074
    075    @Test
    076    public void testPathTranslation()
    077        throws Exception
    078    {
    079        Configuration conf = vs.getConfiguration();
    080        ExtendedProperties ep = ((TurbineVelocityService) vs).createVelocityProperties(conf);
    081
    082        String rootPath = Turbine.getRealPath("");
    083
    084        String [] test1 = ep.getStringArray("test1.resource.loader.path");
    085        assertEquals("No Test1 Property found", 1, test1.length);
    086        assertEquals("Test1 Path translation failed", rootPath
    087                +fileSeperator+"relative"+fileSeperator+"path" , test1[0]);
    088
    089        String [] test2 = ep.getStringArray("test2.resource.loader.path");
    090        assertEquals("No Test2 Property found", 1, test2.length);
    091        assertEquals("Test2 Path translation failed", rootPath
    092                +fileSeperator+"absolute"+fileSeperator+"path" , test2[0]);
    093
    094        String [] test3 = ep.getStringArray("test3.resource.loader.path");
    095        assertEquals("No Test3 Property found", 1, test2.length);
    096        assertEquals("Test3 Path translation failed", rootPath
    097                +fileSeperator+"jar-file.jar!/", test3[0]);
    098
    099        String [] test4 = ep.getStringArray("test4.resource.loader.path");
    100        assertEquals("No Test4 Property found", 1, test4.length);
    101        assertEquals("Test4 Path translation failed", rootPath
    102                +fileSeperator+"jar-file.jar!/with/some/extensions" , test4[0]);
    103
    104        String [] test5 = ep.getStringArray("test5.resource.loader.path");
    105        assertEquals("No Test5 Property found", 1, test5.length);
    106        assertEquals("Test5 Path translation failed", rootPath
    107                +fileSeperator+"jar-file.jar" , test5[0]);
    108
    109        String [] test6 = ep.getStringArray("test6.resource.loader.path");
    110        assertEquals("No Test6 Property found", 1, test6.length);
    111        assertEquals("Test6 Path translation failed", "jar:http://jar.on.website/" , test6[0]);
    112
    113        String [] test7 = ep.getStringArray("test7.resource.loader.path");
    114        assertEquals("No Test7 Property found", 1, test7.length);
    115        assertEquals("Test7 Path translation failed", rootPath
    116                +fileSeperator+"file"+fileSeperator
    117                +"system"+fileSeperator+"reference" , test7[0]);
    118
    119        String [] test8 = ep.getStringArray("test8.resource.loader.path");
    120        assertEquals("No Test8 Property found", 1, test8.length);
    121        assertEquals("Test8 Path translation failed", "http://reference.on.website/" , test8[0]);
    122
    123    }
    124}