/bie-profile/src/test/java/org/ala/io/ProtoBufsTest.java

http://ala-bie.googlecode.com/ · Java · 85 lines · 48 code · 13 blank · 24 comment · 1 complexity · 28fc131c091b701fb467828b20be4bfe MD5 · raw file

  1. /***************************************************************************
  2. * Copyright (C) 2010 Atlas of Living Australia
  3. * All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public
  6. * License Version 1.1 (the "License"); you may not use this file
  7. * except in compliance with the License. You may obtain a copy of
  8. * the License at http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS
  11. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  12. * implied. See the License for the specific language governing
  13. * rights and limitations under the License.
  14. ***************************************************************************/
  15. package org.ala.io;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.FileOutputStream;
  19. import junit.framework.TestCase;
  20. import org.ala.io.CommonNamesProto.CommonNames;
  21. /**
  22. * Junits for testing proto bufs functionality.
  23. *
  24. * @author Dave Martin (David.Martin@csiro.au)
  25. */
  26. public class ProtoBufsTest extends TestCase {
  27. public void testCommonName() throws Exception {
  28. //to create a names list
  29. CommonNamesProto.CommonNames.Builder builder = CommonNamesProto.CommonNames.newBuilder();
  30. CommonNamesProto.CommonNames.CommonName cn1 = CommonNamesProto.CommonNames.CommonName.newBuilder()
  31. .setGuid("urn:lsid:afd.taxon:123")
  32. .setLocality("New South Wales")
  33. .setNameString("Red Kangaroo")
  34. .setDocumentId("document-1")
  35. .setInfoSourceId("infosource-1")
  36. .build();
  37. CommonNamesProto.CommonNames.CommonName cn2 = CommonNamesProto.CommonNames.CommonName.newBuilder()
  38. .setGuid("urn:lsid:afd.taxon:124")
  39. .setLocality("New South Wales")
  40. .setNameString("Eastern Grey Kangaroo")
  41. .setDocumentId("document-2")
  42. .setInfoSourceId("infosource-1")
  43. .build();
  44. //build the list
  45. builder.addNames(cn1);
  46. builder.addNames(cn2);
  47. CommonNames commonNames = builder.build();
  48. //serialise to file
  49. File tmpFile = File.createTempFile("test", ".txt");
  50. FileOutputStream fOut = new FileOutputStream(tmpFile);
  51. commonNames.writeTo(fOut);
  52. //read the protobufs from file
  53. FileInputStream fIn = new FileInputStream(tmpFile);
  54. CommonNamesProto.CommonNames deserialisedCommonNames = CommonNamesProto.CommonNames.parseFrom(fIn);
  55. System.out.println(deserialisedCommonNames.getNamesCount());
  56. for(CommonNamesProto.CommonNames.CommonName commonName: deserialisedCommonNames.getNamesList()){
  57. System.out.println("Deserialised: "+commonName.getNameString());
  58. }
  59. //add a common name
  60. CommonNamesProto.CommonNames.CommonName cn3 = CommonNamesProto.CommonNames.CommonName.newBuilder()
  61. .setGuid("urn:lsid:afd.taxon:124")
  62. .setLocality("New South Wales")
  63. .setNameString("Grey Kangaroo")
  64. .setDocumentId("document-2")
  65. .setInfoSourceId("infosource-1")
  66. .build();
  67. deserialisedCommonNames = deserialisedCommonNames.toBuilder().addNames(cn3).build();
  68. tmpFile = File.createTempFile("test", ".txt");
  69. fOut = new FileOutputStream(tmpFile);
  70. deserialisedCommonNames.writeTo(fOut);
  71. }
  72. }