Search in sources :

Example 21 with Matcher

use of org.hamcrest.Matcher in project material-components-android by material-components.

the class NavigationViewTest method testBackground.

@Test
public void testBackground() {
    // Open our drawer
    onView(withId(R.id.drawer_layout)).perform(openDrawer(GravityCompat.START));
    final Resources res = activityTestRule.getActivity().getResources();
    @ColorInt final int defaultFillColor = ResourcesCompat.getColor(res, R.color.sand_default, null);
    // Check the default fill color of the menu items in our NavigationView
    for (int i = 0; i < MENU_CONTENT_ITEM_IDS.length; i++) {
        // Note that here we're tying ourselves to the implementation details of the
        // internal structure of the NavigationView. Specifically, we're looking at the
        // direct child of RecyclerView which is expected to have the background set
        // on it. If the internal implementation of NavigationView changes, the second
        // Matcher below will need to be tweaked.
        Matcher menuItemMatcher = allOf(hasDescendant(withText(mMenuStringContent.get(MENU_CONTENT_ITEM_IDS[i]))), isChildOfA(isAssignableFrom(RecyclerView.class)), isDescendantOfA(withId(R.id.start_drawer)));
        onView(menuItemMatcher).check(matches(withBackgroundFill(defaultFillColor)));
    }
    // Set a new background (flat fill color) on our NavigationView
    onView(withId(R.id.start_drawer)).perform(setItemBackgroundResource(R.drawable.test_background_blue));
    // And check that all the menu items have the new fill
    @ColorInt final int newFillColorBlue = ResourcesCompat.getColor(res, R.color.test_blue, null);
    for (int i = 0; i < MENU_CONTENT_ITEM_IDS.length; i++) {
        Matcher menuItemMatcher = allOf(hasDescendant(withText(mMenuStringContent.get(MENU_CONTENT_ITEM_IDS[i]))), isChildOfA(isAssignableFrom(RecyclerView.class)), isDescendantOfA(withId(R.id.start_drawer)));
        onView(menuItemMatcher).check(matches(withBackgroundFill(newFillColorBlue)));
    }
    // Set another new background on our NavigationView
    onView(withId(R.id.start_drawer)).perform(setItemBackground(ResourcesCompat.getDrawable(res, R.drawable.test_background_green, null)));
    // And check that all the menu items have the new fill
    @ColorInt final int newFillColorGreen = ResourcesCompat.getColor(res, R.color.test_green, null);
    for (int i = 0; i < MENU_CONTENT_ITEM_IDS.length; i++) {
        Matcher menuItemMatcher = allOf(hasDescendant(withText(mMenuStringContent.get(MENU_CONTENT_ITEM_IDS[i]))), isChildOfA(isAssignableFrom(RecyclerView.class)), isDescendantOfA(withId(R.id.start_drawer)));
        onView(menuItemMatcher).check(matches(withBackgroundFill(newFillColorGreen)));
    }
}
Also used : ColorInt(android.support.annotation.ColorInt) Matcher(org.hamcrest.Matcher) Resources(android.content.res.Resources) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 22 with Matcher

use of org.hamcrest.Matcher in project neo4j by neo4j.

the class CappedLoggerTest method assertLoggedLines.

public void assertLoggedLines(String[] lines, int count, int skip) throws IOException {
    Matcher<String>[] matchers = new Matcher[count];
    int i;
    for (i = 0; i < skip; i++) {
        matchers[i] = any(String.class);
    }
    for (; i < count; i++) {
        String line = lines[i];
        matchers[i] = containsString(line);
    }
    logProvider.assertContainsLogCallsMatching(skip, matchers);
}
Also used : Matcher(org.hamcrest.Matcher) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 23 with Matcher

use of org.hamcrest.Matcher in project pulsar by yahoo.

the class NamespacesTest method testDeleteNamespaceWithBundles.

