/**
 * The MIT License
 * Copyright (c) 2010 JmxTrans team
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.googlecode.jmxtrans.util;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * @author lanyonm
 */
public class PropertyResolverTest {

	private PropertyResolver propertyResolver;

	@Before
	public void setSomeProperties() {
		System.setProperty("myhost", "w2");
		System.setProperty("myport", "1099");

		propertyResolver = new PropertyResolver();
	}

	@Test
	public void testProps() {
		String s1 = "${xxx} : ${yyy}";
		String s2 = propertyResolver.resolveProps(s1);
		Assert.assertEquals(s1, s2);

		s1 = "${myhost} : ${myport}";
		s2 = propertyResolver.resolveProps(s1);
		Assert.assertEquals("w2 : 1099", s2);

		s1 = "${myhost} : ${myaltport:2099}";
		s2 = propertyResolver.resolveProps(s1);
		Assert.assertEquals("w2 : 2099", s2);
		s1 = "${myhost}:2099";
		s2 = propertyResolver.resolveProps(s1);
		Assert.assertEquals("w2:2099", s2);

	}

	@After
	public void removeSystemProperties() {
		System.clearProperty("myhost");
		System.clearProperty("myport");
	}

}

@@ -1170,16 +1170,113 @@
 .util;%0A%0A
+import com.google.common.io.Closer;%0Aimport com.googlecode.jmxtrans.test.ResetableSystemProperty;%0A
 import o
@@ -1287,24 +1287,24 @@
 unit.After;%0A
-
 import org.j
@@ -1365,16 +1365,45 @@
 .Test;%0A%0A
+import java.io.IOException;%0A%0A
 /**%0A * @
@@ -1503,16 +1503,59 @@
 olver;%0A%0A
+%09private Closer closer = Closer.create();%0A%0A
 %09@Before
@@ -1590,28 +1590,68 @@
 ies() %7B%0A
+%0A
 %09%09
+closer.register(ResetableSystemProperty.set
 System
-.set
 Property
@@ -1670,22 +1670,62 @@
 w2%22)
+)
 ;%0A%09%09
+closer.register(ResetableSystemProperty.set
 System
-.set
 Prop
@@ -1746,16 +1746,17 @@
  %221099%22)
+)
 ;%0A%0A%09%09pro
@@ -2344,75 +2344,44 @@
 s() 
-%7B%0A%09%09System.clearProperty(%22myhost%22);%0A%09%09System.clearProperty(%22myport%22
+throws IOException %7B%0A%09%09closer.close(
 );%0A%09

replace system.setProperty by resetableSystemProperty