Skip to content

Commit 52c44ae

Browse files
authored
Merge pull request #156 from domaframework/case-sensitive-path-check
SQLファイルの存在チェックでパスの大文字小文字を区別するように修正
2 parents e01cc22 + 861b7e1 commit 52c44ae

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

src/main/java/org/seasar/doma/internal/apt/meta/AbstractSqlFileQueryMetaFactory.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.File;
2121
import java.io.IOException;
22+
import java.io.UncheckedIOException;
2223
import java.net.URI;
2324
import java.util.LinkedHashMap;
2425

@@ -91,7 +92,7 @@ protected File getFile(M queryMeta, ExecutableElement method,
9192
if (!uri.isAbsolute()) {
9293
uri = new File(".").toURI().resolve(uri);
9394
}
94-
File file = new File(uri);
95+
File file = getCanonicalFile(new File(uri));
9596
if (!file.exists()) {
9697
throw new AptException(Message.DOMA4019, env, method, new Object[] {
9798
filePath, file.getAbsolutePath() });
@@ -100,9 +101,21 @@ protected File getFile(M queryMeta, ExecutableElement method,
100101
throw new AptException(Message.DOMA4021, env, method, new Object[] {
101102
filePath, file.getAbsolutePath() });
102103
}
104+
if (!IOUtil.endsWith(file, filePath)) {
105+
throw new AptException(Message.DOMA4309, env, method, new Object[] {
106+
filePath, file.getAbsolutePath() });
107+
}
103108
return file;
104109
}
105110

111+
protected File getCanonicalFile(File file) {
112+
try {
113+
return file.getCanonicalFile();
114+
} catch (IOException e) {
115+
throw new UncheckedIOException(e);
116+
}
117+
}
118+
106119
protected File[] getSiblingFiles(M queryMeta, ExecutableElement method,
107120
File file) {
108121
File dir = getDir(file);

src/main/java/org/seasar/doma/internal/util/IOUtil.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.seasar.doma.internal.util;
1717

18-
import static org.seasar.doma.internal.util.AssertionUtil.*;
18+
import static org.seasar.doma.internal.util.AssertionUtil.assertNotNull;
1919

2020
import java.io.BufferedReader;
2121
import java.io.Closeable;
@@ -26,6 +26,8 @@
2626
import java.io.InputStream;
2727
import java.io.InputStreamReader;
2828
import java.nio.CharBuffer;
29+
import java.nio.file.Path;
30+
import java.nio.file.Paths;
2931

3032
import org.seasar.doma.internal.Constants;
3133
import org.seasar.doma.internal.WrapException;
@@ -81,4 +83,20 @@ public static void close(Closeable closeable) {
8183
}
8284
}
8385

86+
public static boolean endsWith(File file, String pathname) {
87+
Path path = file.toPath();
88+
Path other = Paths.get(pathname);
89+
int i = path.getNameCount() - 1;
90+
int j = other.getNameCount() - 1;
91+
for (; i >= 0 && j >= 0; i--, j--) {
92+
// avoid Path#equals to make comparison case sensitive
93+
String element = path.getName(i).toString();
94+
String otherElement = other.getName(j).toString();
95+
if (!element.equals(otherElement)) {
96+
return false;
97+
}
98+
}
99+
return true;
100+
}
101+
84102
}

src/main/java/org/seasar/doma/message/Message.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ public enum Message implements MessageResource {
449449
DOMA4306("@Columnは@Embeddableが注釈された型のプロパティに注釈できません。 at {0}.{1}"),
450450
DOMA4307("永続プロパティの型[{0}]とコンストラクタパラメータの型[{1}]が[{2}]番目で一致しません。Kotlinでエンベッダブルクラスを定義する場合、永続プロパティとコンストラクタのパラメータの宣言順序は一致しなければいけません。継承している場合、コンストラクタパラメータの宣言順序は親クラスに定義されたプロパティ順でなければいけません。 at {3}"),
451451
DOMA4308("永続プロパティの型[{0}]とコンストラクタパラメータの型[{1}]が[{2}]番目で一致しません。Kotlinでエンティティクラスを定義する場合、永続プロパティとコンストラクタのパラメータの宣言順序は一致しなければいけません。継承している場合、コンストラクタパラメータの宣言順序は親クラスに定義されたプロパティ順でなければいけません。 at {3}"),
452+
DOMA4309("ファイルパス[{0}]の大文字小文字がファイルシステム上のパスと一致しません。ファイルシステム上のパスは\"{1}\"です。"),
452453

453454
// other
454455
DOMA5001("JDBCドライバがロードされていない可能性があります。まず、JDBCドライバがクラスパスにあることを確認してください。次に、JDBCドライバが自動でロードされない場合は、Class.forNameで明示的にロードしてください。 ex) Class.forName(\"oracle.jdbc.driver.OracleDriver\")"),

src/test/java/org/seasar/doma/internal/util/IOUtilTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.seasar.doma.internal.util;
1717

1818
import java.io.Closeable;
19+
import java.io.File;
1920
import java.io.IOException;
2021

2122
import junit.framework.TestCase;
@@ -35,4 +36,17 @@ public void close() throws IOException {
3536
}
3637
});
3738
}
39+
40+
public void testEndWith_true() throws Exception {
41+
File file = new File("/fuga/META-INF/piyo/HogeDao/selectById.sql");
42+
String pathname = "META-INF/piyo/HogeDao/selectById.sql";
43+
assertTrue(IOUtil.endsWith(file, pathname));
44+
}
45+
46+
public void testEndWith_false() throws Exception {
47+
File file = new File("/fuga/META-INF/piyo/hogeDao/selectById.sql");
48+
String pathname = "META-INF/piyo/HogeDao/selectById.sql";
49+
assertFalse(IOUtil.endsWith(file, pathname));
50+
}
51+
3852
}

0 commit comments

Comments
 (0)