Java tutorial
/** * Copyright (C) 2007 Asterios Raptis * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sourceforge.jaulp.lang; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; import java.net.URL; import java.util.List; import java.util.Properties; import java.util.Set; import org.jaulp.test.objects.Person; import org.jaulp.test.objects.PremiumMember; import org.testng.AssertJUnit; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class ClassUtilsTest { /** The result. */ private boolean result; @BeforeMethod public void setUp() throws Exception { } @AfterMethod public void tearDown() throws Exception { } @Test(enabled = true) public void testGetSuperClass() { Class<?> actual = ClassUtils.getBaseClass(PremiumMember.class); AssertJUnit.assertEquals(Person.class, actual); actual = ClassUtils.getBaseClass(Object.class); AssertJUnit.assertEquals(Object.class, actual); } /** * Test method for. * * {@link net.sourceforge.jaulp.lang.ClassUtils#getClassnameWithSuffix(java.lang.Object)}. */ @Test(enabled = true) public void testGetClassnameWithSuffix() { final String expected = "ClassUtilsTest.class"; final String classname = ClassUtils.getClassnameWithSuffix(this); this.result = expected.equals(classname); AssertJUnit.assertTrue("", this.result); } @Test public void testGetPath() { String expected = "/java/lang/Object.class"; String actual = ClassUtils.getPath(Object.class); AssertJUnit.assertEquals(expected, actual); expected = "/java/lang/Class.class"; actual = ClassUtils.getPath(Class.class); AssertJUnit.assertEquals(expected, actual); } @Test public void testGetURL() { URL actual = ClassUtils.getURL(Object.class); AssertJUnit.assertTrue(actual.toString().startsWith("jar:file:")); AssertJUnit.assertTrue(actual.toString().endsWith("/java/lang/Object.class")); } @Test public void testGetManifestURL() { String actual = ClassUtils.getManifestUrl(Object.class); AssertJUnit.assertTrue(actual.toString().startsWith("jar:file:")); AssertJUnit.assertTrue(actual.toString().endsWith("/jre/lib/rt.jar!/META-INF/MANIFEST.MF")); actual = ClassUtils.getManifestUrl(ClassUtils.class); AssertJUnit.assertNull(actual); } @Test public void testGetResourceString() { } @Test(enabled = true) public void testGetResourceAsFileString() throws URISyntaxException { final String propertiesFilename = "net/sourceforge/jaulp/lang/resources.properties"; final File file = ClassUtils.getResourceAsFile(propertiesFilename); this.result = file != null; AssertJUnit.assertTrue("File should not be null", this.result); AssertJUnit.assertTrue("File should exist.", file.exists()); } /** * Test method for {@link net.sourceforge.jaulp.lang.ClassUtils#getResourceAsStream(java.lang.Class, java.lang.String)}. * * @throws IOException * Signals that an I/O exception has occurred. */ @Test(enabled = false) public void testGetRessourceAsStream() throws IOException { final String propertiesFilename = "resources.properties"; final String pathFromObject = PackageUtils.getPackagePathWithSlash(this); final String path = pathFromObject + propertiesFilename; final ClassUtilsTest obj = new ClassUtilsTest(); final InputStream is = ClassUtils.getResourceAsStream(obj.getClass(), path); this.result = is != null; AssertJUnit.assertTrue("InputStream should not be null", this.result); final Properties prop = new Properties(); prop.load(is); this.result = prop.size() == 3; AssertJUnit.assertTrue("Size of prop should be 3.", this.result); } /** * Test method for. * * @throws IOException * Signals that an I/O exception has occurred. * {@link net.sourceforge.jaulp.lang.ClassUtils#getResourceAsStream(java.lang.String)} * . */ @Test(enabled = true) public void testGetResourceAsStreamString() throws IOException { final String propertiesFilename = "net/sourceforge/jaulp/lang/resources.properties"; final InputStream is = ClassUtils.getResourceAsStream(propertiesFilename); this.result = is != null; AssertJUnit.assertTrue("", this.result); final Properties prop = new Properties(); prop.load(is); this.result = prop.size() == 3; } @Test(enabled = true) public void testGetResource() { final String propertiesFilename = "net/sourceforge/jaulp/lang/resources.properties"; URL url = ClassUtils.getResource(propertiesFilename); this.result = url != null; AssertJUnit.assertTrue("", this.result); } @Test(enabled = true) public void testGetRessource() { final String propertiesFilename = "resources.properties"; final URL url = ClassUtils.getResource(ClassUtilsTest.class, propertiesFilename); this.result = url != null; AssertJUnit.assertTrue("", this.result); } @Test(enabled = true) public void testGetResourceStringObject() { final String propertiesFilename = "resources.properties"; final ClassUtilsTest obj = new ClassUtilsTest(); final URL url = ClassUtils.getResource(propertiesFilename, obj); this.result = url != null; AssertJUnit.assertTrue("", this.result); } @Test(enabled = true) public void testScanClassesFromPackage() throws Exception, IOException { List<File> directories = ClassUtils.getDirectoriesFromResources("net.sourceforge.jaulp.lang", true); Set<Class<?>> foundClasses = ClassUtils.scanClassesFromPackage(directories.get(0), "net.sourceforge.jaulp.lang"); AssertJUnit.assertTrue("", foundClasses.contains(ClassUtilsTest.class)); Set<Class<?>> list = null; list = ClassUtils.scanClassNames("net.sourceforge.jaulp.lang"); for (Class<?> entry : list) { System.out.println(entry); } System.out.println("#####################################"); list = ClassUtils.scanClassNames("org.apache.commons.beanutils"); for (Class<?> entry : list) { System.out.println(entry); } System.out.println("#####################################"); list = ClassUtils.scanClassNames("org.apache.commons.beanutils", true); for (Class<?> entry : list) { System.out.println(entry); } } }