### Question:
FileBasedNetworkTopology implements NetworkTopology { @Override public NetworkLocation locate(HostAddress address) { return getTopology().getOrDefault(address.getHostText(), ROOT_LOCATION); } @Inject  FileBasedNetworkTopology(TopologyFileConfig topologyConfig);   FileBasedNetworkTopology(File networkTopologyFile, Duration refreshPeriod, Ticker ticker); @Managed synchronized void refreshTopology(); @Override NetworkLocation locate(HostAddress address);  }### Answer:
@Test public void testLocate() { NetworkTopology topology = new FileBasedNetworkTopology(topologyFile, Duration.valueOf("1d"), new TestingTicker()); assertEquals(topology.locate(HostAddress.fromString("0.0.0.0")), new NetworkLocation()); assertEquals(topology.locate(HostAddress.fromString("not-exist.example.com")), new NetworkLocation()); assertEquals(topology.locate(HostAddress.fromString("192.168.0.1")), new NetworkLocation("region1", "rack1", "machine1")); assertEquals(topology.locate(HostAddress.fromString("192.168.0.2")), new NetworkLocation("region1", "rack1", "machine2")); assertEquals(topology.locate(HostAddress.fromString("hdfs01.example.com")), new NetworkLocation("region2", "rack2", "machine3")); assertEquals(topology.locate(HostAddress.fromString("192.168.0.1:8080")), new NetworkLocation("region1", "rack1", "machine1")); }
@Test public void testRefresh() throws Exception { try (TempFile tempFile = new TempFile()) { Files.copy(topologyFile, tempFile.file()); TestingTicker ticker = new TestingTicker(); FileBasedNetworkTopology topology = new FileBasedNetworkTopology(tempFile.file(), Duration.valueOf("1d"), ticker); assertEquals(topology.locate(HostAddress.fromString("not-exist.example.com")), new NetworkLocation()); assertEquals(topology.locate(HostAddress.fromString("192.168.0.1")), new NetworkLocation("region1", "rack1", "machine1")); assertEquals(topology.locate(HostAddress.fromString("192.168.0.2")), new NetworkLocation("region1", "rack1", "machine2")); assertEquals(topology.locate(HostAddress.fromString("192.168.0.3")), new NetworkLocation()); assertEquals(topology.locate(HostAddress.fromString("new")), new NetworkLocation()); Files.copy(topologyNewFile, tempFile.file()); ticker.increment(1, TimeUnit.DAYS); assertEquals(topology.locate(HostAddress.fromString("new")), new NetworkLocation("new", "rack", "machine")); assertEquals(topology.locate(HostAddress.fromString("not-exist.example.com")), new NetworkLocation()); assertEquals(topology.locate(HostAddress.fromString("192.168.0.1")), new NetworkLocation("region1", "rack1", "machine5")); assertEquals(topology.locate(HostAddress.fromString("192.168.0.2")), new NetworkLocation()); assertEquals(topology.locate(HostAddress.fromString("192.168.0.3")), new NetworkLocation("region1", "rack1", "machine6")); } }