1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.configuration; 19 20 import java.io.File; 21 import java.net.MalformedURLException; 22 import java.net.URL; 23 import java.util.Iterator; 24 25 import junit.framework.Assert; 26 27 /** 28 * Assertions on configurations for the unit tests. This class also provides 29 * access to test files. 30 * 31 * @author Emmanuel Bourg 32 * @version $Id: ConfigurationAssert.java 1221893 2011-12-21 21:34:38Z oheger $ 33 */ 34 public class ConfigurationAssert 35 { 36 /** Constant for the name of the directory with the test files. */ 37 public static final String TEST_DIR_NAME = "target/test-classes"; 38 39 /** Constant for the name of the directory with the output files. */ 40 public static final String OUT_DIR_NAME = "target"; 41 42 /** The directory with the test files. */ 43 public static final File TEST_DIR = new File(TEST_DIR_NAME); 44 45 /** The directory with the output files. */ 46 public static final File OUT_DIR = new File(OUT_DIR_NAME); 47 48 /** 49 * Checks the content of a configuration. 50 * 51 * @param expected the expected properties 52 * @param actual the configuration to check 53 */ 54 public static void assertEquals(Configuration expected, Configuration actual) 55 { 56 // check that the actual configuration contains all the properties of the expected configuration 57 for (Iterator<String> it = expected.getKeys(); it.hasNext();) 58 { 59 String key = it.next(); 60 Assert.assertTrue("The actual configuration doesn't contain the expected key '" + key + "'", actual.containsKey(key)); 61 Assert.assertEquals("Value of the '" + key + "' property", expected.getProperty(key), actual.getProperty(key)); 62 } 63 64 // check that the actual configuration has no extra properties 65 for (Iterator<String> it = actual.getKeys(); it.hasNext();) 66 { 67 String key = it.next(); 68 Assert.assertTrue("The actual configuration contains an extra key '" + key + "'", expected.containsKey(key)); 69 } 70 } 71 72 /** 73 * Returns a {@code File} object for the specified test file. 74 * 75 * @param name the name of the test file 76 * @return a {@code File} object pointing to that test file 77 */ 78 public static File getTestFile(String name) 79 { 80 return new File(TEST_DIR, name); 81 } 82 83 /** 84 * Returns a {@code File} object for the specified out file. 85 * 86 * @param name the name of the out file 87 * @return a {@code File} object pointing to that out file 88 */ 89 public static File getOutFile(String name) 90 { 91 return new File(OUT_DIR, name); 92 } 93 94 /** 95 * Returns a URL pointing to the specified test file. If the URL cannot be 96 * constructed, a runtime exception is thrown. 97 * 98 * @param name the name of the test file 99 * @return the corresponding URL 100 */ 101 public static URL getTestURL(String name) 102 { 103 return urlFromFile(getTestFile(name)); 104 } 105 106 /** 107 * Returns a URL pointing to the specified output file. If the URL cannot be 108 * constructed, a runtime exception is thrown. 109 * 110 * @param name the name of the output file 111 * @return the corresponding URL 112 */ 113 public static URL getOutURL(String name) 114 { 115 return urlFromFile(getOutFile(name)); 116 } 117 118 /** 119 * Helper method for converting a file to a URL. 120 * 121 * @param file the file 122 * @return the corresponding URL 123 * @throws ConfigurationRuntimeException if the URL cannot be constructed 124 */ 125 private static URL urlFromFile(File file) 126 { 127 try 128 { 129 return file.toURI().toURL(); 130 } 131 catch (MalformedURLException mex) 132 { 133 throw new ConfigurationRuntimeException(mex); 134 } 135 } 136 }