Java Code Examples for org.powermock.api.easymock.PowerMock#createNiceMock()

The following examples show how to use org.powermock.api.easymock.PowerMock#createNiceMock() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OdeAuthFilterTest.java    From appinventor-extensions with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  helper.setUp();
  localUserMock = PowerMock.createMock(LocalUser.class);
  PowerMock.mockStatic(LocalUser.class);
  expect(LocalUser.getInstance()).andReturn(localUserMock).anyTimes();
  localUserMock.set(new User("1", "NonSuch", "NoName", null, 0, false, false, 0, null));
  expectLastCall().times(1);
  expect(localUserMock.getUserEmail()).andReturn("NonSuch").times(1);
  localUserInfo = PowerMock.createMock(OdeAuthFilter.UserInfo.class);
  expect(localUserInfo.buildCookie(false)).andReturn("NoCookie").anyTimes();
  expect(localUserInfo.buildCookie(true)).andReturn("NoCookie").anyTimes();
  mockFilterChain = PowerMock.createNiceMock(FilterChain.class);
  mockServletRequest = PowerMock.createNiceMock(HttpServletRequest.class);
  mockServletResponse = PowerMock.createNiceMock(HttpServletResponse.class);
}
 
Example 2
Source File: DownloadServletTest.java    From appinventor-extensions with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  PowerMock.mockStatic(LocalUser.class);
  localUserMock = PowerMock.createNiceMock(LocalUser.class);
  expect(LocalUser.getInstance()).andReturn(localUserMock).anyTimes();
  expect(localUserMock.getUserId()).andReturn(USER_ID).anyTimes();
  exporterMock = PowerMock.createNiceMock(FileExporterImpl.class);
  PowerMock.expectNew(FileExporterImpl.class).andReturn(exporterMock).anyTimes();

  storageIoMock = PowerMock.createNiceMock(StorageIo.class);
  StorageIoInstanceHolder.setInstance(storageIoMock);

  dummyZip = new ProjectSourceZip(DUMMY_ZIP_FILENAME, new byte[] {}, 2);
  dummyZipWithTitle = new ProjectSourceZip(DUMMY_ZIP_FILENAME_WITH_TITLE, new byte[] {}, 2);
  dummyApk = new RawFile(DUMMY_APK_FILENAME, new byte[] {});
  dummyFile = new RawFile(DUMMY_FILENAME, new byte[] {});
}
 
Example 3
Source File: EclipseMocker.java    From saros with GNU General Public License v2.0 6 votes vote down vote up
/** Mock a somewhat clever Saros, with a version number and a preference store. */
public static Saros mockSaros() {
  // saros.getBundle() is final --> need PowerMock
  Saros saros = PowerMock.createNiceMock(Saros.class);

  Bundle bundle = createNiceMock(Bundle.class);
  expect(bundle.getVersion()).andStubReturn(new Version("1.0.0.dummy"));
  expect(saros.getBundle()).andStubReturn(bundle);

  expect(saros.getPreferenceStore()).andStubReturn(new EclipseMemoryPreferenceStore());

  Preferences globalPref = createNiceMock(Preferences.class);
  expect(saros.getGlobalPreferences()).andStubReturn(globalPref);

  replay(bundle, globalPref, saros);

  return saros;
}
 
Example 4
Source File: SarosSessionManagerTest.java    From saros with GNU General Public License v2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  SarosSession session = PowerMock.createNiceMock(SarosSession.class);

  ConnectionHandler network = PowerMock.createNiceMock(ConnectionHandler.class);

  ITransmitter transmitter = PowerMock.createNiceMock(ITransmitter.class);
  IReceiver receiver = PowerMock.createNiceMock(IReceiver.class);

  IContainerContext context = PowerMock.createNiceMock(IContainerContext.class);

  PowerMock.expectNew(
          SarosSession.class,
          EasyMock.anyObject(String.class),
          EasyMock.anyObject(IPreferenceStore.class),
          EasyMock.anyObject(IContainerContext.class))
      .andStubReturn(session);

  PowerMock.replayAll();

  manager = new SarosSessionManager(context, null, null, network, transmitter, receiver);
}
 
