Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serializable is only a one way test and not complete #28

Open
hazendaz opened this issue Aug 21, 2017 · 1 comment
Open

serializable is only a one way test and not complete #28

hazendaz opened this issue Aug 21, 2017 · 1 comment
Assignees

Comments

@hazendaz
Copy link
Owner

hazendaz commented Aug 21, 2017

Because serialization will work if the object attempting to be serialized is serialized, it does nothing to confirm the underlying objects themselves are serializable. If a field for example is transient but nothing is defined to restore it such as List, then it won't be restored. We should look to enhance this so we can test the contents written are the contents retrieved.

Sample code I used to visually confirm a problem which could be basis for this. This uses mybatis CacheKey as there were questions regarding List and scanners stating not serializable. After testing it was determined that it is serializable if contents themselves are. If the contents is just another bland object, then it is not. However, marking the class transient actually causes a problem resulting in the data being null entirely when reading back the object as it will never be written.

  @Test
  public void serializationTest() {
      CacheKey cacheKey = new CacheKey();
      cacheKey.update(new Object());
      canSerialize(cacheKey);
  }

  void canSerialize(final CacheKey object) {
      ObjectOutputStream output = null;
      try {
          FileOutputStream fout = new FileOutputStream("address.ser");
          output = new ObjectOutputStream(fout);
          output.writeObject(object);
      } catch (final IOException e) {
          Assert.fail(String.format("An exception was thrown while serializing the class '%s': '%s',", object
                  .getClass().getName(), e.toString()));
      } finally {
          if (output != null) {
              try {
                  output.close();
              } catch (IOException e) {
                  Assert.fail(String.format("An exception was thrown while closing stream for class '%s': '%s',",
                          object.getClass().getName(), e.toString()));
              }
          }
      }
      
      ObjectInputStream input = null;
      try {
          FileInputStream fin = new FileInputStream("address.ser");
          input = new ObjectInputStream(fin);
          CacheKey readCache = (CacheKey) input.readObject();
          int count = readCache.getUpdateCount();
      } catch (Exception e) {
          Assert.fail(String.format("An exception was thrown while serializing the class '%s': '%s',", object
                  .getClass().getName(), e.toString()));
      } finally {
          if (input != null) {
              try {
                  input.close();
              } catch (IOException e) {
                  Assert.fail(String.format("An exception was thrown while closing stream for class '%s': '%s',",
                          object.getClass().getName(), e.toString()));
              }
          }
      }
  }

@hazendaz
Copy link
Owner Author

Added code that covers this but testing is very weak at best.

Need to properly name objects a bit better.

Need to have one that has a transient List so that it breaks as that is the use case for deserialization as it will serialize fine but will lose data if populated.

Keeping this open until tests can be further refined and cleaned up. Code is solid and has been adjusted per guidence given over on mybatis.

@hazendaz hazendaz self-assigned this Nov 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant