I have to come up with a specific data structure to hold subscriptions for a messaging queue. I couldn't find pre made collections that fit the bill for a variety of reasons, so I decided to roll my own. I thought about it for a while and as a base structure it wouldn't be much different than a hierarchy tree with some special rules for inserting and removing. So my basic structure was going to be something like this:
And maybe other supporting methods. Basically, any Topic would look something like "/root/some/levels/then/a/node" with each portion (in this example delineated by "/") would be translated to a TopicLevel and could have a subscription for it (plus a few wildcards so you can subscribe to many topics at once).
My problem is, I have no idea how to write
unit tests for it. I could write tests that put things into the tree (subscribe), then pulls them out (getSubscriptions) and assert that I get what I expect. But then I am testing both the put and get functions in a single test, and a failure could mean a failure in either unit, and just as bad, success could mean a failure in one being hidden by a failure in the other creating a false success. I also thought I might be able to separate the putting from the getting by first putting some things into the tree, then iterating over it, building a representation of what it is holding (a List, or Map or something) and comparing with expectations, but that requires knowledge of the inner-workings (i.e. I would have to get at the root, then iterate recursively through all the children map - the root, the inner class, and the map holding the children all what I would consider implementation details) and so would break if the implementation changes (not to mention I would have to be careful not to require some sort of dependency on returned order).
So how would you (or better, how should I) write tests for this? Or should I consider a different design?