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 |
235 import java.io.File; | 238 import java.io.File; |
236 import java.io.IOException; | 239 import java.io.IOException; |
237 import java.io.RandomAccessFile; | 240 import java.io.RandomAccessFile; |
238 import junit.framework.Assert; | |
239 | 241 |
240 /** | 242 /** |
241 * Contains methods for setting all fields of {@code TestAllTypes} to | 243 * Contains methods for setting all fields of {@code TestAllTypes} to |
242 * some values as well as checking that all the fields are set to those values. | 244 * some values as well as checking that all the fields are set to those values. |
243 * These are useful for testing various protocol message features, e.g. | 245 * These are useful for testing various protocol message features, e.g. |
244 * set all fields of a message, serialize it, parse it, and check that all | 246 * set all fields of a message, serialize it, parse it, and check that all |
245 * fields are set. | 247 * fields are set. |
246 * | 248 * |
247 * <p>This code is not to be used outside of {@code com.google.protobuf} and | 249 * <p>This code is not to be used outside of {@code com.google.protobuf} and |
248 * subpackages. | 250 * subpackages. |
249 * | 251 * |
250 * @author kenton@google.com Kenton Varda | 252 * @author kenton@google.com Kenton Varda |
251 */ | 253 */ |
252 public final class TestUtil { | 254 public final class TestUtil { |
253 private TestUtil() {} | 255 private TestUtil() {} |
254 | 256 |
255 /** Helper to convert a String to ByteString. */ | 257 /** Helper to convert a String to ByteString. */ |
256 static ByteString toBytes(String str) { | 258 static ByteString toBytes(String str) { |
257 return ByteString.copyFrom(str.getBytes(Internal.UTF_8)); | 259 return ByteString.copyFrom(str.getBytes(Internal.UTF_8)); |
258 } | 260 } |
259 | 261 |
260 /** | 262 /** |
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 /** | |
268 * Get a {@code TestAllTypes} with all fields set as they would be by | 263 * Get a {@code TestAllTypes} with all fields set as they would be by |
269 * {@link #setAllFields(TestAllTypes.Builder)}. | 264 * {@link #setAllFields(TestAllTypes.Builder)}. |
270 */ | 265 */ |
271 public static TestAllTypes getAllSet() { | 266 public static TestAllTypes getAllSet() { |
272 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | 267 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); |
273 setAllFields(builder); | 268 setAllFields(builder); |
274 return builder.build(); | 269 return builder.build(); |
275 } | 270 } |
276 | 271 |
277 /** | 272 /** |
(...skipping 3484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3762 /** | 3757 /** |
3763 * @param filePath The path relative to | 3758 * @param filePath The path relative to |
3764 * {@link #getTestDataDir}. | 3759 * {@link #getTestDataDir}. |
3765 */ | 3760 */ |
3766 public static String readTextFromFile(String filePath) { | 3761 public static String readTextFromFile(String filePath) { |
3767 return readBytesFromFile(filePath).toStringUtf8(); | 3762 return readBytesFromFile(filePath).toStringUtf8(); |
3768 } | 3763 } |
3769 | 3764 |
3770 private static File getTestDataDir() { | 3765 private static File getTestDataDir() { |
3771 // Search each parent directory looking for "src/google/protobuf". | 3766 // Search each parent directory looking for "src/google/protobuf". |
3772 File ancestor = new File(System.getProperty("protobuf.dir", ".")); | 3767 File ancestor = new File("."); |
3773 String initialPath = ancestor.getAbsolutePath(); | |
3774 try { | 3768 try { |
3775 ancestor = ancestor.getCanonicalFile(); | 3769 ancestor = ancestor.getCanonicalFile(); |
3776 } catch (IOException e) { | 3770 } catch (IOException e) { |
3777 throw new RuntimeException( | 3771 throw new RuntimeException( |
3778 "Couldn't get canonical name of working directory.", e); | 3772 "Couldn't get canonical name of working directory.", e); |
3779 } | 3773 } |
3780 while (ancestor != null && ancestor.exists()) { | 3774 while (ancestor != null && ancestor.exists()) { |
3781 if (new File(ancestor, "src/google/protobuf").exists()) { | 3775 if (new File(ancestor, "src/google/protobuf").exists()) { |
3782 return new File(ancestor, "src/google/protobuf/testdata"); | 3776 return new File(ancestor, "src/google/protobuf/testdata"); |
3783 } | 3777 } |
3784 ancestor = ancestor.getParentFile(); | 3778 ancestor = ancestor.getParentFile(); |
3785 } | 3779 } |
3786 | 3780 |
3787 throw new RuntimeException( | 3781 throw new RuntimeException( |
3788 "Could not find golden files. This test must be run from within the " + | 3782 "Could not find golden files. This test must be run from within the " + |
3789 "protobuf source package so that it can read test data files from the " + | 3783 "protobuf source package so that it can read test data files from the " + |
3790 "C++ source tree: " + initialPath); | 3784 "C++ source tree."); |
3791 } | 3785 } |
3792 | 3786 |
3793 /** | 3787 /** |
3794 * @param filename The path relative to | 3788 * @param filename The path relative to |
3795 * {@link #getTestDataDir}. | 3789 * {@link #getTestDataDir}. |
3796 */ | 3790 */ |
3797 public static ByteString readBytesFromFile(String filename) { | 3791 public static ByteString readBytesFromFile(String filename) { |
3798 File fullPath = new File(getTestDataDir(), filename); | 3792 File fullPath = new File(getTestDataDir(), filename); |
3799 try { | 3793 try { |
3800 RandomAccessFile file = new RandomAccessFile(fullPath, "r"); | 3794 RandomAccessFile file = new RandomAccessFile(fullPath, "r"); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3854 @Override | 3848 @Override |
3855 public void markDirty() { | 3849 public void markDirty() { |
3856 invalidations++; | 3850 invalidations++; |
3857 } | 3851 } |
3858 | 3852 |
3859 public int getInvalidationCount() { | 3853 public int getInvalidationCount() { |
3860 return invalidations; | 3854 return invalidations; |
3861 } | 3855 } |
3862 } | 3856 } |
3863 } | 3857 } |
OLD | NEW |