OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 import protobuf_unittest.UnittestProto.ForeignEnum; | 225 import protobuf_unittest.UnittestProto.ForeignEnum; |
226 import protobuf_unittest.UnittestProto.ForeignMessage; | 226 import protobuf_unittest.UnittestProto.ForeignMessage; |
227 import protobuf_unittest.UnittestProto.TestAllExtensions; | 227 import protobuf_unittest.UnittestProto.TestAllExtensions; |
228 import protobuf_unittest.UnittestProto.TestAllExtensionsOrBuilder; | 228 import protobuf_unittest.UnittestProto.TestAllExtensionsOrBuilder; |
229 import protobuf_unittest.UnittestProto.TestAllTypes; | 229 import protobuf_unittest.UnittestProto.TestAllTypes; |
230 import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder; | 230 import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder; |
231 import protobuf_unittest.UnittestProto.TestOneof2; | 231 import protobuf_unittest.UnittestProto.TestOneof2; |
232 import protobuf_unittest.UnittestProto.TestPackedExtensions; | 232 import protobuf_unittest.UnittestProto.TestPackedExtensions; |
233 import protobuf_unittest.UnittestProto.TestPackedTypes; | 233 import protobuf_unittest.UnittestProto.TestPackedTypes; |
234 import protobuf_unittest.UnittestProto.TestUnpackedTypes; | 234 import protobuf_unittest.UnittestProto.TestUnpackedTypes; |
235 | |
236 import junit.framework.Assert; | |
237 | |
238 import java.io.File; | 235 import java.io.File; |
239 import java.io.IOException; | 236 import java.io.IOException; |
240 import java.io.RandomAccessFile; | 237 import java.io.RandomAccessFile; |
| 238 import junit.framework.Assert; |
241 | 239 |
242 /** | 240 /** |
243 * Contains methods for setting all fields of {@code TestAllTypes} to | 241 * Contains methods for setting all fields of {@code TestAllTypes} to |
244 * some values as well as checking that all the fields are set to those values. | 242 * some values as well as checking that all the fields are set to those values. |
245 * These are useful for testing various protocol message features, e.g. | 243 * These are useful for testing various protocol message features, e.g. |
246 * set all fields of a message, serialize it, parse it, and check that all | 244 * set all fields of a message, serialize it, parse it, and check that all |
247 * fields are set. | 245 * fields are set. |
248 * | 246 * |
249 * <p>This code is not to be used outside of {@code com.google.protobuf} and | 247 * <p>This code is not to be used outside of {@code com.google.protobuf} and |
250 * subpackages. | 248 * subpackages. |
251 * | 249 * |
252 * @author kenton@google.com Kenton Varda | 250 * @author kenton@google.com Kenton Varda |
253 */ | 251 */ |
254 public final class TestUtil { | 252 public final class TestUtil { |
255 private TestUtil() {} | 253 private TestUtil() {} |
256 | 254 |
257 /** Helper to convert a String to ByteString. */ | 255 /** Helper to convert a String to ByteString. */ |
258 static ByteString toBytes(String str) { | 256 static ByteString toBytes(String str) { |
259 return ByteString.copyFrom(str.getBytes(Internal.UTF_8)); | 257 return ByteString.copyFrom(str.getBytes(Internal.UTF_8)); |
260 } | 258 } |
261 | 259 |
262 /** | 260 /** |
| 261 * Dirties the message by resetting the momoized serialized size. |
| 262 */ |
| 263 public static void resetMemoizedSize(AbstractMessage message) { |
| 264 message.memoizedSize = -1; |
| 265 } |
| 266 |
| 267 /** |
263 * Get a {@code TestAllTypes} with all fields set as they would be by | 268 * Get a {@code TestAllTypes} with all fields set as they would be by |
264 * {@link #setAllFields(TestAllTypes.Builder)}. | 269 * {@link #setAllFields(TestAllTypes.Builder)}. |
265 */ | 270 */ |
266 public static TestAllTypes getAllSet() { | 271 public static TestAllTypes getAllSet() { |
267 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | 272 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); |
268 setAllFields(builder); | 273 setAllFields(builder); |
269 return builder.build(); | 274 return builder.build(); |
270 } | 275 } |
271 | 276 |
272 /** | 277 /** |
(...skipping 3484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3757 /** | 3762 /** |
3758 * @param filePath The path relative to | 3763 * @param filePath The path relative to |
3759 * {@link #getTestDataDir}. | 3764 * {@link #getTestDataDir}. |
3760 */ | 3765 */ |
3761 public static String readTextFromFile(String filePath) { | 3766 public static String readTextFromFile(String filePath) { |
3762 return readBytesFromFile(filePath).toStringUtf8(); | 3767 return readBytesFromFile(filePath).toStringUtf8(); |
3763 } | 3768 } |
3764 | 3769 |
3765 private static File getTestDataDir() { | 3770 private static File getTestDataDir() { |
3766 // Search each parent directory looking for "src/google/protobuf". | 3771 // Search each parent directory looking for "src/google/protobuf". |
3767 File ancestor = new File("."); | 3772 File ancestor = new File(System.getProperty("protobuf.dir", ".")); |
| 3773 String initialPath = ancestor.getAbsolutePath(); |
3768 try { | 3774 try { |
3769 ancestor = ancestor.getCanonicalFile(); | 3775 ancestor = ancestor.getCanonicalFile(); |
3770 } catch (IOException e) { | 3776 } catch (IOException e) { |
3771 throw new RuntimeException( | 3777 throw new RuntimeException( |
3772 "Couldn't get canonical name of working directory.", e); | 3778 "Couldn't get canonical name of working directory.", e); |
3773 } | 3779 } |
3774 while (ancestor != null && ancestor.exists()) { | 3780 while (ancestor != null && ancestor.exists()) { |
3775 if (new File(ancestor, "src/google/protobuf").exists()) { | 3781 if (new File(ancestor, "src/google/protobuf").exists()) { |
3776 return new File(ancestor, "src/google/protobuf/testdata"); | 3782 return new File(ancestor, "src/google/protobuf/testdata"); |
3777 } | 3783 } |
3778 ancestor = ancestor.getParentFile(); | 3784 ancestor = ancestor.getParentFile(); |
3779 } | 3785 } |
3780 | 3786 |
3781 throw new RuntimeException( | 3787 throw new RuntimeException( |
3782 "Could not find golden files. This test must be run from within the " + | 3788 "Could not find golden files. This test must be run from within the " + |
3783 "protobuf source package so that it can read test data files from the " + | 3789 "protobuf source package so that it can read test data files from the " + |
3784 "C++ source tree."); | 3790 "C++ source tree: " + initialPath); |
3785 } | 3791 } |
3786 | 3792 |
3787 /** | 3793 /** |
3788 * @param filename The path relative to | 3794 * @param filename The path relative to |
3789 * {@link #getTestDataDir}. | 3795 * {@link #getTestDataDir}. |
3790 */ | 3796 */ |
3791 public static ByteString readBytesFromFile(String filename) { | 3797 public static ByteString readBytesFromFile(String filename) { |
3792 File fullPath = new File(getTestDataDir(), filename); | 3798 File fullPath = new File(getTestDataDir(), filename); |
3793 try { | 3799 try { |
3794 RandomAccessFile file = new RandomAccessFile(fullPath, "r"); | 3800 RandomAccessFile file = new RandomAccessFile(fullPath, "r"); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3848 @Override | 3854 @Override |
3849 public void markDirty() { | 3855 public void markDirty() { |
3850 invalidations++; | 3856 invalidations++; |
3851 } | 3857 } |
3852 | 3858 |
3853 public int getInvalidationCount() { | 3859 public int getInvalidationCount() { |
3854 return invalidations; | 3860 return invalidations; |
3855 } | 3861 } |
3856 } | 3862 } |
3857 } | 3863 } |
OLD | NEW |