From 4477fc15a560677c365969bcf0c0bbc111f623e8 Mon Sep 17 00:00:00 2001 From: Lu Liu <2w6f8c@gmail.com> Date: Tue, 10 Mar 2020 21:48:00 +0800 Subject: [PATCH] fix Google Sheets importer when URL is missing a trailing slash (#2380) (#2382) * fix #2380 * update test * rename test method --- .../extension/gdata/GoogleAPIExtension.java | 2 +- .../gdata/GoogleAPIExtensionTest.java | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 extensions/gdata/tests/src/com/google/refine/extension/gdata/GoogleAPIExtensionTest.java diff --git a/extensions/gdata/src/com/google/refine/extension/gdata/GoogleAPIExtension.java b/extensions/gdata/src/com/google/refine/extension/gdata/GoogleAPIExtension.java index a28ec3029..51c167f5a 100644 --- a/extensions/gdata/src/com/google/refine/extension/gdata/GoogleAPIExtension.java +++ b/extensions/gdata/src/com/google/refine/extension/gdata/GoogleAPIExtension.java @@ -183,7 +183,7 @@ abstract public class GoogleAPIExtension { throws IllegalArgumentException { URL urlAsUrl; - Matcher matcher = Pattern.compile("(?<=\\/d\\/).*(?=\\/.*)").matcher(url); + Matcher matcher = Pattern.compile("(?<=/d/).*?(?=[/?#]|$)").matcher(url); if (matcher.find()) { return matcher.group(0); } diff --git a/extensions/gdata/tests/src/com/google/refine/extension/gdata/GoogleAPIExtensionTest.java b/extensions/gdata/tests/src/com/google/refine/extension/gdata/GoogleAPIExtensionTest.java new file mode 100644 index 000000000..1a48a1ad3 --- /dev/null +++ b/extensions/gdata/tests/src/com/google/refine/extension/gdata/GoogleAPIExtensionTest.java @@ -0,0 +1,40 @@ +package com.google.refine.extension.gdata; + +import com.google.refine.ProjectManager; +import org.testng.annotations.Test; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static org.mockito.Mockito.mock; +import static org.testng.Assert.*; + +public class GoogleAPIExtensionTest { + + @Test + public void extractSpreadSheetIdTest() { + // GoogleAPIExtension will call ProjectManager.singleton.getPreferenceStore() during class initialization, + // which will cause NullPointerException if this line is omitted. + ProjectManager.singleton = mock(ProjectManager.class); + + String spreadSheetId = "16L0JfpBWPfBJTqKtm-YU5-UBWLpkwXII-IRLMLnoKw0"; + + String url1 = "https://docs.google.com/spreadsheets/d/16L0JfpBWPfBJTqKtm-YU5-UBWLpkwXII-IRLMLnoKw0/edit#gid=0"; + assertEquals(GoogleAPIExtension.extractSpreadSheetId(url1), spreadSheetId); + + String url2 = "https://docs.google.com/spreadsheets/d/16L0JfpBWPfBJTqKtm-YU5-UBWLpkwXII-IRLMLnoKw0/"; + assertEquals(GoogleAPIExtension.extractSpreadSheetId(url2), spreadSheetId); + + String url3 = "https://docs.google.com/spreadsheets/d/16L0JfpBWPfBJTqKtm-YU5-UBWLpkwXII-IRLMLnoKw0?foo=bar"; + assertEquals(GoogleAPIExtension.extractSpreadSheetId(url3), spreadSheetId); + + String url4 = "https://docs.google.com/spreadsheets/d/16L0JfpBWPfBJTqKtm-YU5-UBWLpkwXII-IRLMLnoKw0/?foo=bar"; + assertEquals(GoogleAPIExtension.extractSpreadSheetId(url4), spreadSheetId); + + String url5 = "https://docs.google.com/spreadsheets/d/16L0JfpBWPfBJTqKtm-YU5-UBWLpkwXII-IRLMLnoKw0#foo"; + assertEquals(GoogleAPIExtension.extractSpreadSheetId(url5), spreadSheetId); + + String url6 = "https://docs.google.com/spreadsheets/d/16L0JfpBWPfBJTqKtm-YU5-UBWLpkwXII-IRLMLnoKw0"; + assertEquals(GoogleAPIExtension.extractSpreadSheetId(url6), spreadSheetId); + } +}