When I run junit tests on my project I receive the following error when trying to test that my project can build a url correctly. I am not sure what I am doing wrong below is the trace of the failed test run as well as the distancematrixconnection class and test class. It is producing a blank output when trying to compile the url string.
org.junit.ComparisonFailure: expected:<[http://maps.googleapis.com/maps/api/distancematrix/xml?origins=albany&destinations=albany%20in&language=en-EN&sensor=false&language=en-EN&units=imperial]> but was:<[]>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at edu.bsu.cs222.gascalculator.tests.GoogleUrlTests.testAlbanyNYtoAlbanyINURL(GoogleUrlTests.java:26)
public class GoogleDistanceMatrixConnection
{
String startLocation;
String endLocation;
final String urlString = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + startLocation +"&destinations=" + endLocation +"&language=en-EN&sensor=false&language=en-EN&units=imperial";
private static String XMLFile;
public String makeXMLFile(String start, String end) throws IOException
{
startLocation = start;
endLocation = end;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.connect();
BufferedReader reader = new BufferedReader( new InputStreamReader(
connection.getInputStream()));
for(String line = reader.readLine(); line != null; line =
reader.readLine())
{
setXMLFile(line);
}
return getXMLFile();
}
// public static void main(String[] args) throws IOException{
// GoogleDistanceMatrixConnection c = new GoogleDistanceMatrixConnection();
// }
public static String getXMLFile() {
return XMLFile;
}
public static void setXMLFile(String xMLFile) {
XMLFile = xMLFile;
}
public boolean doesPageExist() {
if(XMLFile == null)
return true;
else
return false;
}
}
public class GoogleUrlTests {
private GoogleDistanceMatrixConnection urlString = new GoogleDistanceMatrixConnection();
private String generatedUrl = "";
private String actualUrl = "";
#Test
public void testAlbanyNYtoAlbanyINURL() throws IOException {
generatedUrl = urlString.makeXMLFile("albany", "albany+in");
actualUrl = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=albany&destinations=albany%20in&language=en-EN&sensor=false&language=en-EN&units=imperial";
Assert.assertEquals(actualUrl, generatedUrl);
}
#Test
public void testLosAngelesToNewYorkURL() throws IOException {
generatedUrl = urlString.makeXMLFile("losangeles", "newyork");
actualUrl = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=losangeles&destinations=newyork&language=en-EN&sensor=false&language=en-EN&units=imperial";
Assert.assertEquals(actualUrl, generatedUrl);
}
}
Comparing your test cases and your code in makeXMLFile, I'm confused of what you are really trying to do here.
If you want to pass you tests, then I think this code will do that for you. You can use URLEncoder to properly encode your URL string.
public class GoogleDistanceMatrixConnection
{
public String makeXMLFile(String start, String end) throws IOException
{
return "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + URLEncoder.encode(start) +"&destinations=" + URLEncoder.encode(end) +"&language=en-EN&sensor=false&language=en-EN&units=imperial";
}
}
Otherwise, you need to clarify your question.
Related
suppose i want to read 1st line in one method and 2nd line in another method , then how to read? how to access file globally?
class File{
private static void createFogDevices(int userId, String appId) throws Exception{
File file = new File("/home/madhu/Desktop/data.txt");
BufferedReader reader = null;
reader = new BufferedReader(new FileReader(file));
FogDevice cloud = createFogDevice(reader.readLine());
}
private static void addMobile(int userId, String appId) throws Exception{
FogDevice mobile = createFogDevice(reader.readLine());
}
}
please help,i am getting error in reader.readLine() in addMobile method.
First of all you can pass additional parameters path, and line number.
If you use java 8. Please see the following example for small files:
private static void addMobile(int userId, String appId, String path, int lineNumb) throws Exception{
String mobile =Files.readAllLines(Paths.get(path)).get(lineNumb);
}
If you use java 7. Please see the following example:
private static void addMobile(int userId, String appId, String path, int lineNumb) throws Exception{
String infoString;
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
for (int i = 0; i < lineNumb; i++) {
br.readLine();
}
infoString = br.readLine();
}
}
If you would like to make BufferedReader and File as global variables you can use the following example:
public class File {
private static java.io.File file;
private static BufferedReader reader;
static {
try {
file = new java.io.File("/home/madhu/Desktop/data.txt");
reader = new BufferedReader(new FileReader(String.valueOf(file)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private static void createFogDevices(int userId, String appId) throws Exception {
FogDevice cloud = createFogDevice(reader.readLine());
}
private static void addMobile(int userId, String appId) throws Exception {
FogDevice mobile = createFogDevice(reader.readLine());
}
}
If you want to read from 6 to 8 lines you can use the following code:
String line;
try (Stream<String> lines = Files.lines(Paths.get("G:\\B\\1.txt"))) {
line = lines.skip(6).limit(8).collect(Collectors.joining());
}
My program uses SQL to build a URL to call a web-service, this service will return the following JSON:
[{
"MmisItemNo": "106552",
"CatalogNo": "UM18840041R",
"ContractOn": "False"
}
]
What I'm trying to do is strip off the "True" or "False" and use that value to do additional logic. Here is my code after the SQL builds the URL:
while (rs1.next()){
String first = (rs1.getString("PROP_VALUE"));
String second = (rs1.getString("VEN_ITEM"));
String url = (first.trim()+second.trim());
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " +url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
LightswitchResponse[] lightswitchResponses = mapper.readValue((response.toString()), LightswitchResponse[].class);
LightswitchResponse lightswitchResponse = lightswitchResponses[0];
System.out.println(lightswitchResponse.ContractOn);
}
} catch (SQLException q) {
logger.error(q);
System.exit(1);
} catch (Exception e) {
logger.error(e,e);
System.exit(1);
} finally {
}
logger.debug("getlightswitchQuery(): End");
Here is my LightswitchResponse class:
public class LightswitchResponse {
String MmisItemNo;
String CatalogNo;
boolean ContractOn;
public String getMmisItemNo() {
return MmisItemNo;
}
public void setMmisItemNo(String mmisItemNo) {
MmisItemNo = mmisItemNo;
}
public String getCatalogNo() {
return CatalogNo;
}
public void setCatalogNo(String catalogNo) {
CatalogNo = catalogNo;
}
public boolean ContractOn() {
return ContractOn;
}
public void setContractOn(boolean contractOn) {
ContractOn = contractOn;
}
}
My problem is that when I print my results = > ContractOn just returns false for every single line record/json regardless if its True or False.
For Example:
Response Code : 200
[{"MmisItemNo":"106552","CatalogNo":"UM18840041R","ContractOn":"False"}]
false
Response Code : 200
[{"MmisItemNo":"164065","CatalogNo":"UM005979091RH","ContractOn":"True"}]
false
I was originally thinking that it was something in my buffer not clearing, but after digging into it more, I don't think that's the case. Any idea what might be causing this issue of repeated false regardless of the string response?
The main trouble here is that you are using non-standard Java naming conventions for your JSON.
Annotating your LightswitchResponse as follows will work:
public static class LightswitchResponse {
String MmisItemNo;
String CatalogNo;
boolean ContractOn;
public String getMmisItemNo() {
return MmisItemNo;
}
#JsonProperty("MmisItemNo")
public void setMmisItemNo(String mmisItemNo) {
MmisItemNo = mmisItemNo;
}
public String getCatalogNo() {
return CatalogNo;
}
#JsonProperty("CatalogNo")
public void setCatalogNo(String catalogNo) {
CatalogNo = catalogNo;
}
public boolean ContractOn() {
return ContractOn;
}
#JsonProperty("ContractOn")
public void setContractOn(boolean contractOn) {
ContractOn = contractOn;
}
}
The following test passes OK:
#Test
public void testDeserializeBoolean() throws IOException{
final String test = "[{\"MmisItemNo\": \"106552\",\"CatalogNo\": \"UM18840041R\",\"ContractOn\": \"False\"}," +
"{\"MmisItemNo\": \"106552\",\"CatalogNo\": \"UM18840041R\",\"ContractOn\": \"True\"}]";
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
LightswitchResponse[] resp = objectMapper.readValue(test
,
LightswitchResponse[].class);
Assert.assertEquals(false, resp[0].ContractOn());
Assert.assertEquals(true, resp[1].ContractOn());
}
Here is code of my getStream method:
public static Twitch_Stream getStream(String channelname) {
try {
String json = API.readJsonFromUrl("https://api.twitch.tv/kraken/streams?channel=" + channelname);
Twitch_Stream stream = new Twitch_Stream();
if (json.equalsIgnoreCase("[]")) {
stream.setOnline(false);
return stream;
}
JsonArray jb = gson.fromJson(json, JsonArray.class);
if (jb.size() != 0) {
JsonObject jo = (JsonObject) jb.get(0);
stream.setOnline(true);
stream.load(jo);
}
return stream;
} catch (Exception error) {
error.printStackTrace();
}
return null;
}
and here is code of Twitch_Stream class http://pastebin.com/3RX1L1cv
When I make something like this
Twitch_Stream streamer = Twitch_API.getStream("Jankos");
Bukkit.broadcastMessage("getName " + streamer.getName());
Bukkit.broadcastMessage(streamer.isOnline() + "");
streamer.getName() return null and streamer.isOnline() returns false, even when stream is on.
Where did I make a mistake?
I don't know what problem is in your code but simple workaround would be reading content from "https://api.twitch.tv/kraken/streams/" + channel which is JSON in format:
{
"_links" : {
//links to stream and channel
},
"stream" : {
//details about current stream
}
}
Now if value of stream key is null stream is off-line. If it is not null, it is on-line.
So your code can look like
public static void main(String[] argv) throws IOException {
System.out.println(checkIfOnline("Jankos"));
System.out.println(checkIfOnline("nightblue3"));
}
public static boolean checkIfOnline(String channel) throws IOException {
String channerUrl = "https://api.twitch.tv/kraken/streams/" + channel;
String jsonText = readFromUrl(channerUrl);// reads text from URL
JSONObject json = new JSONObject(jsonText);
return !json.isNull("stream");
}
private static String readFromUrl(String url) throws IOException {
URL page = new URL(url);
try (Stream<String> stream = new BufferedReader(new InputStreamReader(
page.openStream(), StandardCharsets.UTF_8)).lines()) {
return stream.collect(Collectors.joining(System.lineSeparator()));
}
}
I used JSONObject from org.json library. I am also using Java 8 and its streams.
If you want to use gson you can use instead something like
public static boolean checkIfOnline(String channel) throws IOException {
String channerUrl = "https://api.twitch.tv/kraken/streams/" + channel;
String jsonText = readFromUrl(channerUrl);// reads text from URL
JsonParser parser = new JsonParser();
JsonObject json = parser.parse(jsonText).getAsJsonObject();
return !json.get("stream").isJsonNull();
}
If you don't have Java 8 you can rewrite code reading text from URL to something like
private static String readFromUrl(String url) throws IOException {
URL page = new URL(url);
StringBuilder sb = new StringBuilder();
Scanner scanner = null;
try{
scanner = new Scanner(page.openStream(), StandardCharsets.UTF_8.name());
while (scanner.hasNextLine()){
sb.append(scanner.nextLine());
}
}finally{
if (scanner!=null)
scanner.close();
}
return sb.toString();
}
or from what I see you can use your API.readJsonFromUrl instead of readFromUrl.
I have started working with Apache Pig for one of our projects. I have to create a custom input format to load our data files. For this, I followed this example Hadoop:Custom Input format. I also created my custom RecordReader implementation to read the data (we get our data in binary format from some other application) and parse that to proper JSON format.
The problem occurs when I use my custom loader in Pig script. As soon as my loader's getNext() method is invoked, it calls my custom RecordReader's nextKeyValue() method, which works fine. It reads the data properly, passes it back to my loader which parses the data and returns a Tuple. So far so good.
The problem arises when my loader's getNext() method is called again and again. It gets called, works fine, and returns the proper output (I debugged it till return statement). But then, instead of letting the execution go further, my loader gets called again. I tried to see the number of times my loader is called, and I could see the number go till 20K!
Can somebody please help me understand the problem in my code?
Loader
public class SimpleTextLoaderCustomFormat extends LoadFunc {
protected RecordReader in = null;
private byte fieldDel = '\t';
private ArrayList<Object> mProtoTuple = null;
private TupleFactory mTupleFactory = TupleFactory.getInstance();
#Override
public Tuple getNext() throws IOException {
Tuple t = null;
try {
boolean notDone = in.nextKeyValue();
if (!notDone) {
return null;
}
String value = (String) in.getCurrentValue();
byte[] buf = value.getBytes();
int len = value.length();
int start = 0;
for (int i = 0; i < len; i++) {
if (buf[i] == fieldDel) {
readField(buf, start, i);
start = i + 1;
}
}
// pick up the last field
readField(buf, start, len);
t = mTupleFactory.newTupleNoCopy(mProtoTuple);
mProtoTuple = null;
} catch (InterruptedException e) {
int errCode = 6018;
String errMsg = "Error while reading input";
e.printStackTrace();
throw new ExecException(errMsg, errCode,
PigException.REMOTE_ENVIRONMENT, e);
}
return t;
}
private void readField(byte[] buf, int start, int end) {
if (mProtoTuple == null) {
mProtoTuple = new ArrayList<Object>();
}
if (start == end) {
// NULL value
mProtoTuple.add(null);
} else {
mProtoTuple.add(new DataByteArray(buf, start, end));
}
}
#Override
public InputFormat getInputFormat() throws IOException {
//return new TextInputFormat();
return new CustomStringInputFormat();
}
#Override
public void setLocation(String location, Job job) throws IOException {
FileInputFormat.setInputPaths(job, location);
}
#Override
public void prepareToRead(RecordReader reader, PigSplit split)
throws IOException {
in = reader;
}
Custom InputFormat
public class CustomStringInputFormat extends FileInputFormat<String, String> {
#Override
public RecordReader<String, String> createRecordReader(InputSplit arg0,
TaskAttemptContext arg1) throws IOException, InterruptedException {
return new CustomStringInputRecordReader();
}
}
Custom RecordReader
public class CustomStringInputRecordReader extends RecordReader<String, String> {
private String fileName = null;
private String data = null;
private Path file = null;
private Configuration jc = null;
private static int count = 0;
#Override
public void close() throws IOException {
// jc = null;
// file = null;
}
#Override
public String getCurrentKey() throws IOException, InterruptedException {
return fileName;
}
#Override
public String getCurrentValue() throws IOException, InterruptedException {
return data;
}
#Override
public float getProgress() throws IOException, InterruptedException {
return 0;
}
#Override
public void initialize(InputSplit genericSplit, TaskAttemptContext context)
throws IOException, InterruptedException {
FileSplit split = (FileSplit) genericSplit;
file = split.getPath();
jc = context.getConfiguration();
}
#Override
public boolean nextKeyValue() throws IOException, InterruptedException {
InputStream is = FileSystem.get(jc).open(file);
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, "UTF-8");
data = writer.toString();
fileName = file.getName();
writer.close();
is.close();
System.out.println("Count : " + ++count);
return true;
}
}
Try this in Loader
//....
boolean notDone = ((CustomStringInputFormat)in).nextKeyValue();
//...
Text value = new Text(((CustomStringInputFormat))in.getCurrentValue().toString())
I'm try to create one simple reservation system, we'll read a file, then we'll add Train, Bus, etc., then we'll writer everything to output.
import java.io.*;
import java.util.*;
public class Company
{
private static ArrayList<Bus> bus = new ArrayList<Bus>();
static int buscount = 0, traincount = 0;
public static void main (String[] args) throws IOException
{
FileParser();
}
public Company()
{
}
public static void FileParser()
{
try {
File file = new File(); //i fill this later
File file2 = new File(); // i fill this later
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file2);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
String line;
while ((line = br.readLine()) != null)
{
String[] splitted = line.split(",");
if(splitted[0].equals("ADDBUS"))
{
bus.add(buscount) = Bus(splitted[0],splitted[1],splitted[2],splitted[3],splitted[4],splitted[5]);
}
}
}
catch (FileNotFoundException fnfe) {
}
catch (IOException ioe) {
}
}
}
I try to read the file line by line. For example one of the line is "ADDBUS,78KL311,10,140,54" I split the line for "," then i try to add every pieces of array to Bus' class' constructor but i couldn't figured it out.
My Bus Class is like `
public class Bus extends Vehicle{
private String command;
private String busName;
private String busPlate;
private String busAge;
private String busSpeed;
private String busSeat;
public Bus(String command, String busname, String busplate, String busage, String busspeed, String busseat)
{
this.command = command;
this.busName = busname;
this.busPlate = busplate;
this.busAge = busage;
this.busSpeed = busspeed;
this.busSeat = busseat;
}
public String getBusName() {
return busName;
}
public void setBusName(String busName) {
this.busName = busName;
}
public String getBusPlate() {
return busPlate;
}
public void setBusPlate(String busPlate) {
this.busPlate = busPlate;
}
public String getBusAge() {
return busAge;
}
public void setBusAge(String busAge) {
this.busAge = busAge;
}
public String getBusSpeed() {
return busSpeed;
}
public void setBusSpeed(String busSpeed) {
this.busSpeed = busSpeed;
}
public String getBusSeat() {
return busSeat;
}
public void setBusSeat(String busSeat) {
this.busSeat = busSeat;
}
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
}
can someone show me a way to solve this problem?
Thank you,
You are missing the keyword new to create a new instance of the class:
bus.add(new Bus(...));
You can add items to ArrayList like this
bus.add( new Bus(splitted[0],splitted[1],splitted[2],splitted[3],splitted[4],splitted[5]));
you were missing new keyword before Bus constructor call. Then you can increment the counter (or do whatever)
bus.add( new Bus(splitted[0],splitted[1],splitted[2],splitted[3],splitted[4],splitted[5]));
buscount++;
try to add new Bus(...)
bus.add( new
Bus(splitted[0],splitted[1],splitted[2],splitted[3],splitted[4],splitted[5]));
As I understand if you want to call constructor you need to call new Bus(parms).
when you say new it will call constructor of your class
when you say this() again it going to call enclosing class' constructor
if you say super() it will call super class' constructor.
if you want it into a map order by counter you can use this:
Map(Integer, Bus) busPosition = new HashMap<>();
busPosition.put(buscount, new
Bus(splitted[0],splitted[1],splitted[2],splitted[3],splitted[4],splitted[5]));