test.TestInputFiles.java Source code

Java tutorial

Introduction

Here is the source code for test.TestInputFiles.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import loggerapi.CustomLogger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author harsha-1916
 */
public class TestInputFiles {
    private static final CustomLogger loggerProperties = new CustomLogger();
    private static Logger loggerObj;

    static {
        initializeLoggerParams();
    }

    private static boolean initializeLoggerParams() {

        new File("./Logs").mkdirs();
        if (!(new File("./Logs").exists())) {
            loggerObj.log(Level.SEVERE, "Error: Cannot create  the file: ./Logs");
            return false;
        }
        boolean isValidLogger = loggerProperties.setLoggerProperties("TestInputFiles",
                "./Logs/TestInputFiles.%u.%g.txt");
        if (isValidLogger) {
            loggerObj = loggerProperties.getLogger();
            //loggerObj.setUseParentHandlers(false);
            return true;
        }
        return false;
    }

    private String[] InternalFilesForSupToRepSync() {
        Integer index = 104;
        BufferedReader fileReader = null;
        String fileToParse = "./Files/TestFiles/SupToRepSync/MEDCServer/DC/JSONResponse_";
        String fileToParseNew = fileToParse + 1 + ".json";
        try {
            String line = "";
            //Read the file line by line
            int i = 1;
            JSONArray test = new JSONArray();
            while (i <= index) {
                String output = "";
                fileToParseNew = fileToParse + i + ".json";
                try {
                    fileReader = new BufferedReader(new FileReader(fileToParseNew));
                } catch (FileNotFoundException ex) {
                    loggerObj.log(Level.INFO, "Error in reading MEMDM server files from internal file"
                            + fileToParseNew + "Exception is: " + ex.toString());
                }
                while ((line = fileReader.readLine()) != null) {
                    output += line;
                }
                JSONParser parser = new JSONParser();
                try {
                    JSONObject json = (JSONObject) parser.parse(output);
                    test.add(json);
                } catch (ParseException ex) {
                    loggerObj.log(Level.INFO,
                            "Error in parsing MEMDM server files from internal file" + fileToParseNew);
                }
                i++;
            }
        } catch (IOException ex) {
            loggerObj.log(Level.INFO, "Error in reading MEMDM server files from internal files" + fileToParseNew);
            return null;
        } finally {
            try {
                fileReader.close();
            } catch (IOException ex) {
                loggerObj.log(Level.INFO,
                        "Error in closing the fileReader while closing the file" + fileToParseNew);
            }
        }
        return new String[] { fileToParse, index.toString() };
    }

    public String[] readFrmIntrnalFilesForMEDCSup(String folderName) {
        loggerObj.log(Level.INFO, "Inside InternalFilesForIssMgrRprts method");
        try {
            String fileToParse = folderName + "/JSONResponse_";
            String fileToParseNext = null;
            BufferedReader fileReader = null;
            String line = "";
            //Read the file line by line
            int i = 1;
            Integer index = 134;
            loggerObj.log(Level.INFO, "Going to read internally files(" + fileToParse + 1 + ".json to "
                    + fileToParse + index + ".json" + ") from folder " + folderName + "");
            while (i <= index) {
                String output = "";
                try {
                    fileToParseNext = fileToParse + i + ".json";
                    loggerObj.log(Level.INFO, "Going to read the internal file" + fileToParseNext);
                    fileReader = new BufferedReader(new FileReader(fileToParseNext));
                    while ((line = fileReader.readLine()) != null) {
                        output += line;
                    }
                    JSONParser parser = new JSONParser();
                    JSONObject json = (JSONObject) parser.parse(output);
                    fileReader.close();
                } catch (Exception ex) {
                    try {
                        loggerObj.log(Level.INFO, "Problem in reading the internal files for MEDCServer from"
                                + fileToParseNext + ex.toString());
                        fileReader.close();
                        return null;
                    } catch (IOException ex1) {
                        loggerObj.log(Level.INFO,
                                "Problem in closing the internal files for MEDCServer after reading "
                                        + fileToParseNext + ex.toString());
                    }
                }
                i++;
            }
            fileReader.close();
            loggerObj.log(Level.INFO, "successfully read the internal files for MEDCServer after reading");
            return new String[] { fileToParse, index.toString() };
        } catch (IOException ex) {
            loggerObj.log(Level.INFO, "Problem in reading the internal files for MEDCServer " + ex.toString());
            return null;
        }
    }

    public String[] InternalFilesForIssMgrRprts(String folderToReadResponse) {
        loggerObj.log(Level.INFO, "Inside InternalFilesForIssMgrRprts method");
        Integer index = 52;
        BufferedReader fileReader = null;
        String fileToParse = folderToReadResponse + "/JSONResponse_";
        loggerObj.log(Level.INFO, "Going to read internally files(" + fileToParse + 1 + ".json to " + fileToParse
                + index + ".json" + ") from folder " + folderToReadResponse + "");
        String fileToParseNew = fileToParse + 1 + ".json";
        try {
            String line = "";
            //Read the file line by line
            int i = 1;
            while (i <= index) {
                String output = "";
                fileToParseNew = fileToParse + i + ".json";
                try {
                    fileReader = new BufferedReader(new FileReader(fileToParseNew));
                } catch (FileNotFoundException ex) {
                    loggerObj.log(Level.INFO, "Error in reading MEMDM server files from internal file"
                            + fileToParseNew + "Exception is: " + ex.toString());
                }
                while ((line = fileReader.readLine()) != null) {
                    output += line;
                }
                JSONParser parser = new JSONParser();
                try {
                    parser.parse(output);
                } catch (ParseException ex) {
                    loggerObj.log(Level.INFO,
                            "Error in parsing MEMDM server files from internal file" + fileToParseNew);
                }
                i++;
            }
        } catch (IOException ex) {
            loggerObj.log(Level.INFO, "Error in reading MEMDM server files from internal files" + fileToParseNew);
            return null;
        } finally {
            try {
                fileReader.close();
            } catch (IOException ex) {
                loggerObj.log(Level.INFO,
                        "Error in closing the fileReader while closing the file" + fileToParseNew);
            }
        }
        return new String[] { fileToParse, index.toString() };
    }

    public String[] readFromInternalAutoForward(String rootFolder) {
        Integer index = 1;
        BufferedReader fileReader = null;
        String fileToParseNew = rootFolder + 1 + ".json";
        try {
            String line = "";
            //Read the file line by line
            int i = 1;
            while (i <= index) {
                String output = "";
                fileToParseNew = rootFolder + i + ".json";
                try {
                    fileReader = new BufferedReader(new FileReader(fileToParseNew));
                } catch (FileNotFoundException ex) {
                    loggerObj.log(Level.INFO, "Error in reading MEMDM server files from internal file"
                            + fileToParseNew + "Exception is: " + ex.toString());
                }
                while ((line = fileReader.readLine()) != null) {
                    output += line;
                }
                JSONParser parser = new JSONParser();
                try {
                    JSONObject json = (JSONObject) parser.parse(output);
                } catch (ParseException ex) {
                    loggerObj.log(Level.INFO,
                            "Error in parsing MEMDM server files from internal file" + fileToParseNew);
                }
                i++;
            }
        } catch (IOException ex) {
            loggerObj.log(Level.INFO, "Error in reading MEMDM server files from internal files" + fileToParseNew);
            return null;
        } finally {
            try {
                fileReader.close();
            } catch (IOException ex) {
                loggerObj.log(Level.INFO,
                        "Error in closing the fileReader while closing the file" + fileToParseNew);
            }
        }
        return new String[] { rootFolder, index.toString() };
    }

}