1 /** 2 * Copyright (c) 2008, SnakeYAML 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package org.yaml.snakeyaml.env; 15 16 import static org.yaml.snakeyaml.env.EnvScalarConstructor.ENV_FORMAT; 17 18 import java.util.Map; 19 import junit.framework.TestCase; 20 import org.yaml.snakeyaml.Util; 21 import org.yaml.snakeyaml.Yaml; 22 import org.yaml.snakeyaml.error.MissingEnvironmentVariableException; 23 24 public class EnvVariableTest extends TestCase { 25 26 // the variables EnvironmentKey1 and EnvironmentEmpty are set by Maven 27 private static final String KEY1 = "EnvironmentKey1"; 28 private static final String EMPTY = "EnvironmentEmpty"; 29 private static final String VALUE1 = "EnvironmentValue1"; 30 load(String template)31 private String load(String template) { 32 Yaml yaml = new Yaml(new EnvScalarConstructor()); 33 yaml.addImplicitResolver(EnvScalarConstructor.ENV_TAG, ENV_FORMAT, "$"); 34 String loaded = yaml.load(template); 35 return loaded; 36 } 37 testEnvironmentSet()38 public void testEnvironmentSet() { 39 assertEquals("Surefire plugin must set the variable.", VALUE1, System.getenv(KEY1)); 40 assertEquals("Surefire plugin must set the variable.", "", System.getenv(EMPTY)); 41 } 42 testEnvConstructor()43 public void testEnvConstructor() { 44 assertEquals(VALUE1, load("${EnvironmentKey1}")); 45 assertEquals(VALUE1, load("${EnvironmentKey1-any}")); 46 assertEquals(VALUE1, load("${EnvironmentKey1:-any}")); 47 assertEquals(VALUE1, load("${EnvironmentKey1:?any}")); 48 assertEquals(VALUE1, load("${EnvironmentKey1?any}")); 49 } 50 testEnvConstructorForEmpty()51 public void testEnvConstructorForEmpty() { 52 assertEquals("", load("${EnvironmentEmpty}")); 53 assertEquals("", load("${EnvironmentEmpty?}")); 54 assertEquals("detected", load("${EnvironmentEmpty:-detected}")); 55 assertEquals("", load("${EnvironmentEmpty-detected}")); 56 assertEquals("", load("${EnvironmentEmpty?detectedError}")); 57 try { 58 load("${EnvironmentEmpty:?detectedError}"); 59 } catch (MissingEnvironmentVariableException e) { 60 assertEquals("Empty mandatory variable EnvironmentEmpty: detectedError", e.getMessage()); 61 } 62 } 63 testEnvConstructorForUnset()64 public void testEnvConstructorForUnset() { 65 assertEquals("", load("${EnvironmentUnset}")); 66 assertEquals("", load("${EnvironmentUnset:- }")); 67 assertEquals("detected", load("${EnvironmentUnset:-detected}")); 68 assertEquals("detected", load("${EnvironmentUnset-detected}")); 69 try { 70 load("${EnvironmentUnset:?detectedError}"); 71 } catch (MissingEnvironmentVariableException e) { 72 assertEquals("Missing mandatory variable EnvironmentUnset: detectedError", e.getMessage()); 73 } 74 try { 75 load("${EnvironmentUnset?detectedError}"); 76 } catch (MissingEnvironmentVariableException e) { 77 assertEquals("Missing mandatory variable EnvironmentUnset: detectedError", e.getMessage()); 78 } 79 } 80 testDockerCompose()81 public void testDockerCompose() { 82 Yaml yaml = new Yaml(new EnvScalarConstructor()); 83 yaml.addImplicitResolver(EnvScalarConstructor.ENV_TAG, ENV_FORMAT, "$"); 84 String resource = Util.getLocalResource("env/docker-compose.yaml"); 85 Map<String, Object> compose = yaml.load(resource); 86 String output = compose.toString(); 87 assertTrue(output, output.endsWith( 88 "environment={URL1=EnvironmentValue1, URL2=, URL3=server3, URL4=, URL5=server5, URL6=server6}}}}")); 89 } 90 testIssue493()91 public void testIssue493() { 92 Yaml yaml = new Yaml(new EnvScalarConstructor()); 93 yaml.addImplicitResolver(EnvScalarConstructor.ENV_TAG, ENV_FORMAT, "$"); 94 String resource = Util.getLocalResource("env/env-493.yaml"); 95 Map<String, Object> compose = yaml.load(resource); 96 String output = compose.toString(); 97 assertEquals( 98 "{database={url=jdbc:postgresql://localhost:5432/server493, user=user493, password=password493}}", 99 output); 100 } 101 } 102