Menu

Persistence - lesson 5 problem

Nir B
2008-02-21
2013-04-09
  • Nir B

    Nir B - 2008-02-21

    Hi Mark,

    First I extremely wants to thank you about the efficient and lovely tutorials
    you have created and will be happy if there will be continue in other tutorial issues
    for Java and Eclipse.
    Thanks for your efforts!

    Regarding to lesson 5 in persistence tutorial, I'm afraid I have problem with execute the "MyUtilitiesTest.java".
    When trying run as Junit I receive error at the "Fialure Trace":
    -- Java.lang.Assertion error: save and get strings should be equal --

    ==> If I remarks(CNTR+/) the line:
    "assertTrue("save and get strings should be equal", saveString.equals(newString));"
    the error disappears.

    It happen although enter the whole previous code from the beginning of this tutorial.
    My Eclipse version is 3.3.1 and Java 1.6.

    Kindly, what is you advise on this problem?

    Many thanks!
    Nir

     
    • Mark Dexter

      Mark Dexter - 2008-02-21

      Hi, Nir. Have you tried copying the code snapshots from the Tutorial Companion Guide to see if they work? If they do, then you need to compare them to your files to find the difference. Let me know what you find out. Thanks. Mark

       
    • Nir B

      Nir B - 2008-02-22

      Hi Mark,

      Indeed, I already have copied the sbapshots from the Tutorial Companion Guide,
      consequently got the error mentioned previously.
      Kindly, what do you think is necessary to solve it?

      Thanking you in advance,
      Nir

       
    • Nir B

      Nir B - 2008-02-22

      Hi Mark,

      I recall to mention that for the problem line:
      "assertTrue("save and get strings should be equal", saveString.equals(newString));"
      --> If I change the 'assertTrue' to 'assertFalse'
          then the error is gone.
      However, if I'm not wrong at the Tutorial written for this line code 'asserTrue'.
      Do you think that the mistake is in my side?

      Thanks again,
      Nir

       
    • Mark Dexter

      Mark Dexter - 2008-02-22

      Hi Nir. If you like, you could copy and paste the entire contents of your MyUtiltities.java and MyUtilitiesTest.java files into a message and I'll look at them. Thanks. Mark

       
    • Nir B

      Nir B - 2008-02-24

      Hi Mark,

      Here are the MyUtiltities & MyUtilitiesTest classes that I've enter:

      /*------------------------------------------------------------------ */
      package org.persistence.tutorial;

      import java.io.*;

      public class MyUtilities {

          public static boolean saveStringToFile(String fileName,
                  String saveString) {
              boolean saved = false;
              BufferedWriter bw = null;
             
              try{
                  bw = new BufferedWriter(
                              new FileWriter(fileName));
                 
      //             FileWriter fw = new FileWriter(fileName);
      //             bw = new BufferedWriter(fw);
      //We would never use this variable -fw-, so we shouldn't create it.       
                  try {
                      bw.write(saveString);
                      saved = true;
                  }
                  finally {
                      bw.close();
                  }
              }
              catch (IOException ex) {
                  ex.printStackTrace();
              }
             
              return saved;
          }
      /* --------------------------------------------------------------------------*/

      package org.persistence.tutorial;

      import java.io.File;

      import org.junit.*;
      import static org.junit.Assert.*;

      public class MyUtilitiesTest {
         
          @Test
          public void saveStringToFile() {
              String saveString = "this is test line one\n" +
                  "this is test line two\n";
             
              File testFile = new File("testsaveto string.txt");
              testFile.delete();
              assertFalse("File should not exist", testFile.exists());
             
              assertTrue("File should have been saved",
                      MyUtilities.saveStringToFile("testsavestring.txt",saveString));
             
              String newString = MyUtilities.getStringFromFile(
                      "testsavestring.txt");
              assertTrue("Save and get strings should be equal",
                      saveString.equals(newString));
             
              assertFalse("File should not be saved",
                  MyUtilities.saveStringToFile(
                   "non-existent directory/thisshouldfail.txt",
                       saveString));
             
              String emptyString = MyUtilities.getStringFromFile(
                      "badfilename.txt");
              assertTrue("String should be empty",
                      emptyString.length() == 0);
             
             
          }
      }

      /*-----------------------------------------------------------------------------*/
      Thanking you in advance,
      Nir

       
    • Mark Dexter

      Mark Dexter - 2008-02-26

      Hi Nir. Your MyUtilities class doesn't have a getStringFromFile() method. I copied the MyUtilities.java file from the Lesson 5 snapshot and ran it with your MyUtilitiesTest.java file, and it ran correctly. So I think you need to compare your MyUtilities.java to the Lesson 5 snapshot and add the getStringFromFile() method . Hope this helps. Mark

       
    • Nir B

      Nir B - 2008-02-27

      Hi Markus,

      parentely I tried it with the getStringFromFile() method but still have error at the
      ----assertTrue("File should have been saved",
      MyUtilities.saveStringToFile("testsavestring.txt",saveString));
      ----
      this error apears by execute (JUnit run) of MyUtilitiesTest.java.

      Here is the new "MyUtilities class" that I entered:
      /**********************************************************************/
      package org.persistence.tutorial;

      import java.io.File;

      import org.junit.*;
      import static org.junit.Assert.*;

      public class MyUtilitiesTest {
         
          @Test
          public void saveStringToFile() {
              String saveString = "this is test line one\n" +
                  "this is test line two\n";
             
              File testFile = new File("testsaveto string.txt");
              testFile.delete();
              assertFalse("File should not exist", testFile.exists());
             
              assertTrue("File should have been saved",
                      MyUtilities.saveStringToFile("testsavestring.txt",saveString));
             
              String newString = MyUtilities.getStringFromFile(
                      "testsavestring.txt");
              assertTrue("Save and get strings should be equal",
                      saveString.equals(newString));
             
              assertFalse("File should not be saved",
                  MyUtilities.saveStringToFile(
                   "non-existent directory/thisshouldfail.txt",
                       saveString));
             
              String emptyString = MyUtilities.getStringFromFile(
                      "badfilename.txt");
              assertTrue("String should be empty",
                      emptyString.length() == 0);
             
             
          }
         
          public MyLibrary createMyLibrary() {
              Book b1;
              Book b2;
              Person p1;
              Person p2;
              MyLibrary ml;
             
              b1 = new Book("Book1");
              b2 = new Book("Book2");
              p1 = new Person();
              p1.setName("Fred");
              p2 = new Person();
              p2.setName("Sue");
              ml = new MyLibrary("Test");
             
              ml.addBook(b1);
              ml.addBook(b2);
              ml.addPerson(p1);
              ml.addPerson(p2);
              ml.checkOut(b1, p1);
              return ml;
             
             
             
          }

      }
      /****************************************************************************/

      Kindly, Can you direct me how to fix it?

      Many thanks,
      Nir

       
    • Mark Dexter

      Mark Dexter - 2008-02-27

      Hi Nir. You posted the MyUtilitiesTest class, not the MyUtilities class. If you post the MyUtilities class, I'll look at it and see if I can help. Thanks. Mark

       
    • Pitelk

      Pitelk - 2008-03-25

      Hi !

      I had the same problem and the error was that in the saveString i was using the \n but in the test method i had /n .
      So try to check the saveString and the string you have in the test method.

      To find this tiny error of mine i had to use the debugger......

      PS. Thank you Mark for these GREAT tutorials .... So long until your next new tutorial i hope you make!!!

       
    • Neo

      Neo - 2008-03-25

      Hi Nir,

      please see https://sourceforge.net/forum/message.php?msg_id=4827148 under "Some suggestions :"

       

Log in to post a comment.