Java tutorial
/* * 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 org.graylogalert.component; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.graylogalert.model.Config; import org.json.simple.JSONObject; import org.json.simple.parser.ParseException; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.*; import org.junit.Ignore; /** * * @author roman */ public class ConfigurationTest { public ConfigurationTest() { } @BeforeClass public static void setUpClass() { } @AfterClass public static void tearDownClass() { } @Before public void setUp() { } @After public void tearDown() { } /** * ? * * @param canWrite - ? ? */ public void setAccess(Boolean canWrite) { Path path = Paths.get(".").toAbsolutePath(); File file = path.toFile(); file.setWritable(canWrite); } /** * */ public void deleteConfig() { File file = new File("config.json"); file.delete(); } public void changeConfigFile(String range, String[] queries) throws IOException { JSONObject jsonConfig = new JSONObject(); List<String> queryList = new ArrayList<>(); queryList.addAll(Arrays.asList(queries)); try (Writer writer = new FileWriter("config.json", false)) { jsonConfig.put("queries", queryList); jsonConfig.put("range", ""); jsonConfig.writeJSONString(writer); writer.flush(); writer.close(); } } /** * ? */ @Test public void testAccessConfigFile() { System.out.println("Access Created Config File"); this.deleteConfig(); this.setAccess(false); try { Configuration.createConfigFile(); fail(" ?"); } catch (IOException ex) { } } /** * ?? */ @Test public void testCheckExistFile() { System.out.println("Exist Config File"); this.deleteConfig(); this.setAccess(true); try { Configuration.createConfigFile(); Boolean isFileExist = new File("config.json").exists(); assertEquals(true, isFileExist); } catch (IOException ex) { System.out.println(ex.getLocalizedMessage()); fail(" ???"); } } /** * ? */ //@Test public void testCheckTemplateFile() throws IOException, ParseException { System.out.println("testCheckTemplateFile"); this.setAccess(true); this.deleteConfig(); Configuration.getInstance(); String template = "{\"range\":3600,\"queries\":[\"full_message:(Exception)\",\"full_message:(Error)\"]}"; String templateFile = new BufferedReader(new FileReader(new File("config.json"))).readLine(); assertEquals(template, templateFile); } /** * ? */ @Test public void testLoadConfigFile() { System.out.println("testLoadConfigFile"); try { Config config = Configuration.loadConfigFile(); assertNotNull(config); } catch (IOException | ParseException ex) { System.out.println(ex.getLocalizedMessage()); fail(" ? "); } } /** * ? json */ @Test public void testLoadConfigFileFullEmpty() { System.out.println("testLoadConfigFileNotCreated"); this.deleteConfig(); try { String[] queries = {}; this.changeConfigFile("", queries); Config config = Configuration.loadConfigFile(); assertNull(config); } catch (IOException | ParseException ex) { System.out.println(ex.getLocalizedMessage()); fail(" ? "); } } /** * ? range */ @Test public void testLoadConfigFileRangeEmpty() { System.out.println("testLoadConfigFileRangeEmpty"); this.deleteConfig(); try { String[] queries = { "full_message:(error)" }; this.changeConfigFile("", queries); Config config = Configuration.loadConfigFile(); assertNull(config); } catch (IOException | ParseException ex) { System.out.println(ex.getLocalizedMessage()); fail(" ? "); } } /** * ? queries */ @Test public void testLoadConfigFileQueriesEmpty() { System.out.println("testLoadConfigFileQueriesEmpty"); this.deleteConfig(); try { String[] queries = {}; this.changeConfigFile("3600", queries); Config config = Configuration.loadConfigFile(); assertNull(config); } catch (IOException | ParseException ex) { System.out.println(ex.getLocalizedMessage()); fail(" ? "); } } /** * ? */ @Test public void testLoadConfigFileEmpty() { System.out.println("testLoadConfigFileEmpty"); this.deleteConfig(); this.setAccess(true); try { Config config = Configuration.loadConfigFile(); assertNotNull(config); } catch (IOException | ParseException ex) { System.out.println(ex.getLocalizedMessage()); fail(" ? "); } } /** * Test of getInstance method, of class Configuration. */ @Test public void testGetInstance() { System.out.println("testGetInstance"); this.deleteConfig(); this.setAccess(true); try { Config config = Configuration.getInstance(); assertNotNull(config); } catch (IOException | ParseException ex) { System.out.println(ex.getLocalizedMessage()); fail(" ? "); } } }