blob: 3ee780a4cd576292ba5c2e40538cca85fc20eb13 [file] [log] [blame]
/******************************************************************************
* Copyright (c) David Orme and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* David Orme - initial API and implementation
******************************************************************************/
package org.eclipse.e4.core.metaconfig;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;
import org.eclipse.e4.core.metaconfig.Configuration;
import org.eclipse.e4.core.metaconfig.ConfigurationException;
import org.eclipse.e4.ui.test.utils.StringInputStream;
import junit.framework.TestCase;
public class ConfigurationTest extends TestCase {
Configuration testee = null;
public void testReturnedPropertiesAreCloned() throws Exception {
testee = new Configuration() {
@Override
Properties populateProperties(String configFileSource) {
Properties stunt = new Properties();
stunt.setProperty("cow", "moo");
stunt.setProperty("pig", "oink");
return stunt;
}
@Override
String getPropertiesFileLocation() throws ConfigurationException {
return "this is ignored too";
}
};
Properties properties1 = testee.getProperties();
Properties properties2 = testee.getProperties();
assertTrue(properties1.equals(properties2));
assertFalse(properties1 == properties2);
}
public void testPopulatePropertiesWithNullPath_shouldThrowConfigurationException() throws Exception {
testee = new Configuration();
try {
testee.populateProperties(null);
fail();
} catch (ConfigurationException e) {
// want to be here
}
}
public void testGetPropertiesFileLocation_missingConfigFile_shouldThrowConfigurationException() throws Exception {
testee = new Configuration() {
@Override
InputStream openMetaPropertiesFile() throws FileNotFoundException {
throw new FileNotFoundException("File not found");
}
@Override
public String installLocation() {
return INSTALL_LOCATION;
}
};
try {
testee.getPropertiesFileLocation();
fail();
} catch (ConfigurationException e) {
assertTrue(e.getCause() instanceof FileNotFoundException);
}
}
public void testGetPropertiesFileLocation_ValidConfigFile_success() throws Exception {
final String LOCATION = "configuration.location";
testee = new Configuration() {
@Override
InputStream openMetaPropertiesFile() throws FileNotFoundException {
String propertiesFile = Configuration.CONFIGURATION_KEY + "=" + LOCATION;
return new StringInputStream(propertiesFile);
}
};
String result = testee.getPropertiesFileLocation();
assertEquals("Found result", LOCATION, result);
}
// --------------------------------------------------------------------------
public void testSubstitute_nullInputThrowsIllegalArgumentException() throws Exception {
testee = new Configuration();
try {
testee.substitute(null, null, null);
fail();
} catch (IllegalArgumentException e) {
// success
}
try {
testee.substitute("", "", null);
fail();
} catch (IllegalArgumentException e) {
// success
}
try {
testee.substitute("", null, "");
fail();
} catch (IllegalArgumentException e) {
// success
}
try {
testee.substitute(null, "", "");
fail();
} catch (IllegalArgumentException e) {
// success
}
try {
testee.substitute(null, null, "");
fail();
} catch (IllegalArgumentException e) {
// success
}
try {
testee.substitute("", null, null);
fail();
} catch (IllegalArgumentException e) {
// success
}
try {
testee.substitute(null, "", null);
fail();
} catch (IllegalArgumentException e) {
// success
}
}
public void testSubstitute_substitutionNotFound() throws Exception {
testee = new Configuration();
String source = "Source";
String result = testee.substitute(source, "fubar", "blah");
assertSame("Nothing substituted", source, result);
}
public void testSubstitute_success() throws Exception {
testee = new Configuration();
String source = "Source";
String result = testee.substitute(source, "ourc", "af");
assertEquals("Substituted", "Safe", result);
}
// --------------------------------------------------------------------------
public void testSubstituteVariables_nullInput() throws Exception {
testee = new Configuration();
try {
testee.substituteVariables(null);
fail();
} catch (IllegalArgumentException e) {
// success
}
}
final String INSTALL_LOCATION = "/install/location";
final String USER_LOCATION = "/user/location";
final String WORKSPACE_LOCATION = "/workspace/location";
Configuration substitutionTestee = new Configuration() {
@Override
public String installLocation() {
return INSTALL_LOCATION;
}
@Override
public String userLocation() {
return USER_LOCATION;
}
@Override
public String workspaceLocation() {
return WORKSPACE_LOCATION;
}
};
public void testSubstituteVariables_InstallDir() throws Exception {
String result = substitutionTestee.substituteVariables("some ${install_location} string");
assertTrue("Found ${install_location}", result.indexOf(INSTALL_LOCATION) > 0);
}
public void testSubstituteVariables_UserDir() throws Exception {
String result = substitutionTestee.substituteVariables("some ${user_location} string");
assertTrue("Found ${user_location}", result.indexOf(USER_LOCATION) > 0);
}
public void testSubstituteVariables_WorkspaceDir() throws Exception {
String result = substitutionTestee.substituteVariables("some ${workspace_location} string");
assertTrue("Found ${workspace_location}", result.indexOf(WORKSPACE_LOCATION) > 0);
}
}