Assumption 1) Resources loaded via the classpath have no namespace, they only have a file name.
Resources loaded from the classpath are actually identified by a pathname not a simple filename. You could view the directory parts of the pathnames as forming namespaces.
Also if you load using Class.getResourceAsStream(String pathname)
, a pathname that does not start with a "/" will be interpreted as relative to the pathname for the classes package.
The classpath mechanism works by overlaying the namespaces of each JAR file, etc on the classpath. So you could have multiple resources in different JARs with the same pathname, but only one will be actually visible. (You describe this later as a "collision".) But from the perspective of the set of pathnames that are visible, each pathname uniquely identifies a resource.
Assumption 2) It's wisest to always load resources via the classpath, never via the file system, even in unit tests.
If we are talking about the resources of the application, then loading from the classpath has definite advantages. However, if we are talking about resources that are created and managed by the application, then loading via the classpath has problems ... because you cannot write resources to the classpath.
Also, even with a clarification of what you mean by "resource", I don't think it is correct to say that it is always wisest to load resources via the classpath. We cannot possibly anticipate all scenarios / use-cases, and therefore cannot say that there may not be some scenario / use-case where the wisest approach is to load the resource some other way. (However, if you were to say "generally" or "usually" instead of "always" I would agree with the assumption.)
Conclusion 3) Therefore, resources must always have unique file names, or collisions will occur.
If resources coming from different JARs on the classpath have non-unique pathnames, then you do get a kind of collision and one or other of the resources will not be loadable (at least, not via the normal classloader APIs). But this may be exactly what you may want to happen!
Are there flaws in my assumptions or my conclusion?
Assumption 1) is false on the face of it, but it can be reinterpreted in a way that makes it true.
Conclusion 3) logically follows from the reinterpreted assumption 1).
Assumption 2) is also false, but can be reinterpreted in a way that makes it true: i.e. relax "always", and define what you mean by "resource". However, this assumption is not logically required for the conclusion.