Commit bec5507f authored by Evan W. Patton's avatar Evan W. Patton Committed by Susan Rati Lane

Make FormPropertiesAnalyzer handle Windows line endings (#1556)

Change-Id: Id58ad71b23a4132db983e8a2c9d8dce6919c4433
parent 5fe7669e
// -*- mode: java; c-basic-offset: 2; -*- // -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved // Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved // Copyright 2011-2019 MIT, All rights reserved
// Released under the Apache License, Version 2.0 // Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0 // http://www.apache.org/licenses/LICENSE-2.0
...@@ -57,6 +57,7 @@ public class YoungAndroidSourceAnalyzer { ...@@ -57,6 +57,7 @@ public class YoungAndroidSourceAnalyzer {
* @return the properties as a JSONObject * @return the properties as a JSONObject
*/ */
public static JSONObject parseSourceFile(String source, JSONParser jsonParser) { public static JSONObject parseSourceFile(String source, JSONParser jsonParser) {
source = source.replaceAll("\r\n", "\n");
// First, locate the beginning of the $JSON section. // First, locate the beginning of the $JSON section.
// Older files have a $Properties before the $JSON section and we need to make sure we skip // Older files have a $Properties before the $JSON section and we need to make sure we skip
// that. // that.
......
...@@ -53,6 +53,7 @@ public class FormPropertiesAnalyzer { ...@@ -53,6 +53,7 @@ public class FormPropertiesAnalyzer {
* @return the properties as a JSONObject * @return the properties as a JSONObject
*/ */
public static JSONObject parseSourceFile(String source) { public static JSONObject parseSourceFile(String source) {
source = source.replaceAll("\r\n", "\n");
// First, locate the beginning of the $JSON section. // First, locate the beginning of the $JSON section.
// Older files have a $Properties before the $JSON section and we need to make sure we skip // Older files have a $Properties before the $JSON section and we need to make sure we skip
// that. // that.
...@@ -64,7 +65,7 @@ public class FormPropertiesAnalyzer { ...@@ -64,7 +65,7 @@ public class FormPropertiesAnalyzer {
} }
beginningOfJsonSection += jsonSectionPrefix.length(); beginningOfJsonSection += jsonSectionPrefix.length();
// Then, locate the end of the $JSON section; // Then, locate the end of the $JSON section
String jsonSectionSuffix = FORM_PROPERTIES_SUFFIX; String jsonSectionSuffix = FORM_PROPERTIES_SUFFIX;
int endOfJsonSection = source.lastIndexOf(jsonSectionSuffix); int endOfJsonSection = source.lastIndexOf(jsonSectionSuffix);
if (endOfJsonSection == -1) { if (endOfJsonSection == -1) {
......
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright © 2019 Massachusetts Institute of Technology, All rights reserved.
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
package com.google.appinventor.buildserver;
import org.codehaus.jettison.json.JSONObject;
import org.junit.Test;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class FormPropertiesAnalyzerTest {
private static final String TEST_DATA =
"#|\n$JSON\n{\"Properties\":{\"$Type\":\"Form\",\"$Components\":[]}}\n|#\n";
@Test
public void testParseSourceFileValid() {
JSONObject data = FormPropertiesAnalyzer.parseSourceFile(TEST_DATA);
assertNotNull(data.optJSONObject("Properties"));
}
@Test(expected = IllegalArgumentException.class)
public void testParseSourceFileMissingHeader() {
FormPropertiesAnalyzer.parseSourceFile("\n|#\n");
}
@Test(expected = IllegalArgumentException.class)
public void testParseSourceFileMissingFooter() {
FormPropertiesAnalyzer.parseSourceFile("#|\n$JSON\n");
}
@Test(expected = IllegalArgumentException.class)
public void testParseSourceFileInvalidJSON() {
FormPropertiesAnalyzer.parseSourceFile("#|\n$JSON\n{]\n|#\n");
}
@Test
public void testParseSourceFileWindowsLineEndings() {
JSONObject data = FormPropertiesAnalyzer.parseSourceFile(TEST_DATA.replaceAll("\n", "\r\n"));
assertNotNull(data.optJSONObject("Properties"));
}
@Test
public void testGetComponentTypesFromFormFile() {
Set<String> result = FormPropertiesAnalyzer.getComponentTypesFromFormFile(TEST_DATA);
assertEquals(1, result.size());
assertTrue(result.contains("Form"));
}
@Test(expected = IllegalArgumentException.class)
public void testGetComponentTypesFromFormFileThrows() {
FormPropertiesAnalyzer.getComponentTypesFromFormFile("#|\n$JSON\n{}\n|$\n");
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment