-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #308 from sberkley/add_networks_model
Add networks.txt model
- Loading branch information
Showing
8 changed files
with
202 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/Network.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright (C) 2024 Sound Transit <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.onebusaway.gtfs.model; | ||
|
||
import org.onebusaway.csv_entities.schema.annotations.CsvField; | ||
import org.onebusaway.csv_entities.schema.annotations.CsvFields; | ||
import org.onebusaway.gtfs.serialization.mappings.DefaultAgencyIdFieldMappingFactory; | ||
|
||
@CsvFields(filename = "networks.txt", required = false) | ||
public final class Network extends IdentityBean<AgencyAndId> { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
@CsvField(name = "network_id", mapping = DefaultAgencyIdFieldMappingFactory.class) | ||
private AgencyAndId id; | ||
|
||
@CsvField(name = "network_name", optional = true) | ||
private String name; | ||
|
||
@Override | ||
public AgencyAndId getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public void setId(AgencyAndId id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "<Network " + this.id + ">"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/model/NetworkTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/** | ||
* Copyright (C) 2024 Sound Transit | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.onebusaway.gtfs.model; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.*; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.onebusaway.gtfs.serialization.GtfsWriter; | ||
import org.onebusaway.gtfs.serialization.GtfsWriterTest; | ||
import org.onebusaway.gtfs.services.MockGtfs; | ||
import org.onebusaway.gtfs.services.GtfsRelationalDao; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class NetworkTest { | ||
|
||
private MockGtfs _gtfs; | ||
|
||
private File _tmpDirectory; | ||
|
||
@BeforeEach | ||
public void before() throws IOException { | ||
_gtfs = MockGtfs.create(); | ||
|
||
//make temp directory for gtfs writing output | ||
_tmpDirectory = File.createTempFile("NetworkTest-", "-tmp"); | ||
if (_tmpDirectory.exists()) | ||
GtfsWriterTest.deleteFileRecursively(_tmpDirectory); | ||
_tmpDirectory.mkdirs(); | ||
} | ||
|
||
@Test | ||
public void testBasicNetworks() throws IOException { | ||
_gtfs.putMinimal(); | ||
_gtfs.putDefaultTrips(); | ||
_gtfs.putDefaultStops(); | ||
_gtfs.putLines("networks.txt", | ||
"network_id,network_name", | ||
"rail_network,Rail Network", "bus_network,Bus Network"); | ||
|
||
GtfsRelationalDao dao = _gtfs.read(); | ||
assertEquals(2, dao.getAllNetworks().size()); | ||
|
||
GtfsWriter writer = new GtfsWriter(); | ||
writer.setOutputLocation(_tmpDirectory); | ||
writer.run(dao); | ||
|
||
Scanner scan = new Scanner(new File(_tmpDirectory + "/networks.txt")); | ||
Set<String> expectedNetworkNames = new HashSet<String>(); | ||
Set<String> foundNetworkNames = new HashSet<String>(); | ||
expectedNetworkNames.add("Rail Network"); | ||
expectedNetworkNames.add("Bus Network"); | ||
boolean onHeader = true; | ||
while(scan.hasNext()){ | ||
String line = scan.nextLine(); | ||
if (onHeader) { | ||
onHeader = false; | ||
} else { | ||
String[] lineParts = line.split(","); | ||
|
||
if (lineParts.length > 1) { | ||
foundNetworkNames.add(lineParts[1]); | ||
} | ||
} | ||
} | ||
scan.close(); | ||
|
||
assertEquals(expectedNetworkNames, foundNetworkNames, "Did not find network names in output"); | ||
} | ||
|
||
@Test | ||
public void testPutMinimal() throws IOException { | ||
_gtfs.putMinimal(); | ||
// Just make sure it parses without throwing an error. | ||
_gtfs.read(); | ||
} | ||
|
||
@AfterEach | ||
public void teardown() { | ||
deleteFileRecursively(_tmpDirectory); | ||
} | ||
|
||
public static void deleteFileRecursively(File file) { | ||
|
||
if (!file.exists()) | ||
return; | ||
|
||
if (file.isDirectory()) { | ||
File[] files = file.listFiles(); | ||
if (files != null) { | ||
for (File child : files) | ||
deleteFileRecursively(child); | ||
} | ||
} | ||
|
||
file.delete(); | ||
} | ||
|
||
} | ||
|