-
Notifications
You must be signed in to change notification settings - Fork 775
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply IDE suggestions, code-formating, tests, ...
Add test for DefaultTempFileCreationStrategy Adjust comments, add test, improve error message git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1923054 13f79535-47bb-0310-9956-ffa450edef68
- Loading branch information
Showing
6 changed files
with
192 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,12 +108,14 @@ protected ValueEval evaluate(String arg) { | |
return new NumberEval(arg.length()); | ||
} | ||
}; | ||
|
||
public static final Function LOWER = new SingleArgTextFunc() { | ||
@Override | ||
protected ValueEval evaluate(String arg) { | ||
return new StringEval(arg.toLowerCase(Locale.ROOT)); | ||
} | ||
}; | ||
|
||
public static final Function UPPER = new SingleArgTextFunc() { | ||
@Override | ||
protected ValueEval evaluate(String arg) { | ||
|
@@ -246,13 +248,16 @@ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, | |
private static final class LeftRight extends Var1or2ArgFunction { | ||
private static final ValueEval DEFAULT_ARG1 = new NumberEval(1.0); | ||
private final boolean _isLeft; | ||
|
||
protected LeftRight(boolean isLeft) { | ||
_isLeft = isLeft; | ||
} | ||
|
||
@Override | ||
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) { | ||
return evaluate(srcRowIndex, srcColumnIndex, arg0, DEFAULT_ARG1); | ||
} | ||
|
||
@Override | ||
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, | ||
ValueEval arg1) { | ||
|
@@ -369,7 +374,6 @@ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, V | |
try { | ||
valueDouble = DateUtil.parseDateTime(evaluated); | ||
} catch (Exception ignored) { | ||
valueDouble = null; | ||
} | ||
} | ||
} | ||
|
@@ -393,7 +397,7 @@ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, V | |
* Using it instead of {@link OperandResolver#coerceValueToString(ValueEval)} in order to handle booleans differently. | ||
*/ | ||
private String formatPatternValueEval2String(ValueEval ve) { | ||
String format = null; | ||
final String format; | ||
if (!(ve instanceof BoolEval) && (ve instanceof StringValueEval)) { | ||
StringValueEval sve = (StringValueEval) ve; | ||
format = sve.getStringValue(); | ||
|
@@ -414,6 +418,7 @@ private static final class SearchFind extends Var2or3ArgFunction { | |
public SearchFind(boolean isCaseSensitive) { | ||
_isCaseSensitive = isCaseSensitive; | ||
} | ||
|
||
@Override | ||
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) { | ||
try { | ||
|
@@ -424,6 +429,7 @@ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, V | |
return e.getErrorEval(); | ||
} | ||
} | ||
|
||
@Override | ||
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, | ||
ValueEval arg2) { | ||
|
@@ -440,6 +446,7 @@ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, V | |
return e.getErrorEval(); | ||
} | ||
} | ||
|
||
private ValueEval eval(String haystack, String needle, int startIndex) { | ||
int result; | ||
if (_isCaseSensitive) { | ||
|
@@ -454,6 +461,7 @@ private ValueEval eval(String haystack, String needle, int startIndex) { | |
return new NumberEval(result + 1.); | ||
} | ||
} | ||
|
||
/** | ||
* Implementation of the FIND() function.<p> | ||
* | ||
|
@@ -468,6 +476,7 @@ private ValueEval eval(String haystack, String needle, int startIndex) { | |
* Author: Torstein Tauno Svendsen ([email protected]) | ||
*/ | ||
public static final Function FIND = new SearchFind(true); | ||
|
||
/** | ||
* Implementation of the FIND() function.<p> | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
poi/src/test/java/org/apache/poi/util/DefaultTempFileCreationStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* ==================================================================== | ||
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.poi.util; | ||
|
||
import static org.apache.poi.util.DefaultTempFileCreationStrategy.DELETE_FILES_ON_EXIT; | ||
import static org.apache.poi.util.DefaultTempFileCreationStrategy.POIFILES; | ||
import static org.apache.poi.util.TempFile.JAVA_IO_TMPDIR; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class DefaultTempFileCreationStrategyTest { | ||
|
||
private String propBefore; | ||
private String tmpBefore; | ||
|
||
@BeforeEach | ||
void before() { | ||
propBefore = System.getProperty(DELETE_FILES_ON_EXIT); | ||
tmpBefore = System.getProperty(JAVA_IO_TMPDIR); | ||
} | ||
|
||
@AfterEach | ||
void after() { | ||
if (propBefore == null) { | ||
System.clearProperty(DELETE_FILES_ON_EXIT); | ||
} else { | ||
System.setProperty(DELETE_FILES_ON_EXIT, propBefore); | ||
} | ||
|
||
System.setProperty(JAVA_IO_TMPDIR, tmpBefore); | ||
} | ||
|
||
@Test | ||
void testDefaultFile() throws IOException { | ||
DefaultTempFileCreationStrategy strategy = new DefaultTempFileCreationStrategy(); | ||
checkGetFile(strategy); | ||
} | ||
|
||
private static void checkGetFile(DefaultTempFileCreationStrategy strategy) throws IOException { | ||
File file = strategy.createTempFile("POITest", ".tmp"); | ||
try { | ||
assertTrue(file.getParentFile().exists(), | ||
"Failed for " + file.getParentFile()); | ||
|
||
assertTrue(file.exists(), | ||
"Failed for " + file); | ||
} finally { | ||
assertTrue(file.delete()); | ||
} | ||
} | ||
|
||
@Test | ||
void testDefaultDir() throws IOException { | ||
DefaultTempFileCreationStrategy strategy = new DefaultTempFileCreationStrategy(); | ||
File dir = strategy.createTempDirectory("POITest"); | ||
try { | ||
assertTrue(dir.getParentFile().exists(), | ||
"Failed for " + dir.getParentFile()); | ||
|
||
assertTrue(dir.exists(), | ||
"Failed for " + dir); | ||
} finally { | ||
assertTrue(dir.delete()); | ||
} | ||
} | ||
|
||
@Test | ||
void testWithProperty() throws IOException { | ||
System.setProperty(DELETE_FILES_ON_EXIT, "true"); | ||
|
||
// we can set the property, but not easily check if it works | ||
// so let's just call the main method | ||
testDefaultFile(); | ||
} | ||
|
||
@Test | ||
void testEmptyTempDir() { | ||
System.clearProperty(JAVA_IO_TMPDIR); | ||
|
||
DefaultTempFileCreationStrategy strategy = new DefaultTempFileCreationStrategy(); | ||
assertThrows(IOException.class, | ||
() -> strategy.createTempDirectory("POITest")); | ||
} | ||
|
||
@Test | ||
void testCustomDir() throws IOException { | ||
File dirTest = File.createTempFile("POITest", ".dir"); | ||
try { | ||
assertTrue(dirTest.delete()); | ||
|
||
DefaultTempFileCreationStrategy strategy = new DefaultTempFileCreationStrategy(dirTest); | ||
checkGetFile(strategy); | ||
} finally { | ||
FileUtils.deleteDirectory(dirTest); | ||
} | ||
} | ||
|
||
@Test | ||
void testCustomDirExists() throws IOException { | ||
File dirTest = File.createTempFile("POITest", ".dir"); | ||
try { | ||
assertTrue(dirTest.delete()); | ||
assertTrue(dirTest.mkdir()); | ||
|
||
DefaultTempFileCreationStrategy strategy = new DefaultTempFileCreationStrategy(dirTest); | ||
checkGetFile(strategy); | ||
} finally { | ||
FileUtils.deleteDirectory(dirTest); | ||
} | ||
} | ||
|
||
@Test | ||
void testCustomDirAndPoiFilesExists() throws IOException { | ||
File dirTest = File.createTempFile("POITest", ".dir"); | ||
try { | ||
assertTrue(dirTest.delete()); | ||
assertTrue(dirTest.mkdir()); | ||
assertTrue(new File(dirTest, POIFILES).mkdir()); | ||
|
||
DefaultTempFileCreationStrategy strategy = new DefaultTempFileCreationStrategy(dirTest); | ||
checkGetFile(strategy); | ||
} finally { | ||
FileUtils.deleteDirectory(dirTest); | ||
} | ||
} | ||
} |