@Test
public void testDeleteNamespaceWithBundles() throws Exception {
    URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
    String bundledNsLocal = "test-bundled-namespace-1";
    BundlesData bundleData = new BundlesData(Lists.newArrayList("0x00000000", "0x80000000", "0xffffffff"));
    createBundledTestNamespaces(this.testProperty, this.testLocalCluster, bundledNsLocal, bundleData);
    final NamespaceName testNs = new NamespaceName(this.testProperty, this.testLocalCluster, bundledNsLocal);
    com.yahoo.pulsar.client.admin.Namespaces namespacesAdmin = mock(com.yahoo.pulsar.client.admin.Namespaces.class);
    doReturn(namespacesAdmin).when(admin).namespaces();
    doReturn(null).when(nsSvc).getWebServiceUrl(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        }

        @Override
        public boolean matches(Object item) {
            if (item instanceof NamespaceBundle) {
                NamespaceBundle bundle = (NamespaceBundle) item;
                return bundle.getNamespaceObject().equals(testNs);
            }
            return false;
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        }
    }), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyBoolean());
    doReturn(false).when(nsSvc).isServiceUnitOwned(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        }

        @Override
        public boolean matches(Object item) {
            if (item instanceof NamespaceBundle) {
                NamespaceBundle bundle = (NamespaceBundle) item;
                return bundle.getNamespaceObject().equals(testNs);
            }
            return false;
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        }
    }));
    doReturn(Optional.of(new NamespaceEphemeralData())).when(nsSvc).getOwner(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        }

        @Override
        public boolean matches(Object item) {
            if (item instanceof NamespaceBundle) {
                NamespaceBundle bundle = (NamespaceBundle) item;
                return bundle.getNamespaceObject().equals(testNs);
            }
            return false;
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        }
    }));
    doThrow(new PulsarAdminException.PreconditionFailedException(new ClientErrorException(Status.PRECONDITION_FAILED))).when(namespacesAdmin).deleteNamespaceBundle(Mockito.anyString(), Mockito.anyString());
    try {
        namespaces.deleteNamespaceBundle(testProperty, testLocalCluster, bundledNsLocal, "0x00000000_0x80000000", false);
        fail("Should have failed");
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    try {
        namespaces.deleteNamespace(testProperty, testLocalCluster, bundledNsLocal, false);
        fail("Should have failed");
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    NamespaceBundles nsBundles = nsSvc.getNamespaceBundleFactory().getBundles(testNs, bundleData);
    // make one bundle owned
    doReturn(localWebServiceUrl).when(nsSvc).getWebServiceUrl(nsBundles.getBundles().get(0), false, true, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(nsBundles.getBundles().get(0));
    doNothing().when(namespacesAdmin).deleteNamespaceBundle(testProperty + "/" + testLocalCluster + "/" + bundledNsLocal, "0x00000000_0x80000000");
    try {
        namespaces.deleteNamespaceBundle(testProperty, testLocalCluster, bundledNsLocal, "0x80000000_0xffffffff", false);
        fail("Should have failed");
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    try {
        namespaces.deleteNamespace(testProperty, testLocalCluster, bundledNsLocal, false);
        fail("should have failed");
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    // ensure all three bundles are owned by the local broker
    for (NamespaceBundle bundle : nsBundles.getBundles()) {
        doReturn(localWebServiceUrl).when(nsSvc).getWebServiceUrl(bundle, false, true, false);
        doReturn(true).when(nsSvc).isServiceUnitOwned(bundle);
    }
    doNothing().when(namespacesAdmin).deleteNamespaceBundle(Mockito.anyString(), Mockito.anyString());
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) Description(org.hamcrest.Description) Matcher(org.hamcrest.Matcher) NamespaceBundles(com.yahoo.pulsar.common.naming.NamespaceBundles) BundlesData(com.yahoo.pulsar.common.policies.data.BundlesData) RestException(com.yahoo.pulsar.broker.web.RestException) URL(java.net.URL) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) ClientErrorException(javax.ws.rs.ClientErrorException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) NamespaceEphemeralData(com.yahoo.pulsar.broker.namespace.NamespaceEphemeralData) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 24 with Matcher

use of org.hamcrest.Matcher in project pulsar by yahoo.

the class NamespacesTest method testUnloadNamespaceWithBundles.

@Test
public void testUnloadNamespaceWithBundles() throws Exception {
    URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
    String bundledNsLocal = "test-bundled-namespace-1";
    BundlesData bundleData = new BundlesData(Lists.newArrayList("0x00000000", "0x80000000", "0xffffffff"));
    createBundledTestNamespaces(this.testProperty, this.testLocalCluster, bundledNsLocal, bundleData);
    final NamespaceName testNs = new NamespaceName(this.testProperty, this.testLocalCluster, bundledNsLocal);
    doReturn(localWebServiceUrl).when(nsSvc).getWebServiceUrl(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        // TODO Auto-generated method stub
        }

        @Override
        public boolean matches(Object item) {
            if (item instanceof NamespaceBundle) {
                NamespaceBundle bundle = (NamespaceBundle) item;
                return bundle.getNamespaceObject().equals(testNs);
            }
            return false;
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        // TODO Auto-generated method stub
        }
    }), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyBoolean());
    doReturn(true).when(nsSvc).isServiceUnitOwned(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        // TODO Auto-generated method stub
        }

        @Override
        public boolean matches(Object item) {
            if (item instanceof NamespaceBundle) {
                NamespaceBundle bundle = (NamespaceBundle) item;
                return bundle.getNamespaceObject().equals(testNs);
            }
            return false;
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        // TODO Auto-generated method stub
        }
    }));
    NamespaceBundles nsBundles = nsSvc.getNamespaceBundleFactory().getBundles(testNs, bundleData);
    NamespaceBundle testBundle = nsBundles.getBundles().get(0);
    // make one bundle owned
    doReturn(localWebServiceUrl).when(nsSvc).getWebServiceUrl(testBundle, false, true, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(testBundle);
    doNothing().when(nsSvc).unloadNamespaceBundle(testBundle);
    namespaces.unloadNamespaceBundle(testProperty, testLocalCluster, bundledNsLocal, "0x00000000_0x80000000", false);
    verify(nsSvc, times(1)).unloadNamespaceBundle(testBundle);
    try {
        namespaces.unloadNamespaceBundle(testProperty, testLocalCluster, bundledNsLocal, "0x00000000_0x88000000", false);
        fail("should have failed");
    } catch (RestException re) {
    // ok
    }
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Description(org.hamcrest.Description) Matcher(org.hamcrest.Matcher) NamespaceBundles(com.yahoo.pulsar.common.naming.NamespaceBundles) BundlesData(com.yahoo.pulsar.common.policies.data.BundlesData) RestException(com.yahoo.pulsar.broker.web.RestException) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 25 with Matcher

use of org.hamcrest.Matcher in project pulsar by yahoo.

the class NamespacesTest method mockWebUrl.

private void mockWebUrl(URL localWebServiceUrl, NamespaceName namespace) throws Exception {
    doReturn(localWebServiceUrl).when(nsSvc).getWebServiceUrl(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        }

        @Override
        public boolean matches(Object item) {
            if (item instanceof NamespaceBundle) {
                NamespaceBundle bundle = (NamespaceBundle) item;
                return bundle.getNamespaceObject().equals(namespace);
            }
            return false;
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        }
    }), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyBoolean());
    doReturn(true).when(nsSvc).isServiceUnitOwned(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        }

        @Override
        public boolean matches(Object item) {
            if (item instanceof NamespaceBundle) {
                NamespaceBundle bundle = (NamespaceBundle) item;
                return bundle.getNamespaceObject().equals(namespace);
            }
            return false;
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        }
    }));
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) Description(org.hamcrest.Description) Matcher(org.hamcrest.Matcher)

Aggregations

Matcher (org.hamcrest.Matcher)28 Description (org.hamcrest.Description)7 View (android.view.View)6 Test (org.junit.Test)6 Espresso.onView (android.support.test.espresso.Espresso.onView)4 UiController (android.support.test.espresso.UiController)4 ViewAction (android.support.test.espresso.ViewAction)4 TextView (android.widget.TextView)4 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)4 NamespaceBundle (com.yahoo.pulsar.common.naming.NamespaceBundle)4 NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)4 URL (java.net.URL)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Test (org.testng.annotations.Test)4 RestException (com.yahoo.pulsar.broker.web.RestException)3 ArrayList (java.util.ArrayList)3 Resources (android.content.res.Resources)2 ColorInt (android.support.annotation.ColorInt)2 MediumTest (android.support.test.filters.MediumTest)2 ViewGroup (android.view.ViewGroup)2