MetaDataTest.java revision 47a4ba7be3fd9069df963aafcac49fdb96ca1426
14ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrishpackage org.robolectric.manifest;
24ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish
34ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrishimport com.google.common.collect.ImmutableList;
44ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrishimport org.junit.Before;
54ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrishimport org.junit.Test;
64ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrishimport org.junit.runner.RunWith;
74ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrishimport org.junit.runners.JUnit4;
84ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrishimport org.mockito.Mock;
94ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrishimport org.mockito.MockitoAnnotations;
109cdf65df76a6329399f29da4f4fdfeebdd2ac787Jonathan Gerrishimport org.robolectric.res.ResourceTable;
114ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrishimport org.w3c.dom.Element;
124ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrishimport org.w3c.dom.Node;
134ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish
144ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrishimport javax.xml.parsers.DocumentBuilder;
154ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrishimport javax.xml.parsers.DocumentBuilderFactory;
164ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrishimport javax.xml.parsers.ParserConfigurationException;
174ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish
184ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish/**
194ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish * Tests for {@link MetaData}
204ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish */
214ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish@RunWith(JUnit4.class)
224ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrishpublic class MetaDataTest {
234ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish
249cdf65df76a6329399f29da4f4fdfeebdd2ac787Jonathan Gerrish  @Mock private ResourceTable resourceProvider;
254ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish
264ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish  @Before
274ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish  public void setUp() {
284ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish    MockitoAnnotations.initMocks(this);
294ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish  }
304ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish
3147a4ba7be3fd9069df963aafcac49fdb96ca1426Christian Williams  @Test(expected = RoboNotFoundException.class)
3247a4ba7be3fd9069df963aafcac49fdb96ca1426Christian Williams  public void testNonExistantResource_throwsResourceNotFoundException() throws Exception {
334ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish    Element metaDataElement = createMetaDataNode("aName", "@xml/non_existant_resource");
344ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish
354ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish    MetaData metaData = new MetaData(ImmutableList.<Node>of(metaDataElement));
366a7bdc7a1b1fbe38142995bde5f05023b8ab86adJonathan Gerrish    metaData.init(resourceProvider, "a.package");
374ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish  }
384ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish
394ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish  private static Element createMetaDataNode(String name, String value) {
404ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
414ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish    Element metaDataElement;
424ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish    try {
434ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish      DocumentBuilder db = dbf.newDocumentBuilder();
444ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish      metaDataElement = db.newDocument().createElement("meta-data");
454ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish      metaDataElement.setAttribute("android:name", name);
464ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish      metaDataElement.setAttribute("android:value", value);
474ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish    } catch (ParserConfigurationException e) {
484ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish      throw new RuntimeException(e);
494ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish    }
504ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish    return metaDataElement;
514ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish  }
524ed75595fae0ca16a2531b26099603810af54f57Jonathan Gerrish}
53