Skip to content

Commit

Permalink
[JBVFS-125]; test temp store.
Browse files Browse the repository at this point in the history
  • Loading branch information
alesj committed Oct 2, 2009
1 parent 18c896f commit 785066a
Show file tree
Hide file tree
Showing 6 changed files with 383 additions and 0 deletions.
@@ -0,0 +1,63 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.virtual.plugins.copy;

import java.io.File;

import org.jboss.virtual.spi.TempStore;
import org.jboss.virtual.VirtualFile;

/**
* Delete on exit temp store wrapper.
*
* @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
*/
public class DeleteOnExitTempStore implements TempStore
{
private TempStore delegate;

public DeleteOnExitTempStore(TempStore delegate)
{
if (delegate == null)
throw new IllegalArgumentException("Null delegate");
this.delegate = delegate;
}

public File createTempFolder(VirtualFile file)
{
File dir = delegate.createTempFolder(file);
dir.deleteOnExit();
return dir;
}

public File createTempFolder(String outerName, String innerName)
{
File dir = delegate.createTempFolder(outerName, innerName);
dir.deleteOnExit();
return dir;
}

public void clear()
{
// do nothing
}
}
74 changes: 74 additions & 0 deletions src/main/java/org/jboss/virtual/plugins/copy/MkdirTempStore.java
@@ -0,0 +1,74 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.virtual.plugins.copy;

import java.io.File;

import org.jboss.virtual.spi.TempStore;
import org.jboss.virtual.VirtualFile;

/**
* Mkdir temp store wrapper.
*
* @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
*/
public class MkdirTempStore implements TempStore
{
private TempStore delegate;

public MkdirTempStore(TempStore delegate)
{
if (delegate == null)
throw new IllegalArgumentException("Null delegate");
this.delegate = delegate;
}

/**
* Assert directory creation.
*
* @param dir the current directory
*/
protected void assertMkdir(File dir)
{
if (dir.mkdir() == false)
throw new IllegalArgumentException("Cannot create directory: " + dir);
}

public File createTempFolder(VirtualFile file)
{
File dir = delegate.createTempFolder(file);
assertMkdir(dir);
return dir;
}

public File createTempFolder(String outerName, String innerName)
{
File dir = delegate.createTempFolder(outerName, innerName);
assertMkdir(dir);
return dir;
}

public void clear()
{
delegate.clear();
}
}
@@ -0,0 +1,83 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.virtual.plugins.copy;

import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.jboss.util.collection.ConcurrentSet;
import org.jboss.util.file.Files;
import org.jboss.virtual.VirtualFile;
import org.jboss.virtual.spi.TempStore;

/**
* Track files temp store.
*
* @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
*/
public class TrackingTempStore implements TempStore
{
private TempStore delegate;
private Set<File> files;

public TrackingTempStore(TempStore delegate)
{
if (delegate == null)
throw new IllegalArgumentException("Null delegate");
this.delegate = delegate;
this.files = new ConcurrentSet<File>();
}

/**
* Get files.
*
* @return the files
*/
public Set<File> getFiles()
{
return Collections.unmodifiableSet(files);
}

public File createTempFolder(VirtualFile file)
{
File dir = delegate.createTempFolder(file);
files.add(dir);
return dir;
}

public File createTempFolder(String outerName, String innerName)
{
File dir = delegate.createTempFolder(outerName, innerName);
files.add(dir);
return dir;
}

public void clear()
{
Set<File> copy = new HashSet<File>(files);
files.clear();
for (File dir : copy)
Files.delete(dir);
}
}
57 changes: 57 additions & 0 deletions src/test/java/org/jboss/test/virtual/support/MockTempStore.java
@@ -0,0 +1,57 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.test.virtual.support;

import java.io.File;

import org.jboss.virtual.VirtualFile;
import org.jboss.virtual.plugins.copy.AbstractCopyMechanism;
import org.jboss.virtual.spi.TempStore;

/**
* @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
*/
public class MockTempStore implements TempStore
{
private File tempRoot = AbstractCopyMechanism.getTempDirectory();
private long seed;

public MockTempStore(long seed)
{
this.seed = seed;
}

public File createTempFolder(VirtualFile file)
{
String name = file.getName();
return new File(tempRoot, name + '_' + seed);
}

public File createTempFolder(String outerName, String innerName)
{
return new File(tempRoot, innerName + '_' + seed);
}

public void clear()
{
}
}
104 changes: 104 additions & 0 deletions src/test/java/org/jboss/test/virtual/test/TempStoreTestCase.java
@@ -0,0 +1,104 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2006, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.test.virtual.test;

import java.io.File;
import java.net.URL;

import junit.framework.Test;
import org.jboss.test.virtual.support.MockTempStore;
import org.jboss.virtual.VFS;
import org.jboss.virtual.VirtualFile;
import org.jboss.virtual.VFSUtils;
import org.jboss.virtual.plugins.copy.AbstractCopyMechanism;
import org.jboss.virtual.plugins.copy.DeleteOnExitTempStore;
import org.jboss.virtual.plugins.copy.MkdirTempStore;
import org.jboss.virtual.plugins.copy.TrackingTempStore;
import org.jboss.virtual.spi.TempStore;

/**
* Test TempStore usage.
*
* @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
*/
public class TempStoreTestCase extends AbstractVFSTest
{
public TempStoreTestCase(String s)
{
super(s);
}

public static Test suite()
{
return suite(TempStoreTestCase.class);
}

public void testCopyMechanism() throws Throwable
{
long seed = System.nanoTime();
URL url = getResource("/vfs/test");
VFS vfs = VFS.getVFS(url);
TempStore store = new MkdirTempStore(new DeleteOnExitTempStore(new MockTempStore(seed)));
vfs.setTempStore(store);
VirtualFile file = vfs.getChild("jar1.jar");
VirtualFile temp = VFSUtils.explode(file);
try
{
File tempRoot = AbstractCopyMechanism.getTempDirectory();
File test = new File(tempRoot, "jar1.jar" + '_' + seed);
assertTrue(test.exists()); // should be created by MockTS
}
finally
{
temp.cleanup();
}
}

public void testNestedZip() throws Throwable
{
long seed = System.nanoTime();
URL url = getResource("/vfs/test/nested");
VFS vfs = VFS.getVFS(url);
VFSUtils.enableCopy(vfs);
TempStore store = new MkdirTempStore(new TrackingTempStore(new MockTempStore(seed)));
try
{
vfs.setTempStore(store);
VirtualFile file = vfs.getChild("nested.jar");
assertNotNull(file.getChild("complex.jar/subfolder/subchild"));
try
{
File tempRoot = AbstractCopyMechanism.getTempDirectory();
File test = new File(tempRoot, "complex.jar" + '_' + seed);
assertTrue(test.exists()); // should be created by MockTS
}
finally
{
file.cleanup();
}
}
finally
{
store.clear();
}
}
}
Expand Up @@ -100,6 +100,8 @@ public static Test suite()
suite.addTest(CombinedVFSCacheTestCase.suite());
// exception handler
suite.addTest(ExceptionHandlerTestCase.suite());
// temp store
suite.addTest(TempStoreTestCase.suite());
// operations
suite.addTest(TempCleanupUnitTestCase.suite());
suite.addTest(ExplodeCleanupUnitTestCase.suite());
Expand Down

0 comments on commit 785066a

Please sign in to comment.