Skip to content

Commit

Permalink
WIP #8 - Converting JSON data to Java objects
Browse files Browse the repository at this point in the history
- Added Gson dependency (library for manipulating Json)
- Moved json files to a dedicated directory
- Created main for simple test
- Added Java classes to represent Json data
  • Loading branch information
jeandersonbc committed Mar 23, 2016
1 parent 1de7f8b commit 3c0b577
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 0 deletions.
86 changes: 86 additions & 0 deletions mock-data/intent_tests/Activity1-Intents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"sourceName": "Activity1",
"declaredPkg": "br.ufpe.cin.androidintenttests",
"intents": [{
"declaredMethod": "func1",
"target" : "startActivity",
"identifier" : "toActivity2",
"properties": {
"action": "Intent.ACTION_VIEW",
"data": "Uri.parse(\"custom://base#specific\")"
}
},
{
"declaredMethod": "func1",
"target" : "startActivity",
"identifier" : "i",
"properties": {
"action": "Intent.ACTION_CALL",
"data": "Uri.parse(\"tel:123456789\")"
}
},
{
"declaredMethod": "func2",
"target" : "startActivity",
"identifier": "i",
"properties": {
"action": "Intent.ACTION_VIEW",
"data": "Uri.parse(\"https://www.google.com\")"
}
},
{
"declaredMethod": "func2",
"target" : "startActivity",
"identifier": "i2",
"properties": {
"component": {
"context": "br.ufpe.cin.androidintenttests",
"class": "Activity1"
},
"data": "Uri.parse(\"https://www.google.com\")"
}
},
{
"declaredMethod": "func2",
"target" : "startActivity",
"identifier": "i3",
"properties": {
"action": "Intent.ACTION_VIEW",
"data": "Uri.parse(\"https://www.google.com\")"
}
},
{
"target" : "startActivity",
"declaredMethod": "func2",
"identifier": "i4",
"properties": {
"action": "Intent.ACTION_VIEW",
"data": "Uri.parse(\"https://www.google.com\")",
"type": "text/plain"
}
},
{
"declaredMethod": "func2",
"target" : "startActivity",
"identifier": "i5",
"properties": {
"action": "Intent.ACTION_VIEW",
"data": "Uri.parse(\"https://www.google.com\")",
"type": "text/plain"
}
},
{
"declaredMethod": "func2",
"target" : "startActivity",
"identifier": "i6",
"properties": {
"component": {
"context": "br.ufpe.cin.androidintenttests",
"class": "Activity1"
},
"data": "Uri.parse(\"https://www.google.com\")",
"type": "text/plain"
}
}
]
}
112 changes: 112 additions & 0 deletions mock-data/intent_tests/Activity2-Intents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"sourceName": "Activity2",
"declaredPkg": "br.ufpe.cin.androidintenttests",
"intents": [{
"declaredMethod": "func1",
"target" : "startActivity",
"identifier" : "i",
"properties": {
"component": {
"context": "this",
"class": "Activity1.class"
},
"data": "Uri.parse(\"custom://base#specific\")"
}
},
{
"declaredMethod": "func1",
"target": "startActivity",
"identifier": "i2",
"properties": {
"component": {
"context": "this",
"class": "Activity1.class"
},
"data": "Uri.parse(\"custom://base#specific\")",
"extra": {
"key_bool": false,
"key_string": "somevalue",
"key_int": 10,
"key_double": 12.5
}
}
},
{
"declaredMethod": "func2",
"target": "startActivity",
"identifier": "i1",
"properties": {
"action": "Intent.ACTION_VIEW"
}
},
{
"declaredMethod": "func2",
"target": "startActivity",
"identifier": "i2",
"properties": {
"action": "Intent.ACTION_VIEW",
"data": "Uri.parse(\"http://www.google.com\")"
}
},
{
"declaredMethod": "func2",
"target": "startActivity",
"identifier": "i3",
"properties": {
"component": {
"context": "this",
"class": "Activity1.class"
}
}
},
{
"declaredMethod": "func2",
"target": "startActivity",
"identifier": "i4",
"properties": {
"action": "Intent.ACTION_VIEW",
"data": "Uri.parse(\"http://www.google.com\")",
"component": {
"context": "this",
"class": "Activity1.class"
}
}
},
{
"declaredMethod": "func3",
"target": "startActivity",
"identifier": "i",
"properties": {
"package": "br.ufpe.cin.androidintenttests",
"component": {
"context": "this",
"class": "Activity"
}
}
},
{
"declaredMethod": "func3",
"target": "startActivity",
"identifier": "i2",
"properties": {
"data": "Uri.parse(\"https://www.google.com\")",
"component": {
"context": "br.ufpe.cin.androidintenttests",
"class": "Activity1"
}
}
},
{
"declaredMethod": "func3",
"target": "startActivity",
"identifier": "i3",
"properties": {
"component": {
"context": "this",
"class": "Activity1"
},
"data": "Uri.parse(\"https://www.google.com\")"
}
}
]
}
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
Expand Down
45 changes: 45 additions & 0 deletions tests/subjects/mock/IntentDataJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package subjects.mock;

/*
* FIXME: Not finished yet
*/
public class IntentDataJson {

/*
* FIXME: Not finished yet
*/
class IntentProperties {
private String action;
private String data;

@Override
public String toString() {
return action;
}

}

private String declaredMethod;
private String target;
private String identifier;
private IntentProperties properties;

@Override
public String toString() {
StringBuilder sb = new StringBuilder("{\n");

if (this.declaredMethod != null)
sb.append("declaredMethod: ").append(this.declaredMethod).append("\n");
if (this.target != null)
sb.append("target: ").append(this.target).append("\n");
if (this.identifier != null)
sb.append("identifier: ").append(this.identifier).append("\n");
if (this.properties != null)
sb.append("properties: {").append(this.properties).append("}\n");

sb.append("}");

return sb.toString();
}

}
24 changes: 24 additions & 0 deletions tests/subjects/mock/IntentResultJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package subjects.mock;

import java.util.List;

public class IntentResultJson {

/*
* Field names matches JSON field data
*/
private String sourceName;
private String declaredPkg;
private List<IntentDataJson> intents;

@Override
public String toString() {
StringBuilder sb = new StringBuilder();

sb.append("sourceName: ").append(this.sourceName).append("\n");
sb.append("declaredPkg: ").append(this.declaredPkg).append("\n");
sb.append("intents: ").append(this.intents).append("\n");

return sb.toString();
}
}
47 changes: 47 additions & 0 deletions tests/subjects/mock/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package subjects.mock;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class Main {

public static void main(String[] args) throws IOException {
String jsonString1 = extractStringFrom("mock-data/intent_tests/Activity1-Intents.json");
String jsonString2 = extractStringFrom("mock-data/intent_tests/Activity2-Intents.json");

Gson gson = new GsonBuilder().create();

// Magic is done here with Reflection. GSON automatically matches JSON
// attributes/values with the target class by names.
IntentResultJson data1 = gson.fromJson(jsonString1, IntentResultJson.class);
IntentResultJson data2 = gson.fromJson(jsonString2, IntentResultJson.class);

System.out.println(data1);
System.out.println(data2);
}

/**
* Reads a file and returns a whole string from the read content
*
* @param filePath
* Path to the file to be read
* @return The content of filePath as String
* @throws FileNotFoundException
* @throws IOException
*/
private static String extractStringFrom(String filePath) throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader(filePath));
StringBuilder sb = new StringBuilder();
while (br.ready()) {
sb.append(br.readLine().trim());
}
br.close();

return sb.toString();
}
}

0 comments on commit 3c0b577

Please sign in to comment.