Clover Coverage Report - guice
Coverage timestamp: Wed Mar 10 2010 18:14:24 CST
../../../img/srcFileCovDistChart10.png 0% of files have more coverage
27   112   9   3.86
0   62   0.33   7
7     1.29  
1    
 
  Asserts       Line # 34 27 9 3 91.2% 0.9117647
 
  (333)
 
1    /**
2    * Copyright (C) 2008 Google Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16   
17   
18    package com.google.inject;
19   
20    import java.io.ByteArrayInputStream;
21    import java.io.ByteArrayOutputStream;
22    import java.io.IOException;
23    import java.io.NotSerializableException;
24    import java.io.ObjectInputStream;
25    import java.io.ObjectOutputStream;
26    import junit.framework.Assert;
27    import static junit.framework.Assert.assertEquals;
28    import static junit.framework.Assert.assertNotNull;
29    import static junit.framework.Assert.assertTrue;
30   
31    /**
32    * @author jessewilson@google.com (Jesse Wilson)
33    */
 
34    public class Asserts {
 
35  0 toggle private Asserts() {}
36   
37    /**
38    * Fails unless {@code expected.equals(actual)}, {@code
39    * actual.equals(expected)} and their hash codes are equal. This is useful
40    * for testing the equals method itself.
41    */
 
42  40 toggle public static void assertEqualsBothWays(Object expected, Object actual) {
43  40 assertNotNull(expected);
44  40 assertNotNull(actual);
45  40 assertTrue("expected.equals(actual)", expected.equals(actual));
46  40 assertTrue("actual.equals(expected)", actual.equals(expected));
47  40 assertEquals("hashCode", expected.hashCode(), actual.hashCode());
48    }
49   
50    /**
51    * Fails unless {@code text} includes all {@code substrings}, in order.
52    */
 
53  690 toggle public static void assertContains(String text, String... substrings) {
54    /*if[NO_AOP]
55    // when we strip out bytecode manipulation, we lose the ability to generate some source lines.
56    if (text.contains("(Unknown Source)")) {
57    return;
58    }
59    end[NO_AOP]*/
60   
61  690 int startingFrom = 0;
62  690 for (String substring : substrings) {
63  1059 int index = text.indexOf(substring, startingFrom);
64  1059 assertTrue(String.format("Expected \"%s\" to contain substring \"%s\"", text, substring),
65    index >= startingFrom);
66  1059 startingFrom = index + substring.length();
67    }
68   
69  690 String lastSubstring = substrings[substrings.length - 1];
70  690 assertTrue(String.format("Expected \"%s\" to contain substring \"%s\" only once),",
71    text, lastSubstring), text.indexOf(lastSubstring, startingFrom) == -1);
72    }
73   
74    /**
75    * Fails unless {@code object} doesn't equal itself when reserialized.
76    */
 
77  15 toggle public static void assertEqualWhenReserialized(Object object)
78    throws IOException {
79  15 Object reserialized = reserialize(object);
80  15 assertEquals(object, reserialized);
81  15 assertEquals(object.hashCode(), reserialized.hashCode());
82    }
83   
84    /**
85    * Fails unless {@code object} has the same toString value when reserialized.
86    */
 
87  1 toggle public static void assertSimilarWhenReserialized(Object object) throws IOException {
88  1 Object reserialized = reserialize(object);
89  1 assertEquals(object.toString(), reserialized.toString());
90    }
91   
 
92  51 toggle public static <E> E reserialize(E original) throws IOException {
93  51 try {
94  51 ByteArrayOutputStream out = new ByteArrayOutputStream();
95  51 new ObjectOutputStream(out).writeObject(original);
96  18 ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
97  18 @SuppressWarnings("unchecked") // the reserialized type is assignable
98    E reserialized = (E) new ObjectInputStream(in).readObject();
99  18 return reserialized;
100    } catch (ClassNotFoundException e) {
101  0 throw new RuntimeException(e);
102    }
103    }
104   
 
105  33 toggle public static void assertNotSerializable(Object object) throws IOException {
106  33 try {
107  33 reserialize(object);
108  0 Assert.fail();
109    } catch (NotSerializableException expected) {
110    }
111    }
112    }