| 1 |
/* |
| 2 |
* Copyright (C) 2013 The Android Open Source Project |
| 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 |
package com.android.ide.common.res2; |
| 18 |
|
| 19 |
import static org.junit.Assert.assertEquals; |
| 20 |
import static org.junit.Assert.assertNotNull; |
| 21 |
import static org.junit.Assert.assertThat; |
| 22 |
import static org.junit.Assert.assertTrue; |
| 23 |
import static org.junit.Assert.fail; |
| 24 |
|
| 25 |
import com.android.ide.common.blame.Message; |
| 26 |
import com.android.ide.common.blame.SourceFilePosition; |
| 27 |
import com.android.ide.common.blame.SourcePosition; |
| 28 |
import com.google.common.collect.ImmutableList; |
| 29 |
import com.google.common.collect.Iterables; |
| 30 |
|
| 31 |
import org.junit.Test; |
| 32 |
import org.xml.sax.SAXParseException; |
| 33 |
|
| 34 |
import java.io.File; |
| 35 |
import java.io.IOException; |
| 36 |
import java.util.List; |
| 37 |
|
| 38 |
@SuppressWarnings({"ThrowableInstanceNeverThrown", "ThrowableResultOfMethodCallIgnored"}) |
| 39 |
public class MergingExceptionTest { |
| 40 |
|
| 41 |
private static final File file = new File("/some/random/path"); |
| 42 |
|
| 43 |
@Test |
| 44 |
public void testGetMessage() { |
| 45 |
assertEquals("Error: My error message", |
| 46 |
MergingException.withMessage("My error message").build().getMessage()); |
| 47 |
assertEquals("Error: My error message", |
| 48 |
MergingException.wrapException(new Exception()).withMessage( |
| 49 |
"Error: My error message").build().getMessage()); |
| 50 |
assertEquals("Error: My error message", |
| 51 |
MergingException.withMessage("Error: My error message").build().getMessage()); |
| 52 |
assertEquals("/some/random/path: Error: My error message", |
| 53 |
MergingException.wrapException(new Exception("My error message")).withFile(file) |
| 54 |
.build().getMessage()); |
| 55 |
MergingException.Builder builder2 = MergingException |
| 56 |
.wrapException(new Exception("My error message")).withFile(file) |
| 57 |
.withPosition(new SourcePosition(49, -1, -1)); |
| 58 |
assertEquals("/some/random/path:50: Error: My error message", |
| 59 |
builder2.build() |
| 60 |
.getMessage()); |
| 61 |
MergingException.Builder builder1 = MergingException |
| 62 |
.wrapException(new Exception("My error message")).withFile(file) |
| 63 |
.withPosition(new SourcePosition(49, 3, -1)); |
| 64 |
assertEquals("/some/random/path:50:4: Error: My error message", |
| 65 |
builder1.build() |
| 66 |
.getMessage()); |
| 67 |
MergingException.Builder builder = MergingException |
| 68 |
.wrapException(new Exception("My error message")).withFile(file) |
| 69 |
.withPosition(new SourcePosition(49, 3, -1)); |
| 70 |
assertEquals("/some/random/path:50:4: Error: My error message", |
| 71 |
builder.build() |
| 72 |
.getLocalizedMessage()); |
| 73 |
assertEquals("/some/random/path: Error: My error message", |
| 74 |
MergingException.withMessage("/some/random/path: My error message").withFile(file) |
| 75 |
.build().getMessage()); |
| 76 |
assertEquals("/some/random/path: Error: My error message", |
| 77 |
MergingException.withMessage("/some/random/path My error message").withFile(file) |
| 78 |
.build().getMessage()); |
| 79 |
|
| 80 |
// end of string handling checks |
| 81 |
assertEquals("/some/random/path: Error: ", |
| 82 |
MergingException.withMessage("/some/random/path").withFile(file).build() |
| 83 |
.getMessage()); |
| 84 |
assertEquals("/some/random/path: Error: ", |
| 85 |
MergingException.withMessage("/some/random/path").withFile(file).build().getMessage()); |
| 86 |
assertEquals("/some/random/path: Error: ", |
| 87 |
MergingException.withMessage("/some/random/path:").withFile(file).build().getMessage()); |
| 88 |
assertEquals("/some/random/path: Error: ", |
| 89 |
MergingException.withMessage("/some/random/path: ").withFile(file).build().getMessage()); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
@Test |
| 94 |
public void testWrapSaxParseExceptionWithLocation() { |
| 95 |
SAXParseException saxParseException = new SAXParseException("message", "", "", 5, 7); |
| 96 |
List<Message> messages = MergingException |
| 97 |
.wrapException(saxParseException).withFile(file).build().getMessages(); |
| 98 |
Message message = Iterables.getOnlyElement(messages); |
| 99 |
assertEquals("message.getKind()", Message.Kind.ERROR, message.getKind()); |
| 100 |
assertEquals("message.getText()", "message", message.getText()); |
| 101 |
assertTrue("message.getRawMessage()", |
| 102 |
message.getRawMessage().startsWith("org.xml.sax.SAXParseException")); |
| 103 |
assertEquals("message.getSourceFilePositions()", |
| 104 |
new SourceFilePosition(file, new SourcePosition(4, 6, -1)), |
| 105 |
Iterables.getOnlyElement(message.getSourceFilePositions())); |
| 106 |
} |
| 107 |
|
| 108 |
@Test |
| 109 |
public void testWrapSaxParseExceptionWithoutLocation() { |
| 110 |
SAXParseException saxParseException = new SAXParseException("message2", "", "", -1, -1); |
| 111 |
|
| 112 |
List<Message> messages = MergingException |
| 113 |
.wrapException(saxParseException).withFile(file).build().getMessages(); |
| 114 |
Message message = Iterables.getOnlyElement(messages); |
| 115 |
assertEquals("message.getKind()", Message.Kind.ERROR, message.getKind()); |
| 116 |
assertEquals("message.getText()", "message2", message.getText()); |
| 117 |
assertTrue("message.getRawMessage()", |
| 118 |
message.getRawMessage().startsWith("org.xml.sax.SAXParseException")); |
| 119 |
assertEquals("message.getSourceFilePositions()", |
| 120 |
new SourceFilePosition(file, SourcePosition.UNKNOWN), |
| 121 |
Iterables.getOnlyElement(message.getSourceFilePositions())); |
| 122 |
} |
| 123 |
|
| 124 |
@Test |
| 125 |
public void testThrowIfNonEmpty() throws MergingException{ |
| 126 |
MergingException.throwIfNonEmpty(ImmutableList.<Message>of()); |
| 127 |
try { |
| 128 |
MergingException.throwIfNonEmpty(ImmutableList.<Message>of( |
| 129 |
new Message(Message.Kind.ERROR, "Message", SourceFilePosition.UNKNOWN))); |
| 130 |
fail(); |
| 131 |
} catch (MergingException e) { |
| 132 |
// ok |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
@Test |
| 138 |
public void testMergingExceptionWithNullMessage() { |
| 139 |
MergingException exception = MergingException.wrapException(new IOException()).build(); |
| 140 |
Message message = Iterables.getOnlyElement(exception.getMessages()); |
| 141 |
assertNotNull(message.getText()); |
| 142 |
assertNotNull(message.getRawMessage()); |
| 143 |
} |
| 144 |
|
| 145 |
@Test |
| 146 |
public void testConsumerExceptionWithNullMessage() { |
| 147 |
MergingException exception = new MergeConsumer.ConsumerException(new IOException()); |
| 148 |
Message message = Iterables.getOnlyElement(exception.getMessages()); |
| 149 |
assertNotNull(message.getText()); |
| 150 |
assertNotNull(message.getRawMessage()); |
| 151 |
} |
| 152 |
} |