fix Google Sheets importer when URL is missing a trailing slash (#2380) (#2382)

* fix #2380

* update test

* rename test method
This commit is contained in:
Lu Liu 2020-03-10 21:48:00 +08:00 committed by GitHub
parent fec414cb14
commit 4477fc15a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 1 deletions

View File

@ -183,7 +183,7 @@ abstract public class GoogleAPIExtension {
throws IllegalArgumentException { throws IllegalArgumentException {
URL urlAsUrl; URL urlAsUrl;
Matcher matcher = Pattern.compile("(?<=\\/d\\/).*(?=\\/.*)").matcher(url); Matcher matcher = Pattern.compile("(?<=/d/).*?(?=[/?#]|$)").matcher(url);
if (matcher.find()) { if (matcher.find()) {
return matcher.group(0); return matcher.group(0);
} }

View File

@ -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);
}
}