From 3c11500618acbf5a7f2e06fa35b4f482f7154976 Mon Sep 17 00:00:00 2001 From: Alessandro Lazarotti Date: Tue, 22 Nov 2011 15:39:42 -0200 Subject: [PATCH] JBPAPP-7491 , JBPAPP-7488 , HHH-6806 Add support for vfsfile and vfszip in JarVisitor The method getVisitor(URL jarUrl, Filter[] filters, String entry) does not have conditionals for protocols vfs based, like vfszip or vfsfile. It returns an InputStreamZippedJarVisitor implementation for both protocols. If it is a vfszip, it will work, but will not not for vfsfile. The fix includes vfszip and vfsfile to JarVisitorFactory, so it can return FileZippedJarVisitor (more efficient than InputStreamZippedJarVisitor) for vfszip and ExplodedJarVisitor for vfsfile --- .../ejb/packaging/JarVisitorFactory.java | 2 +- .../ejb/test/packaging/JarVisitorTest.java | 58 +++++++++++++++---- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/ejb/packaging/JarVisitorFactory.java b/hibernate-entitymanager/src/main/java/org/hibernate/ejb/packaging/JarVisitorFactory.java index 9e53bcbf25..d24decc4e8 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/ejb/packaging/JarVisitorFactory.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/ejb/packaging/JarVisitorFactory.java @@ -162,7 +162,7 @@ public class JarVisitorFactory { if ( "jar".equals( protocol ) ) { return new JarProtocolVisitor( jarUrl, filters, entry ); } - else if ( StringHelper.isEmpty( protocol ) || "file".equals( protocol ) ) { + else if ( StringHelper.isEmpty( protocol ) || "file".equals( protocol ) || "vfszip".equals( protocol ) || "vfsfile".equals( protocol ) ) { File file; try { final String filePart = jarUrl.getFile(); diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/packaging/JarVisitorTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/packaging/JarVisitorTest.java index 9fc9abaa00..0c24a4dcd9 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/packaging/JarVisitorTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/packaging/JarVisitorTest.java @@ -23,15 +23,23 @@ */ package org.hibernate.ejb.test.packaging; -import javax.persistence.Embeddable; -import javax.persistence.Entity; -import javax.persistence.MappedSuperclass; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + import java.io.File; import java.io.IOException; import java.net.URL; import java.net.URLConnection; +import java.net.URLStreamHandler; +import java.net.URLStreamHandlerFactory; import java.util.Set; +import javax.persistence.Embeddable; +import javax.persistence.Entity; +import javax.persistence.MappedSuperclass; + import org.hibernate.ejb.packaging.ClassFilter; import org.hibernate.ejb.packaging.Entry; import org.hibernate.ejb.packaging.ExplodedJarVisitor; @@ -45,15 +53,8 @@ import org.hibernate.ejb.packaging.JarVisitorFactory; import org.hibernate.ejb.packaging.PackageFilter; import org.hibernate.ejb.test.pack.defaultpar.ApplicationServer; import org.hibernate.ejb.test.pack.explodedpar.Carpet; - -import org.junit.Test; - import org.hibernate.testing.TestForIssue; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import org.junit.Test; /** * @author Emmanuel Bernard @@ -220,6 +221,41 @@ public class JarVisitorTest extends PackagingTestCase { localEntry.getInputStream().close(); } } + + @Test + @TestForIssue(jiraKey = "HHH-6806") + public void testJarVisitorFactory() throws Exception{ + + //setting URL to accept vfs based protocol + URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() { + public URLStreamHandler createURLStreamHandler(String protocol) { + if("vfszip".equals(protocol) || "vfsfile".equals(protocol) ) + return new URLStreamHandler() { + protected URLConnection openConnection(URL u) + throws IOException { + return null; + } + }; + return null; + } + }); + + URL jarUrl = new URL ("file:./target/packages/defaultpar.par"); + JarVisitor jarVisitor = JarVisitorFactory.getVisitor(jarUrl, getFilters(), null); + assertEquals(FileZippedJarVisitor.class.getName(), jarVisitor.getClass().getName()); + + jarUrl = new URL ("file:./target/packages/explodedpar"); + jarVisitor = JarVisitorFactory.getVisitor(jarUrl, getFilters(), null); + assertEquals(ExplodedJarVisitor.class.getName(), jarVisitor.getClass().getName()); + + jarUrl = new URL ("vfszip:./target/packages/defaultpar.par"); + jarVisitor = JarVisitorFactory.getVisitor(jarUrl, getFilters(), null); + assertEquals(FileZippedJarVisitor.class.getName(), jarVisitor.getClass().getName()); + + jarUrl = new URL ("vfsfile:./target/packages/explodedpar"); + jarVisitor = JarVisitorFactory.getVisitor(jarUrl, getFilters(), null); + assertEquals(ExplodedJarVisitor.class.getName(), jarVisitor.getClass().getName()); + } @Test @TestForIssue( jiraKey = "EJB-230" )