001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.modeshape.sequencer.zip; 018 019import static org.junit.Assert.assertEquals; 020import static org.junit.Assert.assertNotNull; 021import java.io.EOFException; 022import java.io.IOException; 023import java.util.Map; 024import javax.jcr.Binary; 025import javax.jcr.Node; 026import javax.jcr.RepositoryException; 027import org.junit.Test; 028import org.modeshape.common.util.IoUtil; 029import org.modeshape.jcr.api.JcrConstants; 030import org.modeshape.jcr.api.observation.Event; 031import org.modeshape.jcr.sequencer.AbstractSequencerTest; 032 033/** 034 * Unit test for {@link ZipSequencer} 035 * 036 * @author Horia Chiorean 037 */ 038public class ZipSequencerTest extends AbstractSequencerTest { 039 040 @Test 041 public void shouldSequenceZip1() throws Exception { 042 String filename = "testzip.zip"; 043 createNodeWithContentFromFile(filename, filename); 044 045 Node outputZip = getOutputNode(rootNode, "zip/" + filename); 046 assertNotNull(outputZip); 047 assertEquals(ZipLexicon.FILE, outputZip.getPrimaryNodeType().getName()); 048 049 assertEquals(2, outputZip.getNodes().getSize()); 050 assertFile(outputZip, "test1.txt", "This is a test content of file 1\n"); 051 052 Node folder = outputZip.getNode("test subfolder"); 053 assertEquals(1, folder.getNodes().getSize()); 054 assertEquals(JcrConstants.NT_FOLDER, folder.getPrimaryNodeType().getName()); 055 056 assertFile(folder, "test2.txt", "This is a test content of file2\n"); 057 } 058 059 private void assertFile( Node parentNode, 060 String relativePath, 061 String expectedContent ) throws RepositoryException, IOException { 062 Node file = parentNode.getNode(relativePath); 063 assertNotNull(file); 064 assertEquals(1, file.getNodes().getSize()); 065 assertEquals(JcrConstants.NT_FILE, file.getPrimaryNodeType().getName()); 066 Node fileContent = file.getNode(JcrConstants.JCR_CONTENT); 067 assertNotNull(fileContent); 068 assertEquals("text/plain", fileContent.getProperty(JcrConstants.JCR_MIME_TYPE).getString()); 069 Binary fileData = fileContent.getProperty(JcrConstants.JCR_DATA).getBinary(); 070 assertNotNull(fileData); 071 if (expectedContent != null) { 072 assertEquals(expectedContent, IoUtil.read(fileData.getStream())); 073 } 074 } 075 076 @Test 077 public void shouldSequenceZip2() throws Exception { 078 String filename = "test-files.zip"; 079 Node parent = createNodeWithContentFromFile(filename, filename); 080 Node outputNode = parent.getNode("jcr:content"); 081 082 Node outputZip = getOutputNode(rootNode, "zip/" + filename); 083 assertNotNull(outputZip); 084 assertEquals(ZipLexicon.FILE, outputZip.getPrimaryNodeType().getName()); 085 assertSequencingEventInfo(outputNode, session.getUserID(), "ZIP sequencer", outputNode.getPath(), "/zip"); 086 087 // Find the sequenced node ... 088 String path = "/zip/test-files.zip"; 089 assertNode(path + "/MODE-966-fix.patch", JcrConstants.NT_FILE); 090 assertNode(path + "/MODE-966-fix.patch/jcr:content", JcrConstants.NT_RESOURCE); 091 assertNode(path + "/testFolder", JcrConstants.NT_FOLDER); 092 assertNode(path + "/testFolder/MODE-962-fix.patch", JcrConstants.NT_FILE); 093 assertNode(path + "/testFolder/MODE-962-fix.patch/jcr:content", JcrConstants.NT_RESOURCE); 094 assertNode(path + "/testFolder/testInnerFolder", JcrConstants.NT_FOLDER); 095 assertNode(path + "/testFolder/testInnerFolder/MODE-960-fix.patch", JcrConstants.NT_FILE); 096 assertNode(path + "/testFolder/testInnerFolder/MODE-960-fix.patch/jcr:content", JcrConstants.NT_RESOURCE); 097 assertNode(path + "/testFolder/testInnerFolder/MODE-960-fix2.patch", JcrConstants.NT_FILE); 098 assertNode(path + "/testFolder/testInnerFolder/MODE-960-fix2.patch/jcr:content", JcrConstants.NT_RESOURCE); 099 } 100 101 @Test 102 public void shouldFailIfZipCorrupted() throws Throwable { 103 String filename = "corrupt.zip"; 104 Node parent = createNodeWithContentFromFile(filename, filename); 105 Node outputNode = parent.getNode("jcr:content"); 106 expectSequencingFailure(outputNode); 107 108 Map<?, ?> sequencingEventInfo = assertSequencingEventInfo(outputNode, 109 session.getUserID(), 110 "ZIP sequencer", 111 outputNode.getPath(), 112 "/zip"); 113 assertEquals(EOFException.class.getName(), sequencingEventInfo.get(Event.Sequencing.SEQUENCING_FAILURE_CAUSE) 114 .getClass() 115 .getName()); 116 } 117 118}