Example 5
Source File: ResultSetDataWrapperTest.java    From fastods with GNU General Public License v3.0 5 votes vote down vote up
@Before
public final void setUp() {
    this.converter = new ObjectToCellValueConverter("USD");
    this.odsFactory = OdsFactory.create(Logger.getLogger(""), Locale.US);
    this.tester = ResultSetTester.create();
    this.logger = PowerMock.createMock(Logger.class);
    this.tcls = PowerMock.createNiceMock(TableCellStyle.class);
    this.walker = PowerMock.createMock(TableCellWalker.class);
}
 
Example 6
Source File: ContentElementTest.java    From fastods with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testAddChildCellStyle() {
    final TableCellStyle style = PowerMock.createNiceMock(TableCellStyle.class);

    PowerMock.resetAll();
    EasyMock.expect(this.container.addChildCellStyle(TableCellStyle.DEFAULT_CELL_STYLE,
            this.format.getBooleanDataStyle())).andReturn(style);

    PowerMock.replayAll();
    final TableCellStyle actual =
            this.content.addChildCellStyle(TableCellStyle.DEFAULT_CELL_STYLE, CellType.BOOLEAN);

    PowerMock.verifyAll();
    Assert.assertEquals(style, actual);
}
 
Example 7
Source File: NamedOdsDocumentTest.java    From fastods with GNU General Public License v3.0 5 votes vote down vote up
@Before
public final void setUp() {
    this.logger = PowerMock.createNiceMock(Logger.class);
    this.xmlUtil = XMLUtil.create();
    this.odsElements = PowerMock.createMock(OdsElements.class);
    this.aDataStyle = new BooleanStyleBuilder("o", Locale.US).build();
    this.aStyle = TableCellStyle.builder("a").dataStyle(this.aDataStyle).build();
}
 
Example 8
Source File: AnonymousOdsFileWriterTest.java    From fastods with GNU General Public License v3.0 5 votes vote down vote up
@Before
public final void setUp() {
    this.logger = PowerMock.createNiceMock(Logger.class);
    final ConsoleHandler handler = new ConsoleHandler();
    handler.setLevel(Level.FINEST);
    this.logger.addHandler(handler);
    this.os = new ByteArrayOutputStream();
    this.xmlUtil = XMLUtil.create();
    this.odsElements = PowerMock.createMock(OdsElements.class);
    this.builder = PowerMock.createMock(ZipUTF8WriterBuilderImpl.class);
    this.odsFactory = OdsFactory.create(this.logger, Locale.US);
}
 
Example 9
Source File: AnonymousOdsFileWriterTest.java    From fastods with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = IOException.class)
public final void testFileIsDir() throws IOException {
    final Logger l = PowerMock.createNiceMock(Logger.class);
    final OdsFactory of = OdsFactory.create(l, Locale.US);

    PowerMock.resetAll();
    TestHelper.initMockDocument(this.odsElements);

    PowerMock.replayAll();
    of.createWriter().saveAs(".");

    PowerMock.verifyAll();
}
 
