Skip to content

Commit

Permalink
Merge pull request #1 from Buuhuu/bugfix/SLING-7358
Browse files Browse the repository at this point in the history
SLING-7358: FileDistributionPackageBuilder temp directory fallback
  • Loading branch information
tteofili committed Jan 8, 2018
2 parents d8d25fe + 352010e commit 36c7bf8
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,15 @@ public FileDistributionPackageBuilder(String type,
this.distributionContentSerializer = distributionContentSerializer;
this.nodeFilters = VltUtils.parseFilters(nodeFilters);
this.propertyFilters = VltUtils.parseFilters(propertyFilters);
this.tempDirectory = VltUtils.getTempFolder(tempFilesFolder);
this.digestAlgorithm = digestAlgorithm;

File tempDirectory = VltUtils.getTempFolder(tempFilesFolder);

if (tempDirectory == null) {
tempDirectory = new File(System.getProperty("java.io.tmpdir"));
}

this.tempDirectory = tempDirectory;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.sling.distribution.packaging.impl;

import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.distribution.DistributionRequest;
import org.apache.sling.distribution.common.DistributionException;
import org.apache.sling.distribution.packaging.DistributionPackage;
import org.apache.sling.distribution.serialization.DistributionContentSerializer;
import org.apache.sling.distribution.serialization.DistributionExportOptions;
import org.junit.Test;

public class FileDistributionPackageBuilderTest {

@Test
public void testDefaultTempDirectory() throws DistributionException, IOException {
FileDistributionPackageBuilder builder = new FileDistributionPackageBuilder("test", new TestSerializer(), null, null, new String[0],
new String[0]);
DistributionPackage createdPackage = builder.createPackageForAdd(mock(ResourceResolver.class), mock(DistributionRequest.class));

try {
assertNotNull(createdPackage.createInputStream());
DistributionPackage gotPackage = builder.getPackageInternal(mock(ResourceResolver.class), createdPackage.getId());
assertNotNull(gotPackage.createInputStream()); // this will throw an exception when the file doesn't exist
} finally {
createdPackage.delete();
}
}

class TestSerializer implements DistributionContentSerializer {

@Override public void exportToStream(ResourceResolver resourceResolver, DistributionExportOptions exportOptions,
OutputStream outputStream) throws DistributionException {
try {
outputStream.write("test".getBytes());
} catch (IOException ex) {
throw new DistributionException(ex);
}
}

@Override public void importFromStream(ResourceResolver resourceResolver, InputStream inputStream) throws DistributionException {
throw new DistributionException("unsupported");
}

@Override public String getName() {
return "test";
}

@Override public boolean isRequestFiltering() {
return true;
}
}
}

0 comments on commit 36c7bf8

Please sign in to comment.