Package org.quickconnectfamily.json

Examples of org.quickconnectfamily.json.JSONOutputStream


     * Make sure that the internal writer object gets
     * set.
     */
    try{
      FileOutputStream aFileOutStream = new FileOutputStream("testFile");
      JSONOutputStream aJSONFileStream = new JSONOutputStream(aFileOutStream);
      Class<?> theJSONOutputStreamClass = aJSONFileStream.getClass();

        Field theWriterField = theJSONOutputStreamClass.getDeclaredField("theWriter");
        theWriterField.setAccessible(true);
        assertNotNull(theWriterField.get(aJSONFileStream));
    }
    catch(Exception e){
      fail("Exception thrown: "+e);
    }
   
    try{
      JSONOutputStream aNullJSONStream = new JSONOutputStream(null);
      //fail since a null pointer exception should have been thrown
      fail("Should have thrown exception");
    }
    catch(NullPointerException e){/*do nothing since it should end up being caught here*/}
   
 
View Full Code Here


  @Test
  public void testWriteObject() {
   
   
   
    JSONOutputStream testOut = setupTestFile();
   
   
    /*
     * Happy path test
     */
    byte blah = 3;
    @SuppressWarnings("rawtypes")
    HashMap aMap = new HashMap();
    aMap.put("stringOne", "Some sort of string");
    //aMap.put("aNumber", 16.5);
    aMap.put("aNumber",blah);
    aMap.put(20,"some other stuff" );
    aMap.put("aTester",new Tester());
   
    try {
      testOut.writeObject(aMap);
      closeTheWriterIn(testOut);
      File testFile = new File("test.json");
      assertTrue(testFile.exists());
     
    } catch (JSONException e) {
      e.printStackTrace();

      fail("Exception thrown");
    }
    String readResult = readTestFile();
    System.out.println(readResult);
    System.out.println("{\"aNumber\":\"16.5\",\"stringOne\":\"Some sort of string\",\"20\":\"some other stuff\",\"aTester\":{\"stringAtt\":\"hello\",\"doubleAtt\":\"-4.5\",\"doubleObjAtt\":\"1000.567789\",\"listAtt\":[\"7\",\"hello there from list\"],\"parentString\":\"In The Parent\"}}");
    assertEquals(readResult, "{\"aNumber\":\"16.5\",\"stringOne\":\"Some sort of string\",\"20\":\"some other stuff\",\"aTester\":{\"stringAtt\":\"hello\",\"doubleAtt\":\"-4.5\",\"doubleObjAtt\":\"1000.567789\",\"listAtt\":[\"7\",\"hello there from list\"],\"parentString\":\"In The Parent\"}}");
   
   
    /*
     * Testing null
     */
    testOut = setupTestFile();
    try {
      testOut.writeObject(null);
      closeTheWriterIn(testOut);
      readResult = readTestFile();
      assertNull(readResult);
       
    }
    catch (JSONException e) {
      e.printStackTrace();
      fail("Threw exception");
    }

    /*
     * Testing an array of Objects.  awt.Containters should be ignored.
     */
   
    testOut = setupTestFile();
   
    Object[] anArray = {new Integer(4), "Hello", new JButton()};
    try {
      testOut.writeObject(anArray);
    } catch (JSONException e) {
      e.printStackTrace();
      fail("exception thrown");
    }
    readResult = readTestFile();
   
    assertEquals("[\"4\",\"Hello\"]", readResult);
   

    testOut = setupTestFile();
    Tester aTesterObject = new Tester();
    Object[] anArrayOfObjects = new Object[5];
    Arrays.fill(anArrayOfObjects, aTesterObject);
    try {
      testOut.writeObject(anArrayOfObjects);
    } catch (JSONException e) {
      e.printStackTrace();
      fail("exception thrown");
    }
    readResult = readTestFile();
    assertEquals("[{\"stringAtt\":\"hello\",\"doubleAtt\":\"-4.5\",\"doubleObjAtt\":\"1000.567789\",\"listAtt\":[\"7\",\"hello there from list\"],\"parentString\":\"In The Parent\"},{\"stringAtt\":\"hello\",\"doubleAtt\":\"-4.5\",\"doubleObjAtt\":\"1000.567789\",\"listAtt\":[\"7\",\"hello there from list\"],\"parentString\":\"In The Parent\"},{\"stringAtt\":\"hello\",\"doubleAtt\":\"-4.5\",\"doubleObjAtt\":\"1000.567789\",\"listAtt\":[\"7\",\"hello there from list\"],\"parentString\":\"In The Parent\"},{\"stringAtt\":\"hello\",\"doubleAtt\":\"-4.5\",\"doubleObjAtt\":\"1000.567789\",\"listAtt\":[\"7\",\"hello there from list\"],\"parentString\":\"In The Parent\"}{\"stringAtt\":\"hello\",\"doubleAtt\":\"-4.5\",\"doubleObjAtt\":\"1000.567789\",\"listAtt\":[\"7\",\"hello there from list\"],\"parentString\":\"In The Parent\"}]", readResult);
     

   
    /*
     * Testing an array of ints
     */
   
    testOut = setupTestFile();
   
    int[] anIntArray = {1,2,417,3,60, 50};
    try {
      testOut.writeObject(anIntArray);
    } catch (JSONException e) {
      e.printStackTrace();
      fail("exception thrown");
    }
    readResult = readTestFile();
   
    assertEquals("[\"1\",\"2\",\"417\",\"3\",\"60\",\"50\"]", readResult);
   
    /*
     * Testing an array of shorts
     */
    testOut = setupTestFile();
    short[] aShortArray = {1,2,417,3,60, 50};
    try {
      testOut.writeObject(aShortArray);
    } catch (JSONException e) {
      e.printStackTrace();
      fail("exception thrown");
    }
    readResult = readTestFile();
   
    assertEquals("[\"1\",\"2\",\"417\",\"3\",\"60\",\"50\"]", readResult);
   
    /*
     * Testing an array of longs
     */
    testOut = setupTestFile();
    long[] aLongArray = {1,2,417,3,60, 50};
    try {
      testOut.writeObject(aLongArray);
    } catch (JSONException e) {
      e.printStackTrace();
      fail("exception thrown");
    }
    readResult = readTestFile();
   
    assertEquals("[\"1\",\"2\",\"417\",\"3\",\"60\",\"50\"]", readResult);
    /*
     * Testing an array of doubles
     */
   
    testOut = setupTestFile();
    double[] aDoubleArray = {1.1,3.14,2.124,3.0,.00078, 1.0/3.0};
    try {
      testOut.writeObject(aDoubleArray);
    } catch (JSONException e) {
      e.printStackTrace();
      fail("exception thrown");
    }
    readResult = readTestFile();
   
    assertEquals("[\"1.1\",\"3.14\",\"2.124\",\"3.0\",\"7.8E-4\",\"0.3333333333333333\"]", readResult);
   
    /*
     * Testing an array of floats
     */

    testOut = setupTestFile();
    double[] aFloatArray = {1.1,3.14,2.124,3.0,.00078, 1.0/3};
    try {
      testOut.writeObject(aFloatArray);
    } catch (JSONException e) {
      e.printStackTrace();
      fail("exception thrown");
    }
    readResult = readTestFile();
   
    assertEquals("[\"1.1\",\"3.14\",\"2.124\",\"3.0\",\"7.8E-4\",\"0.3333333333333333\"]", readResult);
   
    /*
     * Testing an array of chars
     */

    testOut = setupTestFile();
    char[] aCharArray = new String("Hello there!").toCharArray();
    try {
      testOut.writeObject(aCharArray);
    } catch (JSONException e) {
      e.printStackTrace();
      fail("exception thrown");
    }
    readResult = readTestFile();
   
    assertEquals("[\"H\",\"e\",\"l\",\"l\",\"o\",\" \",\"t\",\"h\",\"e\",\"r\",\"e\",\"!\"]", readResult);
   
    /*
     * Testing an array of bytes
     */

    testOut = setupTestFile();
    byte[] aByteArray = {4,1,0,7,89,76};
    try {
      testOut.writeObject(aByteArray);
    } catch (JSONException e) {
      e.printStackTrace();
      fail("exception thrown");
    }
    readResult = readTestFile();
   
    assertEquals("[\"4\",\"1\",\"0\",\"7\",\"89\",\"76\"]", readResult);
   
    /*
     * Testing array of null object values passed as the parameter
     */
    testOut = setupTestFile();
    Object[] aObjectArray = new Object[5];
    try {
      testOut.writeObject(aObjectArray);
    } catch (JSONException e) {
      e.printStackTrace();
      fail("exception thrown");
    }
    readResult = readTestFile();
View Full Code Here

      e.printStackTrace();
      fail("unable to create test.json file");
    }
   
   
    return new JSONOutputStream(testFileStream);
   
  }
View Full Code Here

TOP

Related Classes of org.quickconnectfamily.json.JSONOutputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.