001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.schematic.internal.schema; 017 018import static org.hamcrest.core.Is.is; 019import static org.junit.Assert.assertThat; 020import org.junit.Before; 021import org.junit.Test; 022import org.modeshape.schematic.internal.schema.DocumentTransformer.SystemPropertyAccessor; 023 024/** 025 * Test property variable syntax options 026 * <ul> 027 * <li>${variable}</li> 028 * <li>${variable [,variable,..] }</li> 029 * <li>${variable [:defaultvalue] }</li> 030 * </ul> 031 * 032 * @author Van Halbert 033 */ 034public class SystemPropertyFactoryTest { 035 036 private static final String TESTPROP = "test.prop"; 037 private static final String TESTPROPVALUE = "test.prop.value"; 038 039 private static final String TESTPROP2 = "test.prop2"; 040 private static final String TESTPROPVALUE2 = "test.prop.value2"; 041 042 /** 043 * @throws java.lang.Exception 044 */ 045 @Before 046 public void setUp() throws Exception { 047 System.setProperty(TESTPROP, TESTPROPVALUE); 048 System.setProperty(TESTPROP2, TESTPROPVALUE2); 049 } 050 051 protected void assertSystemPropertySubstituted( String input, 052 String expected ) { 053 assertThat(DocumentTransformer.getSubstitutedProperty(input, SystemPropertyAccessor.INSTANCE), is(expected)); 054 } 055 056 @Test 057 public void shouldSubstituteSingleVariable() { 058 059 assertSystemPropertySubstituted("${" + TESTPROP + "}", TESTPROPVALUE); 060 assertSystemPropertySubstituted("find.the.property." + "${" + TESTPROP + "}", "find.the.property." + TESTPROPVALUE); 061 assertSystemPropertySubstituted("${" + TESTPROP + "}.find.the.property", TESTPROPVALUE + ".find.the.property"); 062 assertSystemPropertySubstituted("find.the.property.${" + TESTPROP + "}.find.the.property", "find.the.property." 063 + TESTPROPVALUE 064 + ".find.the.property"); 065 String pathEnvironmentValue = System.getenv("PATH"); 066 if (pathEnvironmentValue != null) { 067 //test variable substitution for environment values 068 assertSystemPropertySubstituted("${PATH}", pathEnvironmentValue); 069 } 070 } 071 072 @Test 073 public void shouldSubstituteFirstFoundMultipleVariables() { 074 075 assertSystemPropertySubstituted("${any.prop," + TESTPROP + "}", TESTPROPVALUE); 076 assertSystemPropertySubstituted("find.the.property.${any.prop," + TESTPROP + "}", "find.the.property." + TESTPROPVALUE); 077 assertSystemPropertySubstituted("${any.prop," + TESTPROP + "}.find.the.property", TESTPROPVALUE + ".find.the.property"); 078 assertSystemPropertySubstituted("find.the.property.${any.prop," + TESTPROP + "}.find.the.property", 079 "find.the.property." + TESTPROPVALUE + ".find.the.property"); 080 081 // this test verifies that the first property is used even though there are 2 properties that can be found 082 assertSystemPropertySubstituted("${any.prop," + TESTPROP + "," + TESTPROP2 + "}.find.the.property", 083 TESTPROPVALUE + ".find.the.property"); 084 085 } 086 087 @Test 088 public void shouldSubstituteMultipleVariablesWithDefault() { 089 090 assertSystemPropertySubstituted("${any.prop1,any.prop2:" + TESTPROPVALUE + "}", TESTPROPVALUE); 091 assertSystemPropertySubstituted("find.the.property.${any.prop1:" + TESTPROPVALUE + "}", "find.the.property." 092 + TESTPROPVALUE); 093 assertSystemPropertySubstituted("${any.prop1,any.prop2:" + TESTPROPVALUE + "}.find.the.property", TESTPROPVALUE 094 + ".find.the.property"); 095 assertSystemPropertySubstituted("find.the.property.${any.prop:" + TESTPROPVALUE + "}.find.the.property", 096 "find.the.property." + TESTPROPVALUE + ".find.the.property"); 097 } 098 099 @Test 100 public void shouldSubstituteMultipleVariableGroups() { 101 102 assertSystemPropertySubstituted("${" + TESTPROP + "}.double.${" + TESTPROP + "}", TESTPROPVALUE + ".double." 103 + TESTPROPVALUE); 104 assertSystemPropertySubstituted("${any.prop," + TESTPROP + "}.double.${any.prop," + TESTPROP + "}", TESTPROPVALUE 105 + ".double." 106 + TESTPROPVALUE); 107 assertSystemPropertySubstituted("${any.prop:" + TESTPROPVALUE + "}.double.${any.prop:" + TESTPROPVALUE + "}", 108 TESTPROPVALUE + ".double." + TESTPROPVALUE); 109 110 } 111 112}