1b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex/** 2b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex * Copyright (c) 2008, http://www.snakeyaml.org 3b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex * 4b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex * Licensed under the Apache License, Version 2.0 (the "License"); 5b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex * you may not use this file except in compliance with the License. 6b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex * You may obtain a copy of the License at 7b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex * 8b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex * http://www.apache.org/licenses/LICENSE-2.0 9b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex * 10b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex * Unless required by applicable law or agreed to in writing, software 11b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex * distributed under the License is distributed on an "AS IS" BASIS, 12b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex * See the License for the specific language governing permissions and 14b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex * limitations under the License. 15b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex */ 16b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalexpackage org.yaml.snakeyaml.issues.issue318; 17b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 18b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovaleximport static org.junit.Assert.assertEquals; 19b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovaleximport static org.junit.Assert.fail; 20b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 21b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovaleximport java.io.File; 22b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovaleximport java.io.IOException; 23b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovaleximport java.io.InputStream; 24b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovaleximport java.lang.reflect.InvocationTargetException; 25b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovaleximport java.lang.reflect.Method; 26b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovaleximport java.net.MalformedURLException; 27b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovaleximport java.net.URL; 28b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovaleximport java.net.URLClassLoader; 29b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovaleximport java.util.Properties; 30b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 31b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovaleximport org.junit.After; 32b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovaleximport org.junit.Before; 33b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovaleximport org.junit.Test; 34b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovaleximport org.yaml.snakeyaml.Yaml; 35b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 36b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalexpublic class ContextClassLoaderTest { 37b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 38b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex static public class DomainBean { 39b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 40b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex private int value = 0; 41b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 42b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex public void setValue(int value) { 43b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex this.value = value; 44b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex } 45b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 46b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex public int getValue() { 47b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex return value; 48b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex } 49b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 50b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex @Override 51b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex public int hashCode() { 52b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex final int prime = 31; 53b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex int result = 1; 54b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex result = prime * result + value; 55b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex return result; 56b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex } 57b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 58b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex @Override 59b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex public boolean equals(Object obj) { 60b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex if (this == obj) 61b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex return true; 62b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex if (obj == null) 63b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex return false; 64b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex if (getClass() != obj.getClass()) 65b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex return false; 66b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex DomainBean other = (DomainBean) obj; 67b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex if (value != other.value) 68b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex return false; 69b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex return true; 70b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex } 71b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 72b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex } 73b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 74b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex private URLClassLoader yamlCL; 75b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 76b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex @Before 77b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex public void before() throws MalformedURLException { 78b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex Properties classpath = new Properties(); 7951cccc6eebd9aa1dca3c2b0179dbf9d14b3b6804maslovalex InputStream cpProperties = getClass().getResourceAsStream("classpath.properties"); 80b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex try { 81b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex classpath.load(cpProperties); 82b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex } catch (IOException e2) { 83b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex fail(e2.getLocalizedMessage()); 84b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex } 85b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 8651cccc6eebd9aa1dca3c2b0179dbf9d14b3b6804maslovalex File runtimeClassesDir = new File(classpath.getProperty("runtime_classes_dir")); 87b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 8851cccc6eebd9aa1dca3c2b0179dbf9d14b3b6804maslovalex yamlCL = new URLClassLoader(new URL[] { runtimeClassesDir.toURI().toURL() }, null); 89b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex } 90b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 91b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex @After 92b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex public void after() { 932622cb018b159efaf5bf62f93ae4dce9e0356c91maslovalex // URLClassLoader.close is @since 1.7 942622cb018b159efaf5bf62f93ae4dce9e0356c91maslovalex // if (yamlCL != null) { 952622cb018b159efaf5bf62f93ae4dce9e0356c91maslovalex // try { 962622cb018b159efaf5bf62f93ae4dce9e0356c91maslovalex // yamlCL.close(); 972622cb018b159efaf5bf62f93ae4dce9e0356c91maslovalex // } catch (IOException e) { 982622cb018b159efaf5bf62f93ae4dce9e0356c91maslovalex // e.printStackTrace(); 992622cb018b159efaf5bf62f93ae4dce9e0356c91maslovalex // } finally { 1002622cb018b159efaf5bf62f93ae4dce9e0356c91maslovalex yamlCL = null; 1012622cb018b159efaf5bf62f93ae4dce9e0356c91maslovalex // } 1022622cb018b159efaf5bf62f93ae4dce9e0356c91maslovalex // } 103b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex } 104b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 105b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex @Test(expected = ClassNotFoundException.class) 106b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex public void expectNoDomainClassInYamlCL() throws ClassNotFoundException { 107b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex yamlCL.loadClass(DomainBean.class.getName()); 108b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex } 109b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 110b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex @Test 111b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex public void yamlClassInYAMLCL() throws ClassNotFoundException { 112b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex yamlCL.loadClass(Yaml.class.getName()); 113b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex } 114b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 115b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex @Test 11651cccc6eebd9aa1dca3c2b0179dbf9d14b3b6804maslovalex public void domainInDifferentConstructor() throws ClassNotFoundException, 11751cccc6eebd9aa1dca3c2b0179dbf9d14b3b6804maslovalex InstantiationException, IllegalAccessException, NoSuchMethodException, 11851cccc6eebd9aa1dca3c2b0179dbf9d14b3b6804maslovalex SecurityException, IllegalArgumentException, InvocationTargetException { 119b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 120b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex Class<?> yamlClass = yamlCL.loadClass(Yaml.class.getName()); 121b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 122b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex DomainBean bean = new DomainBean(); 123b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex bean.setValue(13); 124b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 125b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex Object yaml = yamlClass.newInstance(); 126b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 12751cccc6eebd9aa1dca3c2b0179dbf9d14b3b6804maslovalex Method dumpMethod = yaml.getClass().getMethod("dump", new Class<?>[] { Object.class }); 128b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex String dump = dumpMethod.invoke(yaml, bean).toString(); 129b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 13051cccc6eebd9aa1dca3c2b0179dbf9d14b3b6804maslovalex Method loadMethod = yaml.getClass().getMethod("load", new Class<?>[] { String.class }); 131b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex DomainBean object = (DomainBean) loadMethod.invoke(yaml, dump); 132b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 133b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex assertEquals(bean, object); 134b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex } 135b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex 136b0454efb20f13cd24450c0bb6d9c5cdda56e0616maslovalex} 137