Example 10
Source File: HadoopTimelineMetricsSinkTest.java    From ambari-metrics with Apache License 2.0 4 votes vote down vote up
@Test
@PrepareForTest({URL.class, OutputStream.class, AbstractTimelineMetricsSink.class, HttpURLConnection.class, TimelineMetric.class, HadoopTimelineMetricsSink.class, SubsetConfiguration.class})
public void testPutMetrics() throws Exception {
  HadoopTimelineMetricsSink sink = new HadoopTimelineMetricsSink();

  HttpURLConnection connection = PowerMock.createNiceMock(HttpURLConnection.class);
  URL url = PowerMock.createNiceMock(URL.class);
  InputStream is = IOUtils.toInputStream(gson.toJson(Collections.singletonList("localhost")));
  TimelineMetric timelineMetric = PowerMock.createNiceMock(TimelineMetric.class);
  expectNew(TimelineMetric.class).andReturn(timelineMetric).times(2);
  expect(timelineMetric.getMetricValues()).andReturn(new TreeMap<Long, Double>()).anyTimes();
  expect(timelineMetric.getMetricName()).andReturn("metricName").anyTimes();
  expectNew(URL.class, anyString()).andReturn(url).anyTimes();
  expect(url.openConnection()).andReturn(connection).anyTimes();
  expect(connection.getInputStream()).andReturn(is).anyTimes();
  expect(connection.getResponseCode()).andReturn(200).anyTimes();
  OutputStream os = PowerMock.createNiceMock(OutputStream.class);
  expect(connection.getOutputStream()).andReturn(os).anyTimes();

  SubsetConfiguration conf = PowerMock.createNiceMock(SubsetConfiguration.class);
  expect(conf.getString("slave.host.name")).andReturn("localhost").anyTimes();
  expect(conf.getParent()).andReturn(null).anyTimes();
  expect(conf.getPrefix()).andReturn("service").anyTimes();
  expect(conf.getStringArray(eq(COLLECTOR_HOSTS_PROPERTY))).andReturn(new String[]{"localhost"," localhost2"}).anyTimes();
  expect(conf.getString(eq("serviceName-prefix"), eq(""))).andReturn("").anyTimes();
  expect(conf.getString(eq(COLLECTOR_PROTOCOL), eq("http"))).andReturn("http").anyTimes();
  expect(conf.getString(eq(COLLECTOR_PORT), eq("6188"))).andReturn("6188").anyTimes();

  expect(conf.getInt(eq(MAX_METRIC_ROW_CACHE_SIZE), anyInt())).andReturn(10).anyTimes();
  expect(conf.getInt(eq(METRICS_SEND_INTERVAL), anyInt())).andReturn(1000).anyTimes();
  expect(conf.getBoolean(eq(SET_INSTANCE_ID_PROPERTY), eq(false))).andReturn(true).anyTimes();
  expect(conf.getString(eq(INSTANCE_ID_PROPERTY), anyString())).andReturn("instanceId").anyTimes();
  expect(conf.getString(eq(HOST_IN_MEMORY_AGGREGATION_PROTOCOL_PROPERTY), anyString())).andReturn("http").anyTimes();

  conf.setListDelimiterHandler(new DefaultListDelimiterHandler(eq(',')));
  expectLastCall().anyTimes();

  expect(conf.getKeys()).andReturn(new Iterator() {
    @Override
    public boolean hasNext() {
      return false;
    }

    @Override
    public Object next() {
      return null;
    }

    @Override
    public void remove() {

    }
  }).once();

  AbstractMetric metric = createNiceMock(AbstractMetric.class);
  expect(metric.name()).andReturn("metricName").anyTimes();
  expect(metric.value()).andReturn(9.5687).anyTimes();
  expect(metric.type()).andReturn(MetricType.COUNTER).anyTimes();
  //TODO currently only numeric metrics are supported

  MetricsRecord record = createNiceMock(MetricsRecord.class);
  expect(record.name()).andReturn("testName").anyTimes();
  expect(record.context()).andReturn("testContext").anyTimes();
  expect(record.timestamp()).andAnswer(new IAnswer<Long>() {
    @Override
    public Long answer() throws Throwable {
      return System.currentTimeMillis();
    }
  }).anyTimes();

  expect(record.metrics()).andReturn(Arrays.asList(metric)).anyTimes();

  timelineMetric.setInstanceId(eq("instanceId"));
  EasyMock.expectLastCall();

  replay(record, metric);
  replayAll();

  sink.init(conf);

  sink.putMetrics(record);

  Thread.sleep(1500L);

  sink.putMetrics(record);

  verifyAll();
}
 
