Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,13 @@ protected void writeInputStreamToFile(InputStream input, String filename) throws
File parent = targetFile.getParentFile();
if (!parent.exists())
parent.mkdirs();
OutputStream output = Files.newOutputStream(targetFile.toPath());
IOUtil.copyCompletely(input, output);
// copyCompletely() closes both streams, but it never runs if the target
// cannot be opened - and callers hand us a live download to close
try (InputStream in = input;
OutputStream output = Files.newOutputStream(targetFile.toPath()))
{
IOUtil.copyCompletely(in, output);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* 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.xmlbeans.impl.tool;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class SchemaResourceManagerWriteTest {

private static class TrackingInputStream extends ByteArrayInputStream {
private boolean closed;

TrackingInputStream(byte[] bytes) {
super(bytes);
}

@Override
public void close() throws IOException {
closed = true;
super.close();
}
}

@Test
void closesInputWhenTheTargetCannotBeOpened(@TempDir File dir) throws IOException {
// a plain file where the target's parent directory should be, so opening
// the output fails
File blocker = new File(dir, "blocker");
assertTrue(blocker.createNewFile());

SchemaResourceManager manager = new SchemaResourceManager(dir);
TrackingInputStream input =
new TrackingInputStream("<xsd/>".getBytes(StandardCharsets.UTF_8));

assertThrows(IOException.class,
() -> manager.writeInputStreamToFile(input, "blocker/child.xsd"));
assertTrue(input.closed, "the caller's stream should have been closed");
}
}
Loading