Uploaded image for project: 'PUBLIC - Liferay Faces'
  1. PUBLIC - Liferay Faces
  2. FACES-3419

UploadedFileFactory.getUploadedFile(Exception) throws ClassNotFoundException when commons-fileupload is not on the classpath

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: util-3.1.0
    • Fix Version/s: util-3.2.0
    • Component/s: Liferay Faces Util
    • Labels:
      None

      Description

      Steps to reproduce:

      1. Create a file called UploadedFileFactoryTest.java under src/test/java with the following contents:
        public final class UploadedFileFactoryTest {
        
        	private static boolean isCommonsClassNotFound(Throwable t) {
        
        		while (!(t instanceof CommonsClassNotFoundException) && (t != null)) {
        			t = t.getCause();
        		}
        
        		return (t instanceof CommonsClassNotFoundException);
        	}
        
        	@Test
        	public void testFACES_XXXX() throws Throwable {
        
        		try {
        
        			Class<?> testClass = getClass();
        			ClassLoader classLoader = new ClassLoaderNoCommonsImpl(testClass.getResource("/"),
        					testClass.getClassLoader());
        			Class<?> uploadedFileFactoryImplClass = classLoader.loadClass(UploadedFileFactoryImpl.class.getName());
        			Object uploadedFileFactoryImpl = uploadedFileFactoryImplClass.newInstance();
        			Method getUploadedFileMethod = uploadedFileFactoryImplClass.getMethod("getUploadedFile", Exception.class);
        			Class<?> ioExceptionClass = classLoader.loadClass(IOException.class.getName());
        			Object ioException = ioExceptionClass.newInstance();
        			getUploadedFileMethod.invoke(uploadedFileFactoryImpl, ioException);
        		}
        		catch (Throwable t) {
        
        			if (isCommonsClassNotFound(t)) {
        				throw new AssertionError("", t);
        			}
        			else {
        				throw t;
        			}
        		}
        	}
        
        	private static final class ClassLoaderNoCommonsImpl extends URLClassLoader {
        
        		public ClassLoaderNoCommonsImpl(URL testClassesURL, ClassLoader classLoader) throws MalformedURLException {
        			super(new URL[] { new URL(testClassesURL.toString().replace("/test-classes", "/classes")), testClassesURL },
        				classLoader);
        		}
        
        		@Override
        		public Class<?> loadClass(String className) throws ClassNotFoundException {
        
        			if (className.startsWith("org.apache.commons.")) {
        				throw new CommonsClassNotFoundException(className);
        			}
        
        			if (className.startsWith("java.") || className.startsWith("javax.")) {
        				return super.loadClass(className);
        			}
        			else {
        				return super.findClass(className);
        			}
        		}
        	}
        
        	private static final class CommonsClassNotFoundException extends ClassNotFoundException {
        
        		// serialVersionUID
        		private static final long serialVersionUID = 2154466466767297423L;
        
        		public CommonsClassNotFoundException(String s) {
        			super(s);
        		}
        	}
        }
        
      2. Run the test:
        mvn clean test -Dtest=*UploadedFileFactoryTest*
        

      If the bug still exists, the test will fail:

      -------------------------------------------------------
       T E S T S
      -------------------------------------------------------
      Running com.liferay.faces.util.model.UploadedFileFactoryTest
      Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.043 sec <<< FAILURE!
      testFACES_XXXX(com.liferay.faces.util.model.UploadedFileFactoryTest)  Time elapsed: 0.01 sec  <<< FAILURE!
      java.lang.AssertionError:
              at com.liferay.faces.util.model.UploadedFileFactoryTest.testFACES_XXXX(UploadedFileFactoryTest.java:61)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:498)
              at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
              at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
              at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
              at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
              at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
              at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
              at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
              at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
              at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
              at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
              at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
              at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
              at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
              at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:236)
              at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:134)
              at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:113)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:498)
              at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
              at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
              at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
              at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:103)
              at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:74)
      Caused by: java.lang.reflect.InvocationTargetException
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:498)
              at com.liferay.faces.util.model.UploadedFileFactoryTest.testFACES_XXXX(UploadedFileFactoryTest.java:56)
              ... 29 more
      Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileUploadException
              at com.liferay.faces.util.model.internal.UploadedFileFactoryImpl.getUploadedFile(UploadedFileFactoryImpl.java:45)
              ... 34 more
      Caused by: com.liferay.faces.util.model.UploadedFileFactoryTest$CommonsClassNotFoundException: org.apache.commons.fileupload.FileUploadException
              at com.liferay.faces.util.model.UploadedFileFactoryTest$ClassLoaderNoCommonsImpl.loadClass(UploadedFileFactoryTest.java:80)
              ... 35 more
      
      Results :
      
      Failed tests:   testFACES_XXXX(com.liferay.faces.util.model.UploadedFileFactoryTest)
      
      Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
      

      If the bug is fixed, the test will pass.

        Attachments

          Activity

            People

            • Assignee:
              kyle.stiemann Kyle Stiemann (Inactive)
              Reporter:
              kyle.stiemann Kyle Stiemann (Inactive)
              Participants of an Issue:
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Packages

                Version Package
                util-3.2.0