PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/protostuff-runtime-registry/src/test/java/com/dyuproject/protostuff/runtime/IncrementalRuntimeObjectSchemaTest.java

http://protostuff.googlecode.com/
Java | 158 lines | 98 code | 26 blank | 34 comment | 9 complexity | 0297152f4cac8e5e37fcd33a86e007ad MD5 | raw file
Possible License(s): Apache-2.0
  1. //================================================================================
  2. //Copyright (c) 2012, David Yu
  3. //All rights reserved.
  4. //--------------------------------------------------------------------------------
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // 1. Redistributions of source code must retain the above copyright notice,
  8. // this list of conditions and the following disclaimer.
  9. // 2. Redistributions in binary form must reproduce the above copyright notice,
  10. // this list of conditions and the following disclaimer in the documentation
  11. // and/or other materials provided with the distribution.
  12. // 3. Neither the name of protostuff nor the names of its contributors may be used
  13. // to endorse or promote products derived from this software without
  14. // specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. // POSSIBILITY OF SUCH DAMAGE.
  27. //================================================================================
  28. package com.dyuproject.protostuff.runtime;
  29. import static com.dyuproject.protostuff.runtime.SampleDelegates.SINGLETON_DELEGATE;
  30. import junit.framework.TestCase;
  31. import com.dyuproject.protostuff.CollectionSchema;
  32. import com.dyuproject.protostuff.MapSchema;
  33. import com.dyuproject.protostuff.runtime.AbstractRuntimeObjectSchemaTest.AcousticGuitar;
  34. import com.dyuproject.protostuff.runtime.AbstractRuntimeObjectSchemaTest.BassGuitar;
  35. import com.dyuproject.protostuff.runtime.AbstractRuntimeObjectSchemaTest.Bat;
  36. import com.dyuproject.protostuff.runtime.AbstractRuntimeObjectSchemaTest.CustomArrayList;
  37. import com.dyuproject.protostuff.runtime.AbstractRuntimeObjectSchemaTest.CustomHashMap;
  38. import com.dyuproject.protostuff.runtime.AbstractRuntimeObjectSchemaTest.GuitarPickup;
  39. import com.dyuproject.protostuff.runtime.AbstractRuntimeObjectSchemaTest.Pojo;
  40. import com.dyuproject.protostuff.runtime.AbstractRuntimeObjectSchemaTest.PojoWithArray;
  41. import com.dyuproject.protostuff.runtime.AbstractRuntimeObjectSchemaTest.PojoWithArray2D;
  42. import com.dyuproject.protostuff.runtime.AbstractRuntimeObjectSchemaTest.PojoWithCollection;
  43. import com.dyuproject.protostuff.runtime.AbstractRuntimeObjectSchemaTest.PojoWithCustomArrayListAndHashMap;
  44. import com.dyuproject.protostuff.runtime.AbstractRuntimeObjectSchemaTest.PojoWithMap;
  45. import com.dyuproject.protostuff.runtime.AbstractRuntimeObjectSchemaTest.Size;
  46. import com.dyuproject.protostuff.runtime.AbstractRuntimeObjectSchemaTest.WrapsBat;
  47. import com.dyuproject.protostuff.runtime.SampleDelegates.ShortArrayDelegate;
  48. /**
  49. * Test for {@link IncrementalIdStrategy}.
  50. *
  51. * @author David Yu
  52. * @created Mar 29, 2012
  53. */
  54. public class IncrementalRuntimeObjectSchemaTest extends TestCase
  55. {
  56. static final boolean runTest;
  57. static
  58. {
  59. // check whether test/run from root module
  60. String strategy = System.getProperty("test_id_strategy");
  61. runTest = strategy == null || strategy.equals("incremental");
  62. if(runTest)
  63. {
  64. System.setProperty("protostuff.runtime.id_strategy_factory",
  65. "com.dyuproject.protostuff.runtime.IncrementalRuntimeObjectSchemaTest$IdStrategyFactory");
  66. }
  67. }
  68. public static class IdStrategyFactory implements IdStrategy.Factory
  69. {
  70. static int INSTANCE_COUNT = 0;
  71. IncrementalIdStrategy.Registry r = new IncrementalIdStrategy.Registry(
  72. 20, 11,
  73. 20, 11,
  74. 20, 11,
  75. 80, 11);
  76. public IdStrategyFactory()
  77. {
  78. ++INSTANCE_COUNT;
  79. System.out.println("@INCREMENTAL");
  80. }
  81. public IdStrategy create()
  82. {
  83. return r.strategy;
  84. }
  85. public void postCreate()
  86. {
  87. r.registerCollection(CollectionSchema.MessageFactories.ArrayList, 1)
  88. .registerCollection(CollectionSchema.MessageFactories.HashSet, 2)
  89. .registerCollection(CustomArrayList.MESSAGE_FACTORY, 3);
  90. r.registerMap(MapSchema.MessageFactories.HashMap, 1)
  91. .registerMap(MapSchema.MessageFactories.LinkedHashMap, 2)
  92. .registerMap(CustomHashMap.MESSAGE_FACTORY, 3);
  93. r.registerEnum(Size.class, 1)
  94. .registerEnum(GuitarPickup.class, 2);
  95. r.registerPojo(AcousticGuitar.class, 1)
  96. .registerPojo(BassGuitar.class, 2)
  97. .registerPojo(Pojo.class, 3)
  98. .registerPojo(PojoWithArray.class, 4)
  99. .registerPojo(PojoWithArray2D.class, 5)
  100. .registerPojo(PojoWithCollection.class, 6)
  101. .registerPojo(PojoWithMap.class, 7)
  102. .registerPojo(Bat.SCHEMA, Bat.PIPE_SCHEMA, 8)
  103. .registerPojo(WrapsBat.class, 9)
  104. .registerPojo(PojoWithCustomArrayListAndHashMap.class, 10);
  105. r.registerDelegate(new ShortArrayDelegate(), 1);
  106. r.registerDelegate(SINGLETON_DELEGATE, 2);
  107. r = null;
  108. }
  109. }
  110. public void testProtostuff() throws Exception
  111. {
  112. if(runTest && RuntimeEnv.ID_STRATEGY instanceof IncrementalIdStrategy)
  113. {
  114. junit.textui.TestRunner tr = new junit.textui.TestRunner();
  115. tr.doRun(tr.getTest(
  116. "com.dyuproject.protostuff.runtime.ProtostuffRuntimeObjectSchemaTest"
  117. ), false);
  118. assertTrue(IdStrategyFactory.INSTANCE_COUNT != 0);
  119. }
  120. }
  121. public void testProtobuf() throws Exception
  122. {
  123. if(runTest && RuntimeEnv.ID_STRATEGY instanceof IncrementalIdStrategy)
  124. {
  125. junit.textui.TestRunner tr = new junit.textui.TestRunner();
  126. tr.doRun(tr.getTest(
  127. "com.dyuproject.protostuff.runtime.ProtobufRuntimeObjectSchemaTest"
  128. ), false);
  129. assertTrue(IdStrategyFactory.INSTANCE_COUNT != 0);
  130. }
  131. }
  132. }