Example 11
Source File: ResultSetDataWrapperTest.java    From fastods with GNU General Public License v3.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public final void testRealDataSets() throws IOException, FastOdsException {
    final Logger logger = PowerMock.createNiceMock(Logger.class);
    final MockResultSet rs = this.tester
            .createResultSet(Arrays.asList("number", "word", "code"),
                    Arrays.asList(Arrays.<Object>asList(13, "a", "13a"),
                            Arrays.<Object>asList(14, "b", "14b"),
                            Arrays.<Object>asList(15, "c", "15c")));
    final ResultSetDataWrapper wrapper =
            ResultSetDataWrapper.builder("range", rs).logger(this.logger).noAutoFilter()
                    .build();

    PowerMock.resetAll();
    EasyMock.expect(this.walker.rowIndex()).andReturn(0);
    EasyMock.expect(this.walker.colIndex()).andReturn(0);

    this.walker.setStringValue("number");
    this.walker.setStyle(EasyMock.isA(TableCellStyle.class));
    this.walker.next();
    this.walker.setStringValue("word");
    this.walker.setStyle(EasyMock.isA(TableCellStyle.class));
    this.walker.next();
    this.walker.setStringValue("code");
    this.walker.setStyle(EasyMock.isA(TableCellStyle.class));
    this.walker.next();
    this.walker.nextRow();
    this.walker.to(0);
    this.walker.setCellValue(FloatValue.from(13));
    this.walker.next();
    this.walker.setCellValue(StringValue.from("a"));
    this.walker.next();
    this.walker.setCellValue(StringValue.from("13a"));
    this.walker.next();
    this.walker.nextRow();
    this.walker.to(0);
    this.walker.setCellValue(FloatValue.from(14));
    this.walker.next();
    this.walker.setCellValue(StringValue.from("b"));
    this.walker.next();
    this.walker.setCellValue(StringValue.from("14b"));
    this.walker.next();
    this.walker.nextRow();
    this.walker.to(0);
    this.walker.setCellValue(FloatValue.from(15));
    this.walker.next();
    this.walker.setCellValue(StringValue.from("c"));
    this.walker.next();
    this.walker.setCellValue(StringValue.from("15c"));
    this.walker.next();
    this.walker.nextRow();
    this.walker.to(0);
    this.walker.setStringValue("");
    this.walker.next();
    this.walker.setStringValue("");
    this.walker.next();
    this.walker.setStringValue("");
    this.walker.next();
    this.walker.nextRow();

    PowerMock.replayAll();
    wrapper.addToTable(this.walker);

    PowerMock.verifyAll();
}
 
Example 12
Source File: ResultSetDataWrapperTest.java    From fastods with GNU General Public License v3.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public final void testWrapperWithColumnHints()
        throws IOException, FastOdsException {
    final Logger logger = PowerMock.createNiceMock(Logger.class);
    final MockResultSet rs = this.tester
            .createResultSet(Arrays.asList("number", "word", "code"),
                    Arrays.asList(Arrays.<Object>asList(0.13, "a", "13a")));
    final ResultSetDataWrapper wrapper =
            ResultSetDataWrapper.builder("range", rs).logger(this.logger).noAutoFilter()
                    .typeValue(0, CellType.PERCENTAGE).build();

    PowerMock.resetAll();
    EasyMock.expect(this.walker.rowIndex()).andReturn(0);
    EasyMock.expect(this.walker.colIndex()).andReturn(0);

    this.walker.setStringValue("number");
    this.walker.setStyle(EasyMock.isA(TableCellStyle.class));
    this.walker.next();
    this.walker.setStringValue("word");
    this.walker.setStyle(EasyMock.isA(TableCellStyle.class));
    this.walker.next();
    this.walker.setStringValue("code");
    this.walker.setStyle(EasyMock.isA(TableCellStyle.class));
    this.walker.next();
    this.walker.nextRow();
    this.walker.to(0);
    this.walker.setCellValue(PercentageValue.from(0.13));
    this.walker.next();
    this.walker.setCellValue(StringValue.from("a"));
    this.walker.next();
    this.walker.setCellValue(StringValue.from("13a"));
    this.walker.next();
    this.walker.nextRow();
    this.walker.to(0);
    this.walker.setStringValue("");
    this.walker.next();
    this.walker.setStringValue("");
    this.walker.next();
    this.walker.setStringValue("");
    this.walker.next();
    this.walker.nextRow();

    PowerMock.replayAll();
    wrapper.addToTable(this.walker);

    PowerMock.verifyAll();
}
 
Example 13
Source File: ContextMocker.java    From saros with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Adds a mocked dependency to the container
 *
 * @param container target for the dependency to mock
 * @param clazz Class for which a {@linkplain PowerMock#createNiceMock(Class) nice mock} will be
 *     created. Must not add the same key twice (neither through this nor through the other add
 *     methods).
 */
public static <T> void addMock(MutablePicoContainer container, Class<T> clazz) {

  T mock = PowerMock.createNiceMock(clazz);
  EasyMock.replay(mock);

  container.addComponent(clazz, mock);
}