001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.configuration; 019 020 import java.io.File; 021 import java.net.MalformedURLException; 022 import java.net.URL; 023 import java.util.Iterator; 024 025 import junit.framework.Assert; 026 027 /** 028 * Assertions on configurations for the unit tests. This class also provides 029 * access to test files. 030 * 031 * @author Emmanuel Bourg 032 * @version $Id: ConfigurationAssert.java 1221893 2011-12-21 21:34:38Z oheger $ 033 */ 034 public class ConfigurationAssert 035 { 036 /** Constant for the name of the directory with the test files. */ 037 public static final String TEST_DIR_NAME = "target/test-classes"; 038 039 /** Constant for the name of the directory with the output files. */ 040 public static final String OUT_DIR_NAME = "target"; 041 042 /** The directory with the test files. */ 043 public static final File TEST_DIR = new File(TEST_DIR_NAME); 044 045 /** The directory with the output files. */ 046 public static final File OUT_DIR = new File(OUT_DIR_NAME); 047 048 /** 049 * Checks the content of a configuration. 050 * 051 * @param expected the expected properties 052 * @param actual the configuration to check 053 */ 054 public static void assertEquals(Configuration expected, Configuration actual) 055 { 056 // check that the actual configuration contains all the properties of the expected configuration 057 for (Iterator<String> it = expected.getKeys(); it.hasNext();) 058 { 059 String key = it.next(); 060 Assert.assertTrue("The actual configuration doesn't contain the expected key '" + key + "'", actual.containsKey(key)); 061 Assert.assertEquals("Value of the '" + key + "' property", expected.getProperty(key), actual.getProperty(key)); 062 } 063 064 // check that the actual configuration has no extra properties 065 for (Iterator<String> it = actual.getKeys(); it.hasNext();) 066 { 067 String key = it.next(); 068 Assert.assertTrue("The actual configuration contains an extra key '" + key + "'", expected.containsKey(key)); 069 } 070 } 071 072 /** 073 * Returns a {@code File} object for the specified test file. 074 * 075 * @param name the name of the test file 076 * @return a {@code File} object pointing to that test file 077 */ 078 public static File getTestFile(String name) 079 { 080 return new File(TEST_DIR, name); 081 } 082 083 /** 084 * Returns a {@code File} object for the specified out file. 085 * 086 * @param name the name of the out file 087 * @return a {@code File} object pointing to that out file 088 */ 089 public static File getOutFile(String name) 090 { 091 return new File(OUT_DIR, name); 092 } 093 094 /** 095 * Returns a URL pointing to the specified test file. If the URL cannot be 096 * constructed, a runtime exception is thrown. 097 * 098 * @param name the name of the test file 099 * @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 }