Update string handling for PackageProcessor

Fixes #819

* If package version undefined, use "1.0"
* Keyman Developer won't allow undefined package name, but just in case return package ID from the filename
* Check for null before String.toLowerCase()
* Added package processor test for undefined version
This commit is contained in:
darcywong00 2018-05-07 14:47:11 +07:00
parent fc60cc4a51
commit c593a45e7d
4 changed files with 35 additions and 16 deletions

View file

@ -53,10 +53,6 @@ public class PackageActivity extends Activity {
final String pkgId = PackageProcessor.getPackageID(kmpFile);
String pkgVersion = PackageProcessor.getPackageVersion(kmpFile, false);
if (pkgVersion == null || pkgVersion.isEmpty()) {
String message = "Invalid package version in " + kmpFile.getName();
showErrorDialog(context, pkgId, message);
}
String pkgName = PackageProcessor.getPackageName(kmpFile, false);
try {
@ -77,10 +73,7 @@ public class PackageActivity extends Activity {
packageActivityTitle.setTextSize(getResources().getDimension(R.dimen.package_label_textsize));
packageActivityTitle.setGravity(Gravity.CENTER);
String titleStr = "Install Keyboard Package";
if (pkgVersion != null) {
titleStr += " " + pkgVersion;
}
String titleStr = "Install Keyboard Package " + pkgVersion;
packageActivityTitle.setText(titleStr);
actionBar.setCustomView(packageActivityTitle);
@ -110,7 +103,7 @@ public class PackageActivity extends Activity {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!url.toLowerCase().equals("about:blank"))
if (url != null && !url.toLowerCase().equals("about:blank"))
view.loadUrl(url);
return true;

View file

@ -33,6 +33,7 @@ import org.json.JSONObject;
*/
public class PackageProcessor {
private static File resourceRoot = null;
public static final String PPDefault_Version = "1.0";
public static void initialize(File resourceRoot) {
PackageProcessor.resourceRoot = resourceRoot;
@ -192,21 +193,22 @@ public class PackageProcessor {
return name;
}
} catch (Exception e) {
return null;
// Developer will never allow package name to be undefined, but just in case, reuse package ID
return getPackageID(kmpPath);
}
}
/**
* Simply extracts the package's version number.
* If undefined, return default version "1.0"
* @param json The metadata JSONObject for the package.
* @return The version number (via String)
* @throws JSONException
*/
public static String getPackageVersion(JSONObject json) throws JSONException {
if(json == null) {
return null;
} else {
public static String getPackageVersion(JSONObject json) {
try {
return json.getJSONObject("info").getJSONObject("version").getString("description");
} catch (JSONException e) {
return PPDefault_Version;
}
}
@ -221,7 +223,7 @@ public class PackageProcessor {
return version;
}
} catch (Exception e) {
return null;
return PPDefault_Version;
}
}

View file

@ -35,12 +35,18 @@ public class PackageProcessorTest {
private static final File TEST_GFF_KMP_TARGET_ALT = new File(TEST_EXTRACTION_ROOT, "packages" +
File.separator + TEST_GFF_KMP_NAME_ALT);
private static final String TEST_GFF_KMP_NAME_UNDEFINED_VER = TEST_GFF_KMP_NAME;
private static final File TEST_GFF_KMP_FILE_UNDEFINED_VER = new File(TEST_RESOURCE_ROOT, "v" + File.separator + TEST_GFF_KMP_NAME_UNDEFINED_VER + ".kmp");
private static final File TEST_GFF_KMP_TARGET_UNDEFINED_VER = new File(TEST_EXTRACTION_ROOT, "packages" +
File.separator + TEST_GFF_KMP_NAME_UNDEFINED_VER);
private static final int TEST_GFF_KBD_COUNT = 2;
private static final String TEST_GFF_PACKAGE_NAME = "GFF Amharic Keyboard";
private static final String TEST_GFF_KBD_ID = "gff_amh_7";
private static File tempPkg;
private static File tempPkgAlt;
private static File tempPkgUndefinedVer;
// Each test gets a fresh version of the extracted package.
@Before
@ -63,6 +69,15 @@ public class PackageProcessorTest {
}
}
// Some tests wish to utilize package with undefined version...
public void extractUndefinedVerTestPackage() {
PackageProcessor.initialize(TEST_EXTRACTION_ROOT);
try {
tempPkgUndefinedVer = PackageProcessor.unzipKMP(TEST_GFF_KMP_FILE_UNDEFINED_VER);
} catch (IOException e) {
System.err.println(e);
}
}
/**
* Post-test cleanup. While the temp/ directory is .gitignore'd, this provides an extra layer
@ -73,6 +88,7 @@ public class PackageProcessorTest {
public void eraseTestPackages() throws IOException {
FileUtils.deleteDirectory(tempPkg);
FileUtils.deleteQuietly(tempPkgAlt);
FileUtils.deleteQuietly(tempPkgUndefinedVer);
FileUtils.deleteDirectory(TEST_GFF_KMP_TARGET);
}
@ -218,6 +234,14 @@ public class PackageProcessorTest {
Assert.assertNotEquals(TEST_GFF_KMP_NAME, PackageProcessor.getPackageName(json));
}
@Test
public void test_getPackageVersion() throws Exception {
extractUndefinedVerTestPackage();
JSONObject json = PackageProcessor.loadPackageInfo(tempPkgUndefinedVer);
Assert.assertEquals(PackageProcessor.PPDefault_Version, PackageProcessor.getPackageVersion(json));
}
@Test
public void test_versionChecks() throws Exception {
Assert.assertFalse(PackageProcessor.isDowngrade(TEST_GFF_KMP_FILE));

Binary file not shown.